示例#1
0
        public async Task <(IEnumerable <NewsFeedPublication>, long)> GetUserFeedAsync(string userId, int skip, int take)
        {
            var publicationsResponse = await publicationsApi.SearchWithHttpInfoAsync(skip, take, order : Ordering.Desc);

            var publications = publicationsResponse.Data;
            var totalCount   = GetTotalCountHeader(publicationsResponse);

            var featuredComments = await LoadCommentsAsync(publications.Select(p => p.Id));

            var reactions = await LoadReactionsAsync(publications.Select(p => p.Id));

            var userReactions = userId == null
                ? new Dictionary <string, UserReaction>()
                : await LoadUserReactionsAsync(userId, publications.Select(p => p.Id));

            var news = publications
                       .Select(publication => new NewsFeedPublication(
                                   publication.Id,
                                   publication.Content,
                                   publication.CreatedOn,
                                   publication.UpdatedOn,
                                   featuredComments[publication.Id],
                                   new ReactionShort(reactions[publication.Id], userReactions[publication.Id]),
                                   ToUser(publication.Author)))
                       .ToList();

            return(news, totalCount);
        }
示例#2
0
        public async Task <(IEnumerable <NewsFeedPublication>, long)> FindManyAsync(int skip, int take, string author)
        {
            var publicationsResponse = await publicationsApi.SearchWithHttpInfoAsync(skip, take, order : Ordering.Desc);

            var publications         = publicationsResponse.Data;
            var newsFeedPublications = new List <NewsFeedPublication>(publications.Count);

            foreach (var publication in publications)
            {
                var commentsResponse = await commentsApi.SearchWithHttpInfoAsync(publication.Id, 0, 3);

                var reactions = new Dictionary <ReactionType, int>();
                try
                {
                    var response = await reactionsApi.GetAsync($"publication_{publication.Id}");

                    reactions = response.Keys
                                .Select(k => (Enum.Parse <ReactionType>(k), response[k]))
                                .ToDictionary(o => o.Item1, o => o.Item2);
                }
                catch (ApiException)
                {
                    // ignored
                }

                UserReaction userReaction = null;

                if (author != null)
                {
                    try
                    {
                        var reactionByAuthor = await reactionsApi.GetReactionByAuthorAsync($"publication_{publication.Id}", author);

                        userReaction = new UserReaction(Enum.Parse <ReactionType>(reactionByAuthor.Type));
                    }
                    catch (ApiException ex) when(ex.ErrorCode == (int)HttpStatusCode.NotFound)
                    {
                        // ignored
                    }
                }

                newsFeedPublications.Add(new NewsFeedPublication(
                                             publication.Id,
                                             publication.Content,
                                             new CommentsShort(commentsResponse.Data.Select(c => new PublicationComment(
                                                                                                c.Id, c.Content, c.PublicationId, c.AuthorId, c.CreatedOn)).ToList(), GetTotalCountHeader(commentsResponse)),
                                             new ReactionShort(reactions, userReaction)));
            }

            return(newsFeedPublications, GetTotalCountHeader(publicationsResponse));
        }