MCPcopy Index your code
hub / github.com/writefreely/writefreely / fetchCollection

Function fetchCollection

collections.go:535–581  ·  view source on GitHub ↗

fetchCollection handles the API endpoint for retrieving collection data.

(app *App, w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

533
534// fetchCollection handles the API endpoint for retrieving collection data.
535func 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.

Callers

nothing calls this directly

Calls 9

IsActivityPubRequestFunction · 0.85
IsJSONFunction · 0.85
ForPublicMethod · 0.80
GetCollectionMethod · 0.65
GetUserByIDMethod · 0.65
GetPostsCountMethod · 0.65
CanonicalURLMethod · 0.45

Tested by

no test coverage detected