From dcc0c24f82cee0b9f28e812567d671468d45e4c2 Mon Sep 17 00:00:00 2001 From: sam Date: Thu, 28 Dec 2023 16:49:48 +0100 Subject: [PATCH] feat: return usernames in GET /v2/admin/audit-log --- backend/db/audit_log.go | 13 ++++++++++++- backend/routes/v2/admin/audit_log.go | 23 +++++++++++++++-------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/backend/db/audit_log.go b/backend/db/audit_log.go index a5806dc..7c8d731 100644 --- a/backend/db/audit_log.go +++ b/backend/db/audit_log.go @@ -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") diff --git a/backend/routes/v2/admin/audit_log.go b/backend/routes/v2/admin/audit_log.go index 40ccfbc..c6d501c 100644 --- a/backend/routes/v2/admin/audit_log.go +++ b/backend/routes/v2/admin/audit_log.go @@ -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, } }