From a7d72047905fd154b183fa84ee547093e737e350 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Wed, 17 Apr 2019 12:50:10 +0200 Subject: [PATCH 1/4] Adds support for Vimeo Resolves https://github.com/brave/brave-browser/issues/4017 --- test/BUILD.gn | 1 + vendor/bat-native-ledger/BUILD.gn | 2 + .../src/bat/ledger/internal/media/media.cc | 3 ++ .../src/bat/ledger/internal/media/media.h | 2 + .../src/bat/ledger/internal/media/vimeo.cc | 38 +++++++++++++++ .../src/bat/ledger/internal/media/vimeo.h | 46 +++++++++++++++++++ .../ledger/internal/media/vimeo_unittest.cc | 23 ++++++++++ .../src/bat/ledger/internal/static_values.h | 1 + 8 files changed, 116 insertions(+) create mode 100644 vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.cc create mode 100644 vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.h create mode 100644 vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo_unittest.cc diff --git a/test/BUILD.gn b/test/BUILD.gn index c6cfa49875bd..f59671bca363 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -131,6 +131,7 @@ test("brave_unit_tests") { "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/media/reddit_unittest.cc", "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch_unittest.cc", "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitter_unittest.cc", + "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo_unittest.cc", "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/media/youtube_unittest.cc", "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/bat_helper_unittest.cc", "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/bat_helper_unittest.h", diff --git a/vendor/bat-native-ledger/BUILD.gn b/vendor/bat-native-ledger/BUILD.gn index ecd2bd4f8fde..9a05ae770fdd 100644 --- a/vendor/bat-native-ledger/BUILD.gn +++ b/vendor/bat-native-ledger/BUILD.gn @@ -123,6 +123,8 @@ source_set("ledger") { "src/bat/ledger/internal/media/twitch.cc", "src/bat/ledger/internal/media/twitter.h", "src/bat/ledger/internal/media/twitter.cc", + "src/bat/ledger/internal/media/vimeo.h", + "src/bat/ledger/internal/media/vimeo.cc", "src/bat/ledger/internal/media/youtube.h", "src/bat/ledger/internal/media/youtube.cc", "src/bat/ledger/internal/wallet/balance.h", diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/media.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/media/media.cc index 1519c5567701..86d2ad262b76 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/media.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/media.cc @@ -36,6 +36,9 @@ std::string Media::GetLinkType(const std::string& url, first_party_url, referrer); } + if (type.empty()) { + type = braveledger_media::MediaVimeo::GetLinkType(url); + } return type; } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/media.h b/vendor/bat-native-ledger/src/bat/ledger/internal/media/media.h index d94c19fd1b2a..9ba98b2f80ac 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/media.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/media.h @@ -14,6 +14,7 @@ #include "bat/ledger/internal/media/reddit.h" #include "bat/ledger/internal/media/twitch.h" #include "bat/ledger/internal/media/twitter.h" +#include "bat/ledger/internal/media/vimeo.h" #include "bat/ledger/internal/media/youtube.h" #include "bat/ledger/ledger.h" @@ -60,6 +61,7 @@ class Media { std::unique_ptr media_twitch_; std::unique_ptr media_twitter_; std::unique_ptr media_reddit_; + std::unique_ptr media_vimeo_; }; } // namespace braveledger_media diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.cc new file mode 100644 index 000000000000..003ce270bb12 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.cc @@ -0,0 +1,38 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "bat/ledger/internal/bat_helper.h" +#include "bat/ledger/internal/ledger_impl.h" +#include "bat/ledger/internal/media/vimeo.h" + +using std::placeholders::_1; +using std::placeholders::_2; + +namespace braveledger_media { + +Vimeo::Vimeo(bat_ledger::LedgerImpl* ledger): + ledger_(ledger) { +} + +Vimeo::~Vimeo() { +} + +// static +std::string Vimeo::GetLinkType(const std::string& url) { + std::string type = VIMEO_MEDIA_TYPE; + return type; +} + +void Vimeo::ProcessMedia(const std::map& parts, + const ledger::VisitData& visit_data) { + +} + +void Vimeo::ProcessActivityFromUrl(uint64_t window_id, + const ledger::VisitData& visit_data) { + +} + +} // namespace braveledger_media diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.h b/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.h new file mode 100644 index 000000000000..6d69f146b2f2 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.h @@ -0,0 +1,46 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVELEDGER_MEDIA_VIMEO_H_ +#define BRAVELEDGER_MEDIA_VIMEO_H_ + +#include +#include + +#include "base/gtest_prod_util.h" +#include "bat/ledger/ledger.h" +#include "bat/ledger/internal/media/helper.h" + +namespace bat_ledger { +class LedgerImpl; +} + +namespace braveledger_media { + +class Vimeo : public ledger::LedgerCallbackHandler { + public: + explicit Vimeo(bat_ledger::LedgerImpl* ledger); + + ~Vimeo() override; + + void ProcessMedia(const std::map& parts, + const ledger::VisitData& visit_data); + + static std::string GetLinkType(const std::string& url); + + void ProcessActivityFromUrl(uint64_t window_id, + const ledger::VisitData& visit_data); + + private: + + bat_ledger::LedgerImpl* ledger_; // NOT OWNED + + // For testing purposes + friend class VimeoTest; +}; + +} // namespace braveledger_media + +#endif // BRAVELEDGER_MEDIA_VIMEO_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo_unittest.cc new file mode 100644 index 000000000000..49e0d3c69c16 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo_unittest.cc @@ -0,0 +1,23 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "bat/ledger/internal/media/vimeo.h" +#include "bat/ledger/ledger.h" +#include "testing/gtest/include/gtest/gtest.h" + +// npm run test -- brave_unit_tests --filter=VimeoTest.* + +namespace braveledger_media { + +class VimeoTest : public testing::Test { +}; + +TEST(VimeoTest, GetLinkType) { + // TODO(nejczdovc): change me + std::string result = Vimeo::GetLinkType("https://vimeo.com/video/32342"); + ASSERT_EQ(result, "vimeo"); +} + +} // namespace braveledger_media diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/static_values.h b/vendor/bat-native-ledger/src/bat/ledger/internal/static_values.h index c2b958b7f6f5..730e00a0c32e 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/static_values.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/static_values.h @@ -65,6 +65,7 @@ #define TWITCH_MEDIA_TYPE "twitch" #define TWITTER_MEDIA_TYPE "twitter" #define REDDIT_MEDIA_TYPE "reddit" +#define VIMEO_MEDIA_TYPE "vimeo" #define YOUTUBE_PROVIDER_URL "https://www.youtube.com/oembed" #define TWITCH_PROVIDER_URL "https://api.twitch.tv/v5/oembed" #define YOUTUBE_TLD "youtube.com" From 519fac071ef2655d98525e9301d535ff1f64ae4f Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Wed, 24 Apr 2019 17:45:37 +0200 Subject: [PATCH 2/4] Generalize twitch event info --- .../browser/publisher_info_database.cc | 2 +- .../browser/rewards_service_impl.cc | 2 +- vendor/bat-native-ledger/BUILD.gn | 2 +- .../include/bat/ledger/ledger.h | 2 +- .../include/bat/ledger/ledger_client.h | 2 +- ...ia_publisher_info.h => media_event_info.h} | 14 ++++++------ .../src/bat/ledger/internal/media/twitch.cc | 22 +++++++++---------- .../src/bat/ledger/internal/media/twitch.h | 12 +++++----- .../ledger/internal/media/twitch_unittest.cc | 4 ++-- .../src/bat/ledger/ledger.cc | 6 ++--- 10 files changed, 34 insertions(+), 34 deletions(-) rename vendor/bat-native-ledger/include/bat/ledger/{media_publisher_info.h => media_event_info.h} (63%) diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index 0999819d03e1..d83579963569 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -13,7 +13,7 @@ #include "base/bind.h" #include "base/command_line.h" #include "base/files/file_util.h" -#include "bat/ledger/media_publisher_info.h" +#include "bat/ledger/media_event_info.h" #include "bat/ledger/pending_contribution.h" #include "build/build_config.h" #include "sql/meta_table.h" diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 142c6253a90e..58df033cea2e 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -31,7 +31,7 @@ #include "base/threading/sequenced_task_runner_handle.h" #include "bat/ledger/ledger.h" #include "bat/ledger/auto_contribute_props.h" -#include "bat/ledger/media_publisher_info.h" +#include "bat/ledger/media_event_info.h" #include "bat/ledger/publisher_info.h" #include "bat/ledger/wallet_properties.h" #include "bat/ledger/transactions_info.h" diff --git a/vendor/bat-native-ledger/BUILD.gn b/vendor/bat-native-ledger/BUILD.gn index 9a05ae770fdd..b9d2518d3d5a 100644 --- a/vendor/bat-native-ledger/BUILD.gn +++ b/vendor/bat-native-ledger/BUILD.gn @@ -73,7 +73,7 @@ source_set("headers") { "include/bat/ledger/ledger.h", "include/bat/ledger/ledger_callback_handler.h", "include/bat/ledger/ledger_client.h", - "include/bat/ledger/media_publisher_info.h", + "include/bat/ledger/media_event_info.h", "include/bat/ledger/pending_contribution.h", "include/bat/ledger/publisher_info.h", "include/bat/ledger/reconcile_info.h", diff --git a/vendor/bat-native-ledger/include/bat/ledger/ledger.h b/vendor/bat-native-ledger/include/bat/ledger/ledger.h index 2b14faaf90f8..2962d4d14270 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger.h @@ -18,7 +18,7 @@ #include "bat/ledger/balance.h" #include "bat/ledger/ledger_client.h" #include "bat/ledger/publisher_info.h" -#include "bat/ledger/media_publisher_info.h" +#include "bat/ledger/media_event_info.h" #include "bat/ledger/transactions_info.h" #include "bat/ledger/rewards_internals_info.h" #include "bat/ledger/pending_contribution.h" diff --git a/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h b/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h index cfa30e1c2e80..70b3ae43e0e1 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h @@ -18,7 +18,7 @@ #include "bat/ledger/export.h" #include "bat/ledger/grant.h" #include "bat/ledger/ledger_callback_handler.h" -#include "bat/ledger/media_publisher_info.h" +#include "bat/ledger/media_event_info.h" #include "bat/ledger/pending_contribution.h" #include "bat/ledger/publisher_info.h" #include "bat/ledger/reconcile_info.h" diff --git a/vendor/bat-native-ledger/include/bat/ledger/media_publisher_info.h b/vendor/bat-native-ledger/include/bat/ledger/media_event_info.h similarity index 63% rename from vendor/bat-native-ledger/include/bat/ledger/media_publisher_info.h rename to vendor/bat-native-ledger/include/bat/ledger/media_event_info.h index 675182cb31a2..88d692372b2e 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/media_publisher_info.h +++ b/vendor/bat-native-ledger/include/bat/ledger/media_event_info.h @@ -3,8 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef BAT_LEDGER_MEDIA_PUBLISHER_INFO_HANDLER_ -#define BAT_LEDGER_MEDIA_PUBLISHER_INFO_HANDLER_ +#ifndef BAT_LEDGER_MEDIA_EVENT_INFO_H_ +#define BAT_LEDGER_MEDIA_EVENT_INFO_H_ #include @@ -13,10 +13,10 @@ namespace ledger { -LEDGER_EXPORT struct TwitchEventInfo { - TwitchEventInfo(); - TwitchEventInfo(const TwitchEventInfo&); - ~TwitchEventInfo(); +LEDGER_EXPORT struct MediaEventInfo { + MediaEventInfo(); + MediaEventInfo(const MediaEventInfo&); + ~MediaEventInfo(); std::string event_; std::string time_; @@ -25,4 +25,4 @@ LEDGER_EXPORT struct TwitchEventInfo { } // namespace ledger -#endif // BAT_LEDGER_MEDIA_PUBLISHER_INFO_HANDLER_ +#endif // BAT_LEDGER_MEDIA_EVENT_INFO_H_ 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 61c0a116a4cf..f712705411f6 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 @@ -79,8 +79,8 @@ std::string Twitch::GetMediaURL(const std::string& media_id) { // static std::string Twitch::GetTwitchStatus( - const ledger::TwitchEventInfo& old_event, - const ledger::TwitchEventInfo& new_event) { + const ledger::MediaEventInfo& old_event, + const ledger::MediaEventInfo& new_event) { std::string status = "playing"; if ( @@ -113,8 +113,8 @@ std::string Twitch::GetTwitchStatus( // static uint64_t Twitch::GetTwitchDuration( - const ledger::TwitchEventInfo& old_event, - const ledger::TwitchEventInfo& new_event) { + const ledger::MediaEventInfo& old_event, + const ledger::MediaEventInfo& new_event) { // Remove duplicated events if (old_event.event_ == new_event.event_ && old_event.time_ == new_event.time_) { @@ -299,7 +299,7 @@ void Twitch::ProcessMedia(const std::map& parts, TWITCH_MEDIA_TYPE); BLOG(ledger_, ledger::LogLevel::LOG_DEBUG) << "Media key: " << media_key; - ledger::TwitchEventInfo twitch_info; + ledger::MediaEventInfo twitch_info; std::map::const_iterator iter = parts.find("event"); if (iter != parts.end()) { twitch_info.event_ = iter->second; @@ -365,7 +365,7 @@ void Twitch::OnSaveMediaVisit( void Twitch::OnMediaPublisherInfo( const std::string& media_id, const std::string& media_key, - const ledger::TwitchEventInfo& twitch_info, + const ledger::MediaEventInfo& twitch_info, const ledger::VisitData& visit_data, const uint64_t window_id, const std::string& user_id, @@ -379,14 +379,14 @@ void Twitch::OnMediaPublisherInfo( } if (publisher_info) { - ledger::TwitchEventInfo old_event; - std::map::const_iterator iter = + ledger::MediaEventInfo old_event; + std::map::const_iterator iter = twitch_events.find(media_key); if (iter != twitch_events.end()) { old_event = iter->second; } - ledger::TwitchEventInfo new_event(twitch_info); + ledger::MediaEventInfo new_event(twitch_info); new_event.status_ = GetTwitchStatus(old_event, new_event); const uint64_t real_duration = GetTwitchDuration(old_event, new_event); @@ -408,13 +408,13 @@ void Twitch::OnMediaPublisherInfo( return; } - ledger::TwitchEventInfo old_event; + ledger::MediaEventInfo old_event; auto iter = twitch_events.find(media_key); if (iter != twitch_events.end()) { old_event = iter->second; } - ledger::TwitchEventInfo new_event(twitch_info); + ledger::MediaEventInfo new_event(twitch_info); new_event.status_ = GetTwitchStatus(old_event, new_event); const uint64_t real_duration = GetTwitchDuration(old_event, new_event); 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 23b78e34b06d..3d7ce75c2be7 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 @@ -48,12 +48,12 @@ class Twitch : public ledger::LedgerCallbackHandler { static std::string GetMediaURL(const std::string& mediaId); static std::string GetTwitchStatus( - const ledger::TwitchEventInfo& old_event, - const ledger::TwitchEventInfo& new_event); + const ledger::MediaEventInfo& old_event, + const ledger::MediaEventInfo& new_event); static uint64_t GetTwitchDuration( - const ledger::TwitchEventInfo& old_event, - const ledger::TwitchEventInfo& new_event); + const ledger::MediaEventInfo& old_event, + const ledger::MediaEventInfo& new_event); static std::string GetMediaIdFromUrl(const std::string& url, const std::string& publisher_blob); @@ -78,7 +78,7 @@ class Twitch : public ledger::LedgerCallbackHandler { void OnMediaPublisherInfo( const std::string& media_id, const std::string& media_key, - const ledger::TwitchEventInfo& twitch_info, + const ledger::MediaEventInfo& twitch_info, const ledger::VisitData& visit_data, const uint64_t window_id, const std::string& user_id, @@ -128,7 +128,7 @@ class Twitch : public ledger::LedgerCallbackHandler { const std::string& publisher_key = ""); bat_ledger::LedgerImpl* ledger_; // NOT OWNED - std::map twitch_events; + std::map twitch_events; // For testing purposes friend class MediaTwitchTest; 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 4424ae3a0422..deec8e217543 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 @@ -153,8 +153,8 @@ TEST(MediaTwitchTest, GetMediaURL) { TEST(MediaTwitchTest, GetTwitchStatus) { // empty - ledger::TwitchEventInfo old_event; - ledger::TwitchEventInfo new_event; + ledger::MediaEventInfo old_event; + ledger::MediaEventInfo new_event; std::string result = Twitch::GetTwitchStatus(old_event, new_event); ASSERT_EQ(result, "playing"); diff --git a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc index a3c255cfea3f..ed89d60f0783 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc @@ -150,14 +150,14 @@ bool PublisherBanner::loadFromJson(const std::string& json) { return !error; } -TwitchEventInfo::TwitchEventInfo() {} +MediaEventInfo::MediaEventInfo() {} -TwitchEventInfo::TwitchEventInfo(const TwitchEventInfo& info): +MediaEventInfo::MediaEventInfo(const MediaEventInfo& info): event_(info.event_), time_(info.time_), status_(info.status_) {} -TwitchEventInfo::~TwitchEventInfo() {} +MediaEventInfo::~MediaEventInfo() {} // static From 08767db3071c2d72de3ff28d034239ce5bf47332 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Wed, 24 Apr 2019 17:45:48 +0200 Subject: [PATCH 3/4] AC support --- browser/brave_rewards/rewards_tab_helper.cc | 1 + .../src/bat/ledger/internal/bat_publishers.cc | 3 + .../src/bat/ledger/internal/ledger_impl.cc | 15 +- .../src/bat/ledger/internal/media/helper.cc | 37 ++ .../src/bat/ledger/internal/media/helper.h | 3 + .../src/bat/ledger/internal/media/media.cc | 10 +- .../src/bat/ledger/internal/media/vimeo.cc | 373 +++++++++++++++++- .../src/bat/ledger/internal/media/vimeo.h | 69 +++- .../ledger/internal/media/vimeo_unittest.cc | 196 ++++++++- .../src/bat/ledger/ledger.cc | 2 +- 10 files changed, 697 insertions(+), 12 deletions(-) diff --git a/browser/brave_rewards/rewards_tab_helper.cc b/browser/brave_rewards/rewards_tab_helper.cc index 9110ce7258b1..e1fe9835d197 100644 --- a/browser/brave_rewards/rewards_tab_helper.cc +++ b/browser/brave_rewards/rewards_tab_helper.cc @@ -74,6 +74,7 @@ void RewardsTabHelper::ResourceLoadComplete( if (!rewards_service_ || !render_frame_host) return; + // TODO(nejczdovc): do we need to get anyother type then XHR?? if (resource_load_info.resource_type == content::ResourceType::kMedia || resource_load_info.resource_type == content::ResourceType::kXhr || resource_load_info.resource_type == content::ResourceType::kImage || 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 f5f9e4f19b0f..b5c10d18fba1 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 @@ -71,7 +71,10 @@ std::string getProviderName(const std::string& publisher_id) { return TWITCH_MEDIA_TYPE; } else if (publisher_id.find(TWITTER_MEDIA_TYPE) != std::string::npos) { return TWITTER_MEDIA_TYPE; + } else if (publisher_id.find(VIMEO_MEDIA_TYPE) != std::string::npos) { + return VIMEO_MEDIA_TYPE; } + return ""; } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc index 9df51585211b..575f40a742cb 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc @@ -234,12 +234,23 @@ void LedgerImpl::OnPostData( return; } - std::vector> twitchParts; - if (TWITCH_MEDIA_TYPE == type) { + if (type == TWITCH_MEDIA_TYPE) { + std::vector> twitchParts; braveledger_media::GetTwitchParts(post_data, &twitchParts); for (size_t i = 0; i < twitchParts.size(); i++) { bat_media_->ProcessMedia(twitchParts[i], type, std::move(visit_data)); } + return; + } + + if (type == VIMEO_MEDIA_TYPE) { + std::vector> parts; + braveledger_media::GetVimeoParts(post_data, &parts); + + for (size_t i = 0; i < parts.size(); i++) { + bat_get_media_->ProcessMedia(parts[i], type, std::move(visit_data)); + } + return; } } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/helper.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/media/helper.cc index 90094b422fe1..ee4696f48560 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/helper.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/helper.cc @@ -3,6 +3,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include "base/json/json_reader.h" #include "bat/ledger/internal/media/helper.h" #include "bat/ledger/internal/bat_helper.h" @@ -68,4 +69,40 @@ std::string ExtractData(const std::string& data, return match; } +void GetVimeoParts( + const std::string& query, + std::vector>* parts) { + base::Optional data = base::JSONReader::Read(query); + if (!data || !data->is_list()) { + return; + } + + for (auto& item : data->GetList()) { + if (item.is_dict()) { + std::map part; + auto* name = item.FindKey("name"); + if (name) { + part.emplace("event", name->GetString()); + } + + auto* clip_id = item.FindKey("clip_id"); + if (clip_id) { + part.emplace("video_id", std::to_string(clip_id->GetInt())); + } + + auto* product = item.FindKey("product"); + if (product) { + part.emplace("type", product->GetString()); + } + + auto* video_time = item.FindKey("video_time"); + if (video_time) { + part.emplace("time", std::to_string(video_time->GetDouble())); + } + + parts->push_back(part); + } + } +} + } // namespace braveledger_media diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/helper.h b/vendor/bat-native-ledger/src/bat/ledger/internal/media/helper.h index 6f852bfdb8ba..0656757e34a0 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/helper.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/helper.h @@ -27,6 +27,9 @@ std::string ExtractData(const std::string& data, const std::string& match_after, const std::string& match_until); +void GetVimeoParts(const std::string& query, + std::vector>* parts); + } // namespace braveledger_media #endif // BRAVELEDGER_MEDIA_HELPER_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/media.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/media/media.cc index 86d2ad262b76..c1a45f85b691 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/media.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/media.cc @@ -20,7 +20,8 @@ Media::Media(bat_ledger::LedgerImpl* ledger): media_youtube_(new braveledger_media::YouTube(ledger)), media_twitch_(new braveledger_media::Twitch(ledger)), media_twitter_(new braveledger_media::Twitter(ledger)), - media_reddit_(new braveledger_media::Reddit(ledger)) { + media_reddit_(new braveledger_media::Reddit(ledger)), + media_vimeo_(new braveledger_media::Vimeo(ledger)) { } Media::~Media() {} @@ -37,7 +38,7 @@ std::string Media::GetLinkType(const std::string& url, referrer); } if (type.empty()) { - type = braveledger_media::MediaVimeo::GetLinkType(url); + type = braveledger_media::Vimeo::GetLinkType(url); } return type; @@ -59,6 +60,11 @@ void Media::ProcessMedia(const std::map& parts, media_twitch_->ProcessMedia(parts, *visit_data); return; } + + if (type == VIMEO_MEDIA_TYPE) { + media_vimeo_->ProcessMedia(parts); + return; + } } void Media::GetMediaActivityFromUrl( diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.cc index 003ce270bb12..a84c7e3c66b4 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.cc @@ -3,12 +3,19 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include +#include +#include + +#include "base/strings/stringprintf.h" #include "bat/ledger/internal/bat_helper.h" #include "bat/ledger/internal/ledger_impl.h" #include "bat/ledger/internal/media/vimeo.h" +#include "net/http/http_status_code.h" using std::placeholders::_1; using std::placeholders::_2; +using std::placeholders::_3; namespace braveledger_media { @@ -21,13 +28,228 @@ Vimeo::~Vimeo() { // static std::string Vimeo::GetLinkType(const std::string& url) { - std::string type = VIMEO_MEDIA_TYPE; + const std::string api = "https://fresnel.vimeocdn.com/add/player-stats?"; + std::string type; + + if (!url.empty() && url.find(api) != std::string::npos) { + type = VIMEO_MEDIA_TYPE; + } + return type; } -void Vimeo::ProcessMedia(const std::map& parts, - const ledger::VisitData& visit_data) { +// static +std::string Vimeo::GetVideoUrl(const std::string& video_id) { + if (video_id.empty()) { + return std::string(); + } + + return "https://vimeo.com/" + video_id; +} + +// static +std::string Vimeo::GetMediaKey(const std::string& video_id, + const std::string& type) { + if (video_id.empty()) { + return std::string(); + } + + if (type == "vimeo-vod") { + return (std::string)VIMEO_MEDIA_TYPE + "_" + video_id; + } + + return std::string(); +} + +// static +std::string Vimeo::GetPublisherKey(const std::string& key) { + if (key.empty()) { + return std::string(); + } + + return (std::string)VIMEO_MEDIA_TYPE + "#channel:" + key; +} + +// static +std::string Vimeo::GetIdFromVideoPage(const std::string& data) { + if (data.empty()) { + return ""; + } + + return braveledger_media::ExtractData(data, + "\\/i.vimeocdn.com\\/portrait\\/", "_75x75"); +} + +// static +std::string Vimeo::GenerateFaviconUrl(const std::string& id) { + if (id.empty()) { + return std::string(); + } + + return base::StringPrintf("https://i.vimeocdn.com/portrait/%s_300x300.webp", + id.c_str()); +} + +// static +std::string Vimeo::GetNameFromVideoPage(const std::string& data) { + if (data.empty()) { + return std::string(); + } + + const std::string name = + braveledger_media::ExtractData(data, ",\"display_name\":\"", "\""); + + std::string publisher_name; + const std::string publisher_json = "{\"brave_publisher\":\"" + + name + "\"}"; + // scraped data could come in with JSON code points added. + // Make to JSON object above so we can decode. + braveledger_bat_helper::getJSONValue( + "brave_publisher", publisher_json, &publisher_name); + return publisher_name; +} + +// static +std::string Vimeo::GetPublisherUrl(const std::string& data) { + if (data.empty()) { + return std::string(); + } + + const std::string wrapper = braveledger_media::ExtractData(data, + "", ""); + + const std::string name = braveledger_media::ExtractData(wrapper, + ""); + + if (name.empty()) { + return std::string(); + } + + return base::StringPrintf("https://vimeo.com/%s/videos", + name.c_str()); +} + +// static +bool Vimeo::AllowedEvent(const std::string& event) { + if (event.empty()) { + return false; + } + + const std::vector allowed = { + "video-start-time", + "video-minute-watched", + "video-paused", + "video-played", + "video-seek", + "video-seeked"}; + auto it = std::find(allowed.begin(), allowed.end(), event); + return it != allowed.end(); +} + +// static +uint64_t Vimeo::GetDuration( + const ledger::MediaEventInfo& old_event, + const ledger::MediaEventInfo& new_event) { + // Remove duplicated events + if (old_event.event_ == new_event.event_ && + old_event.time_ == new_event.time_) { + return 0u; + } + + double time = 0.0; + std::stringstream tempNew(new_event.time_); + double newTime = 0.0; + tempNew >> newTime; + + // Video started + if (new_event.event_ == "video-start-time") { + time = newTime; + } else { + std::stringstream tempOld(old_event.time_); + double oldTime = 0; + tempOld >> oldTime; + + if (new_event.event_ == "video-minute-watched" || + new_event.event_ == "video-paused") { + time = newTime - oldTime; + } + } + + return static_cast(std::round(time)); +} + +void Vimeo::FetchDataFromUrl( + const std::string& url, + braveledger_media::FetchDataFromUrlCallback callback) { + ledger_->LoadURL(url, + std::vector(), + std::string(), + std::string(), + ledger::URL_METHOD::GET, + callback); +} + +void Vimeo::OnMediaActivityError(uint64_t window_id) { + const std::string url = TWITTER_TLD; + const std::string name = TWITTER_MEDIA_TYPE; + + DCHECK(!url.empty()); + + ledger::VisitData new_data; + new_data.domain = url; + new_data.url = "https://" + url; + new_data.path = "/"; + new_data.name = name; + + ledger_->GetPublisherActivityFromUrl(window_id, + ledger::VisitData::New(new_data), + ""); +} + +void Vimeo::ProcessMedia(const std::map& parts) { + auto iter = parts.find("video_id"); + std::string media_id; + if (iter != parts.end()) { + media_id = iter->second; + } + + if (media_id.empty()) { + return; + } + + std::string type; + iter = parts.find("type"); + if (iter != parts.end()) { + type = iter->second; + } + + const std::string media_key = GetMediaKey(media_id, type); + + ledger::MediaEventInfo event_info; + iter = parts.find("event"); + if (iter != parts.end()) { + event_info.event_ = iter->second; + } + + // We should only record events that are relevant to us + if (!AllowedEvent(event_info.event_)) { + return; + } + + iter = parts.find("time"); + if (iter != parts.end()) { + event_info.time_ = iter->second; + } + + ledger_->GetMediaPublisherInfo(media_key, + std::bind(&Vimeo::OnMediaPublisherInfo, + this, + media_id, + media_key, + event_info, + _1, + _2)); } void Vimeo::ProcessActivityFromUrl(uint64_t window_id, @@ -35,4 +257,149 @@ void Vimeo::ProcessActivityFromUrl(uint64_t window_id, } +void Vimeo::OnMediaPublisherInfo( + const std::string& media_id, + const std::string& media_key, + const ledger::MediaEventInfo& event_info, + ledger::Result result, + ledger::PublisherInfoPtr publisher_info) { + if (result != ledger::Result::LEDGER_OK && + result != ledger::Result::NOT_FOUND) { + BLOG(ledger_, ledger::LogLevel::LOG_ERROR) + << "Failed to get publisher info"; + return; + } + + if (!publisher_info && !publisher_info.get()) { + auto callback = std::bind(&Vimeo::OnPublisherVideoPage, + this, + media_key, + event_info, + _1, + _2, + _3); + + FetchDataFromUrl(GetVideoUrl(media_id), callback); + } else { + ledger::MediaEventInfo old_event; + auto iter = events.find(media_key); + if (iter != events.end()) { + old_event = iter->second; + } + + const uint64_t duration = GetDuration(old_event, event_info); + events[media_key] = event_info; + + SavePublisherInfo(std::string(), + duration, + std::string(), + publisher_info->name, + publisher_info->url, + 0, + publisher_info->id, + publisher_info->favicon_url); + } +} + +void Vimeo::OnPublisherVideoPage( + const std::string& media_key, + ledger::MediaEventInfo event_info, + int response_status_code, + const std::string& response, + const std::map& headers) { + if (response_status_code != net::HTTP_OK) { + OnMediaActivityError(); + return; + } + + ledger_->LogResponse( + __func__, + response_status_code, + "HTML from Vimeo Video page", + headers); + + const std::string user_id = GetIdFromVideoPage(response); + + if (user_id.empty()) { + return; + } + + ledger::MediaEventInfo old_event; + auto iter = events.find(media_key); + if (iter != events.end()) { + old_event = iter->second; + } + + const uint64_t duration = GetDuration(old_event, event_info); + events[media_key] = event_info; + + SavePublisherInfo(media_key, + duration, + user_id, + GetNameFromVideoPage(response), + GetPublisherUrl(response), + 0); +} + +void Vimeo::OnSaveMediaVisit( + ledger::Result result, + ledger::PublisherInfoPtr info) { +} + +void Vimeo::SavePublisherInfo( + const std::string& media_key, + const uint64_t duration, + const std::string& user_id, + const std::string& publisher_name, + const std::string& publisher_url, + const uint64_t window_id, + const std::string& publisher_key, + const std::string& publisher_favicon) { + + if (user_id.empty() && publisher_key.empty()) { + OnMediaActivityError(window_id); + BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << + "User id is missing for: " << media_key; + return; + } + + auto callback = std::bind(&Vimeo::OnSaveMediaVisit, + this, + _1, + _2); + + std::string key = publisher_key; + if (key.empty()) { + key = GetPublisherKey(user_id); + } + + if (key.empty()) { + OnMediaActivityError(window_id); + BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << + "Publisher key is missing for: " << media_key; + return; + } + + std::string icon = publisher_favicon; + if (icon.empty()) { + icon = GenerateFaviconUrl(user_id); + } + + ledger::VisitData visit_data; + visit_data.provider = VIMEO_MEDIA_TYPE; + visit_data.url = publisher_url; + visit_data.favicon_url = icon; + visit_data.name = publisher_name; + + ledger_->SaveMediaVisit(key, + visit_data, + duration, + window_id, + callback); + + if (!media_key.empty()) { + ledger_->SetMediaPublisherInfo(media_key, publisher_key); + } +} + } // namespace braveledger_media diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.h b/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.h index 6d69f146b2f2..ced90c881c7a 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.h @@ -6,7 +6,10 @@ #ifndef BRAVELEDGER_MEDIA_VIMEO_H_ #define BRAVELEDGER_MEDIA_VIMEO_H_ +#include + #include +#include #include #include "base/gtest_prod_util.h" @@ -25,8 +28,7 @@ class Vimeo : public ledger::LedgerCallbackHandler { ~Vimeo() override; - void ProcessMedia(const std::map& parts, - const ledger::VisitData& visit_data); + void ProcessMedia(const std::map& parts); static std::string GetLinkType(const std::string& url); @@ -34,11 +36,74 @@ class Vimeo : public ledger::LedgerCallbackHandler { const ledger::VisitData& visit_data); private: + static std::string GetVideoUrl(const std::string& video_id); + + static std::string GetMediaKey(const std::string& video_id, + const std::string& type); + + static std::string GetPublisherKey(const std::string& key); + + static std::string GetIdFromVideoPage(const std::string& data); + + static std::string GenerateFaviconUrl(const std::string& id); + + static std::string GetNameFromVideoPage(const std::string& data); + + static std::string GetPublisherUrl(const std::string& data); + + static bool AllowedEvent(const std::string& event); + + static uint64_t GetDuration(const ledger::MediaEventInfo& old_event, + const ledger::MediaEventInfo& new_event); + + void FetchDataFromUrl( + const std::string& url, + braveledger_media::FetchDataFromUrlCallback callback); + + void OnMediaActivityError(uint64_t window_id = 0); + + void OnMediaPublisherInfo( + const std::string& media_id, + const std::string& media_key, + const ledger::MediaEventInfo& event_info, + ledger::Result result, + ledger::PublisherInfoPtr publisher_info); + + void OnPublisherVideoPage( + const std::string& media_key, + ledger::MediaEventInfo event_info, + int response_status_code, + const std::string& response, + const std::map& headers); + + void OnSaveMediaVisit( + ledger::Result result, + ledger::PublisherInfoPtr info); + + void SavePublisherInfo( + const std::string& media_key, + const uint64_t duration, + const std::string& user_id, + const std::string& publisher_name, + const std::string& publisher_url, + const uint64_t window_id, + const std::string& publisher_key = "", + const std::string& publisher_favicon = ""); bat_ledger::LedgerImpl* ledger_; // NOT OWNED + std::map events; // For testing purposes friend class VimeoTest; + FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetVideoUrl); + FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetMediaKey); + FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetPublisherKey); + FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetIdFromVideoPage); + FRIEND_TEST_ALL_PREFIXES(VimeoTest, GenerateFaviconUrl); + FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetNameFromVideoPage); + FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetPublisherUrl); + FRIEND_TEST_ALL_PREFIXES(VimeoTest, AllowedEvent); + FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetDuration); }; } // namespace braveledger_media diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo_unittest.cc index 49e0d3c69c16..b7b2fcbdf46b 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo_unittest.cc @@ -14,10 +14,202 @@ namespace braveledger_media { class VimeoTest : public testing::Test { }; +const char profile_html[] = + "
" + "

