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.
 
 
 
 
 

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