package db import ( "context" "errors" "github.com/jackc/pgx/v5" "tangled.org/moth11.net/88x31/types" "time" ) func (s *Store) GetBanned(did string, ctx context.Context) (*types.Ban, error) { row := s.pool.QueryRow(ctx, `SELECT id, reason, till, banned_at FROM bans WHERE did = $1 ORDER BY id DESC`, did) var ban types.Ban err := row.Scan(&ban.Id, &ban.Reason, &ban.Till, &ban.BannedAt) if err != nil { return nil, err } ban.Did = did return &ban, nil } func (s *Store) AddBan(did string, reason *string, till *time.Time, ctx context.Context) error { _, err := s.pool.Exec(ctx, `INSERT INTO bans ( did, reason, till ) VALUES ( $1, $2, $3 ) `, did, reason, till) return err } func (s *Store) IsBanned(did string, ctx context.Context) (bool, error) { ban, err := s.GetBanned(did, ctx) if ban != nil { defbanned := false if ban.Till == nil { defbanned = true } else { defbanned = time.Now().Before(*ban.Till) } if defbanned { return true, nil } } if err != nil && !errors.Is(err, pgx.ErrNoRows) { return false, err } return false, nil }