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.
 
 
 
 
 

122 lines
2.8 KiB

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