Beispiel #1
0
        public async Task ToggleBoard(InputModels.ToggleBoardInput input)
        {
            var topic = await DbContext.Topics.FindAsync(input.TopicId);

            var boards = await BoardRepository.Records();

            if (topic is null || topic.Deleted || !boards.Any(r => r.Id == input.BoardId))
            {
                throw new HttpNotFoundError();
            }

            var existingRecords = await DbContext.TopicBoards.Where(p => p.TopicId == input.TopicId && p.BoardId == input.BoardId).ToListAsync();

            if (existingRecords.Count() == 0)
            {
                var topicBoardRecord = new DataModels.TopicBoard {
                    TopicId = input.TopicId,
                    BoardId = input.BoardId,
                    UserId  = UserContext.ApplicationUser.Id
                };

                DbContext.TopicBoards.Add(topicBoardRecord);
            }
            else
            {
                DbContext.TopicBoards.RemoveRange(existingRecords);
            }

            await DbContext.SaveChangesAsync();
        }
Beispiel #2
0
 public TopicRepository(
     ApplicationDbContext dbContext,
     UserContext userContext,
     BoardRepository boardRepository,
     BookmarkRepository bookmarkRepository,
     MessageRepository messageRepository,
     NotificationRepository notificationRepository,
     RoleRepository roleRepository,
     SmileyRepository smileyRepository,
     AccountRepository accountRepository,
     IActionContextAccessor actionContextAccessor,
     IUrlHelperFactory urlHelperFactory
     )
 {
     DbContext              = dbContext;
     UserContext            = userContext;
     AccountRepository      = accountRepository;
     BoardRepository        = boardRepository;
     BookmarkRepository     = bookmarkRepository;
     MessageRepository      = messageRepository;
     NotificationRepository = notificationRepository;
     RoleRepository         = roleRepository;
     SmileyRepository       = smileyRepository;
     UrlHelper              = urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext);
 }
        public async Task <List <ViewModels.Messages.DisplayMessage> > GetUserMessages(string userId, int page)
        {
            var take = CurrentUser.ApplicationUser.MessagesPerPage;
            var skip = (page - 1) * take;

            var messageQuery = from message in DbContext.Messages
                               where message.PostedById == userId
                               where !message.Deleted
                               orderby message.Id descending
                               select new {
                message.Id,
                message.TopicId
            };

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

            foreach (var message in messageQuery)
            {
                if (!await BoardRepository.CanAccess(message.TopicId))
                {
                    if (attempts++ > 100)
                    {
                        break;
                    }

                    continue;
                }

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

                messageIds.Add(message.Id);

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

            var messages = await GetMessages(messageIds);

            foreach (var message in messages)
            {
                message.ShowControls = false;
            }

            return(messages);
        }
Beispiel #4
0
        public async Task Toggle(InputModels.ToggleBoardInput input)
        {
            var messageRecord = await DbContext.Messages.FindAsync(input.MessageId);

            if (messageRecord is null)
            {
                throw new HttpNotFoundError();
            }

            if (!(await BoardRepository.Records()).Any(r => r.Id == input.BoardId))
            {
                throw new HttpNotFoundError();
            }

            var messageId = input.MessageId;

            if (messageRecord.ParentId > 0)
            {
                messageId = messageRecord.ParentId;
            }

            var boardId = input.BoardId;

            var existingRecord = await DbContext.MessageBoards.FirstOrDefaultAsync(p => p.MessageId == messageId && p.BoardId == boardId);

            if (existingRecord is null)
            {
                var messageBoardRecord = new DataModels.MessageBoard {
                    MessageId = messageId,
                    BoardId   = boardId,
                    UserId    = UserContext.ApplicationUser.Id
                };

                DbContext.MessageBoards.Add(messageBoardRecord);
            }
            else
            {
                DbContext.MessageBoards.Remove(existingRecord);
            }

            await DbContext.SaveChangesAsync();
        }
Beispiel #5
0
 public TopicRepository(
     ApplicationDbContext dbContext,
     UserContext userContext,
     BoardRepository boardRepository,
     BookmarkRepository bookmarkRepository,
     MessageRepository messageRepository,
     AccountRepository accountRepository,
     IHubContext <ForumHub> forumHub,
     IActionContextAccessor actionContextAccessor,
     IUrlHelperFactory urlHelperFactory
     )
 {
     DbContext          = dbContext;
     UserContext        = userContext;
     AccountRepository  = accountRepository;
     BoardRepository    = boardRepository;
     BookmarkRepository = bookmarkRepository;
     MessageRepository  = messageRepository;
     ForumHub           = forumHub;
     UrlHelper          = urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext);
 }
