BlueGit

timfa bluegit

Files

bluegit / internal / server / people.go

people.go

feat: deel een profiel via een korte url en een openbare api
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Passed b9293125 History
people.go 5.6 KiB Download Raw
1package server
2
3import (
4 "context"
5 "net/http"
6 "strings"
7 "unicode/utf8"
8
9 "gitlab.com/Timfa/bluegit/internal/gitrepo"
10 "gitlab.com/Timfa/bluegit/internal/i18n"
11 "gitlab.com/Timfa/bluegit/internal/names"
12 "gitlab.com/Timfa/bluegit/internal/store"
13)
14
15// userShort stuurt /<username> door naar /users/<username>.
16// Het eerste paddeel is de gedeelde namespace van gebruikers en groepen
17// (repositories staan op /<owner>/<repo>, groepen op /groups/<pad>).
18// Vaste routes zoals /login blijven specifieker. Alleen een bestaande
19// gebruiker krijgt een redirect; een groepsnaam of onbekende naam niet.
20func (s *Server) userShort(w http.ResponseWriter, r *http.Request) {
21 name := strings.ToLower(strings.TrimSpace(r.PathValue("name")))
22 if names.ValidUsername(name) != nil {
23 http.NotFound(w, r)
24 return
25 }
26 if _, err := s.Store.UserByUsername(r.Context(), name); err != nil {
27 http.NotFound(w, r)
28 return
29 }
30 http.Redirect(w, r, "/users/"+name, http.StatusFound)
31}
32
33func (s *Server) profile(w http.ResponseWriter, r *http.Request) {
34 name := strings.ToLower(strings.TrimSpace(r.PathValue("username")))
35 u, err := s.Store.UserByUsername(r.Context(), name)
36 if err != nil {
37 http.NotFound(w, r)
38 return
39 }
40 v := s.view(w, r, u.Username)
41 v.Profile = &u
42 viewer := int64(0)
43 if v.User != nil {
44 viewer = v.User.ID
45 }
46 ids, err := s.Store.ListRepoIDs(r.Context(), viewer)
47 if err != nil {
48 ids = nil
49 }
50 v.Heat = s.heatmap(r.Context(), u.ID, ids, s.langOf(r))
51 total := 0
52 if v.Heat != nil {
53 total = v.Heat.Total
54 }
55 card, repos, groups := s.profileCard(r, u, viewer, total)
56 card.Heading = "h1"
57 v.Card = &card
58 v.Repos = repos
59 v.Groups = groups
60 s.render(w, "profile", v)
61}
62
63// profileCard bouwt het blok en de lijsten van iemands eigen repositories
64// en groepen, beperkt tot wat de kijker mag zien.
65func (s *Server) profileCard(r *http.Request, person store.User, viewer int64, contrib int) (ProfileCard, []store.Repo, []store.Group) {
66 repos := s.ownedReposVisible(r.Context(), person.ID, viewer)
67 groups := s.groupsVisible(r.Context(), person.ID, viewer)
68 name := strings.TrimSpace(person.DisplayName)
69 if name == "" {
70 name = person.Username
71 }
72 card := ProfileCard{
73 User: person, Name: name, Since: i18n.FormatDate(s.langOf(r), person.CreatedAt),
74 Bio: strings.TrimSpace(person.Bio), URL: s.absURL("/users/" + person.Username),
75 Repos: len(repos), Groups: len(groups), Contrib: contrib,
76 Own: viewer != 0 && viewer == person.ID,
77 }
78 return card, repos, groups
79}
80
81func (s *Server) ownedReposVisible(ctx context.Context, owner, viewer int64) []store.Repo {
82 all, err := s.Store.ReposByOwner(ctx, owner)
83 if err != nil {
84 return nil
85 }
86 allowed, err := s.Store.ListRepoIDs(ctx, viewer)
87 if err != nil {
88 return nil
89 }
90 set := map[int64]bool{}
91 for _, id := range allowed {
92 set[id] = true
93 }
94 var out []store.Repo
95 for _, repo := range all {
96 if set[repo.ID] {
97 out = append(out, repo)
98 }
99 }
100 return out
101}
102
103func (s *Server) groupsVisible(ctx context.Context, member, viewer int64) []store.Group {
104 all, err := s.Store.ListGroups(ctx, member)
105 if err != nil {
106 return nil
107 }
108 var out []store.Group
109 for _, g := range all {
110 if g.Visibility == "public" {
111 out = append(out, g)
112 continue
113 }
114 if viewer == 0 {
115 continue
116 }
117 role, err := s.Store.GroupRole(ctx, g.ID, viewer)
118 if err == nil && role.CanRead() {
119 out = append(out, g)
120 }
121 }
122 return out
123}
124
125func (s *Server) presentCommits(ctx context.Context, repoID int64, in []gitrepo.Commit, lang i18n.Lang) []CommitRow {
126 shas := make([]string, 0, len(in))
127 for _, c := range in {
128 if c.SHA != "" {
129 shas = append(shas, c.SHA)
130 }
131 }
132 pushers := map[string]store.Pusher{}
133 if s.Store != nil && repoID != 0 && len(shas) > 0 {
134 if got, err := s.Store.PushersBySHA(ctx, repoID, shas); err == nil {
135 pushers = got
136 }
137 }
138 cache := map[string]store.User{}
139 miss := map[string]bool{}
140 var rows []CommitRow
141 for _, c := range in {
142 rows = append(rows, s.oneCommit(ctx, c, pushers[c.SHA], lang, cache, miss))
143 }
144 return rows
145}
146
147func (s *Server) oneCommit(ctx context.Context, c gitrepo.Commit, pusher store.Pusher, lang i18n.Lang, cache map[string]store.User, miss map[string]bool) CommitRow {
148 row := CommitRow{
149 SHA: c.SHA, Short: c.Short, Author: c.Author, Date: c.Date,
150 Subject: c.Subject, Body: c.Body, When: i18n.Ago(lang, c.Unix),
151 Initial: initialOf(c.Author),
152 }
153 if u, ok := s.userForEmail(ctx, c.Email, cache, miss); ok {
154 row.Author = u.Username
155 row.Who = strings.TrimSpace(u.DisplayName)
156 if row.Who == "" {
157 row.Who = u.Username
158 }
159 row.WhoURL = "/users/" + u.Username
160 row.UserURL = row.WhoURL
161 row.Avatar = u.AvatarURL
162 row.Initial = initialOf(u.Username)
163 return finishCommit(row, c.Email, c.Author)
164 }
165 if pusher.UserID != 0 && pusher.Username != "" {
166 row.Author = pusher.Username
167 row.UserURL = "/users/" + pusher.Username
168 row.Avatar = pusher.AvatarURL
169 row.Initial = initialOf(pusher.Username)
170 raw := strings.TrimSpace(c.Author)
171 if raw != "" && !strings.EqualFold(raw, pusher.Username) {
172 row.RawName = raw
173 row.PushedBy = pusher.Username
174 }
175 }
176 return finishCommit(row, c.Email, c.Author)
177}
178
179func (s *Server) userForEmail(ctx context.Context, email string, cache map[string]store.User, miss map[string]bool) (store.User, bool) {
180 if s.Store == nil {
181 return store.User{}, false
182 }
183 key := strings.ToLower(strings.TrimSpace(email))
184 if u, ok := cache[key]; ok {
185 return u, true
186 }
187 if miss[key] {
188 return store.User{}, false
189 }
190 u, ok := s.Store.UserByGitEmail(ctx, email)
191 if !ok {
192 miss[key] = true
193 return store.User{}, false
194 }
195 cache[key] = u
196 return u, true
197}
198
199func initialOf(s string) string {
200 s = strings.TrimSpace(s)
201 if s == "" {
202 return "?"
203 }
204 r, _ := utf8.DecodeRuneInString(s)
205 return strings.ToUpper(string(r))
206}