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.
 
 
 
 
 

111 lines
2.6 KiB

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