2024-09-19 13:02:21 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
|
|
|
)
|
|
|
|
|
2024-10-30 15:26:13 +00:00
|
|
|
func ListAbuseReport(account uint) ([]models.AbuseReport, error) {
|
2024-09-19 13:02:21 +00:00
|
|
|
var reports []models.AbuseReport
|
|
|
|
err := database.C.
|
2024-10-30 15:26:13 +00:00
|
|
|
Where("account_id = ?", account).
|
2024-09-19 13:02:21 +00:00
|
|
|
Find(&reports).Error
|
|
|
|
return reports, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAbuseReport(id uint) (models.AbuseReport, error) {
|
|
|
|
var report models.AbuseReport
|
|
|
|
err := database.C.
|
|
|
|
Where("id = ?", id).
|
|
|
|
First(&report).Error
|
|
|
|
return report, err
|
|
|
|
}
|
|
|
|
|
2024-09-19 13:06:35 +00:00
|
|
|
func UpdateAbuseReportStatus(id uint, status, message string) error {
|
2024-09-19 13:02:21 +00:00
|
|
|
var report models.AbuseReport
|
|
|
|
err := database.C.
|
|
|
|
Where("id = ?", id).
|
|
|
|
Preload("Account").
|
|
|
|
First(&report).Error
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
report.Status = status
|
|
|
|
account := report.Account
|
|
|
|
|
|
|
|
err = database.C.Save(&report).Error
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-10-26 16:06:23 +00:00
|
|
|
_ = NewNotification(models.Notification{
|
2024-09-19 13:02:21 +00:00
|
|
|
Topic: "reports.feedback",
|
|
|
|
Title: "Abuse report status has been changed.",
|
2024-10-26 16:06:23 +00:00
|
|
|
Subtitle: fmt.Sprintf("The report #%d's status updated", id),
|
2024-09-19 13:06:35 +00:00
|
|
|
Body: fmt.Sprintf("The report created by you with ID #%d's status has been changed to %s. Moderator message: %s", id, status, message),
|
2024-09-19 13:02:21 +00:00
|
|
|
Account: account,
|
|
|
|
AccountID: account.ID,
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-30 15:26:13 +00:00
|
|
|
func NewAbuseReport(resource string, reason string, account uint) (models.AbuseReport, error) {
|
2024-09-19 13:02:21 +00:00
|
|
|
var report models.AbuseReport
|
|
|
|
if err := database.C.
|
|
|
|
Where(
|
|
|
|
"resource = ? AND account_id = ? AND status IN ?",
|
|
|
|
resource,
|
2024-10-30 15:26:13 +00:00
|
|
|
account,
|
2024-09-19 13:02:21 +00:00
|
|
|
[]string{models.ReportStatusPending, models.ReportStatusReviewing},
|
|
|
|
).First(&report).Error; err == nil {
|
|
|
|
return report, fmt.Errorf("you already reported this resource and it still in process")
|
|
|
|
}
|
|
|
|
|
|
|
|
report = models.AbuseReport{
|
|
|
|
Resource: resource,
|
|
|
|
Reason: reason,
|
2024-10-30 15:26:13 +00:00
|
|
|
AccountID: account,
|
2024-09-19 13:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := database.C.Create(&report).Error
|
|
|
|
return report, err
|
|
|
|
}
|