A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 

101 satır
2.3 KiB

  1. package writefreely
  2. import (
  3. "fmt"
  4. . "github.com/gorilla/feeds"
  5. "github.com/gorilla/mux"
  6. stripmd "github.com/writeas/go-strip-markdown"
  7. "github.com/writeas/web-core/log"
  8. "net/http"
  9. "time"
  10. )
  11. func ViewFeed(app *app, w http.ResponseWriter, req *http.Request) error {
  12. alias := collectionAliasFromReq(req)
  13. // Display collection if this is a collection
  14. var c *Collection
  15. var err error
  16. if app.cfg.App.SingleUser {
  17. c, err = app.db.GetCollectionByID(1)
  18. } else {
  19. c, err = app.db.GetCollection(alias)
  20. }
  21. if err != nil {
  22. return nil
  23. }
  24. if c.IsPrivate() || c.IsProtected() {
  25. return ErrCollectionNotFound
  26. }
  27. // Fetch extra data about the Collection
  28. // TODO: refactor out this logic, shared in collection.go:fetchCollection()
  29. coll := &DisplayCollection{CollectionObj: &CollectionObj{Collection: *c}}
  30. if c.PublicOwner {
  31. u, err := app.db.GetUserByID(coll.OwnerID)
  32. if err != nil {
  33. // Log the error and just continue
  34. log.Error("Error getting user for collection: %v", err)
  35. } else {
  36. coll.Owner = u
  37. }
  38. }
  39. tag := mux.Vars(req)["tag"]
  40. if tag != "" {
  41. coll.Posts, _ = app.db.GetPostsTagged(c, tag, 1, false)
  42. } else {
  43. coll.Posts, _ = app.db.GetPosts(c, 1, false, true)
  44. }
  45. author := ""
  46. if coll.Owner != nil {
  47. author = coll.Owner.Username
  48. }
  49. collectionTitle := coll.DisplayTitle()
  50. if tag != "" {
  51. collectionTitle = tag + " — " + collectionTitle
  52. }
  53. baseUrl := coll.CanonicalURL()
  54. basePermalinkUrl := baseUrl
  55. siteURL := baseUrl
  56. if tag != "" {
  57. siteURL += "tag:" + tag
  58. }
  59. feed := &Feed{
  60. Title: collectionTitle,
  61. Link: &Link{Href: siteURL},
  62. Description: coll.Description,
  63. Author: &Author{author, ""},
  64. Created: time.Now(),
  65. }
  66. var title, permalink string
  67. for _, p := range *coll.Posts {
  68. title = p.PlainDisplayTitle()
  69. permalink = fmt.Sprintf("%s%s", baseUrl, p.Slug.String)
  70. feed.Items = append(feed.Items, &Item{
  71. Id: fmt.Sprintf("%s%s", basePermalinkUrl, p.Slug.String),
  72. Title: title,
  73. Link: &Link{Href: permalink},
  74. Description: "<![CDATA[" + stripmd.Strip(p.Content) + "]]>",
  75. Content: applyMarkdown([]byte(p.Content)),
  76. Author: &Author{author, ""},
  77. Created: p.Created,
  78. Updated: p.Updated,
  79. })
  80. }
  81. rss, err := feed.ToRss()
  82. if err != nil {
  83. return err
  84. }
  85. fmt.Fprint(w, rss)
  86. return nil
  87. }