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.
 
 
 
 
 

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