Beispiel #6
0
        public async Task <ControllerModels.Topics.CreateTopicResult> CreateTopic(ControllerModels.Topics.CreateTopicInput input)
        {
            var result = new ControllerModels.Topics.CreateTopicResult();

            var processedMessage = await MessageRepository.ProcessMessageInput(input.Body);

            result.Errors = processedMessage.Errors;

            if (!result.Errors.Any())
            {
                var message = await MessageRepository.CreateMessageRecord(processedMessage);

                var topic = new DataModels.Topic {
                    FirstMessageId           = message.Id,
                    FirstMessagePostedById   = message.PostedById,
                    FirstMessageTimePosted   = message.TimePosted,
                    FirstMessageShortPreview = message.ShortPreview,
                    LastMessageId            = message.Id,
                    LastMessagePostedById    = message.PostedById,
                    LastMessageTimePosted    = message.TimePosted,
                    LastMessageShortPreview  = message.ShortPreview,
                };

                DbContext.Topics.Add(topic);
                await DbContext.SaveChangesAsync();

                message.TopicId = topic.Id;
                DbContext.Update(message);

                MessageRepository.UpdateTopicParticipation(topic.Id, UserContext.ApplicationUser.Id, message.TimePosted);

                var boards = await BoardRepository.Records();

                foreach (var selectedBoard in input.SelectedBoards)
                {
                    var board = boards.FirstOrDefault(item => item.Id == selectedBoard);

                    if (board != null)
                    {
                        DbContext.TopicBoards.Add(new DataModels.TopicBoard {
                            TopicId   = topic.Id,
                            BoardId   = board.Id,
                            TimeAdded = DateTime.Now,
                            UserId    = UserContext.ApplicationUser.Id
                        });
                    }
                }

                await DbContext.SaveChangesAsync();

                await ForumHub.Clients.All.SendAsync("new-topic", new HubModels.Message {
                    TopicId   = topic.Id,
                    MessageId = message.Id
                });

                result.TopicId   = topic.Id;
                result.MessageId = message.Id;
            }

            return(result);
        }
Beispiel #7
0
        public async Task <List <int> > GetIndexIds(int boardId, int page, int unreadFilter)
        {
            var take             = UserContext.ApplicationUser.TopicsPerPage;
            var skip             = (page - 1) * take;
            var historyTimeLimit = DateTime.Now.AddDays(-14);

            var topicQuery = from topic in DbContext.Topics
                             where !topic.Deleted
                             select new {
                topic.Id,
                topic.LastMessageTimePosted,
                topic.Pinned
            };

            if (boardId > 0)
            {
                topicQuery = from topic in DbContext.Topics
                             join topicBoard in DbContext.TopicBoards on topic.Id equals topicBoard.TopicId
                             where topicBoard.BoardId == boardId
                             where !topic.Deleted
                             select new {
                    topic.Id,
                    topic.LastMessageTimePosted,
                    topic.Pinned
                };
            }

            if (unreadFilter > 0)
            {
                topicQuery = topicQuery.Where(m => m.LastMessageTimePosted > historyTimeLimit);
            }

            var sortedTopicQuery = from topic in topicQuery
                                   orderby topic.LastMessageTimePosted descending
                                   orderby topic.Pinned descending
                                   select new {
                topic.Id,
                topic.LastMessageTimePosted
            };

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

            foreach (var topic in sortedTopicQuery)
            {
                if (!await BoardRepository.CanAccess(topic.Id))
                {
                    if (attempts++ > 100)
                    {
                        break;
                    }

                    continue;
                }

                var unreadLevel = unreadFilter == 0 ? 0 : GetUnreadLevel(topic.Id, topic.LastMessageTimePosted);

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

                    continue;
                }

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

                topicIds.Add(topic.Id);

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

            return(topicIds);
        }
