From dd3ab518b416d5e45f0eaca9f9f9fda47f4f43fe Mon Sep 17 00:00:00 2001 From: "r.kuznetsov" Date: Fri, 10 Nov 2017 14:24:27 +0300 Subject: =?UTF-8?q?Added=20user=E2=80=99s=20reviews=20filtering?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- android/jni/com/mapswithme/maps/Framework.cpp | 2 +- iphone/Maps/UI/PlacePage/MWMPlacePageData.mm | 2 +- map/framework.cpp | 23 +++++++++++++++++++++++ map/framework.hpp | 4 ++++ map/user.cpp | 4 ++++ map/user.hpp | 1 + ugc/api.cpp | 4 ++-- ugc/api.hpp | 5 +++-- 8 files changed, 39 insertions(+), 6 deletions(-) diff --git a/android/jni/com/mapswithme/maps/Framework.cpp b/android/jni/com/mapswithme/maps/Framework.cpp index ec1b6e6a54..ed937d10cc 100644 --- a/android/jni/com/mapswithme/maps/Framework.cpp +++ b/android/jni/com/mapswithme/maps/Framework.cpp @@ -584,7 +584,7 @@ void Framework::RequestViatorProducts(JNIEnv * env, jobject policy, std::string void Framework::RequestUGC(FeatureID const & fid, ugc::Api::UGCCallback const & ugcCallback) { - m_work.GetUGCApi()->GetUGC(fid, ugcCallback); + m_work.GetUGC(fid, ugcCallback); } void Framework::SetUGCUpdate(FeatureID const & fid, ugc::UGCUpdate const & ugc) diff --git a/iphone/Maps/UI/PlacePage/MWMPlacePageData.mm b/iphone/Maps/UI/PlacePage/MWMPlacePageData.mm index 38b3ea9f81..c93aa37519 100644 --- a/iphone/Maps/UI/PlacePage/MWMPlacePageData.mm +++ b/iphone/Maps/UI/PlacePage/MWMPlacePageData.mm @@ -119,7 +119,7 @@ NSString * const kUserDefaultsLatLonAsDMSKey = @"UserDefaultsLatLonAsDMS"; NSAssert(m_info.ShouldShowUGC(), @""); __weak auto wself = self; - GetFramework().GetUGCApi()->GetUGC( + GetFramework().GetUGC( m_info.GetID(), [wself](ugc::UGC const & ugc, ugc::UGCUpdate const & update) { __strong auto self = wself; self.ugc = [[MWMUGCViewModel alloc] initWithUGC:ugc update:update]; diff --git a/map/framework.cpp b/map/framework.cpp index 5223d64195..6d1df75387 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -3338,3 +3338,26 @@ void Framework::UploadUGC(User::CompleteUploadingHandler const & onCompleteUploa } }); } + +void Framework::GetUGC(FeatureID const & id, ugc::Api::UGCCallback const & callback) +{ + m_ugcApi->GetUGC(id, [this, callback](ugc::UGC const & ugc, ugc::UGCUpdate const & update) + { + ugc::UGC filteredUGC = ugc; + filteredUGC.m_reviews = FilterUGCReviews(ugc.m_reviews); + callback(filteredUGC, update); + }); +} + +ugc::Reviews Framework::FilterUGCReviews(ugc::Reviews const & reviews) const +{ + ugc::Reviews result; + auto const details = m_user.GetDetails(); + ASSERT(std::is_sorted(details.m_reviewIds.begin(), details.m_reviewIds.end()), ()); + for (auto const & review : reviews) + { + if (!std::binary_search(details.m_reviewIds.begin(), details.m_reviewIds.end(), review.m_id)) + result.push_back(review); + } + return result; +} diff --git a/map/framework.hpp b/map/framework.hpp index 396079b7ad..8d10a5028e 100644 --- a/map/framework.hpp +++ b/map/framework.hpp @@ -816,4 +816,8 @@ private: public: // UGC. void UploadUGC(User::CompleteUploadingHandler const & onCompleteUploading); + void GetUGC(FeatureID const & id, ugc::Api::UGCCallback const & callback); +private: + // Filters user's reviews. + ugc::Reviews FilterUGCReviews(ugc::Reviews const & reviews) const; }; diff --git a/map/user.cpp b/map/user.cpp index 5a695565f0..9f77849c8f 100644 --- a/map/user.cpp +++ b/map/user.cpp @@ -143,7 +143,10 @@ void User::Init() std::string reviewIds; if (GetPlatform().GetSecureStorage().Load(kReviewIdsKey, reviewIds)) + { m_details.m_reviewIds = DeserializeReviewIds(reviewIds); + std::sort(m_details.m_reviewIds.begin(), m_details.m_reviewIds.end()); + } // Update user details on start up. auto const status = GetPlatform().ConnectionStatus(); @@ -249,6 +252,7 @@ void User::RequestUserDetails() GetPlatform().GetSecureStorage().Save(kReviewIdsKey, response); std::lock_guard lock(m_mutex); m_details.m_reviewIds = reviewIds; + std::sort(m_details.m_reviewIds.begin(), m_details.m_reviewIds.end()); } }); }); diff --git a/map/user.hpp b/map/user.hpp index e730e17173..0c19176c87 100644 --- a/map/user.hpp +++ b/map/user.hpp @@ -21,6 +21,7 @@ public: struct Details { using ReviewId = uint64_t; + // m_reviewIds must be sorted. std::vector m_reviewIds; }; enum SocialTokenType diff --git a/ugc/api.cpp b/ugc/api.cpp index d6b42c4071..65f77af714 100644 --- a/ugc/api.cpp +++ b/ugc/api.cpp @@ -14,7 +14,7 @@ Api::Api(Index const & index) : m_storage(index), m_loader(index) m_thread.Push([this] { m_storage.Load(); }); } -void Api::GetUGC(FeatureID const & id, UGCCallback const & callback) +void Api::GetUGC(FeatureID const & id, UGCCallbackUnsafe const & callback) { m_thread.Push([=] { GetUGCImpl(id, callback); }); } @@ -44,7 +44,7 @@ void Api::SaveUGCOnDisk() m_thread.Push([this] { SaveUGCOnDiskImpl(); }); } -void Api::GetUGCImpl(FeatureID const & id, UGCCallback const & callback) +void Api::GetUGCImpl(FeatureID const & id, UGCCallbackUnsafe const & callback) { CHECK(callback, ()); if (!id.IsValid()) diff --git a/ugc/api.hpp b/ugc/api.hpp index b507ac1f95..9af3ef365a 100644 --- a/ugc/api.hpp +++ b/ugc/api.hpp @@ -19,12 +19,13 @@ class Api { public: using UGCCallback = platform::SafeCallback; + using UGCCallbackUnsafe = std::function; using UGCJsonToSendCallback = std::function; using OnResultCallback = platform::SafeCallback; explicit Api(Index const & index); - void GetUGC(FeatureID const & id, UGCCallback const & callback); + void GetUGC(FeatureID const & id, UGCCallbackUnsafe const & callback); void SetUGCUpdate(FeatureID const & id, UGCUpdate const & ugc, OnResultCallback const & callback = nullptr); void GetUGCToSend(UGCJsonToSendCallback const & callback); @@ -32,7 +33,7 @@ public: void SaveUGCOnDisk(); private: - void GetUGCImpl(FeatureID const & id, UGCCallback const & callback); + void GetUGCImpl(FeatureID const & id, UGCCallbackUnsafe const & callback); Storage::SettingResult SetUGCUpdateImpl(FeatureID const & id, UGCUpdate const & ugc); void GetUGCToSendImpl(UGCJsonToSendCallback const & callback); void SendingCompletedImpl(); -- cgit v1.2.3