Skip to content

Commit

Permalink
#527: code for handling spam reports
Browse files Browse the repository at this point in the history
  • Loading branch information
riipah committed Nov 20, 2019
1 parent 22a1bde commit a6c231e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
48 changes: 45 additions & 3 deletions VocaDbModel/Database/Queries/UserQueries.cs
Expand Up @@ -115,9 +115,10 @@ class CachedUserStats {

}

private void CreateReport(IDatabaseContext ctx, User reportedUser, string hostname, string notes) {
var report = new UserReport(reportedUser, UserReportType.Other, ctx.OfType<User>().GetLoggedUser(PermissionContext), hostname, notes);
private UserReport CreateReport(IDatabaseContext ctx, User reportedUser, UserReportType reportType, string hostname, string notes) {
var report = new UserReport(reportedUser, reportType, ctx.OfType<User>().GetLoggedUser(PermissionContext), hostname, notes);
ctx.Save(report);
return report;
}

private int[] GetFavoriteTagIds(IDatabaseContext<User> ctx, User user) {
Expand Down Expand Up @@ -516,6 +517,47 @@ class CachedUserStats {

}

public (bool created, int reportId) CreateReport(int userId, UserReportType reportType, string hostname, string notes) {

PermissionContext.VerifyPermission(PermissionToken.ReportUser);

return repository.HandleTransaction(ctx => {
var user = ctx.Load(userId);
ctx.AuditLogger.SysLog($"reporting {user} as {reportType}");
if (user.GroupId >= UserGroupId.Moderator) {
log.Error("Cannot report user with group " + user.GroupId);
return (false, 0);
}
if (user.GroupId <= UserGroupId.Regular && reportType == UserReportType.Spamming) {
var activeReportCount = ctx.Query<UserReport>()
.Where(ur => ur.User.Id == userId && ur.Status == ReportStatus.Open && ur.ReportType == UserReportType.Spamming)
.ToArray()
.Distinct(ur => ur.Hostname)
.Count();
if (activeReportCount >= 10) {
log.Info("User disabled");
user.Active = false;
ctx.Update(user);
} else if (activeReportCount >= 5) {
log.Info("User set to limited");
user.GroupId = UserGroupId.Limited;
ctx.Update(user);
}
}
var report = CreateReport(ctx, user, reportType, hostname, notes);
ctx.AuditLogger.AuditLog($"reported {user} as {reportType}");
return (true, report.Id);
});

}

/// <summary>
/// Disconnects Twitter account for the currently logged in user.
/// Twitter account can NOT be disconnected if the user has not set a VocaDB password.
Expand Down Expand Up @@ -1413,7 +1455,7 @@ class CachedUserStats {
var reasonText = !string.IsNullOrEmpty(reason) ? ": " + reason : string.Empty;
if (createReport) {
CreateReport(session, user, hostname, string.Format("removed edit permissions{0}", reasonText));
CreateReport(session, user, UserReportType.Other, hostname, string.Format("removed edit permissions{0}", reasonText));
}
var message = string.Format("updated user {0} by removing edit permissions{1}", EntryLinkFactory.CreateEntryLink(user), reasonText);
Expand Down
6 changes: 4 additions & 2 deletions VocaDbModel/Domain/Users/UserReport.cs
@@ -1,4 +1,4 @@
namespace VocaDb.Model.Domain.Users {
namespace VocaDb.Model.Domain.Users {

public class UserReport : GenericEntryReport<User, UserReportType> {

Expand All @@ -16,7 +16,9 @@ public enum UserReportType {
/// </summary>
MaliciousIP = 1,

Other = 2
Spamming = 2,

Other = 4

}
}
2 changes: 1 addition & 1 deletion VocaDbWeb/Views/User/Details.cshtml
Expand Up @@ -70,7 +70,7 @@
@Html.Raw("&nbsp;")
@Html.ActionLink(ViewRes.SharedStrings.Edit, "Edit", new { id = Model.Id }, new { id = "editUserLink" });
}
@if (Login.Manager.HasPermission(PermissionToken.ReportUser) && UserContext.LoggedUserId != Model.Id && Model.GroupId <= UserGroupId.Regular && Model.Active) {
@if (Login.Manager.HasPermission(PermissionToken.ReportUser) && UserContext.LoggedUserId != Model.Id && Model.GroupId <= UserGroupId.Trusted && Model.Active) {
@Html.Raw("&nbsp;")
<a href="" id="reportUser" data-bind="click: function() { reportUserViewModel.show(); }">@Res.ReportSpamming</a>
}
Expand Down

0 comments on commit a6c231e

Please sign in to comment.