A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

187 рядки
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. 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. } else {
  87. // Editing a floating article
  88. appData.Post = getRawPost(app, action)
  89. appData.Post.Id = action
  90. }
  91. if appData.Post.Gone {
  92. return ErrPostUnpublished
  93. } else if appData.Post.Found && appData.Post.Content != "" {
  94. // Got the post
  95. } else if appData.Post.Found {
  96. return ErrPostFetchError
  97. } else {
  98. return ErrPostNotFound
  99. }
  100. if err = templates[padTmpl].ExecuteTemplate(w, "pad", appData); err != nil {
  101. log.Error("Unable to execute template: %v", err)
  102. }
  103. return nil
  104. }
  105. func handleViewMeta(app *App, w http.ResponseWriter, r *http.Request) error {
  106. vars := mux.Vars(r)
  107. action := vars["action"]
  108. slug := vars["slug"]
  109. collAlias := vars["collection"]
  110. appData := &struct {
  111. page.StaticPage
  112. Post *RawPost
  113. User *User
  114. EditCollection *Collection // Collection of the post we're editing, if any
  115. Flashes []string
  116. NeedsToken bool
  117. Suspended bool
  118. }{
  119. StaticPage: pageForReq(app, r),
  120. Post: &RawPost{Font: "norm"},
  121. User: getUserSession(app, r),
  122. }
  123. var err error
  124. appData.Suspended, err = app.db.IsUserSuspended(appData.User.ID)
  125. if err != nil {
  126. log.Error("view meta: get user suspended status: %v", err)
  127. return ErrInternalGeneral
  128. }
  129. if action == "" && slug == "" {
  130. return ErrPostNotFound
  131. }
  132. // Make sure this isn't cached, so user doesn't accidentally lose data
  133. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  134. w.Header().Set("Expires", "Thu, 28 Jul 1989 12:00:00 GMT")
  135. if slug != "" {
  136. appData.Post = getRawCollectionPost(app, slug, collAlias)
  137. if appData.Post.OwnerID != appData.User.ID {
  138. // TODO: add ErrForbiddenEditPost message to flashes
  139. return impart.HTTPError{http.StatusFound, r.URL.Path[:strings.LastIndex(r.URL.Path, "/meta")]}
  140. }
  141. if app.cfg.App.SingleUser {
  142. // TODO: optimize this query just like we do in GetCollectionForPad (?)
  143. appData.EditCollection, err = app.db.GetCollectionByID(1)
  144. } else {
  145. appData.EditCollection, err = app.db.GetCollectionForPad(collAlias)
  146. }
  147. if err != nil {
  148. return err
  149. }
  150. } else {
  151. // Editing a floating article
  152. appData.Post = getRawPost(app, action)
  153. appData.Post.Id = action
  154. }
  155. appData.NeedsToken = appData.User == nil || appData.User.ID != appData.Post.OwnerID
  156. if appData.Post.Gone {
  157. return ErrPostUnpublished
  158. } else if appData.Post.Found && appData.Post.Content != "" {
  159. // Got the post
  160. } else if appData.Post.Found {
  161. return ErrPostFetchError
  162. } else {
  163. return ErrPostNotFound
  164. }
  165. appData.Flashes, _ = getSessionFlashes(app, w, r, nil)
  166. if err = templates["edit-meta"].ExecuteTemplate(w, "edit-meta", appData); err != nil {
  167. log.Error("Unable to execute template: %v", err)
  168. }
  169. return nil
  170. }