예제 #1
0
        public async Task <ItemViewModels.IndexBoard> GetIndexBoard(DataModels.Board boardRecord, bool includeReplies = false)
        {
            var indexBoard = new ItemViewModels.IndexBoard {
                Id           = boardRecord.Id.ToString(),
                Name         = boardRecord.Name,
                Description  = boardRecord.Description,
                DisplayOrder = boardRecord.DisplayOrder,
                Unread       = false
            };

            if (includeReplies)
            {
                var messages = from messageBoard in DbContext.MessageBoards
                               join message in DbContext.Messages on messageBoard.MessageId equals message.Id
                               where messageBoard.BoardId == boardRecord.Id
                               orderby message.LastReplyPosted descending
                               select new {
                    messageBoard.MessageId,
                    LastReplyId  = message.LastReplyId > 0 ? message.LastReplyId : message.Id,
                    TopicPreview = message.ShortPreview
                };

                var users = await AccountRepository.Records();

                // Only checks the most recent 10 topics. If all 10 are forbidden, then LastMessage stays null.
                foreach (var item in messages.Take(10))
                {
                    var messageBoardQuery = from messageBoard in DbContext.MessageBoards
                                            where messageBoard.MessageId == item.MessageId
                                            select messageBoard.BoardId;

                    var messageBoards = messageBoardQuery.ToList();

                    var messageRoleIds = from role in await RoleRepository.BoardRoles()
                                             where messageBoards.Contains(role.BoardId)
                                         select role.RoleId;

                    if (UserContext.IsAdmin || !messageRoleIds.Any() || messageRoleIds.Intersect(UserContext.Roles).Any())
                    {
                        var lastReplyQuery = from message in DbContext.Messages
                                             where message.Id == item.LastReplyId
                                             select new Models.ViewModels.Topics.Items.MessagePreview {
                            Id              = message.Id,
                            ShortPreview    = item.TopicPreview,
                            LastReplyId     = message.LastReplyId,
                            LastReplyById   = message.LastReplyById,
                            LastReplyPosted = message.LastReplyPosted,
                        };

                        indexBoard.LastMessage = lastReplyQuery.FirstOrDefault();
                        indexBoard.LastMessage.LastReplyByName = users.FirstOrDefault(r => r.Id == indexBoard.LastMessage.LastReplyById)?.DecoratedName ?? "User";
                        break;
                    }
                }
            }

            return(indexBoard);
        }
예제 #2
0
        public ItemViewModels.IndexBoard GetIndexBoard(DataModels.Board boardRecord, bool includeReplies = false)
        {
            var indexBoard = new ItemViewModels.IndexBoard {
                Id           = boardRecord.Id,
                Name         = boardRecord.Name,
                Description  = boardRecord.Description,
                DisplayOrder = boardRecord.DisplayOrder,
                Unread       = false
            };

            if (includeReplies)
            {
                var messages = from messageBoard in DbContext.MessageBoards
                               join message in DbContext.Messages on messageBoard.MessageId equals message.Id
                               where messageBoard.BoardId == boardRecord.Id
                               orderby message.LastReplyPosted descending
                               select new {
                    messageBoard.MessageId,
                    LastReplyId  = message.LastReplyId > 0 ? message.LastReplyId : message.Id,
                    TopicPreview = message.ShortPreview
                };

                // Only checks the most recent 10 topics. If all 10 are forbidden, then LastMessage stays null.
                foreach (var item in messages.Take(10))
                {
                    var messageRoles = from messageBoard in DbContext.MessageBoards
                                       join boardRole in DbContext.BoardRoles on messageBoard.BoardId equals boardRole.BoardId
                                       where messageBoard.MessageId == item.MessageId
                                       select boardRole.RoleId;

                    if (UserContext.IsAdmin || !messageRoles.Any() || messageRoles.Intersect(UserContext.Roles).Any())
                    {
                        var lastReply = from message in DbContext.Messages
                                        join lastReplyBy in DbContext.Users on message.PostedById equals lastReplyBy.Id
                                        where message.Id == item.LastReplyId
                                        select new Models.ViewModels.Topics.Items.MessagePreview {
                            Id              = message.Id,
                            ShortPreview    = item.TopicPreview,
                            LastReplyByName = lastReplyBy.DisplayName,
                            LastReplyId     = message.LastReplyId,
                            LastReplyPosted = message.LastReplyPosted.ToPassedTimeString(),
                        };

                        indexBoard.LastMessage = lastReply.FirstOrDefault();
                        break;
                    }
                }
            }

            return(indexBoard);
        }