Exemplo n.º 1
0
        public async Task <UserPageModel> BuildUserPageModelAsync(Guid userId, IList <Guid> forumIds)
        {
            var result = new UserPageModel();

            var user = await _dbContext.Users
                       .FirstOrDefaultAsync(x =>
                                            x.Id == userId);

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

            result.User = new UserModel
            {
                Id           = user.Id,
                DisplayName  = user.DisplayName,
                TotalTopics  = user.TopicsCount,
                TotalReplies = user.RepliesCount,
                GravatarHash = _gravatarService.HashEmailForGravatar(user.Email),
                Status       = user.Status
            };

            result.Posts = await _searchModelBuilder.SearchPostModels(forumIds, new QueryOptions(), userId);

            return(result);
        }
Exemplo n.º 2
0
        public async Task <CurrentUserModel> CurrentUserAsync()
        {
            var result = new CurrentUserModel();

            var claimsPrincipal = _httpContextAccessor.HttpContext.User;

            if (claimsPrincipal.Identity.IsAuthenticated)
            {
                var identityUserId = _httpContextAccessor.HttpContext.User.Identities.First().Claims
                                     .FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value;

                if (!string.IsNullOrEmpty(identityUserId))
                {
                    var user = await _dbContext.Users.FirstOrDefaultAsync(x => x.IdentityUserId == identityUserId);

                    if (user != null)
                    {
                        result = new CurrentUserModel
                        {
                            Id              = user.Id,
                            IdentityUserId  = user.IdentityUserId,
                            Email           = user.Email,
                            DisplayName     = user.DisplayName,
                            GravatarHash    = _gravatarService.HashEmailForGravatar(user.Email),
                            IsSuspended     = user.Status == UserStatusType.Suspended,
                            IsAuthenticated = true
                        };
                    }
                }
            }

            return(result);
        }
Exemplo n.º 3
0
        public async Task <PaginatedData <ForumPageModel.TopicModel> > BuildForumPageModelTopicsAsync(Guid forumId, QueryOptions options)
        {
            var topicsQuery = _dbContext.Posts
                              .Include(x => x.CreatedByUser)
                              .Include(x => x.LastReply).ThenInclude(x => x.CreatedByUser)
                              .Where(x =>
                                     x.TopicId == null &&
                                     x.ForumId == forumId &&
                                     x.Status == PostStatusType.Published);

            if (!string.IsNullOrWhiteSpace(options.Search))
            {
                topicsQuery = topicsQuery
                              .Where(x => x.Title.Contains(options.Search) || x.Content.Contains(options.Search));
            }

            var topics = await topicsQuery
                         .OrderByDescending(x => x.Pinned)
                         .ThenByDescending(x => x.LastReply != null ? x.LastReply.CreatedOn : x.CreatedOn)
                         .Skip(options.Skip)
                         .Take(options.PageSize)
                         .ToListAsync();

            var items = topics.Select(topic => new ForumPageModel.TopicModel
            {
                Id                        = topic.Id,
                Title                     = topic.Title,
                Slug                      = topic.Slug,
                TotalReplies              = topic.RepliesCount,
                UserId                    = topic.CreatedByUser.Id,
                UserDisplayName           = topic.CreatedByUser.DisplayName,
                TimeStamp                 = topic.CreatedOn,
                GravatarHash              = _gravatarService.HashEmailForGravatar(topic.CreatedByUser.Email),
                MostRecentUserId          = topic.LastReply?.CreatedBy ?? topic.CreatedBy,
                MostRecentUserDisplayName = topic.LastReply?.CreatedByUser?.DisplayName ?? topic.CreatedByUser.DisplayName,
                MostRecentTimeStamp       = topic.LastReply?.CreatedOn ?? topic.CreatedOn,
                Pinned                    = topic.Pinned,
                Locked                    = topic.Locked,
                HasAnswer                 = topic.HasAnswer
            })
                        .ToList();

            var totalRecordsQuery = _dbContext.Posts
                                    .Where(x =>
                                           x.TopicId == null &&
                                           x.ForumId == forumId &&
                                           x.Status == PostStatusType.Published);

            if (!string.IsNullOrWhiteSpace(options.Search))
            {
                totalRecordsQuery = totalRecordsQuery
                                    .Where(x => x.Title.Contains(options.Search) || x.Content.Contains(options.Search));
            }

            var totalRecords = await totalRecordsQuery.CountAsync();

            var result = new PaginatedData <ForumPageModel.TopicModel>(items, totalRecords, options.PageSize);

            return(result);
        }
Exemplo n.º 4
0
        public async Task <TopicPageModel> BuildTopicPageModelAsync(Guid siteId, string forumSlug, string topicSlug, QueryOptions options)
        {
            var topic = await _dbContext.Posts
                        .Include(x => x.Forum).ThenInclude(x => x.Category)
                        .Include(x => x.CreatedByUser)
                        .FirstOrDefaultAsync(x =>
                                             x.TopicId == null &&
                                             x.Forum.Category.SiteId == siteId &&
                                             x.Forum.Slug == forumSlug &&
                                             x.Slug == topicSlug &&
                                             x.Status == StatusType.Published);

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

            var result = new TopicPageModel
            {
                Forum = new TopicPageModel.ForumModel
                {
                    Id   = topic.Forum.Id,
                    Name = topic.Forum.Name,
                    Slug = topic.Forum.Slug
                },
                Topic = new TopicPageModel.TopicModel
                {
                    Id              = topic.Id,
                    Title           = topic.Title,
                    Slug            = topic.Slug,
                    Content         = Markdown.ToHtml(topic.Content),
                    UserId          = topic.CreatedByUser.Id,
                    UserDisplayName = topic.CreatedByUser.DisplayName,
                    TimeStamp       = topic.CreatedOn,
                    IdentityUserId  = topic.CreatedByUser.IdentityUserId,
                    GravatarHash    = _gravatarService.HashEmailForGravatar(topic.CreatedByUser.Email),
                    Pinned          = topic.Pinned,
                    Locked          = topic.Locked,
                    HasAnswer       = topic.HasAnswer
                },
                Replies = await BuildTopicPageModelRepliesAsync(topic.Id, options)
            };

            if (topic.HasAnswer)
            {
                var answer = await _dbContext.Posts
                             .Include(x => x.CreatedByUser)
                             .Where(x =>
                                    x.TopicId == topic.Id &&
                                    x.Status == StatusType.Published &&
                                    x.IsAnswer)
                             .FirstOrDefaultAsync();

                if (answer != null)
                {
                    result.Answer = new TopicPageModel.ReplyModel
                    {
                        Id              = answer.Id,
                        Content         = Markdown.ToHtml(answer.Content),
                        OriginalContent = answer.Content,
                        IdentityUserId  = answer.CreatedByUser.IdentityUserId,
                        UserId          = answer.CreatedByUser.Id,
                        UserDisplayName = answer.CreatedByUser.DisplayName,
                        TimeStamp       = answer.CreatedOn,
                        GravatarHash    = _gravatarService.HashEmailForGravatar(answer.CreatedByUser.Email),
                        IsAnswer        = answer.IsAnswer
                    };
                }
            }

            return(result);
        }