MCPcopy
hub / github.com/writefreely/writefreely / newPost

Function newPost

posts.go:552–698  ·  view source on GitHub ↗

API v2 funcs newPost creates a new post with or without an owning Collection. Endpoints: - /posts - /posts?collection={alias} - ? /collections/{alias}/posts

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

Source from the content-addressed store, hash-verified

550// - /posts?collection={alias}
551// - ? /collections/{alias}/posts
552func newPost(app *App, w http.ResponseWriter, r *http.Request) error {
553 reqJSON := IsJSON(r)
554 vars := mux.Vars(r)
555 collAlias := vars["alias"]
556 if collAlias == "" {
557 collAlias = r.FormValue("collection")
558 }
559 accessToken := r.Header.Get("Authorization")
560 if accessToken == "" {
561 // TODO: remove this
562 accessToken = r.FormValue("access_token")
563 }
564
565 // FIXME: determine web submission with Content-Type header
566 var u *User
567 var userID int64 = -1
568 var username string
569 if accessToken == "" {
570 u = getUserSession(app, r)
571 if u != nil {
572 userID = u.ID
573 username = u.Username
574 }
575 } else {
576 userID = app.db.GetUserID(accessToken)
577 }
578 silenced, err := app.db.IsUserSilenced(userID)
579 if err != nil {
580 log.Error("new post: %v", err)
581 }
582 if silenced {
583 return ErrUserSilenced
584 }
585
586 if userID == -1 {
587 return ErrNotLoggedIn
588 }
589
590 if accessToken == "" && u == nil && collAlias != "" {
591 return impart.HTTPError{http.StatusBadRequest, "Parameter `access_token` required."}
592 }
593
594 // Get post data
595 var p *SubmittedPost
596 if reqJSON {
597 decoder := json.NewDecoder(r.Body)
598 err = decoder.Decode(&p)
599 if err != nil {
600 log.Error("Couldn't parse new post JSON request: %v\n", err)
601 return ErrBadJSON
602 }
603 if p.Title == nil {
604 t := ""
605 p.Title = &t
606 }
607 if strings.TrimSpace(*(p.Title)) == "" && (p.Content == nil || strings.TrimSpace(*(p.Content)) == "") {
608 return ErrNoPublishableContent
609 }

Callers

nothing calls this directly

Calls 15

isFontValidMethod · 0.95
ForPublicMethod · 0.95
CanonicalURLMethod · 0.95
IsJSONFunction · 0.85
getUserSessionFunction · 0.85
federatePostFunction · 0.85
IsUserSilencedMethod · 0.80
extractDataMethod · 0.80
EnabledMethod · 0.80
EmailSubsEnabledMethod · 0.80
InsertJobMethod · 0.80
GetUserIDMethod · 0.65

Tested by

no test coverage detected