Beispiel #8
0
        public async Task <List <ViewModels.TopicPreview> > GetPreviews(List <int> topicIds)
        {
            var topicsQuery = from topic in DbContext.Topics
                              where topicIds.Contains(topic.Id)
                              where !topic.Deleted
                              select topic;

            var topics = await topicsQuery.ToListAsync();

            var topicBoardsQuery = from topicBoard in DbContext.TopicBoards
                                   where topicIds.Contains(topicBoard.TopicId)
                                   select new {
                topicBoard.BoardId,
                topicBoard.TopicId
            };

            var topicBoards = await topicBoardsQuery.ToListAsync();

            var users = await AccountRepository.Records();

            var topicPreviews = new List <ViewModels.TopicPreview>();
            var today         = DateTime.Now.Date;

            var boards = await BoardRepository.Records();

            foreach (var topicId in topicIds)
            {
                var topic = topics.First(item => item.Id == topicId);
                var firstMessagePostedBy = users.First(r => r.Id == topic.FirstMessagePostedById);
                var lastMessagePostedBy  = users.FirstOrDefault(r => r.Id == topic.LastMessagePostedById);

                var topicPreview = new ViewModels.TopicPreview {
                    Id                           = topic.Id,
                    Pinned                       = topic.Pinned,
                    ViewCount                    = topic.ViewCount,
                    ReplyCount                   = topic.ReplyCount,
                    Popular                      = topic.ReplyCount > UserContext.ApplicationUser.PopularityLimit,
                    Pages                        = Convert.ToInt32(Math.Ceiling(1.0 * topic.ReplyCount / UserContext.ApplicationUser.MessagesPerPage)),
                    FirstMessageId               = topic.FirstMessageId,
                    FirstMessageTimePosted       = topic.FirstMessageTimePosted,
                    FirstMessagePostedById       = topic.FirstMessagePostedById,
                    FirstMessagePostedByName     = firstMessagePostedBy?.DecoratedName ?? "User",
                    FirstMessagePostedByBirthday = today == new DateTime(today.Year, firstMessagePostedBy.Birthday.Month, firstMessagePostedBy.Birthday.Day).Date,
                    FirstMessageShortPreview     = string.IsNullOrEmpty(topic.FirstMessageShortPreview.Trim()) ? "No subject" : topic.FirstMessageShortPreview,
                    LastMessageId                = topic.Id,
                    LastMessageTimePosted        = topic.LastMessageTimePosted,
                    LastMessagePostedById        = topic.LastMessagePostedById,
                    LastMessagePostedByName      = lastMessagePostedBy?.DecoratedName ?? "User",
                    LastMessagePostedByBirthday  = today == new DateTime(today.Year, firstMessagePostedBy.Birthday.Month, firstMessagePostedBy.Birthday.Day).Date,
                };

                topicPreviews.Add(topicPreview);

                var historyTimeLimit = DateTime.Now.AddDays(-14);

                if (topic.LastMessageTimePosted > historyTimeLimit)
                {
                    topicPreview.Unread = GetUnreadLevel(topic.Id, topic.LastMessageTimePosted);
                }

                var topicPreviewBoardsQuery = from topicBoard in topicBoards
                                              where topicBoard.TopicId == topic.Id
                                              join board in boards on topicBoard.BoardId equals board.Id
                                              orderby board.DisplayOrder
                                              select new Models.ViewModels.Boards.IndexBoard {
                    Id           = board.Id.ToString(),
                    Name         = board.Name,
                    Description  = board.Description,
                    DisplayOrder = board.DisplayOrder,
                };

                topicPreview.Boards = topicPreviewBoardsQuery.ToList();
            }

            return(topicPreviews);
        }
