pronounss/backend/db/audit_log.go

73 lines
2.2 KiB
Go

package db
import (
"context"
"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
}
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("*").From("audit_log").Limit(100).OrderBy("id DESC")
if before.IsValid() {
b = b.Where("id < ?", before)
}
sql, args, err := b.ToSql()
if err != nil {
return nil, errors.Wrap(err, "building query")
}
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
}