| 1 | package server |
| 2 | |
| 3 | import ( |
| 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 | |
| 14 | func (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 | s.render(w, "profile", v) |
| 33 | } |
| 34 | |
| 35 | func (s *Server) presentCommits(ctx context.Context, repoID int64, in []gitrepo.Commit, lang i18n.Lang) []CommitRow { |
| 36 | shas := make([]string, 0, len(in)) |
| 37 | for _, c := range in { |
| 38 | if c.SHA != "" { |
| 39 | shas = append(shas, c.SHA) |
| 40 | } |
| 41 | } |
| 42 | pushers := map[string]store.Pusher{} |
| 43 | if s.Store != nil && repoID != 0 && len(shas) > 0 { |
| 44 | if got, err := s.Store.PushersBySHA(ctx, repoID, shas); err == nil { |
| 45 | pushers = got |
| 46 | } |
| 47 | } |
| 48 | cache := map[string]store.User{} |
| 49 | miss := map[string]bool{} |
| 50 | var rows []CommitRow |
| 51 | for _, c := range in { |
| 52 | rows = append(rows, s.oneCommit(ctx, c, pushers[c.SHA], lang, cache, miss)) |
| 53 | } |
| 54 | return rows |
| 55 | } |
| 56 | |
| 57 | func (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 { |
| 58 | row := CommitRow{ |
| 59 | SHA: c.SHA, Short: c.Short, Author: c.Author, Date: c.Date, |
| 60 | Subject: c.Subject, Body: c.Body, When: i18n.Ago(lang, c.Unix), |
| 61 | Initial: initialOf(c.Author), |
| 62 | } |
| 63 | if u, ok := s.userForEmail(ctx, c.Email, cache, miss); ok { |
| 64 | row.Author = u.Username |
| 65 | row.UserURL = "/users/" + u.Username |
| 66 | row.Avatar = u.AvatarURL |
| 67 | row.Initial = initialOf(u.Username) |
| 68 | return row |
| 69 | } |
| 70 | if pusher.UserID != 0 && pusher.Username != "" { |
| 71 | row.Author = pusher.Username |
| 72 | row.UserURL = "/users/" + pusher.Username |
| 73 | row.Avatar = pusher.AvatarURL |
| 74 | row.Initial = initialOf(pusher.Username) |
| 75 | raw := strings.TrimSpace(c.Author) |
| 76 | if raw != "" && !strings.EqualFold(raw, pusher.Username) { |
| 77 | row.RawName = raw |
| 78 | row.PushedBy = pusher.Username |
| 79 | } |
| 80 | } |
| 81 | return row |
| 82 | } |
| 83 | |
| 84 | func (s *Server) userForEmail(ctx context.Context, email string, cache map[string]store.User, miss map[string]bool) (store.User, bool) { |
| 85 | if s.Store == nil { |
| 86 | return store.User{}, false |
| 87 | } |
| 88 | key := strings.ToLower(strings.TrimSpace(email)) |
| 89 | if u, ok := cache[key]; ok { |
| 90 | return u, true |
| 91 | } |
| 92 | if miss[key] { |
| 93 | return store.User{}, false |
| 94 | } |
| 95 | u, ok := s.Store.UserByGitEmail(ctx, email) |
| 96 | if !ok { |
| 97 | miss[key] = true |
| 98 | return store.User{}, false |
| 99 | } |
| 100 | cache[key] = u |
| 101 | return u, true |
| 102 | } |
| 103 | |
| 104 | func initialOf(s string) string { |
| 105 | s = strings.TrimSpace(s) |
| 106 | if s == "" { |
| 107 | return "?" |
| 108 | } |
| 109 | r, _ := utf8.DecodeRuneInString(s) |
| 110 | return strings.ToUpper(string(r)) |
| 111 | } |