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

154 рядки
4.3 KiB

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