|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +// BLENDER: spam reporting |
| 5 | + |
| 6 | +package user |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + |
| 12 | + "code.gitea.io/gitea/models/db" |
| 13 | + "code.gitea.io/gitea/models/organization" |
| 14 | + issues_model "code.gitea.io/gitea/models/issues" |
| 15 | + project_model "code.gitea.io/gitea/models/project" |
| 16 | + user_model "code.gitea.io/gitea/models/user" |
| 17 | + "code.gitea.io/gitea/modules/log" |
| 18 | + "code.gitea.io/gitea/modules/optional" |
| 19 | + "code.gitea.io/gitea/modules/structs" |
| 20 | + repo_service "code.gitea.io/gitea/services/repository" |
| 21 | +) |
| 22 | + |
| 23 | +// CanReportSpam tells if a doer is allowed to report spam. |
| 24 | +func CanReportSpam(ctx context.Context, doer *user_model.User) (bool, error) { |
| 25 | + count, err := organization.GetOrganizationCount(ctx, doer) |
| 26 | + if err != nil { |
| 27 | + return false, fmt.Errorf("GetOrganizationCount: %w", err) |
| 28 | + } |
| 29 | + return count > 0, nil |
| 30 | +} |
| 31 | + |
| 32 | +// CreateSpamReport inserts a new record in default status=Pending |
| 33 | +// for further processing, either manual or automatical. |
| 34 | +func CreateSpamReport(ctx context.Context, reporter, user *user_model.User) error { |
| 35 | + return db.Insert(ctx, &user_model.SpamReport{ |
| 36 | + ReporterID: reporter.ID, |
| 37 | + UserID: user.ID, |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +// ProcessSpamReports updates only reports in status "Pending" to avoid race conditions. |
| 42 | +func ProcessSpamReports(ctx context.Context, spamReportIDs []int64) error { |
| 43 | + e := db.GetEngine(ctx) |
| 44 | + var spamReports []user_model.SpamReport |
| 45 | + err := e.In("id", spamReportIDs).Find(&spamReports) |
| 46 | + if err != nil { |
| 47 | + return fmt.Errorf("failed to fetch SpamReports: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + for _, spamReport := range spamReports { |
| 51 | + id := spamReport.ID |
| 52 | + count, err := e.ID(id).And("status = ?", user_model.SpamReportStatusTypePending). |
| 53 | + Update(&user_model.SpamReport{Status: user_model.SpamReportStatusTypeProcessing}) |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("failed to set SpamReport.Status to Processing for id=%d: %w", id, err) |
| 56 | + } |
| 57 | + if count < 1 { |
| 58 | + log.Info("Skipping SpamReport id=%d, status wasn't Pending", id) |
| 59 | + continue |
| 60 | + } |
| 61 | + |
| 62 | + userID := spamReport.UserID |
| 63 | + user := &user_model.User{ID: userID} |
| 64 | + has, err := e.Get(user) |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("failed to fetch user userID=%d: %w", userID, err) |
| 67 | + } |
| 68 | + if !has { |
| 69 | + return fmt.Errorf("user id=%d was not found", userID) |
| 70 | + } |
| 71 | + |
| 72 | + // Clean up everything and update report status if there were no errors. |
| 73 | + // On failure the report will be processed partially and it will be stuck in Processing status. |
| 74 | + // |
| 75 | + // Not wrapping into a transaction due to a deadlock happening on sqlite |
| 76 | + // when querying for comments after they were deleted, may need more investigation. |
| 77 | + log.Info("Processing SpamReport id=%d for user %s", id, user.Name) |
| 78 | + |
| 79 | + // UpdateUser and UpdateAuth to clean the profile and prohibit logins. |
| 80 | + if err := UpdateUser(ctx, user, |
| 81 | + &UpdateOptions{ |
| 82 | + Description: optional.Some(""), |
| 83 | + FullName: optional.Some("Confirmed Spammer"), |
| 84 | + IsActive: optional.Some(false), |
| 85 | + IsRestricted: optional.Some(true), |
| 86 | + Location: optional.Some(""), |
| 87 | + MaxRepoCreation: optional.Some(0), |
| 88 | + Visibility: optional.Some(structs.VisibleTypeLimited), |
| 89 | + Website: optional.Some(""), |
| 90 | + }, |
| 91 | + ); err != nil { |
| 92 | + return fmt.Errorf("failed to UpdateUser: %w", err) |
| 93 | + } |
| 94 | + if err := UpdateAuth(ctx, user, &UpdateAuthOptions{ProhibitLogin: optional.Some(true)}); err != nil { |
| 95 | + return fmt.Errorf("failed to UpdateAuth: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + // Clean up all comments. |
| 99 | + log.Info("Cleaning up comments by user %s", user.Name) |
| 100 | + const batchSize = 50 |
| 101 | + for { |
| 102 | + log.Info("started loop") |
| 103 | + comments := make([]*issues_model.Comment, 0, batchSize) |
| 104 | + if err := e.Where("type=? AND poster_id=?", issues_model.CommentTypeComment, userID).Limit(batchSize, 0).Find(&comments); err != nil { |
| 105 | + return fmt.Errorf("failed to find comments to delete: %w", err) |
| 106 | + } |
| 107 | + log.Info("found %d comments", len(comments)) |
| 108 | + if len(comments) == 0 { |
| 109 | + break |
| 110 | + } |
| 111 | + |
| 112 | + for _, comment := range comments { |
| 113 | + log.Info("deleting comment %+v", *comment) |
| 114 | + if err := issues_model.DeleteComment(ctx, comment); err != nil { |
| 115 | + return fmt.Errorf("failed to delete comments: %w", err) |
| 116 | + } |
| 117 | + } |
| 118 | + log.Info("finished loop") |
| 119 | + e.Context(ctx).Close() |
| 120 | + } |
| 121 | + |
| 122 | + // Clean up all personal repos. |
| 123 | + log.Info("Cleaning up personal repositories of user %s", user.Name) |
| 124 | + if err := repo_service.DeleteOwnerRepositoriesDirectly(ctx, user); err != nil { |
| 125 | + return fmt.Errorf("failed to clean up repositories: %w", err) |
| 126 | + } |
| 127 | + |
| 128 | + // Clean up all personal projects. |
| 129 | + log.Info("Cleaning up personal projects of user %s", user.Name) |
| 130 | + projectIDs, err := project_model.GetAllProjectsIDsByOwnerIDAndType(ctx, user.ID, project_model.TypeIndividual) |
| 131 | + if err != nil { |
| 132 | + return fmt.Errorf("failed to fetch personal project ids: %w", err) |
| 133 | + } |
| 134 | + for _, projectID := range projectIDs { |
| 135 | + if err := project_model.DeleteProjectByID(ctx, projectID); err != nil { |
| 136 | + return fmt.Errorf("failed to clean up personal project id=%d: %w", projectID, err) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // Everything is cleaned up, marking the spam report as Processed. |
| 141 | + count, err = e.ID(id).And("status = ?", user_model.SpamReportStatusTypeProcessing). |
| 142 | + Update(&user_model.SpamReport{Status: user_model.SpamReportStatusTypeProcessed}) |
| 143 | + if err != nil { |
| 144 | + return fmt.Errorf("failed to set SpamReport.Status to Processed for id=%d: %w", id, err) |
| 145 | + } |
| 146 | + if count < 1 { |
| 147 | + return fmt.Errorf("SpamReport id=%d status wasn't Processing, rolling back the transaction", id) |
| 148 | + } |
| 149 | + log.Info("Processed SpamReport id=%d for user %s", id, user.Name) |
| 150 | + } |
| 151 | + return nil |
| 152 | +} |
| 153 | + |
| 154 | +// DismissSpamReports updates only reports in status "Pending" to avoid race conditions |
| 155 | +// with the actual processing. |
| 156 | +func DismissSpamReports(ctx context.Context, spamReportIDs []int64) error { |
| 157 | + _, err := db.GetEngine(ctx).In("id", spamReportIDs). |
| 158 | + And("status = ?", user_model.SpamReportStatusTypePending). |
| 159 | + Update(&user_model.SpamReport{Status: user_model.SpamReportStatusTypeDismissed}) |
| 160 | + return err |
| 161 | +} |
0 commit comments