feat: return usernames in GET /v2/admin/audit-log

This commit is contained in:
sam 2023-12-28 16:49:48 +01:00
parent 73d132eb4c
commit dcc0c24f82
No known key found for this signature in database
GPG key ID: B4EF20DDE721CAA1
2 changed files with 27 additions and 9 deletions

View file

@ -2,6 +2,7 @@ package db
import (
"context"
"fmt"
"codeberg.org/pronounscc/pronouns.cc/backend/common"
"emperror.dev/errors"
@ -19,6 +20,10 @@ type AuditLogEntry struct {
Reason string
ActionTaken string
ClearedData *AuditLogClearedData
TargetUsername *string
TargetMemberName *string
ModeratorUsername *string
}
type AuditLogClearedData struct {
@ -34,7 +39,11 @@ type AuditLogClearedData struct {
// Returns a max of 100 audit log entries created before the time in `before`.
// If `before` is 0, returns the latest entries.
func (db *DB) AuditLog(ctx context.Context, before common.AuditLogID) (es []AuditLogEntry, err error) {
b := sq.Select("*").From("audit_log").Limit(100).OrderBy("id DESC")
b := sq.Select("a.*", "u1.username as target_username", "u2.username as moderator_username", "m.name as target_member_name").
From("audit_log a").Limit(100).OrderBy("id DESC").
LeftJoin("users u1 ON a.target_user_id = u1.snowflake_id").
LeftJoin("users u2 ON a.moderator_id = u2.snowflake_id").
LeftJoin("members m ON a.target_member_id = m.snowflake_id")
if before.IsValid() {
b = b.Where("id < ?", before)
}
@ -43,6 +52,8 @@ func (db *DB) AuditLog(ctx context.Context, before common.AuditLogID) (es []Audi
return nil, errors.Wrap(err, "building query")
}
fmt.Println(sql)
err = pgxscan.Select(ctx, db, &es, sql, args...)
if err != nil {
return nil, errors.Wrap(err, "executing query")

View file

@ -21,18 +21,25 @@ type AuditLogEntryResponse struct {
Reason string `json:"reason"`
ActionTaken string `json:"action_taken"`
ClearedData *db.AuditLogClearedData `json:"cleared_data"`
TargetUsername *string `json:"target_name"`
TargetMemberName *string `json:"target_member_name"`
ModeratorUsername *string `json:"moderator_name"`
}
func dbAuditLogToResponse(e db.AuditLogEntry) AuditLogEntryResponse {
return AuditLogEntryResponse{
ID: e.ID,
TargetUserID: e.TargetUserID,
TargetMemberID: e.TargetMemberID,
ModeratorID: e.ModeratorID,
ReportID: e.ReportID,
Reason: e.Reason,
ActionTaken: e.ActionTaken,
ClearedData: e.ClearedData,
ID: e.ID,
TargetUserID: e.TargetUserID,
TargetMemberID: e.TargetMemberID,
ModeratorID: e.ModeratorID,
ReportID: e.ReportID,
Reason: e.Reason,
ActionTaken: e.ActionTaken,
ClearedData: e.ClearedData,
TargetUsername: e.TargetUsername,
TargetMemberName: e.TargetMemberName,
ModeratorUsername: e.ModeratorUsername,
}
}