Esempio n. 1
0
        /// <summary>
        /// Update like
        /// </summary>
        /// <param name="processType">Process type</param>
        /// <param name="likeHandle">Like handle</param>
        /// <param name="contentType">Content type</param>
        /// <param name="contentHandle">Content handle</param>
        /// <param name="userHandle">User handle</param>
        /// <param name="liked">Like status</param>
        /// <param name="contentPublisherType">Content publisher type</param>
        /// <param name="contentUserHandle">User handle of the content publisher</param>
        /// <param name="contentCreatedTime">Content createdTime</param>
        /// <param name="appHandle">App handle</param>
        /// <param name="lastUpdatedTime">Last updated time</param>
        /// <param name="likeLookupEntity">Like lookup entity</param>
        /// <returns>Update like task</returns>
        public async Task UpdateLike(
            ProcessType processType,
            string likeHandle,
            ContentType contentType,
            string contentHandle,
            string userHandle,
            bool liked,
            PublisherType contentPublisherType,
            string contentUserHandle,
            DateTime contentCreatedTime,
            string appHandle,
            DateTime lastUpdatedTime,
            ILikeLookupEntity likeLookupEntity)
        {
            if (processType == ProcessType.Frontend)
            {
                await this.likesStore.UpdateLike(
                    StorageConsistencyMode.Strong,
                    likeHandle,
                    contentHandle,
                    userHandle,
                    liked,
                    lastUpdatedTime,
                    likeLookupEntity);

                await this.likesQueue.SendLikeMessage(
                    likeHandle,
                    contentType,
                    contentHandle,
                    userHandle,
                    liked,
                    contentPublisherType,
                    contentUserHandle,
                    contentCreatedTime,
                    appHandle,
                    lastUpdatedTime);
            }
            else if (processType == ProcessType.Backend || processType == ProcessType.BackendRetry)
            {
                if (liked & contentPublisherType == PublisherType.User && userHandle != contentUserHandle)
                {
                    await this.notificationsManager.CreateNotification(
                        processType,
                        contentUserHandle,
                        appHandle,
                        likeHandle,
                        ActivityType.Like,
                        userHandle,
                        contentUserHandle,
                        contentType,
                        contentHandle,
                        lastUpdatedTime);
                }

                if (liked && contentType == ContentType.Topic)
                {
                    await this.fanoutActivitiesQueue.SendFanoutActivityMessage(
                        userHandle,
                        appHandle,
                        likeHandle,
                        ActivityType.Like,
                        userHandle,
                        contentUserHandle,
                        contentType,
                        contentHandle,
                        lastUpdatedTime);

                    // TODO: check what happens if the topic is AppPublished?
                    await this.fanoutActivitiesQueue.SendFanoutTopicActivityMessage(
                        contentHandle,
                        appHandle,
                        likeHandle,
                        ActivityType.Like,
                        userHandle,
                        contentUserHandle,
                        contentType,
                        contentHandle,
                        lastUpdatedTime);
                }

                long?likesCount = await this.likesStore.QueryLikesCount(contentHandle);

                long likesCountValue = likesCount.HasValue ? likesCount.Value : 0;
                if (likesCountValue % PopularTopicsUpdateLikesCount == 0)
                {
                    if (contentType == ContentType.Topic)
                    {
                        await this.popularTopicsManager.UpdatePopularTopic(processType, appHandle, contentHandle, contentUserHandle, contentCreatedTime, likesCountValue);
                    }

                    if (contentType == ContentType.Topic && contentPublisherType == PublisherType.User)
                    {
                        await this.popularTopicsManager.UpdatePopularUserTopic(processType, contentUserHandle, appHandle, contentHandle, likesCountValue);
                    }
                }
            }
        }
        /// <summary>
        /// Update like
        /// </summary>
        /// <param name="storageConsistencyMode">Consistency mode</param>
        /// <param name="likeHandle">Like handle</param>
        /// <param name="contentHandle">Content handle</param>
        /// <param name="userHandle">User handle</param>
        /// <param name="liked">Like status</param>
        /// <param name="lastUpdatedTime">Last updated time</param>
        /// <param name="readLikeLookupEntity">Read like lookup entity</param>
        /// <returns>Update like task</returns>
        public async Task UpdateLike(
            StorageConsistencyMode storageConsistencyMode,
            string likeHandle,
            string contentHandle,
            string userHandle,
            bool liked,
            DateTime lastUpdatedTime,
            ILikeLookupEntity readLikeLookupEntity)
        {
            CTStore store = await this.tableStoreManager.GetStore(ContainerIdentifier.Likes);

            ObjectTable lookupTable = this.tableStoreManager.GetTable(ContainerIdentifier.Likes, TableIdentifier.LikesLookup) as ObjectTable;
            FeedTable   feedTable   = this.tableStoreManager.GetTable(ContainerIdentifier.Likes, TableIdentifier.LikesFeed) as FeedTable;
            CountTable  countTable  = this.tableStoreManager.GetTable(ContainerIdentifier.Likes, TableIdentifier.LikesCount) as CountTable;
            Transaction transaction = new Transaction();

            LikeFeedEntity likeFeedEntity = new LikeFeedEntity()
            {
                LikeHandle = likeHandle,
                UserHandle = userHandle
            };

            if (readLikeLookupEntity == null)
            {
                LikeLookupEntity newLikeLookupEntity = new LikeLookupEntity()
                {
                    LikeHandle      = likeHandle,
                    LastUpdatedTime = lastUpdatedTime,
                    Liked           = liked
                };

                transaction.Add(Operation.Insert(lookupTable, contentHandle, userHandle, newLikeLookupEntity));

                if (liked)
                {
                    transaction.Add(Operation.Insert(feedTable, contentHandle, this.tableStoreManager.DefaultFeedKey, likeHandle, likeFeedEntity));
                    transaction.Add(Operation.InsertOrIncrement(countTable, contentHandle, this.tableStoreManager.DefaultCountKey));
                }
            }
            else
            {
                bool   oldLiked      = readLikeLookupEntity.Liked;
                string oldLikeHandle = readLikeLookupEntity.LikeHandle;

                readLikeLookupEntity.LikeHandle      = likeHandle;
                readLikeLookupEntity.Liked           = liked;
                readLikeLookupEntity.LastUpdatedTime = lastUpdatedTime;

                transaction.Add(Operation.Replace(lookupTable, contentHandle, userHandle, readLikeLookupEntity as LikeLookupEntity));

                if (liked == oldLiked)
                {
                    if (liked && likeHandle != oldLikeHandle)
                    {
                        transaction.Add(Operation.Delete(feedTable, contentHandle, this.tableStoreManager.DefaultFeedKey, oldLikeHandle));
                        transaction.Add(Operation.Insert(feedTable, contentHandle, this.tableStoreManager.DefaultFeedKey, likeHandle, likeFeedEntity));
                    }
                }
                else
                {
                    if (liked)
                    {
                        transaction.Add(Operation.Insert(feedTable, contentHandle, this.tableStoreManager.DefaultFeedKey, likeHandle, likeFeedEntity));
                        transaction.Add(Operation.Increment(countTable, contentHandle, this.tableStoreManager.DefaultCountKey, 1));
                    }
                    else
                    {
                        transaction.Add(Operation.Delete(feedTable, contentHandle, this.tableStoreManager.DefaultFeedKey, oldLikeHandle));
                        transaction.Add(Operation.Increment(countTable, contentHandle, this.tableStoreManager.DefaultCountKey, -1.0));
                    }
                }
            }

            await store.ExecuteTransactionAsync(transaction, storageConsistencyMode.ToConsistencyMode());
        }