package db import ( "context" "fmt" "codeberg.org/pronounscc/pronouns.cc/backend/common" "emperror.dev/errors" "github.com/georgysavva/scany/v2/pgxscan" "github.com/jackc/pgx/v5" ) type AuditLogEntry struct { ID common.AuditLogID TargetUserID common.UserID TargetMemberID *common.MemberID ModeratorID common.UserID ReportID *int64 Reason string ActionTaken string ClearedData *AuditLogClearedData TargetUsername *string TargetMemberName *string ModeratorUsername *string } type AuditLogClearedData struct { DisplayName *string `json:"display_name,omitempty"` Bio *string `json:"bio,omitempty"` Links []string `json:"links,omitempty"` Names []FieldEntry `json:"names,omitempty"` Pronouns []PronounEntry `json:"pronouns,omitempty"` Fields []Field `json:"fields,omitempty"` CustomPreferences []CustomPreference `json:"custom_preferences"` } // 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("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) } sql, args, err := b.ToSql() if err != nil { 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") } return NotNull(es), nil } func (db *DB) CreateAuditLogEntry(ctx context.Context, tx pgx.Tx, data AuditLogEntry) (e AuditLogEntry, err error) { sql, args, err := sq.Insert("audit_log").SetMap(map[string]any{ "id": common.GenerateID(), "target_user_id": data.TargetUserID, "target_member_id": data.TargetMemberID, "moderator_id": data.ModeratorID, "report_id": data.ReportID, "reason": data.Reason, "action_taken": data.ActionTaken, "cleared_data": data.ClearedData, }).Suffix("RETURNING *").ToSql() if err != nil { return e, errors.Wrap(err, "building query") } err = pgxscan.Get(ctx, tx, &e, sql, args...) if err != nil { return e, errors.Wrap(err, "executing query") } return e, nil }