示例#1
0
        public TopicRepository(
            ApplicationDbContext dbContext,
            UserContext userContext,
            BoardRepository boardRepository,
            MessageRepository messageRepository,
            PinRepository pinRepository,
            NotificationRepository notificationRepository,
            RoleRepository roleRepository,
            SettingsRepository settingsRepository,
            SmileyRepository smileyRepository,
            AccountRepository accountRepository,
            IActionContextAccessor actionContextAccessor,
            IUrlHelperFactory urlHelperFactory
            )
        {
            DbContext   = dbContext;
            UserContext = userContext;

            AccountRepository      = accountRepository;
            BoardRepository        = boardRepository;
            MessageRepository      = messageRepository;
            NotificationRepository = notificationRepository;
            PinRepository          = pinRepository;
            RoleRepository         = roleRepository;
            SettingsRepository     = settingsRepository;
            SmileyRepository       = smileyRepository;

            UrlHelper = urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext);
        }
示例#2
0
        public List <int> GetIndexIds(int boardId, int page, int unreadFilter, DateTime historyTimeLimit, List <DataModels.Participant> participation, List <DataModels.ViewLog> viewLogs)
        {
            var take = SettingsRepository.TopicsPerPage();
            var skip = (page - 1) * take;

            var forbiddenBoardIdsQuery = from role in RoleRepository.SiteRoles
                                         join board in RoleRepository.BoardRoles on role.Id equals board.RoleId
                                         where !UserContext.Roles.Contains(role.Id)
                                         select board.BoardId;

            var forbiddenBoardIds = forbiddenBoardIdsQuery.ToList();

            var messageQuery = from message in DbContext.Messages
                               where message.ParentId == 0
                               select new {
                message.Id,
                message.LastReplyPosted
            };

            if (boardId > 0)
            {
                messageQuery = from message in DbContext.Messages
                               join messageBoard in DbContext.MessageBoards on message.Id equals messageBoard.MessageId
                               where message.ParentId == 0
                               where messageBoard.BoardId == boardId
                               select new {
                    message.Id,
                    message.LastReplyPosted
                };
            }

            if (unreadFilter > 0)
            {
                messageQuery = messageQuery.Where(m => m.LastReplyPosted > historyTimeLimit);
            }

            var pinnedTopicIds = PinRepository.Select(item => item.MessageId).ToList();

            var sortedMessageQuery = from message in messageQuery
                                     let pinned = pinnedTopicIds.Contains(message.Id)
                                                  orderby message.LastReplyPosted descending
                                                  orderby pinned descending
                                                  select new {
                message.Id,
                message.LastReplyPosted
            };

            var messageIds = new List <int>();
            var attempts   = 0;
            var skipped    = 0;

            foreach (var message in sortedMessageQuery)
            {
                if (IsAccessDenied(message.Id, forbiddenBoardIds))
                {
                    if (attempts++ > 100)
                    {
                        break;
                    }

                    continue;
                }

                var unreadLevel = unreadFilter == 0 ? 0 : GetUnreadLevel(message.Id, message.LastReplyPosted, participation, viewLogs);

                if (unreadLevel < unreadFilter)
                {
                    if (attempts++ > 100)
                    {
                        break;
                    }

                    continue;
                }

                if (skipped++ < skip)
                {
                    continue;
                }

                messageIds.Add(message.Id);

                if (messageIds.Count == take)
                {
                    break;
                }
            }

            return(messageIds);
        }
示例#3
0
        public List <ItemModels.MessagePreview> GetPreviews(int boardId, int page, int unread)
        {
            var participation    = new List <DataModels.Participant>();
            var viewLogs         = new List <DataModels.ViewLog>();
            var historyTimeLimit = SettingsRepository.HistoryTimeLimit();

            if (UserContext.IsAuthenticated)
            {
                participation = DbContext.Participants.Where(r => r.UserId == UserContext.ApplicationUser.Id).ToList();
                viewLogs      = DbContext.ViewLogs.Where(r => r.LogTime >= historyTimeLimit && r.UserId == UserContext.ApplicationUser.Id).ToList();
            }

            var sortedMessageIds = GetIndexIds(boardId, page, unread, historyTimeLimit, participation, viewLogs);

            var messageQuery = from message in DbContext.Messages
                               where sortedMessageIds.Contains(message.Id)
                               select new {
                message.Id,
                message.ShortPreview,
                message.ViewCount,
                message.ReplyCount,
                message.TimePosted,
                message.LastReplyId,
                message.LastReplyById,
                message.LastReplyPosted
            };

            var messages = messageQuery.ToList();

            var take            = SettingsRepository.MessagesPerPage();
            var popularityLimit = SettingsRepository.PopularityLimit();

            var messagePreviews = new List <ItemModels.MessagePreview>();

            foreach (var messageId in sortedMessageIds)
            {
                var message = messages.First(item => item.Id == messageId);

                var messagePreview = new ItemModels.MessagePreview {
                    Id           = message.Id,
                    ShortPreview = string.IsNullOrEmpty(message.ShortPreview.Trim()) ? "No subject" : message.ShortPreview,
                    Views        = message.ViewCount,
                    Replies      = message.ReplyCount,
                    Pages        = Convert.ToInt32(Math.Ceiling(1.0 * message.ReplyCount / take)),
                    LastReplyId  = message.Id,
                    Popular      = message.ReplyCount > popularityLimit,
                    Pinned       = PinRepository.Any(item => item.MessageId == message.Id)
                };

                messagePreviews.Add(messagePreview);

                var lastMessageTime = message.TimePosted;

                if (message.LastReplyId != 0)
                {
                    var lastReplyDetails = (from item in DbContext.Messages
                                            join postedBy in DbContext.Users on item.PostedById equals postedBy.Id
                                            where item.Id == message.LastReplyId
                                            select new {
                        postedBy.DisplayName,
                        item.ShortPreview
                    }).FirstOrDefault();

                    if (lastReplyDetails != null)
                    {
                        messagePreview.LastReplyId       = message.LastReplyId;
                        messagePreview.LastReplyPreview  = lastReplyDetails.ShortPreview;
                        messagePreview.LastReplyByName   = lastReplyDetails.DisplayName;
                        messagePreview.LastReplyById     = message.LastReplyById;
                        messagePreview.LastReplyPosted   = message.LastReplyPosted.ToPassedTimeString();
                        messagePreview.LastReplyPostedDT = message.LastReplyPosted;
                        lastMessageTime = message.LastReplyPosted;
                    }
                }

                if (lastMessageTime > historyTimeLimit)
                {
                    messagePreview.Unread = GetUnreadLevel(message.Id, lastMessageTime, participation, viewLogs);
                }
            }

            return(messagePreviews);
        }