// Package audit is the record of who did what: sign-ins, invitations and // roles, connections, everything sent or published, and deletions. Admins // read it under Settings → Activity. package audit import ( "strings" "time" "supermarket/internal/db" ) // Entry is one thing someone did. type Entry struct { ID int64 At time.Time UserID int64 // 0: Supermarket itself, or the local edition's owner UserName string // as they were called then Kind string // person, agent or system Action string Target string Detail string IP string } // Kinds of actor. const ( Person = "person" Agent = "agent" System = "system" ) const timeFormat = "2006-01-02T15:04:05.000Z" // Record adds an entry. Long fields are cut short. func Record(d *db.DB, e Entry) error { if e.At.IsZero() { e.At = time.Now() } if e.Kind == "" { e.Kind = Person } _, err := d.Exec(`INSERT INTO audit_log (at, user_id, user_name, kind, action, target, detail, ip) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, e.At.UTC().Format(timeFormat), e.UserID, cut(e.UserName, 200), e.Kind, cut(e.Action, 100), cut(e.Target, 500), cut(e.Detail, 2000), cut(e.IP, 64)) return err } func cut(s string, n int) string { if r := []rune(s); len(r) > n { return string(r[:n-1]) + "…" } return s } // Filter narrows the list. type Filter struct { UserID int64 // 0: everyone Action string // a prefix: "signin" matches signin and signin-failed Text string // in the target or detail Before int64 // entries older than this ID (paging); 0: the newest } // List is the newest entries first. func List(d *db.DB, f Filter, limit int) ([]Entry, error) { q := `SELECT id, at, user_id, user_name, kind, action, target, detail, ip FROM audit_log WHERE 1 = 1` var args []any if f.UserID != 0 { q += ` AND user_id = ?` args = append(args, f.UserID) } if f.Action != "" { q += ` AND action LIKE ?` args = append(args, escapeLike(f.Action)+"%") } if t := strings.TrimSpace(f.Text); t != "" { q += ` AND (target LIKE ? OR detail LIKE ? OR user_name LIKE ?)` like := "%" + escapeLike(t) + "%" args = append(args, like, like, like) } if f.Before != 0 { q += ` AND id < ?` args = append(args, f.Before) } q += ` ORDER BY id DESC LIMIT ?` args = append(args, limit) rows, err := d.Query(q, args...) if err != nil { return nil, err } defer rows.Close() var out []Entry for rows.Next() { var e Entry var at string if err := rows.Scan(&e.ID, &at, &e.UserID, &e.UserName, &e.Kind, &e.Action, &e.Target, &e.Detail, &e.IP); err != nil { return nil, err } e.At, _ = time.Parse(timeFormat, at) out = append(out, e) } return out, rows.Err() } // escapeLike makes % and _ match themselves (with ESCAPE-less LIKE, by // dropping them: a search for them finds nothing special). func escapeLike(s string) string { return strings.NewReplacer("%", "", "_", "").Replace(s) } // Prune deletes entries older than keep. func Prune(d *db.DB, keep time.Duration, now time.Time) (int64, error) { res, err := d.Exec(`DELETE FROM audit_log WHERE at < ?`, now.Add(-keep).UTC().Format(timeFormat)) if err != nil { return 0, err } return res.RowsAffected() }