A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

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