| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | "time" |
| 8 | |
| 9 | "gitlab.com/Timfa/bluegit/internal/access" |
| 10 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 11 | "gitlab.com/Timfa/bluegit/internal/store" |
| 12 | ) |
| 13 | |
| 14 | func rfcTime(t time.Time) any { |
| 15 | if t.IsZero() { |
| 16 | return nil |
| 17 | } |
| 18 | return t.UTC().Format(time.RFC3339) |
| 19 | } |
| 20 | |
| 21 | func userJSON(u store.User, self bool, web string) map[string]any { |
| 22 | out := map[string]any{ |
| 23 | "id": u.ID, "username": u.Username, "avatar_url": u.AvatarURL, |
| 24 | "name": strings.TrimSpace(u.DisplayName), "bio": u.Bio, |
| 25 | "created_at": rfcTime(u.CreatedAt), |
| 26 | } |
| 27 | if web != "" { |
| 28 | out["web_url"] = web |
| 29 | } |
| 30 | if self { |
| 31 | out["instance_admin"] = u.InstanceAdmin |
| 32 | out["language"] = u.Language |
| 33 | } |
| 34 | return out |
| 35 | } |
| 36 | |
| 37 | func tokenJSON(t store.TokenInfo, plain string) map[string]any { |
| 38 | out := map[string]any{ |
| 39 | "id": t.ID, "name": t.Name, "prefix": t.Prefix, |
| 40 | "scopes": access.StoredScopes(t.Scopes).List(), |
| 41 | "created_at": rfcTime(t.CreatedAt), |
| 42 | "last_used_at": rfcTime(t.LastUsedAt), |
| 43 | "expires_at": rfcTime(t.ExpiresAt), |
| 44 | } |
| 45 | if plain != "" { |
| 46 | out["token"] = plain |
| 47 | } |
| 48 | return out |
| 49 | } |
| 50 | |
| 51 | func (s *Server) apiGetUser(w http.ResponseWriter, r *http.Request) { |
| 52 | u := apiUser(r) |
| 53 | writeJSON(w, http.StatusOK, userJSON(u, true, s.absURL("/users/"+u.Username))) |
| 54 | } |
| 55 | |
| 56 | func (s *Server) apiPutUser(w http.ResponseWriter, r *http.Request) { |
| 57 | var in struct { |
| 58 | Language *string `json:"language"` |
| 59 | Name *string `json:"name"` |
| 60 | Bio *string `json:"bio"` |
| 61 | } |
| 62 | if err := readJSON(r, &in); err != nil { |
| 63 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 64 | return |
| 65 | } |
| 66 | u := apiUser(r) |
| 67 | if in.Language != nil { |
| 68 | if _, err := s.opSetLanguage(r.Context(), u, *in.Language); !s.apiOK(w, r, err) { |
| 69 | return |
| 70 | } |
| 71 | } |
| 72 | if in.Name != nil || in.Bio != nil { |
| 73 | name, bio := u.DisplayName, u.Bio |
| 74 | if in.Name != nil { |
| 75 | name = *in.Name |
| 76 | } |
| 77 | if in.Bio != nil { |
| 78 | bio = *in.Bio |
| 79 | } |
| 80 | if _, err := s.opSetProfile(r.Context(), u, name, bio); !s.apiOK(w, r, err) { |
| 81 | return |
| 82 | } |
| 83 | } |
| 84 | fresh, err := s.Store.UserByID(r.Context(), u.ID) |
| 85 | if !s.apiOK(w, r, err) { |
| 86 | return |
| 87 | } |
| 88 | writeJSON(w, http.StatusOK, userJSON(fresh, true, s.absURL("/users/"+fresh.Username))) |
| 89 | } |
| 90 | |
| 91 | func (s *Server) apiExport(w http.ResponseWriter, r *http.Request) { |
| 92 | writeJSON(w, http.StatusOK, s.opExport(r.Context(), apiUser(r))) |
| 93 | } |
| 94 | |
| 95 | func (s *Server) apiUserByName(w http.ResponseWriter, r *http.Request) { |
| 96 | u, err := s.Store.UserByUsername(r.Context(), r.PathValue("username")) |
| 97 | if err != nil { |
| 98 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 99 | return |
| 100 | } |
| 101 | viewer := apiUser(r).ID |
| 102 | ids, err := s.Store.ListRepoIDs(r.Context(), viewer) |
| 103 | if err != nil { |
| 104 | ids = nil |
| 105 | } |
| 106 | heat := s.heatmap(r.Context(), u.ID, ids, s.langOf(r)) |
| 107 | total := 0 |
| 108 | if heat != nil { |
| 109 | total = heat.Total |
| 110 | } |
| 111 | card, repos, groups := s.profileCard(r, u, viewer, total) |
| 112 | out := userJSON(u, viewer == u.ID, card.URL) |
| 113 | out["counts"] = map[string]any{ |
| 114 | "repos": card.Repos, "groups": card.Groups, "contributions": card.Contrib, |
| 115 | } |
| 116 | out["repos"] = repoBriefs(s, repos) |
| 117 | out["groups"] = groupBriefs(s, groups) |
| 118 | writeJSON(w, http.StatusOK, out) |
| 119 | } |
| 120 | |
| 121 | func repoBriefs(s *Server, repos []store.Repo) []map[string]any { |
| 122 | out := make([]map[string]any, 0, len(repos)) |
| 123 | for _, repo := range repos { |
| 124 | out = append(out, map[string]any{ |
| 125 | "id": repo.ID, "name": repo.Name, "path": repo.FullPath(), |
| 126 | "description": repo.Description, "visibility": repo.Visibility, |
| 127 | "web_url": s.absURL("/" + repo.FullPath()), |
| 128 | }) |
| 129 | } |
| 130 | return out |
| 131 | } |
| 132 | |
| 133 | func groupBriefs(s *Server, groups []store.Group) []map[string]any { |
| 134 | out := make([]map[string]any, 0, len(groups)) |
| 135 | for _, g := range groups { |
| 136 | out = append(out, map[string]any{ |
| 137 | "id": g.ID, "name": g.Name, "path": g.Path, |
| 138 | "description": g.Description, "visibility": g.Visibility, |
| 139 | "web_url": s.absURL("/groups/" + g.Path), |
| 140 | }) |
| 141 | } |
| 142 | return out |
| 143 | } |
| 144 | |
| 145 | func (s *Server) apiListTokens(w http.ResponseWriter, r *http.Request) { |
| 146 | list, err := s.Store.ListTokens(r.Context(), apiUser(r).ID) |
| 147 | if !s.apiOK(w, r, err) { |
| 148 | return |
| 149 | } |
| 150 | page, per := pageQuery(r) |
| 151 | rows := make([]map[string]any, 0, len(list)) |
| 152 | for _, t := range list { |
| 153 | rows = append(rows, tokenJSON(t, "")) |
| 154 | } |
| 155 | writePage(w, r, slicePage(rows, page, per), len(rows), page, per) |
| 156 | } |
| 157 | |
| 158 | func (s *Server) apiCreateToken(w http.ResponseWriter, r *http.Request) { |
| 159 | var in struct { |
| 160 | Name string `json:"name"` |
| 161 | Scopes []string `json:"scopes"` |
| 162 | ExpiresAt string `json:"expires_at"` |
| 163 | } |
| 164 | if err := readJSON(r, &in); err != nil { |
| 165 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 166 | return |
| 167 | } |
| 168 | var exp time.Time |
| 169 | if in.ExpiresAt != "" { |
| 170 | parsed, err := time.Parse(time.RFC3339, in.ExpiresAt) |
| 171 | if err != nil { |
| 172 | s.writeAPIErr(w, r, fail(400, i18n.E("err.tokenExpired"))) |
| 173 | return |
| 174 | } |
| 175 | exp = parsed |
| 176 | } |
| 177 | plain, info, err := s.opIssueToken(r.Context(), apiUser(r), in.Name, in.Scopes, exp) |
| 178 | if !s.apiOK(w, r, err) { |
| 179 | return |
| 180 | } |
| 181 | writeJSON(w, http.StatusCreated, tokenJSON(info, plain)) |
| 182 | } |
| 183 | |
| 184 | func (s *Server) apiDeleteToken(w http.ResponseWriter, r *http.Request) { |
| 185 | id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) |
| 186 | if err != nil { |
| 187 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 188 | return |
| 189 | } |
| 190 | if !s.apiOK(w, r, s.opRevokeToken(r.Context(), apiUser(r), id)) { |
| 191 | return |
| 192 | } |
| 193 | writeJSON(w, http.StatusNoContent, nil) |
| 194 | } |
| 195 | |
| 196 | func keyJSON(k store.SSHKey) map[string]any { |
| 197 | return map[string]any{ |
| 198 | "id": k.ID, "name": k.Name, "key": k.PublicKey, "fingerprint": k.Fingerprint, |
| 199 | "created_at": rfcTime(k.CreatedAt), |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | func (s *Server) apiListKeys(w http.ResponseWriter, r *http.Request) { |
| 204 | list, err := s.Store.ListSSHKeys(r.Context(), apiUser(r).ID) |
| 205 | if !s.apiOK(w, r, err) { |
| 206 | return |
| 207 | } |
| 208 | page, per := pageQuery(r) |
| 209 | rows := make([]map[string]any, 0, len(list)) |
| 210 | for _, k := range list { |
| 211 | rows = append(rows, keyJSON(k)) |
| 212 | } |
| 213 | writePage(w, r, slicePage(rows, page, per), len(rows), page, per) |
| 214 | } |
| 215 | |
| 216 | func (s *Server) apiAddKey(w http.ResponseWriter, r *http.Request) { |
| 217 | var in struct { |
| 218 | Name string `json:"name"` |
| 219 | Key string `json:"key"` |
| 220 | } |
| 221 | if err := readJSON(r, &in); err != nil { |
| 222 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 223 | return |
| 224 | } |
| 225 | if !s.apiOK(w, r, s.opAddSSH(r.Context(), apiUser(r), in.Name, in.Key)) { |
| 226 | return |
| 227 | } |
| 228 | writeJSON(w, http.StatusCreated, map[string]any{"name": in.Name}) |
| 229 | } |
| 230 | |
| 231 | func (s *Server) apiDeleteKey(w http.ResponseWriter, r *http.Request) { |
| 232 | id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) |
| 233 | if err != nil { |
| 234 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 235 | return |
| 236 | } |
| 237 | if !s.apiOK(w, r, s.opRemoveSSH(r.Context(), apiUser(r), id)) { |
| 238 | return |
| 239 | } |
| 240 | writeJSON(w, http.StatusNoContent, nil) |
| 241 | } |
| 242 | |
| 243 | func (s *Server) apiAdminStorage(w http.ResponseWriter, r *http.Request) { |
| 244 | if !apiUser(r).InstanceAdmin { |
| 245 | s.writeAPIErr(w, r, fail(403, i18n.E("err.forbidden"))) |
| 246 | return |
| 247 | } |
| 248 | if r.Method == http.MethodGet { |
| 249 | writeJSON(w, http.StatusOK, map[string]any{"path": s.StoragePath()}) |
| 250 | return |
| 251 | } |
| 252 | var in struct { |
| 253 | Path string `json:"path"` |
| 254 | } |
| 255 | if err := readJSON(r, &in); err != nil { |
| 256 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 257 | return |
| 258 | } |
| 259 | info, moved, err := s.opSetStorage(in.Path) |
| 260 | if !s.apiOK(w, r, err) { |
| 261 | return |
| 262 | } |
| 263 | writeJSON(w, http.StatusOK, map[string]any{"path": info.Path, "moved": moved, "free": info.Free}) |
| 264 | } |