private async Task GetTopicView()

in code/Server/Common/Managers/ViewsManager.cs [1221:1403]


        private async Task<TopicView> GetTopicView(
            string topicHandle,
            ITopicEntity topicEntity,
            string userHandle,
            string appHandle,
            string queryingUserHandle,
            bool checkFollowingRelationship = false)
        {
            Task<ITopicEntity> taskTopic = null;
            if (topicEntity == null)
            {
                taskTopic = this.topicsStore.QueryTopic(topicHandle);
            }

            var taskLikesCount = this.likesStore.QueryLikesCount(topicHandle);
            var taskCommentsCount = this.commentsStore.QueryTopicCommentsCount(topicHandle);

            Task<ILikeLookupEntity> taskLikeLookup = null;
            Task<IPinLookupEntity> taskPinLookup = null;
            Task<ITopicRelationshipLookupEntity> taskTopicFollower = null;

            if (queryingUserHandle != null)
            {
                taskLikeLookup = this.likesStore.QueryLike(topicHandle, queryingUserHandle);
                taskPinLookup = this.pinsStore.QueryPin(queryingUserHandle, topicHandle);
                taskTopicFollower = this.topicRelationshipsStore.QueryTopicFollowerRelationship(topicHandle, queryingUserHandle, appHandle);
            }

            Task<UserCompactView> taskUser = null;
            if (userHandle != null)
            {
                taskUser = this.GetUserCompactView(userHandle, appHandle, queryingUserHandle);
            }

            Task<AppCompactView> taskApp = null;
            if (appHandle != null)
            {
                taskApp = this.GetAppCompactView(appHandle);
            }

            if (taskTopic != null)
            {
                topicEntity = await taskTopic;
            }

            if (topicEntity == null)
            {
                return null;
            }

            var allowed = await this.ReviewStatusIsAllowed(topicEntity.ReviewStatus, topicEntity.AppHandle);

            // do not return a topic if the review status prohibits it
            if (!allowed)
            {
                return null;
            }

            if (userHandle != null && topicEntity.UserHandle != userHandle)
            {
                return null;
            }

            if (appHandle != null && topicEntity.AppHandle != appHandle)
            {
                return null;
            }

            // if the topic is published by a user, then get the topic author (the user who created the topic)
            UserCompactView topicAuthor = null;
            if (taskUser != null && topicEntity.PublisherType == PublisherType.User)
            {
                topicAuthor = await taskUser;

                if (topicAuthor == null)
                {
                    // this can happen when the topic author is deleted; do not return a topic view
                    return null;
                }

                // if we have a valid topic author and the querying user is not the user who created the topic,
                // then we must check that the queryingUser is allowed to see this topic
                if (queryingUserHandle != userHandle)
                {
                    if (topicAuthor.FollowerStatus == UserRelationshipStatus.Blocked)
                    {
                        // do not allow the queriying user to see this topic
                        return null;
                    }

                    if (topicAuthor.Visibility == UserVisibilityStatus.Private
                        && topicAuthor.FollowerStatus != UserRelationshipStatus.Follow)
                    {
                        // do not allow the queriying user to see this topic
                        return null;
                    }
                }
            }

            // if we should check the following relationship
            if (checkFollowingRelationship)
            {
                // first, check whether the user is a follower of the topic author
                if (!IsTopicAuthorFollower(topicAuthor))
                {
                    // determine the topic follower relationship for the querying user
                    ITopicRelationshipLookupEntity topicFollowerRelationship = null;
                    if (taskTopicFollower != null)
                    {
                        topicFollowerRelationship = await taskTopicFollower;
                    }

                    // check whether the user is a topic follower
                    if (!IsTopicFollower(topicFollowerRelationship))
                    {
                        // if we reach here, we know that the user is not a follower of the topic author
                        // and not a follower of the topic.
                        return null;
                    }
                }
            }

            string blobUrl = await this.GetCdnUrl(topicEntity.BlobType, topicEntity.BlobHandle);

            TopicView view = new TopicView()
            {
                TopicHandle = topicHandle,
                CreatedTime = topicEntity.CreatedTime,
                LastUpdatedTime = topicEntity.LastUpdatedTime,
                PublisherType = topicEntity.PublisherType,
                User = topicAuthor,
                Title = topicEntity.Title,
                Text = topicEntity.Text,
                BlobType = topicEntity.BlobType,
                BlobHandle = topicEntity.BlobHandle,
                BlobUrl = blobUrl,
                Categories = topicEntity.Categories,
                Language = topicEntity.Language,
                Group = topicEntity.Group,
                DeepLink = topicEntity.DeepLink,
                FriendlyName = topicEntity.FriendlyName,
                TotalLikes = 0, // will be filled in below
                TotalComments = 0, // will be filled in below
                Liked = false, // may be filled in below
                Pinned = false, // may be filled in below
                ContentStatus = topicEntity.ReviewStatus,
                App = null // will be filled in below
            };

            long? likesCount = await taskLikesCount;
            long? commentsCount = await taskCommentsCount;
            view.TotalLikes = likesCount ?? 0;
            view.TotalComments = commentsCount ?? 0;

            if (taskLikeLookup != null)
            {
                var likeLookupEntity = await taskLikeLookup;
                if (likeLookupEntity != null)
                {
                    view.Liked = likeLookupEntity.Liked;
                }
            }

            if (taskPinLookup != null)
            {
                var pinLookupEntity = await taskPinLookup;
                if (pinLookupEntity != null)
                {
                    view.Pinned = pinLookupEntity.Pinned;
                }
            }

            if (taskApp != null)
            {
                view.App = await taskApp;
                if (view.App == null)
                {
                    return null;
                }
            }

            return view;
        }