+    " + "
"; + +const char page_config[] = + "window.vimeo.clip_page_config = {\"clip\":{\"id\":331165963,\"title\"" + ":\"IMG_2306\",\"description\":null,\"uploaded_on\":\"2019-04-18 " + "03:15:32\",\"uploaded_on_relative\":\"3 weeks ago\",\"uploaded_on_full" + "\":\"Thursday, April 18, 2019 at 3:15 AM EST\",\"is_spatial\":false,\"" + "is_hdr\":false,\"privacy\":{\"is_public\":true,\"type\":\"anybody\",\"" + "description\":\"Public\"},\"duration\":{\"raw\":72,\"formatted\":\"" + "01:12\"},\"is_liked\":false,\"is_unavailable\":false,\"likes_url\":" + "\"/331165963/likes\",\"is_live\":false,\"unlisted_hash\":null}," + "\"owner\":{\"id\":97518779,\"display_name\":\"Nejcé\"," + "\"has_advanced_stats\":false}"; + +const char user_link[] = + "

from" + "Nejc" + "

" + "
"; + TEST(VimeoTest, GetLinkType) { - // TODO(nejczdovc): change me - std::string result = Vimeo::GetLinkType("https://vimeo.com/video/32342"); + // empty url + std::string result = MediaVimeo::GetLinkType(""); + ASSERT_EQ(result, std::string()); + + // wrong url + result = MediaVimeo::GetLinkType("https://vimeo.com/video/32342"); + ASSERT_EQ(result, std::string()); + + // all good + result = MediaVimeo::GetLinkType( + "https://fresnel.vimeocdn.com/add/player-stats?id=43324123412342"); ASSERT_EQ(result, "vimeo"); } +TEST(VimeoTest, GetVideoUrl) { + // empty id + std::string result = MediaVimeo::GetVideoUrl(""); + ASSERT_EQ(result, std::string()); + + // all good + result = MediaVimeo::GetVideoUrl("234123423"); + ASSERT_EQ(result, "https://vimeo.com/234123423"); +} + +TEST(VimeoTest, GetMediaKey) { + // empty id + std::string result = MediaVimeo::GetMediaKey("", ""); + ASSERT_EQ(result, std::string()); + + // wrong type + result = MediaVimeo::GetMediaKey("234123423", "wrong"); + ASSERT_EQ(result, std::string()); + + // all good + result = MediaVimeo::GetMediaKey("234123423", "vimeo-vod"); + ASSERT_EQ(result, "vimeo_234123423"); +} + +TEST(VimeoTest, GetPublisherKey) { + // empty id + std::string result = MediaVimeo::GetPublisherKey(""); + ASSERT_EQ(result, std::string()); + + // all good + result = MediaVimeo::GetPublisherKey("234123423"); + ASSERT_EQ(result, "vimeo#channel:234123423"); +} + +TEST(VimeoTest, GetIdFromVideoPage) { + // empty id + std::string result = MediaVimeo::GetIdFromVideoPage(""); + ASSERT_EQ(result, std::string()); + + // strange string +result = MediaVimeo::GetIdFromVideoPage("asdfasdfasdfasdfff sdf"); + ASSERT_EQ(result, std::string()); + + // all good + result = MediaVimeo::GetIdFromVideoPage(profile_html); + ASSERT_EQ(result, "31487122"); +} + +TEST(VimeoTest, GenerateFaviconUrl) { + // empty id + std::string result = MediaVimeo::GenerateFaviconUrl(""); + ASSERT_EQ(result, std::string()); + + // all good + result = MediaVimeo::GenerateFaviconUrl("234123423"); + ASSERT_EQ(result, "https://i.vimeocdn.com/portrait/234123423_300x300.webp"); +} + +TEST(VimeoTest, GetNameFromVideoPage) { + // empty data + std::string result = MediaVimeo::GetNameFromVideoPage(""); + ASSERT_EQ(result, std::string()); + + // random data + result = MediaVimeo::GetNameFromVideoPage("asdfsdfdsf sdfdsf"); + ASSERT_EQ(result, std::string()); + + // all good + result = MediaVimeo::GetNameFromVideoPage(page_config); + ASSERT_EQ(result, "Nejcé"); +} + +TEST(VimeoTest, GetPublisherUrl) { + // empty data + std::string result = MediaVimeo::GetPublisherUrl(""); + ASSERT_EQ(result, std::string()); + + // random data + result = MediaVimeo::GetPublisherUrl("asdfsdfdsf sdfdsf"); + ASSERT_EQ(result, std::string()); + + // all good + result = MediaVimeo::GetPublisherUrl(user_link); + ASSERT_EQ(result, "https://vimeo.com/nejcbrave/videos"); +} + +TEST(VimeoTest, AllowedEvent) { + // empty event + bool result = MediaVimeo::AllowedEvent(""); + ASSERT_EQ(result, false); + + // random event + result = MediaVimeo::AllowedEvent("wrong"); + ASSERT_EQ(result, false); + + // all good + result = MediaVimeo::AllowedEvent("video-played"); + ASSERT_EQ(result, true); +} + +TEST(VimeoTest, GetDuration) { + ledger::MediaEventInfo old_event; + ledger::MediaEventInfo new_event; + + // empty events + uint64_t result = MediaVimeo::GetDuration(old_event, new_event); + ASSERT_EQ(result, 0u); + + // remove duplicated events + old_event.event_ = "video-played"; + old_event.time_ = "1.0"; + new_event.event_ = "video-played"; + new_event.time_ = "1.0"; + result = MediaVimeo::GetDuration(old_event, new_event); + ASSERT_EQ(result, 0u); + + // video started + new_event.event_ = "video-start-time"; + new_event.time_ = "2.0"; + result = MediaVimeo::GetDuration(old_event, new_event); + ASSERT_EQ(result, 2u); + + // watch event + old_event.event_ = "video-start-time"; + old_event.time_ = "2.0"; + new_event.event_ = "video-minute-watched"; + new_event.time_ = "5.1"; + result = MediaVimeo::GetDuration(old_event, new_event); + ASSERT_EQ(result, 3u); + + // video paused / video ended + old_event.event_ = "video-minute-watched"; + old_event.time_ = "5.1"; + new_event.event_ = "video-paused"; + new_event.time_ = "20.8"; + result = MediaVimeo::GetDuration(old_event, new_event); + ASSERT_EQ(result, 16u); +} + } // namespace braveledger_media diff --git a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc index ed89d60f0783..cf1d47ab8c9c 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc @@ -384,7 +384,7 @@ bool Ledger::IsMediaLink(const std::string& url, first_party_url, referrer); - return type == TWITCH_MEDIA_TYPE; + return type == TWITCH_MEDIA_TYPE || type == VIMEO_MEDIA_TYPE; } } // namespace ledger From 3fa6bbd234afbab38852098fc574500d78138d00 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Fri, 7 Jun 2019 14:06:37 +0200 Subject: [PATCH 4/4] Panel support --- .../src/bat/ledger/internal/bat_publishers.cc | 5 +- .../src/bat/ledger/internal/ledger_impl.cc | 4 +- .../src/bat/ledger/internal/media/helper.cc | 2 +- .../src/bat/ledger/internal/media/media.cc | 40 +- .../src/bat/ledger/internal/media/vimeo.cc | 405 +++++++++++++++--- .../src/bat/ledger/internal/media/vimeo.h | 57 ++- .../ledger/internal/media/vimeo_unittest.cc | 224 +++++++--- .../src/bat/ledger/internal/static_values.h | 2 + .../Ledger/Generated/Records+Private.h | 6 +- vendor/brave-ios/Ledger/Generated/Records.h | 6 +- vendor/brave-ios/Ledger/Generated/Records.mm | 4 +- 11 files changed, 610 insertions(+), 145 deletions(-) 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 b5c10d18fba1..9d703455786f 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 @@ -711,7 +711,8 @@ void BatPublishers::getPublisherActivityFromUrl( const bool is_media = visit_data.domain == YOUTUBE_TLD || visit_data.domain == TWITCH_TLD || visit_data.domain == TWITTER_TLD || - visit_data.domain == REDDIT_TLD; + visit_data.domain == REDDIT_TLD || + visit_data.domain == VIMEO_TLD; if (is_media && visit_data.path != "" && visit_data.path != "/") { @@ -722,6 +723,8 @@ void BatPublishers::getPublisherActivityFromUrl( type = TWITTER_MEDIA_TYPE; } else if (visit_data.domain == REDDIT_TLD) { type = REDDIT_MEDIA_TYPE; + } else if (visit_data.domain == VIMEO_TLD) { + type = VIMEO_MEDIA_TYPE; } ledger::VisitDataPtr new_visit_data = ledger::VisitData::New(visit_data); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc index 575f40a742cb..fed5d0b877af 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc @@ -247,8 +247,8 @@ void LedgerImpl::OnPostData( std::vector> parts; braveledger_media::GetVimeoParts(post_data, &parts); - for (size_t i = 0; i < parts.size(); i++) { - bat_get_media_->ProcessMedia(parts[i], type, std::move(visit_data)); + for (auto part = parts.begin(); part != parts.end(); part++) { + bat_media_->ProcessMedia(*part, type, std::move(visit_data)); } return; } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/helper.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/media/helper.cc index ee4696f48560..73298499d922 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/helper.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/helper.cc @@ -77,7 +77,7 @@ void GetVimeoParts( return; } - for (auto& item : data->GetList()) { + for (const auto& item : data->GetList()) { if (item.is_dict()) { std::map part; auto* name = item.FindKey("name"); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/media.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/media/media.cc index c1a45f85b691..6a4e209eca5e 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/media.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/media.cc @@ -83,6 +83,8 @@ void Media::GetMediaActivityFromUrl( *visit_data); } else if (type == REDDIT_MEDIA_TYPE) { media_reddit_->ProcessActivityFromUrl(window_id, *visit_data); + } else if (type == VIMEO_MEDIA_TYPE) { + media_vimeo_->ProcessActivityFromUrl(window_id, *visit_data); } else { OnMediaActivityError(std::move(visit_data), type, window_id); } @@ -99,23 +101,30 @@ void Media::OnMediaActivityError(ledger::VisitDataPtr visit_data, } else if (type == TWITCH_MEDIA_TYPE) { url = TWITCH_TLD; name = TWITCH_MEDIA_TYPE; + } else if (type == TWITTER_MEDIA_TYPE) { + url = TWITTER_TLD; + name = TWITTER_MEDIA_TYPE; + } else if (type == REDDIT_MEDIA_TYPE) { + url = REDDIT_TLD; + name = REDDIT_MEDIA_TYPE; + } else if (type == VIMEO_MEDIA_TYPE) { + url = VIMEO_TLD; + name = VIMEO_MEDIA_TYPE; } - if (!url.empty()) { - visit_data->domain = url; - visit_data->url = "https://" + url; - visit_data->path = "/"; - visit_data->name = name; - - ledger_->GetPublisherActivityFromUrl( - window_id, std::move(visit_data), std::string()); - } else { - BLOG(ledger_, ledger::LogLevel::LOG_ERROR) - << "Media activity error for " - << type << " (name: " - << name << ", url: " - << visit_data->url << ")"; + if (url.empty()) { + BLOG(ledger_, ledger::LogLevel::LOG_ERROR) + << "Media activity error for url: " + << visit_data->url; + return; } + + visit_data->domain = url; + visit_data->url = "https://" + url; + visit_data->path = "/"; + visit_data->name = name; + + ledger_->GetPublisherActivityFromUrl(window_id, std::move(visit_data), ""); } void Media::SaveMediaInfo(const std::string& type, @@ -134,8 +143,9 @@ void Media::SaveMediaInfo(const std::string& type, std::string Media::GetShareURL( const std::string& type, const std::map& args) { - if (type == TWITTER_MEDIA_TYPE) + if (type == TWITTER_MEDIA_TYPE) { return braveledger_media::Twitter::GetShareURL(args); + } return std::string(); } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.cc index a84c7e3c66b4..4f6eafadbf1c 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.cc @@ -5,8 +5,10 @@ #include #include +#include #include +#include "base/json/json_reader.h" #include "base/strings/stringprintf.h" #include "bat/ledger/internal/bat_helper.h" #include "bat/ledger/internal/ledger_impl.h" @@ -41,7 +43,7 @@ std::string Vimeo::GetLinkType(const std::string& url) { // static std::string Vimeo::GetVideoUrl(const std::string& video_id) { if (video_id.empty()) { - return std::string(); + return ""; } return "https://vimeo.com/" + video_id; @@ -51,20 +53,20 @@ std::string Vimeo::GetVideoUrl(const std::string& video_id) { std::string Vimeo::GetMediaKey(const std::string& video_id, const std::string& type) { if (video_id.empty()) { - return std::string(); + return ""; } if (type == "vimeo-vod") { return (std::string)VIMEO_MEDIA_TYPE + "_" + video_id; } - return std::string(); + return ""; } // static std::string Vimeo::GetPublisherKey(const std::string& key) { if (key.empty()) { - return std::string(); + return ""; } return (std::string)VIMEO_MEDIA_TYPE + "#channel:" + key; @@ -77,13 +79,13 @@ std::string Vimeo::GetIdFromVideoPage(const std::string& data) { } return braveledger_media::ExtractData(data, - "\\/i.vimeocdn.com\\/portrait\\/", "_75x75"); + "\"creator_id\":", ","); } // static std::string Vimeo::GenerateFaviconUrl(const std::string& id) { if (id.empty()) { - return std::string(); + return ""; } return base::StringPrintf("https://i.vimeocdn.com/portrait/%s_300x300.webp", @@ -93,26 +95,16 @@ std::string Vimeo::GenerateFaviconUrl(const std::string& id) { // static std::string Vimeo::GetNameFromVideoPage(const std::string& data) { if (data.empty()) { - return std::string(); + return ""; } - const std::string name = - braveledger_media::ExtractData(data, ",\"display_name\":\"", "\""); - - std::string publisher_name; - const std::string publisher_json = "{\"brave_publisher\":\"" + - name + "\"}"; - // scraped data could come in with JSON code points added. - // Make to JSON object above so we can decode. - braveledger_bat_helper::getJSONValue( - "brave_publisher", publisher_json, &publisher_name); - return publisher_name; + return braveledger_media::ExtractData(data, ",\"display_name\":\"", "\""); } // static -std::string Vimeo::GetPublisherUrl(const std::string& data) { +std::string Vimeo::GetUrlFromVideoPage(const std::string& data) { if (data.empty()) { - return std::string(); + return ""; } const std::string wrapper = braveledger_media::ExtractData(data, @@ -122,7 +114,7 @@ std::string Vimeo::GetPublisherUrl(const std::string& data) { ""); if (name.empty()) { - return std::string(); + return ""; } return base::StringPrintf("https://vimeo.com/%s/videos", @@ -179,20 +171,111 @@ uint64_t Vimeo::GetDuration( return static_cast(std::round(time)); } +// static +bool Vimeo::IsExcludedPath(const std::string& path) { + if (path.empty()) { + return true; + } + + const std::vector paths({ + "/", + "/log_in", + "/upgrade", + "/live", + "/watch", + "/videoschool", + "/upload", + "/ondemand", + "/ott", + "/site_map", + "/blog", + "/help", + "/about", + "/jobs", + "/stats", + "/watchlater", + "/purchases", + "/settings", + "/stock", + }); + + for (std::string str_path : paths) { + if (str_path == path || str_path + "/" == path) { + return true; + } + } + + const std::vector patterns({ + "/features/", + "/categories/", + "/blog/", + "/ott/", + "/help/", + "/manage/", + "/settings/", + "/stock/", + }); + + for (std::string str_path : patterns) { + if (base::StartsWith(path, + str_path, + base::CompareCase::INSENSITIVE_ASCII)) { + return true; + } + } + + + return false; +} + +// static +std::string Vimeo::GetIdFromPublisherPage(const std::string& data) { + if (data.empty()) { + return ""; + } + + return braveledger_media::ExtractData( + data, + "data-deep-link=\"users/", + "\""); +} + +// static +std::string Vimeo::GetNameFromPublisherPage(const std::string& data) { + if (data.empty()) { + return ""; + } + + return braveledger_media::ExtractData(data, + "LoadURL(url, std::vector(), - std::string(), - std::string(), + "", + "", ledger::URL_METHOD::GET, callback); } void Vimeo::OnMediaActivityError(uint64_t window_id) { - const std::string url = TWITTER_TLD; - const std::string name = TWITTER_MEDIA_TYPE; + const std::string url = VIMEO_TLD; + const std::string name = VIMEO_MEDIA_TYPE; DCHECK(!url.empty()); @@ -253,8 +336,226 @@ void Vimeo::ProcessMedia(const std::map& parts) { } void Vimeo::ProcessActivityFromUrl(uint64_t window_id, - const ledger::VisitData& visit_data) { + const ledger::VisitData& visit_data) { + // not all url's are publisher specific + if (IsExcludedPath(visit_data.path)) { + OnMediaActivityError(window_id); + return; + } + + const std::string url = (std::string)VIMEO_PROVIDER_URL + + "?url=" + + ledger_->URIEncode(visit_data.url); + + auto callback = std::bind(&Vimeo::OnEmbedResponse, + this, + visit_data, + window_id, + _1, + _2, + _3); + + FetchDataFromUrl(url, callback); +} + +void Vimeo::OnEmbedResponse( + const ledger::VisitData& visit_data, + const uint64_t window_id, + int response_status_code, + const std::string& response, + const std::map& headers) { + ledger_->LogResponse(__func__, response_status_code, response, headers); + + if (response_status_code != net::HTTP_OK) { + auto callback = std::bind(&Vimeo::OnUnknownPage, + this, + visit_data, + window_id, + _1, + _2, + _3); + + FetchDataFromUrl(visit_data.url, callback); + return; + } + + base::Optional data = base::JSONReader::Read(response); + if (!data || !data->is_dict()) { + auto callback = std::bind(&Vimeo::OnUnknownPage, + this, + visit_data, + window_id, + _1, + _2, + _3); + + FetchDataFromUrl(visit_data.url, callback); + return; + } + + DCHECK(data->FindStringKey("author_name")); + const std::string publisher_name = *data->FindStringKey("author_name"); + DCHECK(data->FindStringKey("author_url")); + const std::string publisher_url = *data->FindStringKey("author_url"); + DCHECK(data->FindIntKey("video_id")); + const int video_id = *data->FindIntKey("video_id"); + + if (publisher_url.empty()) { + auto callback = std::bind(&Vimeo::OnUnknownPage, + this, + visit_data, + window_id, + _1, + _2, + _3); + + FetchDataFromUrl(visit_data.url, callback); + return; + } + + const std::string media_key = GetMediaKey(std::to_string(video_id), + "vimeo-vod"); + + auto callback = std::bind(&Vimeo::OnPublisherPage, + this, + media_key, + publisher_url, + publisher_name, + visit_data, + window_id, + _1, + _2, + _3); + + FetchDataFromUrl(publisher_url, callback); +} + +void Vimeo::OnPublisherPage( + const std::string& media_key, + const std::string& publisher_url, + const std::string& publisher_name, + const ledger::VisitData& visit_data, + const uint64_t window_id, + int response_status_code, + const std::string& response, + const std::map& headers) { + ledger_->LogResponse( + __func__, + response_status_code, + "HTML from Vimeo publisher page", + headers); + + if (response_status_code != net::HTTP_OK) { + OnMediaActivityError(window_id); + return; + } + + const std::string user_id = GetIdFromPublisherPage(response); + const std::string publisher_key = GetPublisherKey(user_id); + + GetPublisherPanleInfo(media_key, + window_id, + publisher_url, + publisher_key, + publisher_name, + user_id); +} + +void Vimeo::OnUnknownPage( + const ledger::VisitData& visit_data, + const uint64_t window_id, + int response_status_code, + const std::string& response, + const std::map& headers) { + ledger_->LogResponse( + __func__, + response_status_code, + "HTML from Vimeo unknown page", + headers); + + if (response_status_code != net::HTTP_OK) { + OnMediaActivityError(window_id); + return; + } + + std::string user_id = GetIdFromPublisherPage(response); + std::string publisher_name; + std::string media_key; + if (!user_id.empty()) { + // we are on publisher page + publisher_name = GetNameFromPublisherPage(response); + } else { + user_id = GetIdFromVideoPage(response); + + if (user_id.empty()) { + OnMediaActivityError(window_id); + return; + } + + // we are on video page + publisher_name = GetNameFromVideoPage(response); + media_key = GetMediaKey(GetVideoIdFromVideoPage(response), + "vimeo-vod"); + } + + if (publisher_name.empty()) { + OnMediaActivityError(window_id); + return; + } + + const std::string publisher_key = GetPublisherKey(user_id); + GetPublisherPanleInfo(media_key, + window_id, + visit_data.url, + publisher_key, + publisher_name, + user_id); +} + +void Vimeo::OnPublisherPanleInfo( + const std::string& media_key, + uint64_t window_id, + const std::string& publisher_url, + const std::string& publisher_name, + const std::string& user_id, + ledger::Result result, + ledger::PublisherInfoPtr info) { + if (!info || result == ledger::Result::NOT_FOUND) { + SavePublisherInfo(media_key, + 0, + user_id, + publisher_name, + publisher_url, + window_id); + } else { + ledger_->OnPanelPublisherInfo(result, std::move(info), window_id); + } +} +void Vimeo::GetPublisherPanleInfo( + const std::string& media_key, + uint64_t window_id, + const std::string& publisher_url, + const std::string& publisher_key, + const std::string& publisher_name, + const std::string& user_id) { + const auto filter = ledger_->CreateActivityFilter( + publisher_key, + ledger::EXCLUDE_FILTER::FILTER_ALL, + false, + ledger_->GetReconcileStamp(), + true, + false); + ledger_->GetPanelPublisherInfo(filter, + std::bind(&Vimeo::OnPublisherPanleInfo, + this, + media_key, + window_id, + publisher_url, + publisher_name, + user_id, + _1, + _2)); } void Vimeo::OnMediaPublisherInfo( @@ -265,6 +566,7 @@ void Vimeo::OnMediaPublisherInfo( ledger::PublisherInfoPtr publisher_info) { if (result != ledger::Result::LEDGER_OK && result != ledger::Result::NOT_FOUND) { + OnMediaActivityError(); BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << "Failed to get publisher info"; return; @@ -280,25 +582,26 @@ void Vimeo::OnMediaPublisherInfo( _3); FetchDataFromUrl(GetVideoUrl(media_id), callback); - } else { - ledger::MediaEventInfo old_event; - auto iter = events.find(media_key); - if (iter != events.end()) { - old_event = iter->second; - } - - const uint64_t duration = GetDuration(old_event, event_info); - events[media_key] = event_info; + return; + } - SavePublisherInfo(std::string(), - duration, - std::string(), - publisher_info->name, - publisher_info->url, - 0, - publisher_info->id, - publisher_info->favicon_url); + ledger::MediaEventInfo old_event; + auto iter = events.find(media_key); + if (iter != events.end()) { + old_event = iter->second; } + + const uint64_t duration = GetDuration(old_event, event_info); + events[media_key] = event_info; + + SavePublisherInfo("", + duration, + "", + publisher_info->name, + publisher_info->url, + 0, + publisher_info->id, + publisher_info->favicon_url); } void Vimeo::OnPublisherVideoPage( @@ -307,20 +610,21 @@ void Vimeo::OnPublisherVideoPage( int response_status_code, const std::string& response, const std::map& headers) { - if (response_status_code != net::HTTP_OK) { - OnMediaActivityError(); - return; - } - ledger_->LogResponse( __func__, response_status_code, "HTML from Vimeo Video page", headers); + if (response_status_code != net::HTTP_OK) { + OnMediaActivityError(); + return; + } + const std::string user_id = GetIdFromVideoPage(response); if (user_id.empty()) { + OnMediaActivityError(); return; } @@ -337,7 +641,7 @@ void Vimeo::OnPublisherVideoPage( duration, user_id, GetNameFromVideoPage(response), - GetPublisherUrl(response), + GetUrlFromVideoPage(response), 0); } @@ -355,7 +659,6 @@ void Vimeo::SavePublisherInfo( const uint64_t window_id, const std::string& publisher_key, const std::string& publisher_favicon) { - if (user_id.empty() && publisher_key.empty()) { OnMediaActivityError(window_id); BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << @@ -398,7 +701,7 @@ void Vimeo::SavePublisherInfo( callback); if (!media_key.empty()) { - ledger_->SetMediaPublisherInfo(media_key, publisher_key); + ledger_->SetMediaPublisherInfo(media_key, key); } } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.h b/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.h index ced90c881c7a..f4d7d7863e8b 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo.h @@ -49,19 +49,68 @@ class Vimeo : public ledger::LedgerCallbackHandler { static std::string GetNameFromVideoPage(const std::string& data); - static std::string GetPublisherUrl(const std::string& data); + static std::string GetUrlFromVideoPage(const std::string& data); static bool AllowedEvent(const std::string& event); static uint64_t GetDuration(const ledger::MediaEventInfo& old_event, const ledger::MediaEventInfo& new_event); + static bool IsExcludedPath(const std::string& path); + + static std::string GetIdFromPublisherPage(const std::string& data); + + static std::string GetNameFromPublisherPage(const std::string& data); + + static std::string GetVideoIdFromVideoPage(const std::string& data); + void FetchDataFromUrl( const std::string& url, braveledger_media::FetchDataFromUrlCallback callback); void OnMediaActivityError(uint64_t window_id = 0); + void OnEmbedResponse( + const ledger::VisitData& visit_data, + const uint64_t window_id, + int response_status_code, + const std::string& response, + const std::map& headers); + + void OnPublisherPage( + const std::string& media_key, + const std::string& publisher_url, + const std::string& publisher_name, + const ledger::VisitData& visit_data, + const uint64_t window_id, + int response_status_code, + const std::string& response, + const std::map& headers); + + void OnUnknownPage( + const ledger::VisitData& visit_data, + const uint64_t window_id, + int response_status_code, + const std::string& response, + const std::map& headers); + + void OnPublisherPanleInfo( + const std::string& media_key, + uint64_t window_id, + const std::string& publisher_url, + const std::string& publisher_name, + const std::string& user_id, + ledger::Result result, + ledger::PublisherInfoPtr info); + + void GetPublisherPanleInfo( + const std::string& media_key, + uint64_t window_id, + const std::string& publisher_url, + const std::string& publisher_key, + const std::string& publisher_name, + const std::string& user_id); + void OnMediaPublisherInfo( const std::string& media_id, const std::string& media_key, @@ -101,9 +150,13 @@ class Vimeo : public ledger::LedgerCallbackHandler { FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetIdFromVideoPage); FRIEND_TEST_ALL_PREFIXES(VimeoTest, GenerateFaviconUrl); FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetNameFromVideoPage); - FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetPublisherUrl); + FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetUrlFromVideoPage); FRIEND_TEST_ALL_PREFIXES(VimeoTest, AllowedEvent); FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetDuration); + FRIEND_TEST_ALL_PREFIXES(VimeoTest, IsExcludedPath); + FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetIdFromPublisherPage); + FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetNameFromPublisherPage); + FRIEND_TEST_ALL_PREFIXES(VimeoTest, GetVideoIdFromVideoPage); }; } // namespace braveledger_media diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo_unittest.cc index b7b2fcbdf46b..3cfd40ac3dc1 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/vimeo_unittest.cc @@ -15,27 +15,13 @@ class VimeoTest : public testing::Test { }; const char profile_html[] = - "
" - "

-    " - "
"; + "window._gtm = [{\"clip_id\":265045525,\"page_path\":\"\\/265045525\"," + "\"creator_id\":123234205645,\"creator_user_type\":\"plus\"," + "\"video_categories\":\"Animation,Experimental,2D\",\"privacy\":" + "\"anybody\",\"staff_pick\":\"yes\",\"user_id\":,\"page_type\":\"Video\"," + "\"language\":\"en\",\"user_status\":\"logged_in\",\"user_type\":" + "\"basic\",\"ga_universal_id\":\"\",\"comscore_site_id\":\"\",\"new_user\"" + ":false,\"video_count\":1,\"recent_upload_count\":0,\"storage_used_gb\":"; const char page_config[] = "window.vimeo.clip_page_config = {\"clip\":{\"id\":331165963,\"title\"" @@ -58,118 +44,148 @@ const char user_link[] = "title=\"Thursday, April 18, 2019 at 3:15 AM\">3 weeks ago" ""; +const char publisher_page[] = + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "
Watch in our app
Open in app"; + +const char video_page[] = + "" + "" + ""; + TEST(VimeoTest, GetLinkType) { // empty url - std::string result = MediaVimeo::GetLinkType(""); - ASSERT_EQ(result, std::string()); + std::string result = Vimeo::GetLinkType(""); + ASSERT_EQ(result, ""); // wrong url - result = MediaVimeo::GetLinkType("https://vimeo.com/video/32342"); - ASSERT_EQ(result, std::string()); + result = Vimeo::GetLinkType("https://vimeo.com/video/32342"); + ASSERT_EQ(result, ""); // all good - result = MediaVimeo::GetLinkType( + result = Vimeo::GetLinkType( "https://fresnel.vimeocdn.com/add/player-stats?id=43324123412342"); ASSERT_EQ(result, "vimeo"); } TEST(VimeoTest, GetVideoUrl) { // empty id - std::string result = MediaVimeo::GetVideoUrl(""); - ASSERT_EQ(result, std::string()); + std::string result = Vimeo::GetVideoUrl(""); + ASSERT_EQ(result, ""); // all good - result = MediaVimeo::GetVideoUrl("234123423"); + result = Vimeo::GetVideoUrl("234123423"); ASSERT_EQ(result, "https://vimeo.com/234123423"); } TEST(VimeoTest, GetMediaKey) { // empty id - std::string result = MediaVimeo::GetMediaKey("", ""); - ASSERT_EQ(result, std::string()); + std::string result = Vimeo::GetMediaKey("", ""); + ASSERT_EQ(result, ""); // wrong type - result = MediaVimeo::GetMediaKey("234123423", "wrong"); - ASSERT_EQ(result, std::string()); + result = Vimeo::GetMediaKey("234123423", "wrong"); + ASSERT_EQ(result, ""); // all good - result = MediaVimeo::GetMediaKey("234123423", "vimeo-vod"); + result = Vimeo::GetMediaKey("234123423", "vimeo-vod"); ASSERT_EQ(result, "vimeo_234123423"); } TEST(VimeoTest, GetPublisherKey) { // empty id - std::string result = MediaVimeo::GetPublisherKey(""); - ASSERT_EQ(result, std::string()); + std::string result = Vimeo::GetPublisherKey(""); + ASSERT_EQ(result, ""); // all good - result = MediaVimeo::GetPublisherKey("234123423"); + result = Vimeo::GetPublisherKey("234123423"); ASSERT_EQ(result, "vimeo#channel:234123423"); } TEST(VimeoTest, GetIdFromVideoPage) { // empty id - std::string result = MediaVimeo::GetIdFromVideoPage(""); - ASSERT_EQ(result, std::string()); + std::string result = Vimeo::GetIdFromVideoPage(""); + ASSERT_EQ(result, ""); // strange string -result = MediaVimeo::GetIdFromVideoPage("asdfasdfasdfasdfff sdf"); - ASSERT_EQ(result, std::string()); +result = Vimeo::GetIdFromVideoPage("asdfasdfasdfasdfff sdf"); + ASSERT_EQ(result, ""); // all good - result = MediaVimeo::GetIdFromVideoPage(profile_html); - ASSERT_EQ(result, "31487122"); + result = Vimeo::GetIdFromVideoPage(profile_html); + ASSERT_EQ(result, "123234205645"); } TEST(VimeoTest, GenerateFaviconUrl) { // empty id - std::string result = MediaVimeo::GenerateFaviconUrl(""); - ASSERT_EQ(result, std::string()); + std::string result = Vimeo::GenerateFaviconUrl(""); + ASSERT_EQ(result, ""); // all good - result = MediaVimeo::GenerateFaviconUrl("234123423"); + result = Vimeo::GenerateFaviconUrl("234123423"); ASSERT_EQ(result, "https://i.vimeocdn.com/portrait/234123423_300x300.webp"); } TEST(VimeoTest, GetNameFromVideoPage) { // empty data - std::string result = MediaVimeo::GetNameFromVideoPage(""); - ASSERT_EQ(result, std::string()); + std::string result = Vimeo::GetNameFromVideoPage(""); + ASSERT_EQ(result, ""); // random data - result = MediaVimeo::GetNameFromVideoPage("asdfsdfdsf sdfdsf"); - ASSERT_EQ(result, std::string()); + result = Vimeo::GetNameFromVideoPage("asdfsdfdsf sdfdsf"); + ASSERT_EQ(result, ""); // all good - result = MediaVimeo::GetNameFromVideoPage(page_config); + result = Vimeo::GetNameFromVideoPage(page_config); ASSERT_EQ(result, "Nejcé"); } -TEST(VimeoTest, GetPublisherUrl) { +TEST(VimeoTest, GetUrlFromVideoPage) { // empty data - std::string result = MediaVimeo::GetPublisherUrl(""); - ASSERT_EQ(result, std::string()); + std::string result = Vimeo::GetUrlFromVideoPage(""); + ASSERT_EQ(result, ""); // random data - result = MediaVimeo::GetPublisherUrl("asdfsdfdsf sdfdsf"); - ASSERT_EQ(result, std::string()); + result = Vimeo::GetUrlFromVideoPage("asdfsdfdsf sdfdsf"); + ASSERT_EQ(result, ""); // all good - result = MediaVimeo::GetPublisherUrl(user_link); + result = Vimeo::GetUrlFromVideoPage(user_link); ASSERT_EQ(result, "https://vimeo.com/nejcbrave/videos"); } TEST(VimeoTest, AllowedEvent) { // empty event - bool result = MediaVimeo::AllowedEvent(""); + bool result = Vimeo::AllowedEvent(""); ASSERT_EQ(result, false); // random event - result = MediaVimeo::AllowedEvent("wrong"); + result = Vimeo::AllowedEvent("wrong"); ASSERT_EQ(result, false); // all good - result = MediaVimeo::AllowedEvent("video-played"); + result = Vimeo::AllowedEvent("video-played"); ASSERT_EQ(result, true); } @@ -178,7 +194,7 @@ TEST(VimeoTest, GetDuration) { ledger::MediaEventInfo new_event; // empty events - uint64_t result = MediaVimeo::GetDuration(old_event, new_event); + uint64_t result = Vimeo::GetDuration(old_event, new_event); ASSERT_EQ(result, 0u); // remove duplicated events @@ -186,13 +202,13 @@ TEST(VimeoTest, GetDuration) { old_event.time_ = "1.0"; new_event.event_ = "video-played"; new_event.time_ = "1.0"; - result = MediaVimeo::GetDuration(old_event, new_event); + result = Vimeo::GetDuration(old_event, new_event); ASSERT_EQ(result, 0u); // video started new_event.event_ = "video-start-time"; new_event.time_ = "2.0"; - result = MediaVimeo::GetDuration(old_event, new_event); + result = Vimeo::GetDuration(old_event, new_event); ASSERT_EQ(result, 2u); // watch event @@ -200,7 +216,7 @@ TEST(VimeoTest, GetDuration) { old_event.time_ = "2.0"; new_event.event_ = "video-minute-watched"; new_event.time_ = "5.1"; - result = MediaVimeo::GetDuration(old_event, new_event); + result = Vimeo::GetDuration(old_event, new_event); ASSERT_EQ(result, 3u); // video paused / video ended @@ -208,8 +224,86 @@ TEST(VimeoTest, GetDuration) { old_event.time_ = "5.1"; new_event.event_ = "video-paused"; new_event.time_ = "20.8"; - result = MediaVimeo::GetDuration(old_event, new_event); + result = Vimeo::GetDuration(old_event, new_event); ASSERT_EQ(result, 16u); } +TEST(VimeoTest, IsExcludedPath) { + // path is empty + bool result = Vimeo::IsExcludedPath(""); + ASSERT_EQ(result, true); + + // path is simple excluded link + result = + Vimeo::IsExcludedPath("/log_in"); + ASSERT_EQ(result, true); + + // path is simple excluded link with trailing / + result = + Vimeo::IsExcludedPath("/log_in/"); + ASSERT_EQ(result, true); + + // path is complex excluded link + result = + Vimeo::IsExcludedPath("/features/"); + ASSERT_EQ(result, true); + + // path is complex excluded link two levels + result = + Vimeo::IsExcludedPath("/features/video"); + ASSERT_EQ(result, true); + + // path is random link + result = + Vimeo::IsExcludedPath("/asdfs/asdfasdf/"); + ASSERT_EQ(result, false); + + // path is not excluded link + result = + Vimeo::IsExcludedPath("/brave"); + ASSERT_EQ(result, false); +} + +TEST(VimeoTest, GetIdFromPublisherPage) { + // empty data + std::string result = Vimeo::GetIdFromPublisherPage(""); + ASSERT_EQ(result, ""); + + // random data + result = Vimeo::GetIdFromPublisherPage("asdfsdfdsf sdfdsf"); + ASSERT_EQ(result, ""); + + // all good + result = Vimeo::GetIdFromPublisherPage(publisher_page); + ASSERT_EQ(result, "97518779"); +} + +TEST(VimeoTest, GetNameFromPublisherPage) { + // empty data + std::string result = Vimeo::GetNameFromPublisherPage(""); + ASSERT_EQ(result, ""); + + // random data + result = Vimeo::GetNameFromPublisherPage("asdfsdfdsf sdfdsf"); + ASSERT_EQ(result, ""); + + // all good + result = Vimeo::GetNameFromPublisherPage(publisher_page); + ASSERT_EQ(result, "Nejc"); +} + +TEST(VimeoTest, GetVideoIdFromVideoPage) { + // empty data + std::string result = Vimeo::GetVideoIdFromVideoPage(""); + ASSERT_EQ(result, ""); + + // random data + result = Vimeo::GetVideoIdFromVideoPage("asdfsdfdsf sdfdsf"); + ASSERT_EQ(result, ""); + + // all good + result = Vimeo::GetVideoIdFromVideoPage(video_page); + ASSERT_EQ(result, "331165963"); +} + } // namespace braveledger_media diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/static_values.h b/vendor/bat-native-ledger/src/bat/ledger/internal/static_values.h index 730e00a0c32e..b288b7726871 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/static_values.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/static_values.h @@ -68,10 +68,12 @@ #define VIMEO_MEDIA_TYPE "vimeo" #define YOUTUBE_PROVIDER_URL "https://www.youtube.com/oembed" #define TWITCH_PROVIDER_URL "https://api.twitch.tv/v5/oembed" +#define VIMEO_PROVIDER_URL "https://vimeo.com/api/oembed.json" #define YOUTUBE_TLD "youtube.com" #define TWITCH_TLD "twitch.tv" #define TWITTER_TLD "twitter.com" #define REDDIT_TLD "reddit.com" +#define VIMEO_TLD "vimeo.com" #define TWITCH_VOD_URL "https://www.twitch.tv/videos/" #define REDDIT_USER_URL "https://www.reddit.com/user/" #define OLD_REDDIT_DOMAIN "old.reddit.com" diff --git a/vendor/brave-ios/Ledger/Generated/Records+Private.h b/vendor/brave-ios/Ledger/Generated/Records+Private.h index 78324805c2ec..8583674d24fe 100644 --- a/vendor/brave-ios/Ledger/Generated/Records+Private.h +++ b/vendor/brave-ios/Ledger/Generated/Records+Private.h @@ -14,7 +14,7 @@ #include "bat/ledger/ledger.h" #include "bat/ledger/ledger_callback_handler.h" #include "bat/ledger/ledger_client.h" -#include "bat/ledger/media_publisher_info.h" +#include "bat/ledger/media_event_info.h" #include "bat/ledger/pending_contribution.h" #include "bat/ledger/publisher_info.h" #include "bat/ledger/reconcile_info.h" @@ -59,8 +59,8 @@ - (instancetype)initWithTransactionsInfo:(const ledger::TransactionsInfo&)obj; @end -@interface BATTwitchEventInfo (Private) -- (instancetype)initWithTwitchEventInfo:(const ledger::TwitchEventInfo&)obj; +@interface BATMediaEventInfo (Private) +- (instancetype)initWithMediaEventInfo:(const ledger::MediaEventInfo&)obj; @end @interface BATWalletProperties (Private) diff --git a/vendor/brave-ios/Ledger/Generated/Records.h b/vendor/brave-ios/Ledger/Generated/Records.h index 14d0ab74a427..42f628aa69e8 100644 --- a/vendor/brave-ios/Ledger/Generated/Records.h +++ b/vendor/brave-ios/Ledger/Generated/Records.h @@ -7,7 +7,7 @@ #import #import "Enums.h" -@class BATAutoContributeProps, BATBalanceReportInfo, BATContributionInfo, BATGrant, BATPublisherBanner, BATReconcileInfo, BATRewardsInternalsInfo, BATTransactionInfo, BATTransactionsInfo, BATTwitchEventInfo, BATVisitData, BATWalletProperties; +@class BATAutoContributeProps, BATBalanceReportInfo, BATContributionInfo, BATGrant, BATPublisherBanner, BATReconcileInfo, BATRewardsInternalsInfo, BATTransactionInfo, BATTransactionsInfo, BATMediaEventInfo, BATVisitData, BATWalletProperties; NS_ASSUME_NONNULL_BEGIN @@ -94,8 +94,8 @@ NS_SWIFT_NAME(TransactionsInfo) @property (nonatomic) NSArray * transactions; @end -NS_SWIFT_NAME(TwitchEventInfo) -@interface BATTwitchEventInfo : NSObject +NS_SWIFT_NAME(MediaEventInfo) +@interface BATMediaEventInfo : NSObject @property (nonatomic) NSString * event; @property (nonatomic) NSString * time; @property (nonatomic) NSString * status; diff --git a/vendor/brave-ios/Ledger/Generated/Records.mm b/vendor/brave-ios/Ledger/Generated/Records.mm index e5c0a42e0fed..800f94e1ef0d 100644 --- a/vendor/brave-ios/Ledger/Generated/Records.mm +++ b/vendor/brave-ios/Ledger/Generated/Records.mm @@ -131,8 +131,8 @@ - (instancetype)initWithTransactionsInfo:(const ledger::TransactionsInfo&)obj { } @end -@implementation BATTwitchEventInfo -- (instancetype)initWithTwitchEventInfo:(const ledger::TwitchEventInfo&)obj { +@implementation BATMediaEventInfo +- (instancetype)initWithMediaEventInfo:(const ledger::MediaEventInfo&)obj { if ((self = [super init])) { self.event = [NSString stringWithUTF8String:obj.event_.c_str()]; self.time = [NSString stringWithUTF8String:obj.time_.c_str()];