likes.go125 lines, 3.4 KB
| 1 | // Package likes is liking a post, and counting the likes. |
| 2 | package likes |
| 3 | |
| 4 | import ( |
| 5 | "context" |
| 6 | "errors" |
| 7 | "log/slog" |
| 8 | "net/http" |
| 9 | "strconv" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/fiston-user/murmur/internal/jobs" |
| 13 | "github.com/fiston-user/murmur/internal/notify" |
| 14 | "github.com/fiston-user/murmur/internal/platform/httpx" |
| 15 | "github.com/fiston-user/murmur/internal/users" |
| 16 | "github.com/jackc/pgx/v5" |
| 17 | "github.com/jackc/pgx/v5/pgconn" |
| 18 | "github.com/jackc/pgx/v5/pgxpool" |
| 19 | ) |
| 20 | |
| 21 | type Handler struct { |
| 22 | db *pgxpool.Pool |
| 23 | } |
| 24 | |
| 25 | func New(db *pgxpool.Pool) *Handler { |
| 26 | return &Handler{db: db} |
| 27 | } |
| 28 | |
| 29 | // postID reads the post's id from the path, or answers 404. |
| 30 | func postID(w http.ResponseWriter, r *http.Request) (int64, bool) { |
| 31 | id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) |
| 32 | if err != nil { |
| 33 | httpx.Error(w, http.StatusNotFound, "no such post") |
| 34 | return 0, false |
| 35 | } |
| 36 | return id, true |
| 37 | } |
| 38 | |
| 39 | // Like makes the caller like the post in the path. Doing it twice is fine. |
| 40 | func (h *Handler) Like(w http.ResponseWriter, r *http.Request) { |
| 41 | post, ok := postID(w, r) |
| 42 | if !ok { |
| 43 | return |
| 44 | } |
| 45 | user := users.ID(r.Context()) |
| 46 | err := pgx.BeginFunc(r.Context(), h.db, func(tx pgx.Tx) error { |
| 47 | tag, err := tx.Exec(r.Context(), |
| 48 | `insert into likes (post_id, user_id) values ($1, $2) on conflict do nothing`, post, user) |
| 49 | if err != nil || tag.RowsAffected() == 0 { |
| 50 | return err |
| 51 | } |
| 52 | args := notify.LikeArgs{Liker: user, Post: post} |
| 53 | if err := jobs.Enqueue(r.Context(), tx, notify.Like, args); err != nil { |
| 54 | return err |
| 55 | } |
| 56 | _, err = tx.Exec(r.Context(), |
| 57 | `insert into like_deltas (post_id, delta) values ($1, 1)`, post) |
| 58 | return err |
| 59 | }) |
| 60 | var pgErr *pgconn.PgError |
| 61 | if errors.As(err, &pgErr) && pgErr.Code == "23503" { // foreign_key_violation |
| 62 | httpx.Error(w, http.StatusNotFound, "no such post") |
| 63 | return |
| 64 | } |
| 65 | if err != nil { |
| 66 | httpx.ServerError(w, r, err) |
| 67 | return |
| 68 | } |
| 69 | w.WriteHeader(http.StatusNoContent) |
| 70 | } |
| 71 | |
| 72 | // Unlike takes the caller's like back. Without a like, nothing happens. |
| 73 | func (h *Handler) Unlike(w http.ResponseWriter, r *http.Request) { |
| 74 | post, ok := postID(w, r) |
| 75 | if !ok { |
| 76 | return |
| 77 | } |
| 78 | user := users.ID(r.Context()) |
| 79 | err := pgx.BeginFunc(r.Context(), h.db, func(tx pgx.Tx) error { |
| 80 | tag, err := tx.Exec(r.Context(), |
| 81 | `delete from likes where post_id = $1 and user_id = $2`, post, user) |
| 82 | if err != nil || tag.RowsAffected() == 0 { |
| 83 | return err |
| 84 | } |
| 85 | _, err = tx.Exec(r.Context(), |
| 86 | `insert into like_deltas (post_id, delta) values ($1, -1)`, post) |
| 87 | return err |
| 88 | }) |
| 89 | if err != nil { |
| 90 | httpx.ServerError(w, r, err) |
| 91 | return |
| 92 | } |
| 93 | w.WriteHeader(http.StatusNoContent) |
| 94 | } |
| 95 | |
| 96 | // Fold adds every delta that waits to its post's count, and deletes it, in one statement. |
| 97 | // So a delta is counted once, or it's still waiting. It returns how many posts changed. |
| 98 | func Fold(ctx context.Context, db *pgxpool.Pool) (int64, error) { |
| 99 | tag, err := db.Exec(ctx, ` |
| 100 | with gone as ( |
| 101 | delete from like_deltas returning post_id, delta |
| 102 | ), sums as ( |
| 103 | select post_id, sum(delta) as n from gone group by post_id |
| 104 | ) |
| 105 | update posts p set likes_count = p.likes_count + s.n |
| 106 | from sums s |
| 107 | where p.id = s.post_id`) |
| 108 | return tag.RowsAffected(), err |
| 109 | } |
| 110 | |
| 111 | // FoldEvery folds again and again, until ctx is cancelled. |
| 112 | func FoldEvery(ctx context.Context, db *pgxpool.Pool, every time.Duration) { |
| 113 | tick := time.NewTicker(every) |
| 114 | defer tick.Stop() |
| 115 | for { |
| 116 | select { |
| 117 | case <-ctx.Done(): |
| 118 | return |
| 119 | case <-tick.C: |
| 120 | if _, err := Fold(ctx, db); err != nil { |
| 121 | slog.Error("likes were not folded", "error", err) |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |