MCPcopy
hub / github.com/writefreely/writefreely / fetchPost

Function fetchPost

posts.go:1125–1183  ·  view source on GitHub ↗
(app *App, w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

1123}
1124
1125func fetchPost(app *App, w http.ResponseWriter, r *http.Request) error {
1126 var collID int64
1127 var coll *Collection
1128 var err error
1129 vars := mux.Vars(r)
1130 if collAlias := vars["alias"]; collAlias != "" {
1131 // Fetch collection information, since an alias is provided
1132 coll, err = app.db.GetCollection(collAlias)
1133 if err != nil {
1134 return err
1135 }
1136 collID = coll.ID
1137 }
1138
1139 p, err := app.db.GetPost(vars["post"], collID)
1140 if err != nil {
1141 return err
1142 }
1143 if coll == nil && p.CollectionID.Valid {
1144 // Collection post is getting fetched by post ID, not coll alias + post slug, so get coll info now.
1145 coll, err = app.db.GetCollectionByID(p.CollectionID.Int64)
1146 if err != nil {
1147 return err
1148 }
1149 }
1150 if coll != nil {
1151 coll.hostName = app.cfg.App.Host
1152 _, err = apiCheckCollectionPermissions(app, r, coll)
1153 if err != nil {
1154 return err
1155 }
1156 }
1157
1158 silenced, err := app.db.IsUserSilenced(p.OwnerID.Int64)
1159 if err != nil {
1160 log.Error("fetch post: %v", err)
1161 }
1162 if silenced {
1163 return ErrPostNotFound
1164 }
1165
1166 p.extractData()
1167
1168 if IsActivityPubRequest(r) {
1169 if coll == nil {
1170 // This is a draft post; 404 for now
1171 // TODO: return ActivityObject
1172 return impart.HTTPError{http.StatusNotFound, ""}
1173 }
1174
1175 p.Collection = &CollectionObj{Collection: *coll}
1176 po := p.ActivityObject(app)
1177 po.Context = []interface{}{activitystreams.Namespace}
1178 setCacheControl(w, apCacheTime)
1179 return impart.RenderActivityJSON(w, po, http.StatusOK)
1180 }
1181
1182 return impart.WriteSuccess(w, p, http.StatusOK)

Callers

nothing calls this directly

Calls 9

IsActivityPubRequestFunction · 0.85
setCacheControlFunction · 0.85
IsUserSilencedMethod · 0.80
extractDataMethod · 0.80
ActivityObjectMethod · 0.80
GetCollectionMethod · 0.65
GetPostMethod · 0.65
GetCollectionByIDMethod · 0.65

Tested by

no test coverage detected