A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

174 lines
4.9 KiB

  1. /*
  2. * Copyright © 2018-2019 A Bunch Tell LLC.
  3. *
  4. * This file is part of WriteFreely.
  5. *
  6. * WriteFreely is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. package writefreely
  11. import (
  12. "net/http"
  13. "strings"
  14. "github.com/gorilla/mux"
  15. "github.com/writeas/impart"
  16. "github.com/writeas/web-core/log"
  17. "github.com/writeas/writefreely/page"
  18. )
  19. func handleViewPad(app *App, w http.ResponseWriter, r *http.Request) error {
  20. vars := mux.Vars(r)
  21. action := vars["action"]
  22. slug := vars["slug"]
  23. collAlias := vars["collection"]
  24. if app.cfg.App.SingleUser {
  25. // TODO: refactor all of this, especially for single-user blogs
  26. c, err := app.db.GetCollectionByID(1)
  27. if err != nil {
  28. return err
  29. }
  30. collAlias = c.Alias
  31. }
  32. appData := &struct {
  33. page.StaticPage
  34. Post *RawPost
  35. User *User
  36. Blogs *[]Collection
  37. Editing bool // True if we're modifying an existing post
  38. EditCollection *Collection // Collection of the post we're editing, if any
  39. }{
  40. StaticPage: pageForReq(app, r),
  41. Post: &RawPost{Font: "norm"},
  42. User: getUserSession(app, r),
  43. }
  44. var err error
  45. if appData.User != nil {
  46. appData.Blogs, err = app.db.GetPublishableCollections(appData.User, app.cfg.App.Host)
  47. if err != nil {
  48. log.Error("Unable to get user's blogs for Pad: %v", err)
  49. }
  50. }
  51. padTmpl := app.cfg.App.Editor
  52. if templates[padTmpl] == nil {
  53. log.Info("No template '%s' found. Falling back to default 'pad' template.", padTmpl)
  54. padTmpl = "pad"
  55. }
  56. if action == "" && slug == "" {
  57. // Not editing any post; simply render the Pad
  58. if err = templates[padTmpl].ExecuteTemplate(w, "pad", appData); err != nil {
  59. log.Error("Unable to execute template: %v", err)
  60. }
  61. return nil
  62. }
  63. // Retrieve post information for editing
  64. appData.Editing = true
  65. // Make sure this isn't cached, so user doesn't accidentally lose data
  66. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  67. w.Header().Set("Expires", "Thu, 04 Oct 1990 20:00:00 GMT")
  68. if slug != "" {
  69. // TODO: refactor all of this, especially for single-user blogs
  70. appData.Post = getRawCollectionPost(app, slug, collAlias)
  71. if appData.Post.OwnerID != appData.User.ID {
  72. // TODO: add ErrForbiddenEditPost message to flashes
  73. return impart.HTTPError{http.StatusFound, r.URL.Path[:strings.LastIndex(r.URL.Path, "/edit")]}
  74. }
  75. appData.EditCollection, err = app.db.GetCollectionForPad(collAlias)
  76. if err != nil {
  77. return err
  78. }
  79. } else {
  80. // Editing a floating article
  81. appData.Post = getRawPost(app, action)
  82. appData.Post.Id = action
  83. }
  84. if appData.Post.Gone {
  85. return ErrPostUnpublished
  86. } else if appData.Post.Found && appData.Post.Content != "" {
  87. // Got the post
  88. } else if appData.Post.Found {
  89. return ErrPostFetchError
  90. } else {
  91. return ErrPostNotFound
  92. }
  93. if err = templates[padTmpl].ExecuteTemplate(w, "pad", appData); err != nil {
  94. log.Error("Unable to execute template: %v", err)
  95. }
  96. return nil
  97. }
  98. func handleViewMeta(app *App, w http.ResponseWriter, r *http.Request) error {
  99. vars := mux.Vars(r)
  100. action := vars["action"]
  101. slug := vars["slug"]
  102. collAlias := vars["collection"]
  103. appData := &struct {
  104. page.StaticPage
  105. Post *RawPost
  106. User *User
  107. EditCollection *Collection // Collection of the post we're editing, if any
  108. Flashes []string
  109. NeedsToken bool
  110. }{
  111. StaticPage: pageForReq(app, r),
  112. Post: &RawPost{Font: "norm"},
  113. User: getUserSession(app, r),
  114. }
  115. var err error
  116. if action == "" && slug == "" {
  117. return ErrPostNotFound
  118. }
  119. // Make sure this isn't cached, so user doesn't accidentally lose data
  120. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  121. w.Header().Set("Expires", "Thu, 28 Jul 1989 12:00:00 GMT")
  122. if slug != "" {
  123. appData.Post = getRawCollectionPost(app, slug, collAlias)
  124. if appData.Post.OwnerID != appData.User.ID {
  125. // TODO: add ErrForbiddenEditPost message to flashes
  126. return impart.HTTPError{http.StatusFound, r.URL.Path[:strings.LastIndex(r.URL.Path, "/meta")]}
  127. }
  128. if app.cfg.App.SingleUser {
  129. // TODO: optimize this query just like we do in GetCollectionForPad (?)
  130. appData.EditCollection, err = app.db.GetCollectionByID(1)
  131. } else {
  132. appData.EditCollection, err = app.db.GetCollectionForPad(collAlias)
  133. }
  134. if err != nil {
  135. return err
  136. }
  137. } else {
  138. // Editing a floating article
  139. appData.Post = getRawPost(app, action)
  140. appData.Post.Id = action
  141. }
  142. appData.NeedsToken = appData.User == nil || appData.User.ID != appData.Post.OwnerID
  143. if appData.Post.Gone {
  144. return ErrPostUnpublished
  145. } else if appData.Post.Found && appData.Post.Content != "" {
  146. // Got the post
  147. } else if appData.Post.Found {
  148. return ErrPostFetchError
  149. } else {
  150. return ErrPostNotFound
  151. }
  152. appData.Flashes, _ = getSessionFlashes(app, w, r, nil)
  153. if err = templates["edit-meta"].ExecuteTemplate(w, "edit-meta", appData); err != nil {
  154. log.Error("Unable to execute template: %v", err)
  155. }
  156. return nil
  157. }