profile.go94 lines, 2.9 KB
| 1 | package users |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/fiston-user/murmur/internal/platform/httpx" |
| 9 | "github.com/fiston-user/murmur/internal/platform/replica" |
| 10 | "github.com/jackc/pgx/v5" |
| 11 | "github.com/jackc/pgx/v5/pgxpool" |
| 12 | ) |
| 13 | |
| 14 | // Profiles answers what a client asks about accounts. Most of it may be |
| 15 | // a moment old, so it goes wherever reads.For sends it. |
| 16 | type Profiles struct { |
| 17 | db *pgxpool.Pool |
| 18 | reads *replica.Reads |
| 19 | } |
| 20 | |
| 21 | func NewProfiles(db *pgxpool.Pool, reads *replica.Reads) *Profiles { |
| 22 | return &Profiles{db: db, reads: reads} |
| 23 | } |
| 24 | |
| 25 | // Me says who the token belongs to. It asks the primary, like Auth does. |
| 26 | func (p *Profiles) Me(w http.ResponseWriter, r *http.Request) { |
| 27 | id := ID(r.Context()) |
| 28 | var handle string |
| 29 | err := p.db.QueryRow(r.Context(), `select handle from users where id = $1`, id).Scan(&handle) |
| 30 | if err != nil { |
| 31 | httpx.ServerError(w, r, err) |
| 32 | return |
| 33 | } |
| 34 | httpx.JSON(w, http.StatusOK, map[string]any{"id": id, "handle": handle}) |
| 35 | } |
| 36 | |
| 37 | // Profile is one account, the way its page shows it. |
| 38 | type Profile struct { |
| 39 | Handle string `json:"handle"` |
| 40 | Joined time.Time `json:"joined"` |
| 41 | Followers int `json:"followers"` |
| 42 | Following int `json:"following"` |
| 43 | Posts int `json:"posts"` |
| 44 | FollowedByMe bool `json:"followed_by_me"` |
| 45 | } |
| 46 | |
| 47 | // Show returns the profile of the user in the path, to anybody. |
| 48 | func (p *Profiles) Show(w http.ResponseWriter, r *http.Request) { |
| 49 | me := ID(r.Context()) |
| 50 | rows, _ := p.reads.For(r.Context(), me).Query(r.Context(), ` |
| 51 | select u.handle, u.created_at, u.followers_count, |
| 52 | (select count(*) from follows where follower_id = u.id), |
| 53 | (select count(*) from posts where user_id = u.id), |
| 54 | exists (select 1 from follows where follower_id = $2 and followee_id = u.id) |
| 55 | from users u |
| 56 | where u.handle = $1`, r.PathValue("handle"), me) |
| 57 | profile, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByPos[Profile]) |
| 58 | if errors.Is(err, pgx.ErrNoRows) { |
| 59 | httpx.Error(w, http.StatusNotFound, "no such user") |
| 60 | return |
| 61 | } |
| 62 | if err != nil { |
| 63 | httpx.ServerError(w, r, err) |
| 64 | return |
| 65 | } |
| 66 | httpx.JSON(w, http.StatusOK, profile) |
| 67 | } |
| 68 | |
| 69 | // Suggestion is an account worth following. |
| 70 | type Suggestion struct { |
| 71 | Handle string `json:"handle"` |
| 72 | Followers int `json:"followers"` |
| 73 | } |
| 74 | |
| 75 | // Suggest returns three of the most followed accounts that the caller |
| 76 | // doesn't follow yet, picked from the top fifty. |
| 77 | func (p *Profiles) Suggest(w http.ResponseWriter, r *http.Request) { |
| 78 | me := ID(r.Context()) |
| 79 | rows, _ := p.reads.For(r.Context(), me).Query(r.Context(), ` |
| 80 | select handle, followers_count |
| 81 | from (select id, handle, followers_count from users |
| 82 | order by followers_count desc limit 50) top |
| 83 | where id <> $1 |
| 84 | and not exists ( |
| 85 | select 1 from follows where follower_id = $1 and followee_id = top.id) |
| 86 | order by followers_count desc |
| 87 | limit 3`, me) |
| 88 | list, err := pgx.CollectRows(rows, pgx.RowToStructByPos[Suggestion]) |
| 89 | if err != nil { |
| 90 | httpx.ServerError(w, r, err) |
| 91 | return |
| 92 | } |
| 93 | httpx.JSON(w, http.StatusOK, map[string]any{"users": list}) |
| 94 | } |