public AddUserModToSubResponse Handle(AddUserModToSub command) { var response = new AddUserModToSubResponse(); try { var requestingUser = _membershipService.GetUserById(command.RequestingUser); if (requestingUser == null) { response.Error = "Invalid user."; return(response); } var userToAdd = _membershipService.GetUserById(command.UserToAdd); if (userToAdd == null) { response.Error = "Invalid user."; return(response); } Sub sub = null; if (command.SubId.HasValue) { sub = _subService.GetSubById(command.SubId.Value); } else if (!string.IsNullOrEmpty(command.SubName)) { sub = _subService.GetSubByName(command.SubName); } if (sub == null) { response.Error = "Invalid sub."; return(response); } if (requestingUser.IsAdmin) { // admins can do anything _moderationService.AddModToSub(userToAdd.Id, sub.Id, command.Permissions, requestingUser.Id); return(response); } var requestingUserModInfo = _moderationService.GetModeratorInfoForUserInSub(requestingUser.Id, sub.Id); if (requestingUserModInfo == null) { response.Error = "Requesting user is not a mod of the sub."; return(response); } var userToChangeModInfo = _moderationService.GetModeratorInfoForUserInSub(userToAdd.Id, sub.Id); if (userToChangeModInfo != null) { response.Error = "The user to already a mod of the sub."; return(response); } _moderationInviteService.RemoveModeratorInvite(userToAdd.Id, sub.Id); // just in case _moderationService.AddModToSub(userToAdd.Id, sub.Id, command.Permissions, requestingUser.Id); } catch (Exception ex) { response.Error = "An unknown error occured."; _logger.Error("An error occured adding a user as a mod.", ex); } return(response); }
public CreateSubResponse Handle(CreateSub command) { var response = new CreateSubResponse(); try { if (string.IsNullOrEmpty(command.Name)) { response.Error = "Sub name is required."; return(response); } if (!Regex.IsMatch(command.Name, "^[a-zA-Z0-9]*$")) { response.Error = "No spaces or special characters."; return(response); } if (command.Name.Length > 20) { response.Error = "The name length is limited to 20 characters."; return(response); } if (string.IsNullOrEmpty(command.Description)) { response.Error = "Please describe your sub."; return(response); } if (!string.IsNullOrEmpty(command.SubmissionText) && command.SubmissionText.Length > 1000) { response.Error = "The sidebar text cannot be greater than 1000 characters"; return(response); } if (!string.IsNullOrEmpty(command.SidebarText) && command.SidebarText.Length > 3000) { response.Error = "The submission text cannot be greater than 1000 characters"; return(response); } var user = _membershipService.GetUserById(command.CreatedByUserId); if (user == null) { response.Error = "Invalid user."; return(response); } var userAccountAge = Common.CurrentTime() - user.CreatedDate; if (!user.IsAdmin && userAccountAge.TotalDays < _subSettings.Settings.MinUserAgeCreateSub) { response.Error = "Your account is too new to create a sub."; return(response); } if (_subService.GetSubByName(command.Name) != null) { response.Error = "The sub already exist."; return(response); } if (command.Name.Equals("random", StringComparison.InvariantCultureIgnoreCase) || command.Name.Equals("all", StringComparison.InvariantCultureIgnoreCase) || Common.IsReservedKeyword(command.Name)) { response.Error = "The name is invalid."; return(response); } // let's make sure the user creating this sub doesn't already have the maximum number of subs they are modding. var moddedSubsForUser = _moderationService.GetSubsModeratoredByUser(user.Id); if (moddedSubsForUser.Count >= _subSettings.Settings.MaximumNumberOfModdedSubs) { response.Error = "You can only moderate a maximum of " + _subSettings.Settings.MaximumNumberOfModdedSubs + " subs."; return(response); } var sub = new Sub { Id = GuidUtil.NewSequentialId(), CreatedDate = Common.CurrentTime(), Name = command.Name, Description = command.Description, SidebarText = command.SidebarText, SidebarTextFormatted = _markdownCompiler.Compile(command.SidebarText), SubmissionText = command.SubmissionText, SubmissionTextFormatted = _markdownCompiler.Compile(command.SubmissionText), SubType = command.Type, CreatedBy = user.Id, InAll = command.InAll, Nsfw = command.Nsfw }; // only admins can configure default subs if (user.IsAdmin && command.IsDefault.HasValue) { sub.IsDefault = command.IsDefault.Value; } _subService.InsertSub(sub); response.SubId = sub.Id; response.SubName = sub.Name; _subService.SubscribeToSub(user.Id, sub.Id); _moderationService.AddModToSub(user.Id, sub.Id, ModeratorPermissions.All); } catch (Exception ex) { // todo: log response.Error = ex.Message; } return(response); }