@@ -0,0 +1,119 @@ |
| | 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| | 2 | + |
| | 3 | +package profile |
| | 4 | + |
| | 5 | +import ( |
| | 6 | + "fmt" |
| | 7 | + "net/http" |
| | 8 | + "net/url" |
| | 9 | + "strconv" |
| | 10 | + |
| | 11 | + "github.com/jackc/pgx/v5/pgtype" |
| | 12 | + |
| | 13 | + "github.com/tenseleyFlow/shithub/internal/auth/policy" |
| | 14 | + socialdb "github.com/tenseleyFlow/shithub/internal/social/sqlc" |
| | 15 | + usersdb "github.com/tenseleyFlow/shithub/internal/users/sqlc" |
| | 16 | + "github.com/tenseleyFlow/shithub/internal/web/middleware" |
| | 17 | +) |
| | 18 | + |
| | 19 | +const starsTabPageSize = 30 |
| | 20 | + |
| | 21 | +// serveStarsTab renders the `/{user}?tab=stars` view. |
| | 22 | +// |
| | 23 | +// The query returns every star (including private-repo stars); we |
| | 24 | +// post-filter per-row by `policy.IsVisibleTo` so the viewer only |
| | 25 | +// sees stars on repos they can read. Anonymous viewers see only |
| | 26 | +// public stars; the user themselves sees everything. |
| | 27 | +func (h *Handlers) serveStarsTab(w http.ResponseWriter, r *http.Request, user usersdb.User, viewer middleware.CurrentUser, isSelf bool) { |
| | 28 | + page, _ := strconv.Atoi(r.URL.Query().Get("page")) |
| | 29 | + if page < 1 { |
| | 30 | + page = 1 |
| | 31 | + } |
| | 32 | + |
| | 33 | + // We over-fetch a little so the per-row visibility filter can |
| | 34 | + // drop private-repo rows without leaving a short page. With most |
| | 35 | + // stars being public this is a no-op; for users with many private |
| | 36 | + // stars the filter just trims more. |
| | 37 | + const fetch = starsTabPageSize * 2 |
| | 38 | + rows, err := socialdb.New().ListStarsForUser(r.Context(), h.d.Pool, socialdb.ListStarsForUserParams{ |
| | 39 | + UserID: user.ID, |
| | 40 | + Limit: fetch, |
| | 41 | + Offset: int32((page - 1) * starsTabPageSize), |
| | 42 | + }) |
| | 43 | + if err != nil { |
| | 44 | + h.d.Logger.ErrorContext(r.Context(), "stars tab: list", "error", err) |
| | 45 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| | 46 | + return |
| | 47 | + } |
| | 48 | + |
| | 49 | + actor := policy.AnonymousActor() |
| | 50 | + if !viewer.IsAnonymous() { |
| | 51 | + actor = policy.UserActor(viewer.ID, viewer.Username, viewer.IsSuspended, false) |
| | 52 | + } |
| | 53 | + deps := policy.Deps{Pool: h.d.Pool} |
| | 54 | + |
| | 55 | + visible := make([]map[string]any, 0, len(rows)) |
| | 56 | + for _, row := range rows { |
| | 57 | + if len(visible) >= starsTabPageSize { |
| | 58 | + break |
| | 59 | + } |
| | 60 | + // Re-check visibility per row. Star is on a repo that may have |
| | 61 | + // flipped visibility between star-time and view-time. |
| | 62 | + ref := policy.RepoRef{ |
| | 63 | + ID: row.RepoID, |
| | 64 | + OwnerUserID: row.OwnerUserID.Int64, |
| | 65 | + OwnerOrgID: row.OwnerOrgID.Int64, |
| | 66 | + Visibility: string(row.Visibility), |
| | 67 | + } |
| | 68 | + if !policy.IsVisibleTo(r.Context(), deps, actor, ref) { |
| | 69 | + continue |
| | 70 | + } |
| | 71 | + // Resolve the owner's username for the link. The S31 org case |
| | 72 | + // adds an org-username path; for now user-owned only. |
| | 73 | + ownerName := "" |
| | 74 | + if row.OwnerUserID.Valid { |
| | 75 | + if u, err := h.q.GetUserByID(r.Context(), h.d.Pool, row.OwnerUserID.Int64); err == nil { |
| | 76 | + ownerName = u.Username |
| | 77 | + } |
| | 78 | + } |
| | 79 | + visible = append(visible, map[string]any{ |
| | 80 | + "OwnerName": ownerName, |
| | 81 | + "RepoName": row.RepoName, |
| | 82 | + "Description": row.Description, |
| | 83 | + "Visibility": string(row.Visibility), |
| | 84 | + "StarCount": row.StarCount, |
| | 85 | + "PrimaryLanguage": pgTextStringOrEmpty(row.PrimaryLanguage), |
| | 86 | + "StarredAt": row.StarredAt.Time, |
| | 87 | + }) |
| | 88 | + } |
| | 89 | + |
| | 90 | + if isSelf { |
| | 91 | + w.Header().Set("Cache-Control", "no-cache, private") |
| | 92 | + } else { |
| | 93 | + w.Header().Set("Cache-Control", "max-age=120") |
| | 94 | + } |
| | 95 | + |
| | 96 | + avatarURL := fmt.Sprintf("/avatars/%s", url.PathEscape(user.Username)) |
| | 97 | + data := map[string]any{ |
| | 98 | + "Title": "Stars · " + user.DisplayName, |
| | 99 | + "User": user, |
| | 100 | + "IsSelf": isSelf, |
| | 101 | + "AvatarURL": avatarURL, |
| | 102 | + "Stars": visible, |
| | 103 | + "Page": page, |
| | 104 | + "HasNext": len(visible) >= starsTabPageSize, |
| | 105 | + "HasPrev": page > 1, |
| | 106 | + } |
| | 107 | + w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| | 108 | + if err := h.d.Render.RenderPage(w, r, "profile/stars_tab", data); err != nil { |
| | 109 | + h.d.Logger.ErrorContext(r.Context(), "stars tab: render", "error", err) |
| | 110 | + } |
| | 111 | +} |
| | 112 | + |
| | 113 | +// pgTextStringOrEmpty unwraps a nullable text column to "" when null. |
| | 114 | +func pgTextStringOrEmpty(t pgtype.Text) string { |
| | 115 | + if !t.Valid { |
| | 116 | + return "" |
| | 117 | + } |
| | 118 | + return t.String |
| | 119 | +} |