addPost associates a post with the authenticated user.
(app *App, w http.ResponseWriter, r *http.Request)
| 948 | |
| 949 | // addPost associates a post with the authenticated user. |
| 950 | func addPost(app *App, w http.ResponseWriter, r *http.Request) error { |
| 951 | var ownerID int64 |
| 952 | |
| 953 | // Authenticate user |
| 954 | at := r.Header.Get("Authorization") |
| 955 | if at != "" { |
| 956 | ownerID = app.db.GetUserID(at) |
| 957 | if ownerID == -1 { |
| 958 | return ErrBadAccessToken |
| 959 | } |
| 960 | } else { |
| 961 | u := getUserSession(app, r) |
| 962 | if u == nil { |
| 963 | return ErrNotLoggedIn |
| 964 | } |
| 965 | ownerID = u.ID |
| 966 | } |
| 967 | |
| 968 | silenced, err := app.db.IsUserSilenced(ownerID) |
| 969 | if err != nil { |
| 970 | log.Error("add post: %v", err) |
| 971 | } |
| 972 | if silenced { |
| 973 | return ErrUserSilenced |
| 974 | } |
| 975 | |
| 976 | // Parse claimed posts in format: |
| 977 | // [{"id": "...", "token": "..."}] |
| 978 | var claims *[]ClaimPostRequest |
| 979 | decoder := json.NewDecoder(r.Body) |
| 980 | err = decoder.Decode(&claims) |
| 981 | if err != nil { |
| 982 | return ErrBadJSONArray |
| 983 | } |
| 984 | |
| 985 | vars := mux.Vars(r) |
| 986 | collAlias := vars["alias"] |
| 987 | |
| 988 | // Update all given posts |
| 989 | res, err := app.db.ClaimPosts(app.cfg, ownerID, collAlias, claims) |
| 990 | if err != nil { |
| 991 | return err |
| 992 | } |
| 993 | |
| 994 | for _, pRes := range *res { |
| 995 | if pRes.Code != http.StatusOK { |
| 996 | continue |
| 997 | } |
| 998 | if !app.cfg.App.Private && app.cfg.App.Federation { |
| 999 | if !pRes.Post.Created.After(time.Now()) { |
| 1000 | pRes.Post.Collection.hostName = app.cfg.App.Host |
| 1001 | go federatePost(app, pRes.Post, pRes.Post.Collection.ID, false) |
| 1002 | } |
| 1003 | } |
| 1004 | if app.cfg.Email.Enabled() && pRes.Post.Collection.EmailSubsEnabled() { |
| 1005 | go app.db.InsertJob(&PostJob{ |
| 1006 | PostID: pRes.Post.ID, |
| 1007 | Action: "email", |
nothing calls this directly
no test coverage detected