Example #1
0
        public async Task <bool> SetModerator(string groupId, string userId, bool isModerator = true)
        {
            var currentUser = await profileService.GetCurrentUser();

            var group = await database.GroupRepository.FindById(groupId) ??
                        throw new EntityNotFoundException("Group not found");

            if (currentUser.Id != group.AdminId)
            {
                throw new NoPermissionsException("You are not allowed to manage this group");
            }

            var member = GetMember(userId, group);

            if (!member.IsAccepted)
            {
                throw new NoPermissionsException("This user is not member of this group");
            }

            member.SetIsModerator(isModerator);

            if (await database.Complete())
            {
                await notifier.Push(
                    isModerator
                    ?NotificationMessages.GroupModeratorGrantedNotification(group.Name)
                    : NotificationMessages.GroupModeratorRevokedNotification(group.Name),
                    userId,
                    isModerator?NotificationType.GroupModeratorGranted : NotificationType.GroupModeratorRevoked);

                return(true);
            }

            return(false);
        }