tiny 88x31 lexicon for atproto
1package db
2
3import (
4 "context"
5 "errors"
6 "github.com/jackc/pgx/v5"
7 "tangled.org/moth11.net/88x31/types"
8 "time"
9)
10
11func (s *Store) GetBanned(did string, ctx context.Context) (*types.Ban, error) {
12 row := s.pool.QueryRow(ctx, `SELECT
13 id,
14 reason,
15 till,
16 banned_at
17 FROM bans WHERE did = $1 ORDER BY id DESC`, did)
18 var ban types.Ban
19 err := row.Scan(&ban.Id, &ban.Reason, &ban.Till, &ban.BannedAt)
20 if err != nil {
21 return nil, err
22 }
23 ban.Did = did
24 return &ban, nil
25}
26
27func (s *Store) AddBan(did string, reason *string, till *time.Time, ctx context.Context) error {
28 _, err := s.pool.Exec(ctx, `INSERT INTO bans (
29 did,
30 reason,
31 till
32 ) VALUES (
33 $1, $2, $3
34 )
35 `, did, reason, till)
36 return err
37}
38
39func (s *Store) IsBanned(did string, ctx context.Context) (bool, error) {
40 ban, err := s.GetBanned(did, ctx)
41 if ban != nil {
42 defbanned := false
43 if ban.Till == nil {
44 defbanned = true
45 } else {
46 defbanned = time.Now().Before(*ban.Till)
47 }
48 if defbanned {
49 return true, nil
50 }
51 }
52 if err != nil && !errors.Is(err, pgx.ErrNoRows) {
53 return false, err
54 }
55 return false, nil
56}