Beispiel #9
0
        public async Task <List <int> > GetIndexIds(int boardId, int page, int unreadFilter)
        {
            var take             = UserContext.ApplicationUser.TopicsPerPage;
            var skip             = (page - 1) * take;
            var historyTimeLimit = DateTime.Now.AddDays(-14);

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

            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,
                    message.Pinned
                };
            }

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

            var sortedMessageQuery = from message in messageQuery
                                     orderby message.LastReplyPosted descending
                                     orderby message.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 (!await BoardRepository.CanAccess(message.Id))
                {
                    if (attempts++ > 100)
                    {
                        break;
                    }

                    continue;
                }

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

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

                    continue;
                }

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

                messageIds.Add(message.Id);

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

            return(messageIds);
        }
Beispiel #10
0
        public async Task <List <ItemModels.MessagePreview> > GetPreviews(List <int> messageIds)
        {
            var messageQuery = from message in DbContext.Messages
                               where messageIds.Contains(message.Id)
                               select new {
                message.Id,
                message.ShortPreview,
                message.ViewCount,
                message.ReplyCount,
                message.TimePosted,
                message.PostedById,
                message.LastReplyId,
                message.LastReplyById,
                message.LastReplyPosted,
                message.Pinned
            };

            var messages = await messageQuery.ToListAsync();

            var users = await AccountRepository.Records();

            var messagePreviews = new List <ItemModels.MessagePreview>();
            var today           = DateTime.Now.Date;

            var boards = await BoardRepository.Records();

            foreach (var messageId in messageIds)
            {
                var message  = messages.First(item => item.Id == messageId);
                var postedBy = users.First(r => r.Id == message.PostedById);

                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 / UserContext.ApplicationUser.MessagesPerPage)),
                    LastReplyId      = message.Id,
                    Popular          = message.ReplyCount > UserContext.ApplicationUser.PopularityLimit,
                    Pinned           = message.Pinned,
                    TimePosted       = message.TimePosted,
                    PostedById       = message.PostedById,
                    PostedByName     = postedBy.DecoratedName,
                    PostedByBirthday = today == new DateTime(today.Year, postedBy.Birthday.Month, postedBy.Birthday.Day).Date
                };

                messagePreviews.Add(messagePreview);

                var lastMessageTime = message.TimePosted;

                if (message.LastReplyId != 0)
                {
                    var lastReply = (from item in DbContext.Messages
                                     where item.Id == message.LastReplyId
                                     select new {
                        item.ShortPreview
                    }).FirstOrDefault();

                    if (lastReply != null)
                    {
                        var lastReplyBy = users.FirstOrDefault(r => r.Id == message.LastReplyById);

                        messagePreview.LastReplyId      = message.LastReplyId;
                        messagePreview.LastReplyPreview = lastReply.ShortPreview;
                        messagePreview.LastReplyById    = message.LastReplyById;
                        messagePreview.LastReplyPosted  = message.LastReplyPosted;
                        lastMessageTime = message.LastReplyPosted;
                        messagePreview.LastReplyByName = lastReplyBy?.DecoratedName ?? "User";

                        if (!(lastReplyBy is null))
                        {
                            messagePreview.LastReplyByBirthday = today.Date == new DateTime(today.Year, lastReplyBy.Birthday.Month, lastReplyBy.Birthday.Day).Date;
                        }
                    }
                }

                var historyTimeLimit = DateTime.Now.AddDays(-14);

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

                var boardIdQuery = from messageBoard in DbContext.MessageBoards
                                   where messageBoard.MessageId == message.Id
                                   select messageBoard.BoardId;

                var messageBoards = new List <Models.ViewModels.Boards.Items.IndexBoard>();

                foreach (var boardId in boardIdQuery)
                {
                    var boardRecord = boards.Single(r => r.Id == boardId);

                    messageBoards.Add(new Models.ViewModels.Boards.Items.IndexBoard {
                        Id           = boardRecord.Id.ToString(),
                        Name         = boardRecord.Name,
                        Description  = boardRecord.Description,
                        DisplayOrder = boardRecord.DisplayOrder,
                    });
                }

                messagePreview.Boards = messageBoards.OrderBy(r => r.DisplayOrder).ToList();
            }

            return(messagePreviews);
        }