BlueGit

timfa bluegit

Files

bluegit / internal / server / people.go

people.go

feat: geef de commitlijst de GitLab-opbouw
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Passed b026b59a History
people.go 4.9 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/store"
12)
13
14func (s *Server) profile(w http.ResponseWriter, r *http.Request) {
15 name := strings.ToLower(strings.TrimSpace(r.PathValue("username")))
16 u, err := s.Store.UserByUsername(r.Context(), name)
17 if err != nil {
18 http.NotFound(w, r)
19 return
20 }
21 v := s.view(w, r, u.Username)
22 v.Profile = &u
23 viewer := int64(0)
24 if v.User != nil {
25 viewer = v.User.ID
26 }
27 ids, err := s.Store.ListRepoIDs(r.Context(), viewer)
28 if err != nil {
29 ids = nil
30 }
31 v.Heat = s.heatmap(r.Context(), u.ID, ids, s.langOf(r))
32 total := 0
33 if v.Heat != nil {
34 total = v.Heat.Total
35 }
36 card, repos, groups := s.profileCard(r, u, viewer, total)
37 card.Heading = "h1"
38 v.Card = &card
39 v.Repos = repos
40 v.Groups = groups
41 s.render(w, "profile", v)
42}
43
44// profileCard bouwt het blok en de lijsten van iemands eigen repositories
45// en groepen, beperkt tot wat de kijker mag zien.
46func (s *Server) profileCard(r *http.Request, person store.User, viewer int64, contrib int) (ProfileCard, []store.Repo, []store.Group) {
47 repos := s.ownedReposVisible(r.Context(), person.ID, viewer)
48 groups := s.groupsVisible(r.Context(), person.ID, viewer)
49 name := strings.TrimSpace(person.DisplayName)
50 if name == "" {
51 name = person.Username
52 }
53 card := ProfileCard{
54 User: person, Name: name, Since: i18n.FormatDate(s.langOf(r), person.CreatedAt),
55 Bio: strings.TrimSpace(person.Bio), URL: s.absURL("/users/" + person.Username),
56 Repos: len(repos), Groups: len(groups), Contrib: contrib,
57 Own: viewer != 0 && viewer == person.ID,
58 }
59 return card, repos, groups
60}
61
62func (s *Server) ownedReposVisible(ctx context.Context, owner, viewer int64) []store.Repo {
63 all, err := s.Store.ReposByOwner(ctx, owner)
64 if err != nil {
65 return nil
66 }
67 allowed, err := s.Store.ListRepoIDs(ctx, viewer)
68 if err != nil {
69 return nil
70 }
71 set := map[int64]bool{}
72 for _, id := range allowed {
73 set[id] = true
74 }
75 var out []store.Repo
76 for _, repo := range all {
77 if set[repo.ID] {
78 out = append(out, repo)
79 }
80 }
81 return out
82}
83
84func (s *Server) groupsVisible(ctx context.Context, member, viewer int64) []store.Group {
85 all, err := s.Store.ListGroups(ctx, member)
86 if err != nil {
87 return nil
88 }
89 var out []store.Group
90 for _, g := range all {
91 if g.Visibility == "public" {
92 out = append(out, g)
93 continue
94 }
95 if viewer == 0 {
96 continue
97 }
98 role, err := s.Store.GroupRole(ctx, g.ID, viewer)
99 if err == nil && role.CanRead() {
100 out = append(out, g)
101 }
102 }
103 return out
104}
105
106func (s *Server) presentCommits(ctx context.Context, repoID int64, in []gitrepo.Commit, lang i18n.Lang) []CommitRow {
107 shas := make([]string, 0, len(in))
108 for _, c := range in {
109 if c.SHA != "" {
110 shas = append(shas, c.SHA)
111 }
112 }
113 pushers := map[string]store.Pusher{}
114 if s.Store != nil && repoID != 0 && len(shas) > 0 {
115 if got, err := s.Store.PushersBySHA(ctx, repoID, shas); err == nil {
116 pushers = got
117 }
118 }
119 cache := map[string]store.User{}
120 miss := map[string]bool{}
121 var rows []CommitRow
122 for _, c := range in {
123 rows = append(rows, s.oneCommit(ctx, c, pushers[c.SHA], lang, cache, miss))
124 }
125 return rows
126}
127
128func (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 {
129 row := CommitRow{
130 SHA: c.SHA, Short: c.Short, Author: c.Author, Date: c.Date,
131 Subject: c.Subject, Body: c.Body, When: i18n.Ago(lang, c.Unix),
132 Initial: initialOf(c.Author),
133 }
134 if u, ok := s.userForEmail(ctx, c.Email, cache, miss); ok {
135 row.Author = u.Username
136 row.Who = strings.TrimSpace(u.DisplayName)
137 if row.Who == "" {
138 row.Who = u.Username
139 }
140 row.WhoURL = "/users/" + u.Username
141 row.UserURL = row.WhoURL
142 row.Avatar = u.AvatarURL
143 row.Initial = initialOf(u.Username)
144 return finishCommit(row, c.Email, c.Author)
145 }
146 if pusher.UserID != 0 && pusher.Username != "" {
147 row.Author = pusher.Username
148 row.UserURL = "/users/" + pusher.Username
149 row.Avatar = pusher.AvatarURL
150 row.Initial = initialOf(pusher.Username)
151 raw := strings.TrimSpace(c.Author)
152 if raw != "" && !strings.EqualFold(raw, pusher.Username) {
153 row.RawName = raw
154 row.PushedBy = pusher.Username
155 }
156 }
157 return finishCommit(row, c.Email, c.Author)
158}
159
160func (s *Server) userForEmail(ctx context.Context, email string, cache map[string]store.User, miss map[string]bool) (store.User, bool) {
161 if s.Store == nil {
162 return store.User{}, false
163 }
164 key := strings.ToLower(strings.TrimSpace(email))
165 if u, ok := cache[key]; ok {
166 return u, true
167 }
168 if miss[key] {
169 return store.User{}, false
170 }
171 u, ok := s.Store.UserByGitEmail(ctx, email)
172 if !ok {
173 miss[key] = true
174 return store.User{}, false
175 }
176 cache[key] = u
177 return u, true
178}
179
180func initialOf(s string) string {
181 s = strings.TrimSpace(s)
182 if s == "" {
183 return "?"
184 }
185 r, _ := utf8.DecodeRuneInString(s)
186 return strings.ToUpper(string(r))
187}