MCPcopy
hub / github.com/writefreely/writefreely / handleViewCollectionTag

Function handleViewCollectionTag

collections.go:1024–1120  ·  view source on GitHub ↗
(app *App, w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

1022}
1023
1024func handleViewCollectionTag(app *App, w http.ResponseWriter, r *http.Request) error {
1025 vars := mux.Vars(r)
1026 tag := vars["tag"]
1027
1028 cr := &collectionReq{}
1029 err := processCollectionRequest(cr, vars, w, r)
1030 if err != nil {
1031 return err
1032 }
1033
1034 u, err := checkUserForCollection(app, cr, r, false)
1035 if err != nil {
1036 return err
1037 }
1038
1039 page := getCollectionPage(vars)
1040
1041 c, err := processCollectionPermissions(app, cr, u, w, r)
1042 if c == nil || err != nil {
1043 return err
1044 }
1045
1046 coll, _ := newDisplayCollection(c, cr, page)
1047
1048 taggedPostIDs, err := app.db.GetAllPostsTaggedIDs(c, tag, cr.isCollOwner)
1049 if err != nil {
1050 return err
1051 }
1052
1053 ttlPosts := len(taggedPostIDs)
1054 pagePosts := coll.Format.PostsPerPage()
1055 coll.TotalPages = int(math.Ceil(float64(ttlPosts) / float64(pagePosts)))
1056 if coll.TotalPages > 0 && page > coll.TotalPages {
1057 redirURL := fmt.Sprintf("/page/%d", coll.TotalPages)
1058 if !app.cfg.App.SingleUser {
1059 redirURL = fmt.Sprintf("/%s%s%s", cr.prefix, coll.Alias, redirURL)
1060 }
1061 return impart.HTTPError{http.StatusFound, redirURL}
1062 }
1063
1064 coll.Posts, _ = app.db.GetPostsTagged(app.cfg, c, tag, page, cr.isCollOwner)
1065 if coll.Posts != nil && len(*coll.Posts) == 0 {
1066 return ErrCollectionPageNotFound
1067 }
1068
1069 // Serve collection
1070 displayPage := TagCollectionPage{
1071 CollectionPage: CollectionPage{
1072 DisplayCollection: coll,
1073 StaticPage: pageForReq(app, r),
1074 IsCustomDomain: cr.isCustomDomain,
1075 },
1076 Tag: tag,
1077 }
1078 var owner *User
1079 if u != nil {
1080 displayPage.Username = u.Username
1081 displayPage.IsOwner = u.ID == coll.OwnerID

Callers

nothing calls this directly

Calls 14

IsSilencedMethod · 0.95
processCollectionRequestFunction · 0.85
checkUserForCollectionFunction · 0.85
getCollectionPageFunction · 0.85
newDisplayCollectionFunction · 0.85
pageForReqFunction · 0.85
PostsPerPageMethod · 0.80
GetAllPostsTaggedIDsMethod · 0.65
GetPostsTaggedMethod · 0.65

Tested by

no test coverage detected