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.
 
 
 
 
 

189 lines
5.4 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. Suspended bool
  38. Editing bool // True if we're modifying an existing post
  39. EditCollection *Collection // Collection of the post we're editing, if any
  40. }{
  41. StaticPage: pageForReq(app, r),
  42. Post: &RawPost{Font: "norm"},
  43. User: getUserSession(app, r),
  44. }
  45. var err error
  46. if appData.User != nil {
  47. appData.Blogs, err = app.db.GetPublishableCollections(appData.User, app.cfg.App.Host)
  48. if err != nil {
  49. log.Error("Unable to get user's blogs for Pad: %v", err)
  50. }
  51. appData.Suspended, err = app.db.IsUserSuspended(appData.User.ID)
  52. if err != nil {
  53. log.Error("Unable to get users suspension status for Pad: %v", err)
  54. }
  55. }
  56. padTmpl := app.cfg.App.Editor
  57. if templates[padTmpl] == nil {
  58. if padTmpl != "" {
  59. log.Info("No template '%s' found. Falling back to default 'pad' template.", padTmpl)
  60. }
  61. padTmpl = "pad"
  62. }
  63. if action == "" && slug == "" {
  64. // Not editing any post; simply render the Pad
  65. if err = templates[padTmpl].ExecuteTemplate(w, "pad", appData); err != nil {
  66. log.Error("Unable to execute template: %v", err)
  67. }
  68. return nil
  69. }
  70. // Retrieve post information for editing
  71. appData.Editing = true
  72. // Make sure this isn't cached, so user doesn't accidentally lose data
  73. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  74. w.Header().Set("Expires", "Thu, 04 Oct 1990 20:00:00 GMT")
  75. if slug != "" {
  76. // TODO: refactor all of this, especially for single-user blogs
  77. appData.Post = getRawCollectionPost(app, slug, collAlias)
  78. if appData.Post.OwnerID != appData.User.ID {
  79. // TODO: add ErrForbiddenEditPost message to flashes
  80. return impart.HTTPError{http.StatusFound, r.URL.Path[:strings.LastIndex(r.URL.Path, "/edit")]}
  81. }
  82. appData.EditCollection, err = app.db.GetCollectionForPad(collAlias)
  83. if err != nil {
  84. return err
  85. }
  86. appData.EditCollection.hostName = app.cfg.App.Host
  87. } else {
  88. // Editing a floating article
  89. appData.Post = getRawPost(app, action)
  90. appData.Post.Id = action
  91. }
  92. if appData.Post.Gone {
  93. return ErrPostUnpublished
  94. } else if appData.Post.Found && appData.Post.Content != "" {
  95. // Got the post
  96. } else if appData.Post.Found {
  97. return ErrPostFetchError
  98. } else {
  99. return ErrPostNotFound
  100. }
  101. if err = templates[padTmpl].ExecuteTemplate(w, "pad", appData); err != nil {
  102. log.Error("Unable to execute template: %v", err)
  103. }
  104. return nil
  105. }
  106. func handleViewMeta(app *App, w http.ResponseWriter, r *http.Request) error {
  107. vars := mux.Vars(r)
  108. action := vars["action"]
  109. slug := vars["slug"]
  110. collAlias := vars["collection"]
  111. appData := &struct {
  112. page.StaticPage
  113. Post *RawPost
  114. User *User
  115. EditCollection *Collection // Collection of the post we're editing, if any
  116. Flashes []string
  117. NeedsToken bool
  118. Suspended bool
  119. }{
  120. StaticPage: pageForReq(app, r),
  121. Post: &RawPost{Font: "norm"},
  122. User: getUserSession(app, r),
  123. }
  124. var err error
  125. appData.Suspended, err = app.db.IsUserSuspended(appData.User.ID)
  126. if err != nil {
  127. log.Error("view meta: get user suspended status: %v", err)
  128. return ErrInternalGeneral
  129. }
  130. if action == "" && slug == "" {
  131. return ErrPostNotFound
  132. }
  133. // Make sure this isn't cached, so user doesn't accidentally lose data
  134. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  135. w.Header().Set("Expires", "Thu, 28 Jul 1989 12:00:00 GMT")
  136. if slug != "" {
  137. appData.Post = getRawCollectionPost(app, slug, collAlias)
  138. if appData.Post.OwnerID != appData.User.ID {
  139. // TODO: add ErrForbiddenEditPost message to flashes
  140. return impart.HTTPError{http.StatusFound, r.URL.Path[:strings.LastIndex(r.URL.Path, "/meta")]}
  141. }
  142. if app.cfg.App.SingleUser {
  143. // TODO: optimize this query just like we do in GetCollectionForPad (?)
  144. appData.EditCollection, err = app.db.GetCollectionByID(1)
  145. } else {
  146. appData.EditCollection, err = app.db.GetCollectionForPad(collAlias)
  147. }
  148. if err != nil {
  149. return err
  150. }
  151. appData.EditCollection.hostName = app.cfg.App.Host
  152. } else {
  153. // Editing a floating article
  154. appData.Post = getRawPost(app, action)
  155. appData.Post.Id = action
  156. }
  157. appData.NeedsToken = appData.User == nil || appData.User.ID != appData.Post.OwnerID
  158. if appData.Post.Gone {
  159. return ErrPostUnpublished
  160. } else if appData.Post.Found && appData.Post.Content != "" {
  161. // Got the post
  162. } else if appData.Post.Found {
  163. return ErrPostFetchError
  164. } else {
  165. return ErrPostNotFound
  166. }
  167. appData.Flashes, _ = getSessionFlashes(app, w, r, nil)
  168. if err = templates["edit-meta"].ExecuteTemplate(w, "edit-meta", appData); err != nil {
  169. log.Error("Unable to execute template: %v", err)
  170. }
  171. return nil
  172. }