diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index 25e7358fb3d4..0999819d03e1 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -276,7 +276,7 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo( "UPDATE publisher_info SET favIcon = ? " "WHERE publisher_id = ?")); - if (favicon == ledger::_clear_favicon) { + if (favicon == ledger::kClearFavicon) { favicon.clear(); } diff --git a/components/brave_rewards/browser/publisher_info_database_unittest.cc b/components/brave_rewards/browser/publisher_info_database_unittest.cc index 3c69bc821d98..d05f6317fd52 100644 --- a/components/brave_rewards/browser/publisher_info_database_unittest.cc +++ b/components/brave_rewards/browser/publisher_info_database_unittest.cc @@ -260,7 +260,7 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdatePublisherInfo) { /** * If favicon is marked as clear, clear it */ - info.favicon_url = ledger::_clear_favicon; + info.favicon_url = ledger::kClearFavicon; success = publisher_info_database_->InsertOrUpdatePublisherInfo(info); EXPECT_TRUE(success); diff --git a/components/brave_rewards/resources/extension/brave_rewards/actions/rewards_panel_actions.ts b/components/brave_rewards/resources/extension/brave_rewards/actions/rewards_panel_actions.ts index a00e01c24eb1..93f37e423cb8 100644 --- a/components/brave_rewards/resources/extension/brave_rewards/actions/rewards_panel_actions.ts +++ b/components/brave_rewards/resources/extension/brave_rewards/actions/rewards_panel_actions.ts @@ -17,10 +17,10 @@ export const onTabId = (tabId: number | undefined) => action(types.ON_TAB_ID, { tabId }) -export const onTabRetrieved = (tab: chrome.tabs.Tab, publisherBlob: string, activeTabIsLoadingTriggered: boolean) => action(types.ON_TAB_RETRIEVED, { +export const onTabRetrieved = (tab: chrome.tabs.Tab, activeTabIsLoadingTriggered: boolean, publisherBlob: string = 'ignore') => action(types.ON_TAB_RETRIEVED, { tab, - publisherBlob, - activeTabIsLoadingTriggered + activeTabIsLoadingTriggered, + publisherBlob }) export const onPublisherData = (windowId: number, publisher: RewardsExtension.Publisher) => action(types.ON_PUBLISHER_DATA, { diff --git a/components/brave_rewards/resources/extension/brave_rewards/background/api/tabs_api.ts b/components/brave_rewards/resources/extension/brave_rewards/background/api/tabs_api.ts index 9f3ea1af17ae..68e0e67cb330 100644 --- a/components/brave_rewards/resources/extension/brave_rewards/background/api/tabs_api.ts +++ b/components/brave_rewards/resources/extension/brave_rewards/background/api/tabs_api.ts @@ -5,5 +5,5 @@ export const getTabData = (tabId: number) => chrome.tabs.get(tabId, (tab: chrome.tabs.Tab) => { const rewardsPanelActions = require('../actions/rewardsPanelActions').default - rewardsPanelActions.onTabRetrieved(tab, '') + rewardsPanelActions.onTabRetrieved(tab) }) diff --git a/components/brave_rewards/resources/extension/brave_rewards/background/events/tabEvents.ts b/components/brave_rewards/resources/extension/brave_rewards/background/events/tabEvents.ts index 79f338aeca6e..744c080adb5b 100644 --- a/components/brave_rewards/resources/extension/brave_rewards/background/events/tabEvents.ts +++ b/components/brave_rewards/resources/extension/brave_rewards/background/events/tabEvents.ts @@ -6,7 +6,7 @@ import rewardsPanelActions from '../actions/rewardsPanelActions' chrome.tabs.onUpdated.addListener((tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) => { const activeTabIsLoadingTriggered = Boolean(tab.active && changeInfo.status === 'loading') - rewardsPanelActions.onTabRetrieved(tab, '', activeTabIsLoadingTriggered) + rewardsPanelActions.onTabRetrieved(tab, activeTabIsLoadingTriggered) }) chrome.tabs.onActivated.addListener((activeInfo: chrome.tabs.TabActiveInfo) => { diff --git a/components/brave_rewards/resources/extension/brave_rewards/components/app.tsx b/components/brave_rewards/resources/extension/brave_rewards/components/app.tsx index 82d420c17365..54a67645bd2f 100644 --- a/components/brave_rewards/resources/extension/brave_rewards/components/app.tsx +++ b/components/brave_rewards/resources/extension/brave_rewards/components/app.tsx @@ -74,28 +74,24 @@ export class RewardsPanel extends React.Component { if (!tabs || !tabs.length) { return } - const pollTwitchPage = (tab: chrome.tabs.Tab, tabId: number, publisherBlob: string) => { + const pollTwitchPage = (tab: chrome.tabs.Tab, tabId: number) => { // use an interval here to monitor when the DOM has finished // generating. clear after the data is present. // Check every second no more than 'limit' times // clear the interval if panel closes - const markupMatch = '
' + - '
' + - '\"' { clearInterval(interval) const rewardsPanelActions = require('../background/actions/rewardsPanelActions').default - rewardsPanelActions.onTabRetrieved(tab, publisherBlob) + rewardsPanelActions.onTabRetrieved(tab) } } }) @@ -118,15 +114,14 @@ export class RewardsPanel extends React.Component { } const pollData = (tab: chrome.tabs.Tab, tabId: number, url: URL) => { - let publisherBlob = '' if (url && url.href.startsWith('https://www.twitch.tv/')) { chrome.storage.local.get(['rewards_panel_open'], function (result) { if (result['rewards_panel_open'] === 'true') { - pollTwitchPage(tab, tabId, publisherBlob) + pollTwitchPage(tab, tabId) } }) } else { - this.props.actions.onTabRetrieved(tab, publisherBlob) + this.props.actions.onTabRetrieved(tab) } } let tab = tabs[0] @@ -135,10 +130,10 @@ export class RewardsPanel extends React.Component { if (url && url.host.endsWith('.twitch.tv')) { pollData(tab, tab.id, url) } else { - this.props.actions.onTabRetrieved(tab, '') + this.props.actions.onTabRetrieved(tab) } } else { - this.props.actions.onTabRetrieved(tab, '') + this.props.actions.onTabRetrieved(tab) } }) } diff --git a/components/test/brave_rewards/extension/brave_rewards/background/reducers/rewards_panel_reducer_test.ts b/components/test/brave_rewards/extension/brave_rewards/background/reducers/rewards_panel_reducer_test.ts index 6cc7c67ad683..de4d647d6d1f 100644 --- a/components/test/brave_rewards/extension/brave_rewards/background/reducers/rewards_panel_reducer_test.ts +++ b/components/test/brave_rewards/extension/brave_rewards/background/reducers/rewards_panel_reducer_test.ts @@ -59,8 +59,8 @@ describe('rewards panel reducer', () => { state.rewardsPanelData.publishers = { id_1: { tabUrl: 'https://clifton.io', - name: 'Clifton', - publisher_key: 'clifton.io' + publisher_key: 'clifton.io', + name: 'Clifton' } } @@ -72,8 +72,63 @@ describe('rewards panel reducer', () => { publishers: { id_1: { tabUrl: 'https://clifton.io', - name: 'Clifton', - publisher_key: 'clifton.io' + publisher_key: 'clifton.io', + name: 'Clifton' + } + } + } + + state = reducers(state, { + type: types.ON_TAB_RETRIEVED, + payload + }) + + expect(state.rewardsPanelData).toEqual(expectedState2) + }) + + it('url is the same, but publisher was not saved correctly', () => { + const initState: Rewards.State = { ...defaultState, walletCreated: true } + const payload = { + tab: { + url: 'https://clifton.io', + incognito: false, + active: true, + windowId: 1 + } + } + + // first visit + const expectedState1: Rewards.State = { + ...defaultState, + walletCreated: true, + publishers: { + id_1: { + tabUrl: 'https://clifton.io' + } + } + } + + let state = reducers({ rewardsPanelData: initState }, { + type: types.ON_TAB_RETRIEVED, + payload + }) + + expect(state.rewardsPanelData).toEqual(expectedState1) + + // imitates ON_PUBLISHER_DATA + state.rewardsPanelData.publishers = { + id_1: { + tabUrl: 'https://clifton.io' + } + } + + // second visit + const expectedState2: Rewards.State = { + ...defaultState, + walletCreated: true, + publishers: { + id_1: { + tabUrl: 'https://clifton.io' } } } @@ -122,9 +177,9 @@ describe('rewards panel reducer', () => { // imitates ON_PUBLISHER_DATA state.rewardsPanelData.publishers = { id_1: { - tabUrl: 'https://clifton.io', - name: 'Clifton', - publisher_key: 'clifton.io' + tabUrl: 'clifton.io', + publisher_key: 'clifton.io', + name: 'Clifton' } } diff --git a/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h b/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h index 964d0b9b7b21..b2e4403e8ef1 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h +++ b/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h @@ -20,7 +20,8 @@ using PublisherInfo = mojom::PublisherInfo; using PublisherInfoPtr = mojom::PublisherInfoPtr; using PublisherInfoList = std::vector; -const char _clear_favicon[] = "clear"; +const char kClearFavicon[] = "clear"; +const char kIgnorePublisherBlob[] = "ignore"; LEDGER_EXPORT enum REWARDS_CATEGORY { AUTO_CONTRIBUTE = 1 << 1, // 2 diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.cc index a27c3891d811..8aa2417241c9 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.cc @@ -175,7 +175,7 @@ void BatPublishers::saveVisitInternal( publisher_info->favicon_url = fav_icon; } } else { - publisher_info->favicon_url = ledger::_clear_favicon; + publisher_info->favicon_url = ledger::kClearFavicon; } publisher_info->name = visit_data.name; @@ -228,7 +228,7 @@ void BatPublishers::saveVisitInternal( } if (panel_info) { - if (panel_info->favicon_url == ledger::_clear_favicon) { + if (panel_info->favicon_url == ledger::kClearFavicon) { panel_info->favicon_url = std::string(); } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.cc index 71c32eea9a36..d0be8de7f80b 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.cc @@ -8,6 +8,7 @@ #include #include +#include "base/strings/string_util.h" #include "bat/ledger/internal/bat_helper.h" #include "bat/ledger/internal/ledger_impl.h" #include "bat/ledger/internal/media/twitch.h" @@ -199,11 +200,7 @@ std::string MediaTwitch::GetMediaIdFromUrl( if (url.find("twitch.tv/videos/") != std::string::npos) { mediaId = braveledger_media::ExtractData(publisher_blob, - "" - "
" - "\"",", ""); } // static @@ -251,12 +246,11 @@ std::string MediaTwitch::GetFaviconUrl( return std::string(); } - return braveledger_media::ExtractData(publisher_blob, - "
" - "
" - "\"""); + + return braveledger_media::ExtractData(wrapper, "src=\"", "\""); } // static @@ -331,36 +325,34 @@ void MediaTwitch::ProcessMedia(const std::map& parts, void MediaTwitch::ProcessActivityFromUrl(uint64_t window_id, const ledger::VisitData& visit_data, const std::string& publisher_blob) { - if (!publisher_blob.empty()) { - std::string media_id = GetMediaIdFromUrl(visit_data.url, - publisher_blob); - std::transform(media_id.begin(), - media_id.end(), - media_id.begin(), - ::tolower); - std::string media_key = GetMediaKeyFromUrl(media_id, visit_data.url); - if (!media_key.empty() && !media_id.empty()) { - ledger_->GetMediaPublisherInfo( - media_key, - std::bind(&MediaTwitch::OnMediaPublisherActivity, - this, - window_id, - visit_data, - media_key, - media_id, - publisher_blob, - _1, - _2)); - } else { - OnMediaActivityError(visit_data, window_id); - } - } else { - ledger::VisitData new_visit_data(visit_data); - new_visit_data.path = std::string(); - ledger_->GetPublisherActivityFromUrl(window_id, - new_visit_data, - std::string()); + if (publisher_blob.empty() || + publisher_blob == ledger::kIgnorePublisherBlob) { + OnMediaActivityError(visit_data, window_id); + return; + } + + std::string media_id = GetMediaIdFromUrl(visit_data.url, + publisher_blob); + + media_id = base::ToLowerASCII(media_id); + std::string media_key = GetMediaKeyFromUrl(media_id, visit_data.url); + + if (media_key.empty() || media_id.empty()) { + OnMediaActivityError(visit_data, window_id); + return; } + + ledger_->GetMediaPublisherInfo( + media_key, + std::bind(&MediaTwitch::OnMediaPublisherActivity, + this, + window_id, + visit_data, + media_key, + media_id, + publisher_blob, + _1, + _2)); } void MediaTwitch::OnSaveMediaVisit( @@ -385,11 +377,7 @@ void MediaTwitch::OnMediaPublisherInfo( return; } - if (!publisher_info) { - if (media_id.empty()) { - return; - } - + if (publisher_info) { ledger::TwitchEventInfo old_event; std::map::const_iterator iter = twitch_events.find(media_key); @@ -400,99 +388,81 @@ void MediaTwitch::OnMediaPublisherInfo( ledger::TwitchEventInfo new_event(twitch_info); new_event.status_ = GetTwitchStatus(old_event, new_event); - uint64_t real_duration = GetTwitchDuration(old_event, new_event); + const uint64_t real_duration = GetTwitchDuration(old_event, new_event); twitch_events[media_key] = new_event; - if (real_duration == 0) { - return; - } + SavePublisherInfo(real_duration, + std::string(), + publisher_info->url, + publisher_info->name, + visit_data, + window_id, + publisher_info->favicon_url, + std::string(), + publisher_info->id); + return; + } - ledger::VisitData updated_visit_data(visit_data); - updated_visit_data.favicon_url = ""; - updated_visit_data.provider = TWITCH_MEDIA_TYPE; + if (media_id.empty()) { + return; + } - if (media_id.find("_vod_") != std::string::npos) { - // VOD - std::vector media_props = - braveledger_bat_helper::split(media_id, MEDIA_DELIMITER); - if (media_props.empty()) { - return; - } + ledger::TwitchEventInfo old_event; + auto iter = twitch_events.find(media_key); + if (iter != twitch_events.end()) { + old_event = iter->second; + } - std::string new_id = media_props[0]; - std::string media_url = GetMediaURL(user_id); - std::string oembed_url = - (std::string)TWITCH_VOD_URL + media_props[media_props.size() - 1]; - updated_visit_data.name = new_id; - updated_visit_data.url = media_url + "/videos"; - - auto callback = std::bind(&MediaTwitch::OnEmbedResponse, - this, - real_duration, - media_key, - media_url, - updated_visit_data, - window_id, - user_id, - _1, - _2, - _3); - - const std::string url = (std::string)TWITCH_PROVIDER_URL + "?json&url=" + - ledger_->URIEncode(oembed_url); - - FetchDataFromUrl(url, callback); - return; - } + ledger::TwitchEventInfo new_event(twitch_info); + new_event.status_ = GetTwitchStatus(old_event, new_event); - // Live stream - std::string publisher_key = GetPublisherKey(media_id); - updated_visit_data.name = media_id; - updated_visit_data.url = GetMediaURL(media_id) + "/videos"; + const uint64_t real_duration = GetTwitchDuration(old_event, new_event); + twitch_events[media_key] = new_event; - auto callback = std::bind(&MediaTwitch::OnSaveMediaVisit, - this, - _1, - _2); - - ledger_->SaveMediaVisit(publisher_key, - updated_visit_data, - real_duration, - window_id, - callback); - ledger_->SetMediaPublisherInfo(media_key, publisher_key); - } else { - ledger::VisitData updated_visit_data(visit_data); - updated_visit_data.name = publisher_info->name; - updated_visit_data.url = publisher_info->url; - updated_visit_data.provider = TWITCH_MEDIA_TYPE; - updated_visit_data.favicon_url = publisher_info->favicon_url; + if (real_duration == 0) { + return; + } - ledger::TwitchEventInfo old_event; - std::map::const_iterator iter = - twitch_events.find(media_key); - if (iter != twitch_events.end()) { - old_event = iter->second; + if (media_id.find("_vod_") != std::string::npos) { + // VOD + std::vector media_props = + braveledger_bat_helper::split(media_id, MEDIA_DELIMITER); + if (media_props.empty()) { + return; } - ledger::TwitchEventInfo new_event(twitch_info); - new_event.status_ = GetTwitchStatus(old_event, new_event); + std::string media_url = GetMediaURL(user_id); + std::string oembed_url = + (std::string)TWITCH_VOD_URL + media_props[media_props.size() - 1]; - uint64_t real_duration = GetTwitchDuration(old_event, new_event); - twitch_events[media_key] = new_event; - - std::string id = publisher_info->id; - auto callback = std::bind(&MediaTwitch::OnSaveMediaVisit, + auto callback = std::bind(&MediaTwitch::OnEmbedResponse, this, + real_duration, + media_key, + media_url, + visit_data, + window_id, + user_id, _1, - _2); + _2, + _3); + + const std::string url = (std::string)TWITCH_PROVIDER_URL + "?json&url=" + + ledger_->URIEncode(oembed_url); - ledger_->SaveMediaVisit(id, - updated_visit_data, - real_duration, - window_id, - callback); + FetchDataFromUrl(url, callback); + return; } + + // Live stream + SavePublisherInfo(real_duration, + media_key, + GetMediaURL(media_id), + media_id, + visit_data, + window_id, + std::string(), + media_id); } void MediaTwitch::FetchDataFromUrl( @@ -530,26 +500,14 @@ void MediaTwitch::OnEmbedResponse( std::string author_name; braveledger_bat_helper::getJSONValue("author_name", response, &author_name); - std::string id = GetPublisherKey(user_id); - - ledger::VisitData updated_visit_data(visit_data); - updated_visit_data.name = author_name; - - if (fav_icon.length() > 0) { - updated_visit_data.favicon_url = fav_icon; - } - - auto callback = std::bind(&MediaTwitch::OnSaveMediaVisit, - this, - _1, - _2); - - ledger_->SaveMediaVisit(id, - updated_visit_data, - duration, - window_id, - callback); - ledger_->SetMediaPublisherInfo(media_key, id); + SavePublisherInfo(duration, + media_key, + media_url, + author_name, + visit_data, + window_id, + fav_icon, + user_id); } void MediaTwitch::OnMediaPublisherActivity( @@ -583,6 +541,7 @@ void MediaTwitch::OnMediaPublisherActivity( if (info->verified && info->favicon_url.empty()) { std::string publisher_name; std::string publisher_favicon_url; + UpdatePublisherData(&publisher_name, &publisher_favicon_url, publisher_blob); @@ -590,7 +549,7 @@ void MediaTwitch::OnMediaPublisherActivity( if (!publisher_favicon_url.empty()) { SavePublisherInfo(0, media_key, - visit_data.url, + GetMediaURL(media_id), publisher_name, visit_data, window_id, @@ -621,13 +580,18 @@ void MediaTwitch::OnPublisherInfo( if (!publisher_info || result == ledger::Result::NOT_FOUND) { std::string publisher_name; std::string publisher_favicon_url; + UpdatePublisherData(&publisher_name, &publisher_favicon_url, publisher_blob); + if (publisher_name.empty()) { + publisher_name = media_id; + } + SavePublisherInfo(0, media_key, - visit_data.url, + GetMediaURL(media_id), publisher_name, visit_data, window_id, @@ -647,17 +611,20 @@ void MediaTwitch::SavePublisherInfo(const uint64_t duration, const ledger::VisitData& visit_data, const uint64_t window_id, const std::string& fav_icon, - const std::string& channel_id) { - if (channel_id.empty()) { + const std::string& channel_id, + const std::string& publisher_key) { + if (channel_id.empty() && publisher_key.empty()) { BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << "author id is missing for: " << media_key; return; } - std::string publisher_id = GetPublisherKey(channel_id); - std::string url = publisher_url + "/videos"; + std::string key = publisher_key; + if (key.empty()) { + key = GetPublisherKey(channel_id); + } - if (publisher_id.empty()) { + if (key.empty()) { BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << "Publisher id is missing for: " << media_key; return; @@ -671,20 +638,20 @@ void MediaTwitch::SavePublisherInfo(const uint64_t duration, updated_visit_data.provider = TWITCH_MEDIA_TYPE; updated_visit_data.name = publisher_name; - updated_visit_data.url = url; + updated_visit_data.url = publisher_url + "/videos"; auto callback = std::bind(&MediaTwitch::OnSaveMediaVisit, this, _1, _2); - ledger_->SaveMediaVisit(publisher_id, + ledger_->SaveMediaVisit(key, updated_visit_data, duration, window_id, callback); if (!media_key.empty()) { - ledger_->SetMediaPublisherInfo(media_key, publisher_id); + ledger_->SetMediaPublisherInfo(media_key, key); } } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.h b/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.h index cbdc5f53a02f..6448b860401c 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.h @@ -125,7 +125,8 @@ class MediaTwitch : public ledger::LedgerCallbackHandler { const ledger::VisitData& visit_data, const uint64_t window_id, const std::string& fav_icon, - const std::string& channel_id); + const std::string& channel_id, + const std::string& publisher_key = ""); bat_ledger::LedgerImpl* ledger_; // NOT OWNED std::map twitch_events; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch_unittest.cc index d9fdbc67c962..d2172c521e91 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch_unittest.cc @@ -17,34 +17,30 @@ class MediaTwitchTest : public testing::Test { }; const char profile_html[] = + "
" "
\"dakotaz\"
dakotaz
" - "
Verified User
"; + "class=\"tw-border-radius-rounded tw-overflow-hidden\">\"bravesoftware\"" + "
bravesoftware
" + "
Videos
0
"; TEST(MediaTwitchTest, GetMediaIdFromParts) { std::string media_id; @@ -90,36 +86,36 @@ TEST(MediaTwitchTest, GetMediaIdFromParts) { result = MediaTwitch::GetMediaIdFromParts({ {"event", "minute-watched"}, {"properties", ""}, - {"channel", "dakotaz"} + {"channel", "bravesoftware"} }); media_id = result.first; user_id = result.second; - EXPECT_EQ(user_id, "dakotaz"); - ASSERT_EQ(media_id, "dakotaz"); + EXPECT_EQ(user_id, "bravesoftware"); + ASSERT_EQ(media_id, "bravesoftware"); // vod is missing leading v result = MediaTwitch::GetMediaIdFromParts({ {"event", "minute-watched"}, {"properties", ""}, - {"channel", "dakotaz"}, + {"channel", "bravesoftware"}, {"vod", "123312312"} }); media_id = result.first; user_id = result.second; - EXPECT_EQ(user_id, "dakotaz"); - ASSERT_EQ(media_id, "dakotaz"); + EXPECT_EQ(user_id, "bravesoftware"); + ASSERT_EQ(media_id, "bravesoftware"); // vod is provided result = MediaTwitch::GetMediaIdFromParts({ {"event", "minute-watched"}, {"properties", ""}, - {"channel", "dakotaz"}, + {"channel", "bravesoftware"}, {"vod", "v123312312"} }); media_id = result.first; user_id = result.second; - EXPECT_EQ(user_id, "dakotaz"); - ASSERT_EQ(media_id, "dakotaz_vod_123312312"); + EXPECT_EQ(user_id, "bravesoftware"); + ASSERT_EQ(media_id, "bravesoftware_vod_123312312"); // live stream username has '_' result = MediaTwitch::GetMediaIdFromParts({ @@ -151,8 +147,8 @@ TEST(MediaTwitchTest, GetMediaURL) { ASSERT_EQ(result, ""); // all ok - result = MediaTwitch::GetMediaURL("dakotaz"); - ASSERT_EQ(result, "https://www.twitch.tv/dakotaz"); + result = MediaTwitch::GetMediaURL("bravesoftware"); + ASSERT_EQ(result, "https://www.twitch.tv/bravesoftware"); } TEST(MediaTwitchTest, GetTwitchStatus) { @@ -190,7 +186,27 @@ TEST(MediaTwitchTest, GetTwitchStatus) { ASSERT_EQ(result, "playing"); } -TEST(MediaTwitchTest, GetLinkType ) { +TEST(MediaTwitchTest, GetMediaIdFromUrl) { + // for live stream + std::string result = MediaTwitch::GetMediaIdFromUrl( + "https://www.twitch.tv/bravesoftware", profile_html); + + ASSERT_EQ(result, "bravesoftware"); + + // longer url + result = MediaTwitch::GetMediaIdFromUrl( + "https://www.twitch.tv/bravesoftware/clips", profile_html); + + ASSERT_EQ(result, "bravesoftware"); + + // video + result = MediaTwitch::GetMediaIdFromUrl( + "https://www.twitch.tv/videos/11111", profile_html); + + ASSERT_EQ(result, "bravesoftware"); +} + +TEST(MediaTwitchTest, GetLinkType) { const std::string url("https://k8923479-sub.cdn.ttvnw.net/v1/segment/"); // url is not correct @@ -229,13 +245,13 @@ TEST(MediaTwitchTest, GetMediaKeyFromUrl) { // get vod id result = MediaTwitch::GetMediaKeyFromUrl( - "dakotaz", + "bravesoftware", "https://www.twitch.tv/videos/411403500"); - ASSERT_EQ(result, "twitch_dakotaz_vod_411403500"); + ASSERT_EQ(result, "twitch_bravesoftware_vod_411403500"); // regular id - result = MediaTwitch::GetMediaKeyFromUrl("dakotaz", ""); - ASSERT_EQ(result, "twitch_dakotaz"); + result = MediaTwitch::GetMediaKeyFromUrl("bravesoftware", ""); + ASSERT_EQ(result, "twitch_bravesoftware"); } TEST(MediaTwitchTest, GetPublisherKey) { @@ -255,7 +271,7 @@ TEST(MediaTwitchTest, GetPublisherName) { // all ok result = MediaTwitch::GetPublisherName(profile_html); - ASSERT_EQ(result, "dakotaz"); + ASSERT_EQ(result, "bravesoftware"); } TEST(MediaTwitchTest, GetFaviconUrl) { @@ -264,14 +280,14 @@ TEST(MediaTwitchTest, GetFaviconUrl) { ASSERT_EQ(result, ""); // blob is not correct - result = MediaTwitch::GetFaviconUrl("dfsfsdfsdfds", "dakotaz"); + result = MediaTwitch::GetFaviconUrl("dfsfsdfsdfds", "bravesoftware"); ASSERT_EQ(result, ""); // all ok - result = MediaTwitch::GetFaviconUrl(profile_html, "dakotaz"); + result = MediaTwitch::GetFaviconUrl(profile_html, "bravesoftware"); ASSERT_EQ(result, - "https://static-cdn.jtvnw.net/jtv_user_pictures/473aea0f-a724-498" - "f-b7f1-e344f806ba8a-profile_image-70x70.png"); + "https://static-cdn.jtvnw.net/user-default-pictures/" + "0ecbb6c3-fecb-4016-8115-aa467b7c36ed-profile_image-70x70.jpg"); } TEST(MediaTwitchTest, UpdatePublisherData) { @@ -292,10 +308,10 @@ TEST(MediaTwitchTest, UpdatePublisherData) { &favicon_url, profile_html); - ASSERT_EQ(name, "dakotaz"); + ASSERT_EQ(name, "bravesoftware"); ASSERT_EQ(favicon_url, - "https://static-cdn.jtvnw.net/jtv_user_pictures/473aea0f-a724-498" - "f-b7f1-e344f806ba8a-profile_image-70x70.png"); + "https://static-cdn.jtvnw.net/user-default-pictures/" + "0ecbb6c3-fecb-4016-8115-aa467b7c36ed-profile_image-70x70.jpg"); } } // namespace braveledger_media