| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "time" |
| 10 | |
| 11 | "gitlab.com/Timfa/bluegit/internal/access" |
| 12 | "gitlab.com/Timfa/bluegit/internal/store" |
| 13 | ) |
| 14 | |
| 15 | type issueWrite struct { |
| 16 | Title *string `json:"title"` |
| 17 | Description *string `json:"description"` |
| 18 | State *string `json:"state"` |
| 19 | Confidential *bool `json:"confidential"` |
| 20 | Labels *[]string `json:"labels"` |
| 21 | Assignees *[]string `json:"assignees"` |
| 22 | Milestone *string `json:"milestone"` |
| 23 | DueDate *string `json:"due_date"` |
| 24 | } |
| 25 | |
| 26 | func (s *Server) issuesAPI(w http.ResponseWriter, r *http.Request) { |
| 27 | write := r.Method == http.MethodPost |
| 28 | u, repo, role, ok := s.issueAuth(w, r, write) |
| 29 | if !ok { |
| 30 | return |
| 31 | } |
| 32 | if r.Method == http.MethodGet { |
| 33 | state := r.URL.Query().Get("state") |
| 34 | if state != "closed" && state != "all" { |
| 35 | state = "open" |
| 36 | } |
| 37 | list, err := s.Store.ListIssues(r.Context(), repo.ID, store.IssueFilter{ |
| 38 | State: state, Query: r.URL.Query().Get("q"), Label: r.URL.Query().Get("label"), |
| 39 | Assignee: r.URL.Query().Get("assignee"), ViewerID: u.ID, Triage: role.CanTriageIssue(), |
| 40 | }) |
| 41 | if err != nil { |
| 42 | s.fail(w, r, err) |
| 43 | return |
| 44 | } |
| 45 | out := make([]any, 0, len(list)) |
| 46 | for _, iss := range list { |
| 47 | out = append(out, s.issueJSON(repo, iss, nil)) |
| 48 | } |
| 49 | writeJSON(w, http.StatusOK, map[string]any{"issues": out}) |
| 50 | return |
| 51 | } |
| 52 | if !role.CanCreateIssue() { |
| 53 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 54 | return |
| 55 | } |
| 56 | var req issueWrite |
| 57 | if !s.readJSON(w, r, &req) { |
| 58 | return |
| 59 | } |
| 60 | if req.Title == nil || strings.TrimSpace(*req.Title) == "" { |
| 61 | http.Error(w, s.tr(r, "err.issueTitle"), http.StatusBadRequest) |
| 62 | return |
| 63 | } |
| 64 | in := store.NewIssue{Title: *req.Title} |
| 65 | if req.Description != nil { |
| 66 | in.Description = *req.Description |
| 67 | } |
| 68 | if req.Confidential != nil { |
| 69 | in.Confidential = *req.Confidential |
| 70 | } |
| 71 | if req.Labels != nil || req.Assignees != nil || req.Milestone != nil || req.DueDate != nil { |
| 72 | if !role.CanTriageIssue() { |
| 73 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 74 | return |
| 75 | } |
| 76 | if err := s.applyIssueWrite(r, repo, &in, req); err != nil { |
| 77 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 78 | return |
| 79 | } |
| 80 | } |
| 81 | base := quickBase{Confidential: in.Confidential, Due: in.DueOn} |
| 82 | if req.Labels != nil { |
| 83 | base.Labels = *req.Labels |
| 84 | } |
| 85 | if req.Assignees != nil { |
| 86 | base.Assignees = *req.Assignees |
| 87 | } |
| 88 | if req.Milestone != nil { |
| 89 | base.Milestone = *req.Milestone |
| 90 | } |
| 91 | q, err := s.foldQuick(r.Context(), repo, in.Description, role.CanTriageIssue(), true, base) |
| 92 | if err != nil { |
| 93 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 94 | return |
| 95 | } |
| 96 | in.Description = q.Body |
| 97 | if q.Labels { |
| 98 | in.LabelIDs = q.LabelIDs |
| 99 | } |
| 100 | if q.Assignees { |
| 101 | in.AssigneeIDs = q.AssigneeIDs |
| 102 | } |
| 103 | if q.Meta { |
| 104 | in.MilestoneID = q.MilestoneID |
| 105 | in.DueOn = q.Due |
| 106 | in.Confidential = q.Confidential |
| 107 | } |
| 108 | iss, err := s.Store.CreateIssue(r.Context(), repo.ID, u.ID, in) |
| 109 | if err != nil { |
| 110 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 111 | return |
| 112 | } |
| 113 | s.issueHook(repo, iss.Number, u.Username, "opened", "") |
| 114 | notes, _ := s.Store.ListIssueNotes(r.Context(), iss.ID) |
| 115 | writeJSON(w, http.StatusCreated, s.issueJSON(repo, iss, notes)) |
| 116 | } |
| 117 | |
| 118 | func (s *Server) issueAPI(w http.ResponseWriter, r *http.Request) { |
| 119 | write := r.Method != http.MethodGet |
| 120 | u, repo, role, ok := s.issueAuth(w, r, write) |
| 121 | if !ok { |
| 122 | return |
| 123 | } |
| 124 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 125 | if err != nil || n < 1 { |
| 126 | http.NotFound(w, r) |
| 127 | return |
| 128 | } |
| 129 | iss, err := s.Store.IssueByNumber(r.Context(), repo.ID, n) |
| 130 | if err != nil || !issueVisible(iss, u, role) { |
| 131 | http.NotFound(w, r) |
| 132 | return |
| 133 | } |
| 134 | if r.Method == http.MethodGet { |
| 135 | notes, err := s.Store.ListIssueNotes(r.Context(), iss.ID) |
| 136 | if err != nil { |
| 137 | s.fail(w, r, err) |
| 138 | return |
| 139 | } |
| 140 | writeJSON(w, http.StatusOK, s.issueJSON(repo, iss, notes)) |
| 141 | return |
| 142 | } |
| 143 | if r.Method == http.MethodDelete { |
| 144 | if !role.CanManageSettings() { |
| 145 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 146 | return |
| 147 | } |
| 148 | if err := s.Store.DeleteIssue(r.Context(), repo.ID, iss.ID); err != nil { |
| 149 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 150 | return |
| 151 | } |
| 152 | w.WriteHeader(http.StatusNoContent) |
| 153 | return |
| 154 | } |
| 155 | if u.ID != iss.AuthorID && !role.CanTriageIssue() { |
| 156 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 157 | return |
| 158 | } |
| 159 | var req issueWrite |
| 160 | if !s.readJSON(w, r, &req) { |
| 161 | return |
| 162 | } |
| 163 | if (req.Labels != nil || req.Assignees != nil || req.Milestone != nil || req.DueDate != nil || req.Confidential != nil) && !role.CanTriageIssue() { |
| 164 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 165 | return |
| 166 | } |
| 167 | action := "" |
| 168 | var folded quickResult |
| 169 | base := quickFromIssue(iss) |
| 170 | if req.Labels != nil { |
| 171 | base.Labels = *req.Labels |
| 172 | } |
| 173 | if req.Assignees != nil { |
| 174 | base.Assignees = *req.Assignees |
| 175 | } |
| 176 | if req.Milestone != nil { |
| 177 | base.Milestone = *req.Milestone |
| 178 | } |
| 179 | if req.DueDate != nil { |
| 180 | base.Due = *req.DueDate |
| 181 | } |
| 182 | if req.Confidential != nil { |
| 183 | base.Confidential = *req.Confidential |
| 184 | } |
| 185 | if req.Title != nil || req.Description != nil { |
| 186 | title, desc := iss.Title, iss.Description |
| 187 | if req.Title != nil { |
| 188 | title = *req.Title |
| 189 | } |
| 190 | if req.Description != nil { |
| 191 | var err error |
| 192 | folded, err = s.foldQuick(r.Context(), repo, *req.Description, role.CanTriageIssue(), false, base) |
| 193 | if err != nil { |
| 194 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 195 | return |
| 196 | } |
| 197 | desc = folded.Body |
| 198 | } |
| 199 | if err := s.Store.UpdateIssueText(r.Context(), iss.ID, title, desc); err != nil { |
| 200 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 201 | return |
| 202 | } |
| 203 | if err := s.persistQuick(r.Context(), repo, iss.ID, folded); err != nil { |
| 204 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 205 | return |
| 206 | } |
| 207 | action = "updated" |
| 208 | } |
| 209 | if req.Labels != nil || req.Assignees != nil || req.Milestone != nil || req.DueDate != nil || req.Confidential != nil { |
| 210 | if req.Labels != nil && !folded.Labels { |
| 211 | ids, err := s.resolveLabels(r.Context(), repo.ID, *req.Labels) |
| 212 | if err != nil { |
| 213 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 214 | return |
| 215 | } |
| 216 | if err := s.Store.SetIssueLabels(r.Context(), repo.ID, iss.ID, ids); err != nil { |
| 217 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 218 | return |
| 219 | } |
| 220 | } |
| 221 | if req.Assignees != nil && !folded.Assignees { |
| 222 | ids, err := s.resolveAssignees(r.Context(), repo, *req.Assignees) |
| 223 | if err != nil { |
| 224 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 225 | return |
| 226 | } |
| 227 | if err := s.Store.SetIssueAssignees(r.Context(), repo.ID, iss.ID, ids); err != nil { |
| 228 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 229 | return |
| 230 | } |
| 231 | } |
| 232 | if !folded.Meta && (req.Milestone != nil || req.DueDate != nil || req.Confidential != nil) { |
| 233 | mile, due, conf := iss.MilestoneID, iss.DueOn, iss.Confidential |
| 234 | if req.Milestone != nil { |
| 235 | id, err := s.resolveMilestone(r.Context(), repo.ID, *req.Milestone) |
| 236 | if err != nil { |
| 237 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 238 | return |
| 239 | } |
| 240 | mile = id |
| 241 | } |
| 242 | if req.DueDate != nil { |
| 243 | due = *req.DueDate |
| 244 | } |
| 245 | if req.Confidential != nil { |
| 246 | conf = *req.Confidential |
| 247 | } |
| 248 | if err := s.Store.SetIssueMeta(r.Context(), repo.ID, iss.ID, mile, conf, due); err != nil { |
| 249 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 250 | return |
| 251 | } |
| 252 | } |
| 253 | if action == "" { |
| 254 | action = "updated" |
| 255 | } |
| 256 | } |
| 257 | if req.State != nil { |
| 258 | if *req.State != "open" && *req.State != "closed" { |
| 259 | http.Error(w, s.tr(r, "err.issueState"), http.StatusBadRequest) |
| 260 | return |
| 261 | } |
| 262 | changed, err := s.Store.SetIssueState(r.Context(), iss.ID, *req.State) |
| 263 | if err != nil { |
| 264 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 265 | return |
| 266 | } |
| 267 | if changed { |
| 268 | if *req.State == "closed" { |
| 269 | action = "closed" |
| 270 | } else { |
| 271 | action = "reopened" |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | fresh, err := s.Store.IssueByNumber(r.Context(), repo.ID, n) |
| 276 | if err != nil { |
| 277 | s.fail(w, r, err) |
| 278 | return |
| 279 | } |
| 280 | if action != "" { |
| 281 | comment := "" |
| 282 | s.issueHook(repo, fresh.Number, u.Username, action, comment) |
| 283 | } |
| 284 | notes, _ := s.Store.ListIssueNotes(r.Context(), fresh.ID) |
| 285 | writeJSON(w, http.StatusOK, s.issueJSON(repo, fresh, notes)) |
| 286 | } |
| 287 | |
| 288 | func (s *Server) issueNoteAPI(w http.ResponseWriter, r *http.Request) { |
| 289 | u, repo, role, ok := s.issueAuth(w, r, true) |
| 290 | if !ok { |
| 291 | return |
| 292 | } |
| 293 | if !role.CanCreateIssue() { |
| 294 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 295 | return |
| 296 | } |
| 297 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 298 | if err != nil || n < 1 { |
| 299 | http.NotFound(w, r) |
| 300 | return |
| 301 | } |
| 302 | iss, err := s.Store.IssueByNumber(r.Context(), repo.ID, n) |
| 303 | if err != nil || !issueVisible(iss, u, role) { |
| 304 | http.NotFound(w, r) |
| 305 | return |
| 306 | } |
| 307 | var req struct { |
| 308 | Body string `json:"body"` |
| 309 | } |
| 310 | if !s.readJSON(w, r, &req) { |
| 311 | return |
| 312 | } |
| 313 | q, err := s.foldQuick(r.Context(), repo, req.Body, role.CanTriageIssue(), false, quickFromIssue(iss)) |
| 314 | if err != nil { |
| 315 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 316 | return |
| 317 | } |
| 318 | if err := s.persistQuick(r.Context(), repo, iss.ID, q); err != nil { |
| 319 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 320 | return |
| 321 | } |
| 322 | if strings.TrimSpace(q.Body) == "" { |
| 323 | if !q.touched() { |
| 324 | http.Error(w, s.tr(r, "err.issueNote"), http.StatusBadRequest) |
| 325 | return |
| 326 | } |
| 327 | s.issueHook(repo, iss.Number, u.Username, "updated", "") |
| 328 | fresh, err := s.Store.IssueByNumber(r.Context(), repo.ID, n) |
| 329 | if err != nil { |
| 330 | s.fail(w, r, err) |
| 331 | return |
| 332 | } |
| 333 | notes, _ := s.Store.ListIssueNotes(r.Context(), fresh.ID) |
| 334 | writeJSON(w, http.StatusOK, s.issueJSON(repo, fresh, notes)) |
| 335 | return |
| 336 | } |
| 337 | note, err := s.Store.AddIssueNote(r.Context(), repo.ID, iss.ID, u.ID, q.Body) |
| 338 | if err != nil { |
| 339 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 340 | return |
| 341 | } |
| 342 | s.issueHook(repo, iss.Number, u.Username, "commented", note.Body) |
| 343 | writeJSON(w, http.StatusCreated, map[string]any{ |
| 344 | "id": note.ID, "author": note.Author, "body": note.Body, |
| 345 | "created_at": note.CreatedAt.UTC().Format(time.RFC3339), |
| 346 | }) |
| 347 | } |
| 348 | |
| 349 | func (s *Server) labelsAPI(w http.ResponseWriter, r *http.Request) { |
| 350 | u, repo, role, ok := s.issueAuth(w, r, r.Method == http.MethodPost) |
| 351 | if !ok { |
| 352 | return |
| 353 | } |
| 354 | if r.Method == http.MethodGet { |
| 355 | list, err := s.Store.ListLabels(r.Context(), repo.ID) |
| 356 | if err != nil { |
| 357 | s.fail(w, r, err) |
| 358 | return |
| 359 | } |
| 360 | out := make([]any, 0, len(list)) |
| 361 | for _, l := range list { |
| 362 | out = append(out, map[string]any{"id": l.ID, "name": l.Name, "color": l.Color}) |
| 363 | } |
| 364 | writeJSON(w, http.StatusOK, map[string]any{"labels": out}) |
| 365 | return |
| 366 | } |
| 367 | if u.ID == 0 || !role.CanManageSettings() { |
| 368 | http.Error(w, s.tr(r, "err.onlyAdmin"), http.StatusForbidden) |
| 369 | return |
| 370 | } |
| 371 | var req struct { |
| 372 | Name string `json:"name"` |
| 373 | Color string `json:"color"` |
| 374 | } |
| 375 | if !s.readJSON(w, r, &req) { |
| 376 | return |
| 377 | } |
| 378 | lab, err := s.Store.UpsertLabel(r.Context(), repo.ID, req.Name, req.Color) |
| 379 | if err != nil { |
| 380 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 381 | return |
| 382 | } |
| 383 | writeJSON(w, http.StatusCreated, map[string]any{"id": lab.ID, "name": lab.Name, "color": lab.Color}) |
| 384 | } |
| 385 | |
| 386 | func (s *Server) milestonesAPI(w http.ResponseWriter, r *http.Request) { |
| 387 | u, repo, role, ok := s.issueAuth(w, r, r.Method == http.MethodPost) |
| 388 | if !ok { |
| 389 | return |
| 390 | } |
| 391 | if r.Method == http.MethodGet { |
| 392 | list, err := s.Store.ListMilestones(r.Context(), repo.ID) |
| 393 | if err != nil { |
| 394 | s.fail(w, r, err) |
| 395 | return |
| 396 | } |
| 397 | out := make([]any, 0, len(list)) |
| 398 | for _, m := range list { |
| 399 | out = append(out, map[string]any{"id": m.ID, "title": m.Title, "due_on": m.DueOn, "state": m.State}) |
| 400 | } |
| 401 | writeJSON(w, http.StatusOK, map[string]any{"milestones": out}) |
| 402 | return |
| 403 | } |
| 404 | if u.ID == 0 || !role.CanManageSettings() { |
| 405 | http.Error(w, s.tr(r, "err.onlyAdmin"), http.StatusForbidden) |
| 406 | return |
| 407 | } |
| 408 | var req struct { |
| 409 | Title string `json:"title"` |
| 410 | DueOn string `json:"due_on"` |
| 411 | } |
| 412 | if !s.readJSON(w, r, &req) { |
| 413 | return |
| 414 | } |
| 415 | m, err := s.Store.UpsertMilestone(r.Context(), repo.ID, req.Title, req.DueOn) |
| 416 | if err != nil { |
| 417 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 418 | return |
| 419 | } |
| 420 | writeJSON(w, http.StatusCreated, map[string]any{"id": m.ID, "title": m.Title, "due_on": m.DueOn, "state": m.State}) |
| 421 | } |
| 422 | |
| 423 | func (s *Server) applyIssueWrite(r *http.Request, repo store.Repo, in *store.NewIssue, req issueWrite) error { |
| 424 | if req.Labels != nil { |
| 425 | ids, err := s.resolveLabels(r.Context(), repo.ID, *req.Labels) |
| 426 | if err != nil { |
| 427 | return err |
| 428 | } |
| 429 | in.LabelIDs = ids |
| 430 | } |
| 431 | if req.Assignees != nil { |
| 432 | ids, err := s.resolveAssignees(r.Context(), repo, *req.Assignees) |
| 433 | if err != nil { |
| 434 | return err |
| 435 | } |
| 436 | in.AssigneeIDs = ids |
| 437 | } |
| 438 | if req.Milestone != nil { |
| 439 | id, err := s.resolveMilestone(r.Context(), repo.ID, *req.Milestone) |
| 440 | if err != nil { |
| 441 | return err |
| 442 | } |
| 443 | in.MilestoneID = id |
| 444 | } |
| 445 | if req.DueDate != nil { |
| 446 | in.DueOn = *req.DueDate |
| 447 | } |
| 448 | return nil |
| 449 | } |
| 450 | |
| 451 | func (s *Server) issueAuth(w http.ResponseWriter, r *http.Request, write bool) (store.User, store.Repo, access.Role, bool) { |
| 452 | if s.Store == nil { |
| 453 | http.Error(w, s.noStorageText(r), http.StatusServiceUnavailable) |
| 454 | return store.User{}, store.Repo{}, "", false |
| 455 | } |
| 456 | repo, err := s.Store.RepoByPath(r.Context(), r.PathValue("owner"), r.PathValue("repo")) |
| 457 | if err != nil { |
| 458 | http.NotFound(w, r) |
| 459 | return store.User{}, store.Repo{}, "", false |
| 460 | } |
| 461 | token := bearer(r) |
| 462 | if token == "" && !write { |
| 463 | // Anoniem lezen mag alleen in een publieke repository, net als in de UI. |
| 464 | if repo.Visibility != "public" { |
| 465 | http.NotFound(w, r) |
| 466 | return store.User{}, store.Repo{}, "", false |
| 467 | } |
| 468 | return store.User{}, repo, "", true |
| 469 | } |
| 470 | if token == "" { |
| 471 | http.Error(w, s.tr(r, "err.tokenAbsent"), http.StatusUnauthorized) |
| 472 | return store.User{}, store.Repo{}, "", false |
| 473 | } |
| 474 | if strings.HasPrefix(token, "bgj_") || !strings.HasPrefix(token, "bg_") { |
| 475 | http.Error(w, s.tr(r, "err.tokenBad"), http.StatusUnauthorized) |
| 476 | return store.User{}, store.Repo{}, "", false |
| 477 | } |
| 478 | u, err := s.Store.UserByToken(r.Context(), token) |
| 479 | if err != nil { |
| 480 | http.Error(w, s.tr(r, "err.tokenBad"), http.StatusUnauthorized) |
| 481 | return store.User{}, store.Repo{}, "", false |
| 482 | } |
| 483 | role, err := s.Store.EffectiveRole(r.Context(), u.ID, repo.ID) |
| 484 | if err != nil { |
| 485 | http.Error(w, s.tr(r, "err.rights"), http.StatusInternalServerError) |
| 486 | return store.User{}, store.Repo{}, "", false |
| 487 | } |
| 488 | if !role.CanRead() && repo.Visibility != "public" { |
| 489 | http.NotFound(w, r) |
| 490 | return store.User{}, store.Repo{}, "", false |
| 491 | } |
| 492 | return u, repo, role, true |
| 493 | } |
| 494 | |
| 495 | func (s *Server) readJSON(w http.ResponseWriter, r *http.Request, dst any) bool { |
| 496 | if err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(dst); err != nil { |
| 497 | http.Error(w, s.tr(r, "err.json"), http.StatusBadRequest) |
| 498 | return false |
| 499 | } |
| 500 | return true |
| 501 | } |
| 502 | |
| 503 | func (s *Server) issueJSON(repo store.Repo, iss store.Issue, notes []store.IssueNote) map[string]any { |
| 504 | obj := map[string]any{ |
| 505 | "id": iss.Number, "title": iss.Title, "description": iss.Description, |
| 506 | "status": iss.Status, "confidential": iss.Confidential, "author": iss.Author, |
| 507 | "assignees": nonNil(iss.Assignees), "labels": labelNames(iss.Labels), |
| 508 | "milestone": iss.Milestone, "due_date": iss.DueOn, |
| 509 | "created_at": iss.CreatedAt.UTC().Format(time.RFC3339), |
| 510 | "updated_at": iss.UpdatedAt.UTC().Format(time.RFC3339), |
| 511 | "web_url": strings.TrimRight(s.Cfg.BaseURL, "/") + issuePath(repo, iss.Number), |
| 512 | } |
| 513 | if !iss.ClosedAt.IsZero() { |
| 514 | obj["closed_at"] = iss.ClosedAt.UTC().Format(time.RFC3339) |
| 515 | } |
| 516 | if notes != nil { |
| 517 | arr := make([]any, 0, len(notes)) |
| 518 | for _, n := range notes { |
| 519 | arr = append(arr, map[string]any{ |
| 520 | "id": n.ID, "author": n.Author, "body": n.Body, |
| 521 | "created_at": n.CreatedAt.UTC().Format(time.RFC3339), |
| 522 | }) |
| 523 | } |
| 524 | obj["notes"] = arr |
| 525 | } |
| 526 | return obj |
| 527 | } |
| 528 | |
| 529 | func nonNil(s []string) []string { |
| 530 | if s == nil { |
| 531 | return []string{} |
| 532 | } |
| 533 | return s |
| 534 | } |