Example #1
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,
                    Name         = board.Name,
                    Description  = board.Description,
                    DisplayOrder = board.DisplayOrder,
                };

                topicPreview.Boards = topicPreviewBoardsQuery.ToList();

                var topicEventQuery = from topicEvent in DbContext.Events
                                      where topicEvent.TopicId == topic.Id
                                      select topicEvent;

                topicPreview.Event = await topicEventQuery.AnyAsync();
            }

            return(topicPreviews);
        }
Example #2
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);
        }