/// <inheritdoc /> public async Task UpdateCommentAsync(long commentId, PromotionSentiment newSentiment, string newContent) { AuthorizationService.RequireAuthenticatedUser(); AuthorizationService.RequireClaims(AuthorizationClaim.PromotionsComment); ValidateComment(newContent); PromotionActionSummary resultAction; using (var transaction = await PromotionCommentRepository.BeginUpdateTransactionAsync()) { var oldComment = await PromotionCommentRepository.ReadSummaryAsync(commentId); var campaign = await PromotionCampaignRepository.ReadDetailsAsync(oldComment.Campaign.Id); if (!(campaign.CloseAction is null)) { throw new InvalidOperationException($"Campaign {oldComment.Campaign.Id} has already been closed"); } resultAction = await PromotionCommentRepository.TryUpdateAsync(commentId, AuthorizationService.CurrentUserId.Value, x => { x.Sentiment = newSentiment; x.Content = newContent; }); transaction.Commit(); } PublishActionNotificationAsync(resultAction); }
/// <inheritdoc /> public async Task UpdateCommentAsync(long commentId, PromotionSentiment newSentiment, string newContent) { AuthorizationService.RequireAuthenticatedUser(); AuthorizationService.RequireClaims(AuthorizationClaim.PromotionsComment); if (newContent is null || newContent.Length <= 3) { throw new InvalidOperationException("Comment content must be longer than 3 characters."); } using (var transaction = await PromotionCommentRepository.BeginUpdateTransactionAsync()) { var oldComment = await PromotionCommentRepository.ReadSummaryAsync(commentId); var campaign = await PromotionCampaignRepository.ReadDetailsAsync(oldComment.Campaign.Id); if (!(campaign.CloseAction is null)) { throw new InvalidOperationException($"Campaign {oldComment.Campaign.Id} has already been closed"); } await PromotionCommentRepository.TryUpdateAsync(commentId, AuthorizationService.CurrentUserId.Value, x => { x.Sentiment = newSentiment; x.Content = newContent; }); transaction.Commit(); } }
/// <inheritdoc /> public async Task AddCommentAsync(long campaignId, PromotionSentiment sentiment, string content) { AuthorizationService.RequireAuthenticatedUser(); AuthorizationService.RequireClaims(AuthorizationClaim.PromotionsComment); ValidateComment(content); if (await PromotionCommentRepository.AnyAsync(new PromotionCommentSearchCriteria() { CampaignId = campaignId, CreatedById = AuthorizationService.CurrentUserId.Value, IsModified = false })) { throw new InvalidOperationException("Only one comment can be made per user, per campaign"); } var campaign = await PromotionCampaignRepository.ReadDetailsAsync(campaignId); if (campaign.Subject.Id == AuthorizationService.CurrentUserId) { throw new InvalidOperationException("You aren't allowed to comment on your own campaign"); } if (!(campaign.CloseAction is null)) { throw new InvalidOperationException($"Campaign {campaignId} has already been closed"); } var rankRoles = await GetRankRolesAsync(AuthorizationService.CurrentGuildId.Value); if (!await CheckIfUserIsRankOrHigherAsync(rankRoles, AuthorizationService.CurrentUserId.Value, campaign.TargetRole.Id)) { throw new InvalidOperationException($"Commenting on a promotion campaign requires a rank at least as high as the proposed target rank"); } PromotionActionSummary resultAction; using (var transaction = await PromotionCommentRepository.BeginCreateTransactionAsync()) { resultAction = await PromotionCommentRepository.CreateAsync(new PromotionCommentCreationData() { GuildId = campaign.GuildId, CampaignId = campaignId, Sentiment = sentiment, Content = content, CreatedById = AuthorizationService.CurrentUserId.Value }); transaction.Commit(); } PublishActionNotificationAsync(resultAction); }
/// <inheritdoc /> public async Task AcceptCampaignAsync(long campaignId) { AuthorizationService.RequireAuthenticatedUser(); AuthorizationService.RequireClaims(AuthorizationClaim.PromotionsCloseCampaign); using (var transaction = await PromotionCampaignRepository.BeginCloseTransactionAsync()) { var campaign = await PromotionCampaignRepository.ReadDetailsAsync(campaignId); if (campaign is null) throw new InvalidOperationException($"Campaign {campaignId} does not exist"); if (!(campaign.CloseAction is null)) throw new InvalidOperationException($"Campaign {campaignId} is already closed"); var timeSince = DateTime.UtcNow - campaign.CreateAction.Created; if (timeSince < TimeSpan.FromHours(48)) throw new InvalidOperationException($"Campaign {campaignId} cannot be accepted until 48 hours after its creation ({48 - timeSince.TotalHours:#.##} hrs remain)"); try { var subject = await UserService.GetGuildUserAsync(campaign.GuildId, campaign.Subject.Id); if (subject.RoleIds.Contains(campaign.TargetRole.Id)) throw new InvalidOperationException($"User {campaign.Subject.DisplayName} is already a member of role {campaign.TargetRole.Name}"); var guild = await DiscordClient.GetGuildAsync(campaign.GuildId); var targetRole = guild.GetRole(campaign.TargetRole.Id); if (targetRole is null) throw new InvalidOperationException($"Role {campaign.TargetRole.Name} no longer exists"); await subject.AddRoleAsync(targetRole); foreach (var lowerRankRole in (await GetRankRolesAsync(AuthorizationService.CurrentGuildId.Value)) .TakeWhile(x => x.Id != targetRole.Id)) { var lowerRole = guild.GetRole(lowerRankRole.Id); if (!(lowerRole is null) && subject.RoleIds.Contains(lowerRole.Id)) await subject.RemoveRoleAsync(lowerRole); } await PromotionCampaignRepository.TryCloseAsync(campaignId, AuthorizationService.CurrentUserId.Value, PromotionCampaignOutcome.Accepted); } catch { await PromotionCampaignRepository.TryCloseAsync(campaignId, AuthorizationService.CurrentUserId.Value, PromotionCampaignOutcome.Failed); throw; } finally { transaction.Commit(); } } }
/// <inheritdoc /> public async Task <PromotionCampaignDetails> GetCampaignDetailsAsync(long campaignId) { AuthorizationService.RequireClaims(AuthorizationClaim.PromotionsRead); var result = await PromotionCampaignRepository.ReadDetailsAsync(campaignId); if (result.Subject.Id == AuthorizationService.CurrentUserId) { throw new InvalidOperationException("You can't view comments on your own campaign."); } return(result); }
/// <inheritdoc /> public async Task AddCommentAsync(long campaignId, PromotionSentiment sentiment, string content) { AuthorizationService.RequireClaims(AuthorizationClaim.PromotionsComment); if (content == null || content.Length <= 3) { throw new InvalidOperationException("Comment content must be longer than 3 characters."); } using (var transaction = await PromotionCommentRepository.BeginCreateTransactionAsync()) { if (await PromotionCommentRepository.AnyAsync(new PromotionCommentSearchCriteria() { CampaignId = campaignId, CreatedById = AuthorizationService.CurrentUserId.Value })) { throw new InvalidOperationException("Only one comment can be made per user, per campaign"); } var campaign = await PromotionCampaignRepository.ReadDetailsAsync(campaignId); if (!(campaign.CloseAction is null)) { throw new InvalidOperationException($"Campaign {campaignId} has already been closed"); } var rankRoles = await GetRankRolesAsync(AuthorizationService.CurrentGuildId.Value); if (!await CheckIfUserIsRankOrHigher(rankRoles, AuthorizationService.CurrentUserId.Value, campaign.TargetRole.Id)) { throw new InvalidOperationException($"Commenting on a promotion campaign requires a rank at least as high as the proposed target rank"); } await PromotionCommentRepository.CreateAsync(new PromotionCommentCreationData() { GuildId = campaign.GuildId, CampaignId = campaignId, Sentiment = sentiment, Content = content, CreatedById = AuthorizationService.CurrentUserId.Value }); transaction.Commit(); } }
/// <inheritdoc /> public async Task AcceptCampaignAsync(long campaignId, bool force) { AuthorizationService.RequireAuthenticatedUser(); AuthorizationService.RequireClaims(AuthorizationClaim.PromotionsCloseCampaign); PromotionActionSummary resultAction; using (var transaction = await PromotionCampaignRepository.BeginCloseTransactionAsync()) { var campaign = await PromotionCampaignRepository.ReadDetailsAsync(campaignId); if (campaign is null) { throw new InvalidOperationException($"Campaign {campaignId} does not exist"); } if (!(campaign.CloseAction is null)) { throw new InvalidOperationException($"Campaign {campaignId} is already closed"); } var timeSince = DateTime.UtcNow - campaign.CreateAction.Created; if (timeSince < PromotionCampaignEntityExtensions.CampaignAcceptCooldown && !force) { throw new InvalidOperationException($"Campaign {campaignId} cannot be accepted until {PromotionCampaignEntityExtensions.CampaignAcceptCooldown.TotalHours} hours after its creation ({(PromotionCampaignEntityExtensions.CampaignAcceptCooldown - timeSince).Humanize(4)} remain)"); } try { var subject = await UserService.GetGuildUserAsync(campaign.GuildId, campaign.Subject.Id); if (subject.RoleIds.Contains(campaign.TargetRole.Id)) { throw new InvalidOperationException($"User {campaign.Subject.GetFullUsername()} is already a member of role {campaign.TargetRole.Name}"); } var guild = await DiscordClient.GetGuildAsync(campaign.GuildId); var targetRole = guild.GetRole(campaign.TargetRole.Id); if (targetRole is null) { throw new InvalidOperationException($"Role {campaign.TargetRole.Name} no longer exists"); } await subject.AddRoleAsync(targetRole); foreach (var lowerRankRole in (await GetRankRolesAsync(AuthorizationService.CurrentGuildId.Value)) .TakeWhile(x => x.Id != targetRole.Id)) { var lowerRole = guild.GetRole(lowerRankRole.Id); if (!(lowerRole is null) && subject.RoleIds.Contains(lowerRole.Id)) { await subject.RemoveRoleAsync(lowerRole); } } resultAction = await PromotionCampaignRepository.TryCloseAsync(campaignId, AuthorizationService.CurrentUserId.Value, PromotionCampaignOutcome.Accepted); } catch { resultAction = await PromotionCampaignRepository.TryCloseAsync(campaignId, AuthorizationService.CurrentUserId.Value, PromotionCampaignOutcome.Failed); PublishActionNotificationAsync(resultAction); throw; } finally { transaction.Commit(); } } PublishActionNotificationAsync(resultAction); }
/// <inheritdoc /> public async Task <PromotionCampaignDetails> GetCampaignDetailsAsync(long campaignId) => await PromotionCampaignRepository.ReadDetailsAsync(campaignId);