Beispiel #1
0
        public async Task <List <DataModels.Board> > GetTopicBoards(int topicId)
        {
            var topicBoardsQuery = from topicBoard in DbContext.TopicBoards
                                   where topicBoard.TopicId == topicId
                                   select topicBoard.BoardId;

            var boardIds = await topicBoardsQuery.ToListAsync();

            var boards = await Records();

            var assignedBoards = boards.Where(r => boardIds.Contains(r.Id)).ToList();

            if (!await RoleRepository.CanAccessBoards(assignedBoards))
            {
                throw new HttpForbiddenError();
            }

            return(assignedBoards);
        }
Beispiel #2
0
        public async Task <List <ViewModels.Boards.IndexCategory> > CategoryIndex(bool includeReplies = false)
        {
            var categories = (await Categories()).OrderBy(r => r.DisplayOrder).ToList();

            var indexCategories = new List <ViewModels.Boards.IndexCategory>();

            foreach (var categoryRecord in categories)
            {
                var indexCategory = new ViewModels.Boards.IndexCategory {
                    Id           = categoryRecord.Id.ToString(),
                    Name         = categoryRecord.Name,
                    DisplayOrder = categoryRecord.DisplayOrder
                };

                foreach (var board in (await Records()).Where(r => r.CategoryId == categoryRecord.Id))
                {
                    var thisBoardRoles = from role in await RoleRepository.BoardRoles()
                                             where role.BoardId == board.Id
                                         select role;

                    var authorized = UserContext.IsAdmin || !thisBoardRoles.Any() || (UserContext.Roles?.Any(userRole => thisBoardRoles.Any(boardRole => boardRole.RoleId == userRole)) ?? false);

                    if (!authorized)
                    {
                        continue;
                    }

                    var indexBoard = await GetIndexBoard(board, includeReplies);

                    indexCategory.Boards.Add(indexBoard);
                }

                // Don't index the category if there's no boards available to the user
                if (indexCategory.Boards.Any())
                {
                    indexCategories.Add(indexCategory);
                }
            }

            return(indexCategories);
        }
Beispiel #3
0
        public async Task <ServiceModels.ServiceResponse> UpdateBoard(InputModels.EditBoardInput input)
        {
            var serviceResponse = new ServiceModels.ServiceResponse();

            var record = (await Records()).FirstOrDefault(b => b.Id == input.Id);

            if (record is null)
            {
                serviceResponse.Error($"A record does not exist with ID '{input.Id}'");
            }

            DataModels.Category newCategoryRecord = null;

            if (!string.IsNullOrEmpty(input.NewCategory))
            {
                input.NewCategory = input.NewCategory.Trim();
            }

            if (!string.IsNullOrEmpty(input.NewCategory))
            {
                newCategoryRecord = (await Categories()).FirstOrDefault(c => c.Name == input.NewCategory);

                if (newCategoryRecord is null)
                {
                    var displayOrder = (await Categories()).DefaultIfEmpty().Max(c => c.DisplayOrder);

                    newCategoryRecord = new DataModels.Category {
                        Name         = input.NewCategory,
                        DisplayOrder = displayOrder + 1
                    };

                    DbContext.Categories.Add(newCategoryRecord);
                    DbContext.SaveChanges();
                }
            }
            else
            {
                try {
                    var newCategoryId = Convert.ToInt32(input.Category);
                    newCategoryRecord = (await Categories()).FirstOrDefault(c => c.Id == newCategoryId);

                    if (newCategoryRecord is null)
                    {
                        serviceResponse.Error(nameof(input.Category), "No category was found with this ID.");
                    }
                }
                catch (FormatException) {
                    serviceResponse.Error(nameof(input.Category), "Invalid category ID");
                }
            }

            if (!string.IsNullOrEmpty(input.Name))
            {
                input.Name = input.Name.Trim();
            }

            if (string.IsNullOrEmpty(input.Name))
            {
                serviceResponse.Error(nameof(input.Name), "Name is a required field.");
            }

            if (!string.IsNullOrEmpty(input.Description))
            {
                input.Description = input.Description.Trim();
            }

            if (!serviceResponse.Success)
            {
                return(serviceResponse);
            }

            record.Name        = input.Name;
            record.Description = input.Description;

            var oldCategoryId = -1;

            if (record.CategoryId != newCategoryRecord.Id)
            {
                var categoryBoards = (await Records()).Where(r => r.CategoryId == record.CategoryId).ToList();

                if (categoryBoards.Count() <= 1)
                {
                    oldCategoryId = record.CategoryId;
                }

                record.CategoryId = newCategoryRecord.Id;
            }

            var boardRoles = (from role in await RoleRepository.BoardRoles()
                              where role.BoardId == record.Id
                              select role).ToList();

            foreach (var boardRole in boardRoles)
            {
                DbContext.BoardRoles.Remove(boardRole);
            }

            if (input.Roles != null)
            {
                var roleIds = (from role in await RoleRepository.SiteRoles()
                               select role.Id).ToList();

                foreach (var inputRole in input.Roles)
                {
                    if (roleIds.Contains(inputRole))
                    {
                        DbContext.BoardRoles.Add(new DataModels.BoardRole {
                            BoardId = record.Id,
                            RoleId  = inputRole
                        });
                    }
                    else
                    {
                        serviceResponse.Error($"Role does not exist with id '{inputRole}'");
                    }
                }
            }

            if (!serviceResponse.Success)
            {
                return(serviceResponse);
            }

            DbContext.Update(record);
            DbContext.SaveChanges();

            if (oldCategoryId >= 0)
            {
                var oldCategoryRecord = (await Categories()).FirstOrDefault(item => item.Id == oldCategoryId);

                if (oldCategoryRecord != null)
                {
                    DbContext.Categories.Remove(oldCategoryRecord);
                    DbContext.SaveChanges();
                }
            }

            serviceResponse.RedirectPath = UrlHelper.Action(nameof(Controllers.Boards.Manage), nameof(Controllers.Boards), new { id = record.Id });

            return(serviceResponse);
        }
