2018-12-24 17:45:15 +00:00
|
|
|
/*
|
2019-05-12 20:55:30 +00:00
|
|
|
* Copyright © 2018-2019 A Bunch Tell LLC.
|
2018-12-24 17:45:15 +00:00
|
|
|
*
|
|
|
|
* This file is part of WriteFreely.
|
|
|
|
*
|
|
|
|
* WriteFreely is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, included
|
|
|
|
* in the LICENSE file in this source code package.
|
|
|
|
*/
|
2018-12-31 06:05:26 +00:00
|
|
|
|
2018-11-08 05:11:42 +00:00
|
|
|
package writefreely
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/writeas/impart"
|
|
|
|
"github.com/writeas/web-core/log"
|
|
|
|
"github.com/writeas/writefreely/page"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-05-12 20:55:30 +00:00
|
|
|
func handleViewPad(app *App, w http.ResponseWriter, r *http.Request) error {
|
2018-11-08 05:11:42 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
action := vars["action"]
|
|
|
|
slug := vars["slug"]
|
|
|
|
collAlias := vars["collection"]
|
2018-11-10 03:10:46 +00:00
|
|
|
if app.cfg.App.SingleUser {
|
|
|
|
// TODO: refactor all of this, especially for single-user blogs
|
|
|
|
c, err := app.db.GetCollectionByID(1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
collAlias = c.Alias
|
|
|
|
}
|
2018-11-08 05:11:42 +00:00
|
|
|
appData := &struct {
|
|
|
|
page.StaticPage
|
|
|
|
Post *RawPost
|
|
|
|
User *User
|
|
|
|
Blogs *[]Collection
|
|
|
|
|
|
|
|
Editing bool // True if we're modifying an existing post
|
|
|
|
EditCollection *Collection // Collection of the post we're editing, if any
|
|
|
|
}{
|
|
|
|
StaticPage: pageForReq(app, r),
|
|
|
|
Post: &RawPost{Font: "norm"},
|
|
|
|
User: getUserSession(app, r),
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
if appData.User != nil {
|
|
|
|
appData.Blogs, err = app.db.GetPublishableCollections(appData.User)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to get user's blogs for Pad: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 02:20:30 +00:00
|
|
|
padTmpl := app.cfg.App.Editor
|
|
|
|
if padTmpl == "" {
|
|
|
|
padTmpl = "pad"
|
|
|
|
}
|
2018-11-08 05:11:42 +00:00
|
|
|
|
|
|
|
if action == "" && slug == "" {
|
|
|
|
// Not editing any post; simply render the Pad
|
|
|
|
if err = templates[padTmpl].ExecuteTemplate(w, "pad", appData); err != nil {
|
|
|
|
log.Error("Unable to execute template: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve post information for editing
|
|
|
|
appData.Editing = true
|
|
|
|
// Make sure this isn't cached, so user doesn't accidentally lose data
|
|
|
|
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
|
|
w.Header().Set("Expires", "Thu, 04 Oct 1990 20:00:00 GMT")
|
|
|
|
if slug != "" {
|
2018-11-10 03:10:46 +00:00
|
|
|
// TODO: refactor all of this, especially for single-user blogs
|
2018-11-08 05:11:42 +00:00
|
|
|
appData.Post = getRawCollectionPost(app, slug, collAlias)
|
|
|
|
if appData.Post.OwnerID != appData.User.ID {
|
|
|
|
// TODO: add ErrForbiddenEditPost message to flashes
|
|
|
|
return impart.HTTPError{http.StatusFound, r.URL.Path[:strings.LastIndex(r.URL.Path, "/edit")]}
|
|
|
|
}
|
|
|
|
appData.EditCollection, err = app.db.GetCollectionForPad(collAlias)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Editing a floating article
|
|
|
|
appData.Post = getRawPost(app, action)
|
|
|
|
appData.Post.Id = action
|
|
|
|
}
|
|
|
|
|
|
|
|
if appData.Post.Gone {
|
|
|
|
return ErrPostUnpublished
|
|
|
|
} else if appData.Post.Found && appData.Post.Content != "" {
|
|
|
|
// Got the post
|
|
|
|
} else if appData.Post.Found {
|
|
|
|
return ErrPostFetchError
|
|
|
|
} else {
|
|
|
|
return ErrPostNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = templates[padTmpl].ExecuteTemplate(w, "pad", appData); err != nil {
|
|
|
|
log.Error("Unable to execute template: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-12 20:55:30 +00:00
|
|
|
func handleViewMeta(app *App, w http.ResponseWriter, r *http.Request) error {
|
2018-11-08 05:11:42 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
action := vars["action"]
|
|
|
|
slug := vars["slug"]
|
|
|
|
collAlias := vars["collection"]
|
|
|
|
appData := &struct {
|
|
|
|
page.StaticPage
|
|
|
|
Post *RawPost
|
|
|
|
User *User
|
|
|
|
EditCollection *Collection // Collection of the post we're editing, if any
|
|
|
|
Flashes []string
|
|
|
|
NeedsToken bool
|
|
|
|
}{
|
|
|
|
StaticPage: pageForReq(app, r),
|
|
|
|
Post: &RawPost{Font: "norm"},
|
|
|
|
User: getUserSession(app, r),
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if action == "" && slug == "" {
|
|
|
|
return ErrPostNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure this isn't cached, so user doesn't accidentally lose data
|
|
|
|
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
|
|
w.Header().Set("Expires", "Thu, 28 Jul 1989 12:00:00 GMT")
|
|
|
|
if slug != "" {
|
|
|
|
appData.Post = getRawCollectionPost(app, slug, collAlias)
|
|
|
|
if appData.Post.OwnerID != appData.User.ID {
|
|
|
|
// TODO: add ErrForbiddenEditPost message to flashes
|
|
|
|
return impart.HTTPError{http.StatusFound, r.URL.Path[:strings.LastIndex(r.URL.Path, "/meta")]}
|
|
|
|
}
|
2018-12-01 21:27:14 +00:00
|
|
|
if app.cfg.App.SingleUser {
|
|
|
|
// TODO: optimize this query just like we do in GetCollectionForPad (?)
|
|
|
|
appData.EditCollection, err = app.db.GetCollectionByID(1)
|
|
|
|
} else {
|
|
|
|
appData.EditCollection, err = app.db.GetCollectionForPad(collAlias)
|
|
|
|
}
|
2018-11-08 05:11:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Editing a floating article
|
|
|
|
appData.Post = getRawPost(app, action)
|
|
|
|
appData.Post.Id = action
|
|
|
|
}
|
|
|
|
appData.NeedsToken = appData.User == nil || appData.User.ID != appData.Post.OwnerID
|
|
|
|
|
|
|
|
if appData.Post.Gone {
|
|
|
|
return ErrPostUnpublished
|
|
|
|
} else if appData.Post.Found && appData.Post.Content != "" {
|
|
|
|
// Got the post
|
|
|
|
} else if appData.Post.Found {
|
|
|
|
return ErrPostFetchError
|
|
|
|
} else {
|
|
|
|
return ErrPostNotFound
|
|
|
|
}
|
|
|
|
appData.Flashes, _ = getSessionFlashes(app, w, r, nil)
|
|
|
|
|
|
|
|
if err = templates["edit-meta"].ExecuteTemplate(w, "edit-meta", appData); err != nil {
|
|
|
|
log.Error("Unable to execute template: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|