Passport/pkg/internal/services/reports.go

75 lines
1.8 KiB
Go
Raw Normal View History

2024-09-19 13:02:21 +00:00
package services
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
)
func ListAbuseReport(account models.Account) ([]models.AbuseReport, error) {
2024-09-19 13:02:21 +00:00
var reports []models.AbuseReport
err := database.C.
Where("account_id = ?", account.ID).
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
}
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),
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
}
func NewAbuseReport(resource string, reason string, account models.Account) (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,
account.ID,
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,
AccountID: account.ID,
2024-09-19 13:02:21 +00:00
}
err := database.C.Create(&report).Error
return report, err
}