Beispiel #4
0
        public async Task <ViewModels.Boards.IndexBoard> GetIndexBoard(DataModels.Board boardRecord, bool includeReplies = false)
        {
            var indexBoard = new ViewModels.Boards.IndexBoard {
                Id           = boardRecord.Id.ToString(),
                Name         = boardRecord.Name,
                Description  = boardRecord.Description,
                DisplayOrder = boardRecord.DisplayOrder,
                Unread       = false
            };

            if (includeReplies)
            {
                var topicsInThisBoard = from topicBoard in DbContext.TopicBoards
                                        join topic in DbContext.Topics on topicBoard.TopicId equals topic.Id
                                        where topicBoard.BoardId == boardRecord.Id
                                        where !topic.Deleted
                                        orderby topic.LastMessageTimePosted descending
                                        select new {
                    topic.Id,
                    topic.LastMessageId,
                    topic.FirstMessageShortPreview
                };

                var users = await AccountRepository.Records();

                // Only checks the most recent 10 topics. If all 10 are forbidden, then LastMessage stays null.
                foreach (var topic in topicsInThisBoard.Take(10))
                {
                    var topicBoardIdsQuery = from topicBoard in DbContext.TopicBoards
                                             where topicBoard.TopicId == topic.Id
                                             select topicBoard.BoardId;

                    var topicBoardIds = topicBoardIdsQuery.ToList();

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

                    if (UserContext.IsAdmin || !relevantRoleIds.Any() || relevantRoleIds.Intersect(UserContext.Roles).Any())
                    {
                        var lastMessage = await DbContext.Messages.FindAsync(topic.LastMessageId);

                        var lastMessageUser = users.FirstOrDefault(r => r.Id == lastMessage.PostedById);

                        indexBoard.RecentTopic = new ViewModels.Topics.TopicPreview {
                            Id = topic.Id,
                            FirstMessageShortPreview    = topic.FirstMessageShortPreview,
                            LastMessageId               = lastMessage.Id,
                            LastMessagePostedById       = lastMessage.PostedById,
                            LastMessageTimePosted       = lastMessage.TimePosted,
                            LastMessagePostedByName     = lastMessageUser?.DecoratedName ?? "User",
                            LastMessagePostedByBirthday = lastMessageUser.IsBirthday,
                        };

                        break;
                    }
                }
            }

            return(indexBoard);
        }