posts.go120 lines, 3.5 KB
| 1 | // Package posts writes and lists posts. |
| 2 | package posts |
| 3 | |
| 4 | import ( |
| 5 | "math" |
| 6 | "net/http" |
| 7 | "strconv" |
| 8 | "time" |
| 9 | "unicode/utf8" |
| 10 | |
| 11 | "github.com/fiston-user/murmur/internal/jobs" |
| 12 | "github.com/fiston-user/murmur/internal/platform/httpx" |
| 13 | "github.com/fiston-user/murmur/internal/platform/replica" |
| 14 | "github.com/fiston-user/murmur/internal/timeline" |
| 15 | "github.com/fiston-user/murmur/internal/users" |
| 16 | "github.com/jackc/pgx/v5" |
| 17 | "github.com/jackc/pgx/v5/pgxpool" |
| 18 | ) |
| 19 | |
| 20 | // Post is one post, as the API returns it. |
| 21 | type Post struct { |
| 22 | ID int64 `json:"id"` |
| 23 | UserID int64 `json:"user_id"` |
| 24 | Body string `json:"body"` |
| 25 | CreatedAt time.Time `json:"created_at"` |
| 26 | } |
| 27 | |
| 28 | type Handler struct { |
| 29 | db *pgxpool.Pool |
| 30 | reads *replica.Reads |
| 31 | } |
| 32 | |
| 33 | func New(db *pgxpool.Pool, reads *replica.Reads) *Handler { |
| 34 | return &Handler{db: db, reads: reads} |
| 35 | } |
| 36 | |
| 37 | // Create saves a post by the user who is calling. |
| 38 | func (h *Handler) Create(w http.ResponseWriter, r *http.Request) { |
| 39 | var in struct { |
| 40 | Body string `json:"body"` |
| 41 | } |
| 42 | err := httpx.Decode(w, r, &in) |
| 43 | if n := utf8.RuneCountInString(in.Body); err != nil || n < 1 || n > 280 { |
| 44 | httpx.Error(w, http.StatusBadRequest, "body must be 1 to 280 characters") |
| 45 | return |
| 46 | } |
| 47 | post := Post{UserID: users.ID(r.Context()), Body: in.Body} |
| 48 | // The post and its job are saved together, or not at all. |
| 49 | err = pgx.BeginFunc(r.Context(), h.db, func(tx pgx.Tx) error { |
| 50 | err := tx.QueryRow(r.Context(), |
| 51 | `insert into posts (user_id, body) values ($1, $2) returning id, created_at`, |
| 52 | post.UserID, post.Body).Scan(&post.ID, &post.CreatedAt) |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | args := timeline.FanOutArgs{Author: post.UserID, Post: post.ID} |
| 57 | return jobs.Enqueue(r.Context(), tx, timeline.FanOut, args) |
| 58 | }) |
| 59 | if err != nil { |
| 60 | httpx.ServerError(w, r, err) |
| 61 | return |
| 62 | } |
| 63 | httpx.JSON(w, http.StatusCreated, post) |
| 64 | } |
| 65 | |
| 66 | // Listed is a post in a list: by whom, how many like it, and if the caller does. |
| 67 | type Listed struct { |
| 68 | Post |
| 69 | Handle string `json:"handle"` |
| 70 | Likes int `json:"likes"` |
| 71 | Liked bool `json:"liked"` |
| 72 | } |
| 73 | |
| 74 | // Page is one page of a user's posts. NextCursor is null on the last page. |
| 75 | type Page struct { |
| 76 | NextCursor *int64 `json:"next_cursor"` |
| 77 | Posts []Listed `json:"posts"` |
| 78 | } |
| 79 | |
| 80 | // List returns a page of one user's posts, newest first: 20, or ?limit= up to |
| 81 | // 100, below ?cursor= (a post id). |
| 82 | func (h *Handler) List(w http.ResponseWriter, r *http.Request) { |
| 83 | q := r.URL.Query() |
| 84 | limit, _ := strconv.Atoi(q.Get("limit")) |
| 85 | if limit < 1 { |
| 86 | limit = 20 |
| 87 | } |
| 88 | limit = min(limit, 100) |
| 89 | cursor := int64(math.MaxInt64) |
| 90 | if s := q.Get("cursor"); s != "" { |
| 91 | id, err := strconv.ParseInt(s, 10, 64) |
| 92 | if err != nil || id < 1 { |
| 93 | httpx.Error(w, http.StatusBadRequest, "cursor must be a post id") |
| 94 | return |
| 95 | } |
| 96 | cursor = id |
| 97 | } |
| 98 | me := users.ID(r.Context()) |
| 99 | // The author's id first, by itself: then Postgres reads their posts in order, |
| 100 | // and stops at the limit. |
| 101 | rows, _ := h.reads.For(r.Context(), me).Query(r.Context(), ` |
| 102 | select p.id, p.user_id, p.body, p.created_at, u.handle, p.likes_count, |
| 103 | exists (select 1 from likes where post_id = p.id and user_id = $4) |
| 104 | from posts p |
| 105 | join users u on u.id = p.user_id |
| 106 | where p.user_id = (select id from users where handle = $1) and p.id < $2 |
| 107 | order by p.id desc |
| 108 | limit $3`, r.PathValue("handle"), cursor, limit+1, me) |
| 109 | posts, err := pgx.CollectRows(rows, pgx.RowToStructByPos[Listed]) |
| 110 | if err != nil { |
| 111 | httpx.ServerError(w, r, err) |
| 112 | return |
| 113 | } |
| 114 | page := Page{Posts: posts} |
| 115 | if len(posts) > limit { |
| 116 | page.Posts = posts[:limit] |
| 117 | page.NextCursor = &posts[limit-1].ID |
| 118 | } |
| 119 | httpx.JSON(w, http.StatusOK, page) |
| 120 | } |