public async Task <ISpamOperatorResult <Reply> > UpdateModelAsync(ISpamOperatorContext <Reply> context) { // Perform validation var validation = await ValidateModelAsync(context); // Create result var result = new SpamOperatorResult <Reply>(); // Not an operator of interest if (validation == null) { return(result.Success(context.Model)); } // If validation succeeded no need to perform further actions if (validation.Succeeded) { return(result.Success(context.Model)); } // Get reply author var user = await BuildUserAsync(context.Model); if (user == null) { return(null); } // Flag user as SPAM? if (context.Operation.FlagAsSpam) { var bot = await _platoUserStore.GetPlatoBotAsync(); // Mark user as SPAM if (!user.IsSpam) { user.IsSpam = true; user.IsSpamUpdatedUserId = bot?.Id ?? 0; user.IsSpamUpdatedDate = DateTimeOffset.UtcNow; await _platoUserStore.UpdateAsync(user); } // Mark reply as SPAM if (!context.Model.IsSpam) { context.Model.IsSpam = true; await _replyStore.UpdateAsync(context.Model); } } // Defer notifications for execution after request completes _deferredTaskManager.AddTask(async ctx => { await NotifyAsync(context); }); // Return failed with our updated model and operation // This provides the calling code with the operation error message return(result.Failed(context.Model, context.Operation)); }
public Task ReportAsync(ReportSubmission <Reply> submission) { // Defer notifications for execution after request completes _deferredTaskManager.AddTask(async ctx => { // Get users to notify var users = await _platoUserStore.QueryAsync() .Select <UserQueryParams>(q => { q.RoleName.IsIn(new[] { DefaultRoles.Administrator, DefaultRoles.Staff }); }) .ToList(); // No users to notify if (users?.Data == null) { return; } // If anonymous use bot as sender var from = submission.Who ?? await _platoUserStore.GetPlatoBotAsync(); // Send notifications foreach (var user in users.Data) { // Web notification if (user.NotificationEnabled(_userNotificationTypeDefaults, WebNotifications.ReplyReport)) { await _notificationManager.SendAsync(new Notification(WebNotifications.ReplyReport) { To = user, From = from }, submission); } // Email notification if (user.NotificationEnabled(_userNotificationTypeDefaults, EmailNotifications.ReplyReport)) { await _notificationManager.SendAsync(new Notification(EmailNotifications.ReplyReport) { To = user }, submission); } } }); return(Task.CompletedTask); }
public async Task ExecuteAsync(object sender, SafeTimerEventArgs args) { var feature = await _featureFacade.GetFeatureByIdAsync("Plato.Questions"); if (feature == null) { return; } var bot = await _userStore.GetPlatoBotAsync(); foreach (var badge in this.Badges) { // Replacements for SQL script var replacements = new Dictionary <string, string>() { ["{name}"] = badge.Name, ["{threshold}"] = badge.Threshold.ToString(), ["{featureId}"] = feature.Id.ToString() }; var userIds = await _dbHelper.ExecuteReaderAsync <IList <int> >(Sql, replacements, async reader => { var users = new List <int>(); while (await reader.ReadAsync()) { if (reader.ColumnIsNotNull("UserId")) { users.Add(Convert.ToInt32(reader["UserId"])); } } return(users); }); if (userIds?.Count > 0) { // Get all users awarded the badge var users = await _userStore.QueryAsync() .Take(1, userIds.Count) .Select <UserQueryParams>(q => { q.Id.IsIn(userIds.ToArray()); }) .OrderBy("LastLoginDate", OrderBy.Desc) .ToList(); // Send notifications if (users != null) { foreach (var user in users.Data) { // --------------- // Award reputation for new badges // --------------- var badgeReputation = badge.GetReputation(); if (badgeReputation.Points != 0) { await _userReputationAwarder.AwardAsync(badgeReputation, user.Id, $"{badge.Name} badge awarded"); } // --------------- // Trigger notifications // --------------- // Email notification if (user.NotificationEnabled(_userNotificationTypeDefaults, EmailNotifications.NewBadge)) { await _notificationManager.SendAsync(new Notification(EmailNotifications.NewBadge) { To = user, From = bot }, badge); } // Web notification if (user.NotificationEnabled(_userNotificationTypeDefaults, WebNotifications.NewBadge)) { await _notificationManager.SendAsync(new Notification(WebNotifications.NewBadge) { To = user, From = bot }, badge); } } } _cacheManager.CancelTokens(typeof(UserBadgeStore)); } } }