fetchCollection handles the API endpoint for retrieving collection data.
(app *App, w http.ResponseWriter, r *http.Request)
| 533 | |
| 534 | // fetchCollection handles the API endpoint for retrieving collection data. |
| 535 | func fetchCollection(app *App, w http.ResponseWriter, r *http.Request) error { |
| 536 | if IsActivityPubRequest(r) { |
| 537 | return handleFetchCollectionActivities(app, w, r) |
| 538 | } |
| 539 | |
| 540 | vars := mux.Vars(r) |
| 541 | alias := vars["alias"] |
| 542 | |
| 543 | // TODO: move this logic into a common getCollection function |
| 544 | // Get base Collection data |
| 545 | c, err := app.db.GetCollection(alias) |
| 546 | if err != nil { |
| 547 | return err |
| 548 | } |
| 549 | c.hostName = app.cfg.App.Host |
| 550 | |
| 551 | // Redirect users who aren't requesting JSON |
| 552 | reqJSON := IsJSON(r) |
| 553 | if !reqJSON { |
| 554 | return impart.HTTPError{http.StatusFound, c.CanonicalURL()} |
| 555 | } |
| 556 | |
| 557 | // Check permissions |
| 558 | userID, err := apiCheckCollectionPermissions(app, r, c) |
| 559 | if err != nil { |
| 560 | return err |
| 561 | } |
| 562 | isCollOwner := userID == c.OwnerID |
| 563 | |
| 564 | // Fetch extra data about the Collection |
| 565 | res := &CollectionObj{Collection: *c} |
| 566 | if c.PublicOwner { |
| 567 | u, err := app.db.GetUserByID(res.OwnerID) |
| 568 | if err != nil { |
| 569 | // Log the error and just continue |
| 570 | log.Error("Error getting user for collection: %v", err) |
| 571 | } else { |
| 572 | res.Owner = u |
| 573 | } |
| 574 | } |
| 575 | // TODO: check status for silenced |
| 576 | app.db.GetPostsCount(res, isCollOwner) |
| 577 | // Strip non-public information |
| 578 | res.Collection.ForPublic() |
| 579 | |
| 580 | return impart.WriteSuccess(w, res, http.StatusOK) |
| 581 | } |
| 582 | |
| 583 | // fetchCollectionPosts handles an API endpoint for retrieving a collection's |
| 584 | // posts. |
nothing calls this directly
no test coverage detected