2018-12-24 18:45:15 +01:00
|
|
|
/*
|
2022-11-11 05:49:16 +01:00
|
|
|
* Copyright © 2018-2021 Musing Studio LLC.
|
2018-12-24 18:45:15 +01:00
|
|
|
*
|
|
|
|
* This file is part of WriteFreely.
|
|
|
|
*
|
|
|
|
* WriteFreely is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, included
|
|
|
|
* in the LICENSE file in this source code package.
|
|
|
|
*/
|
2018-12-31 07:05:26 +01:00
|
|
|
|
2018-11-08 07:19:03 +01:00
|
|
|
package writefreely
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-08-28 21:37:45 +02:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2021-09-29 18:48:50 +02:00
|
|
|
"github.com/gorilla/feeds"
|
2018-11-08 07:19:03 +01:00
|
|
|
"github.com/gorilla/mux"
|
2021-06-25 17:16:03 +02:00
|
|
|
stripmd "github.com/writeas/go-strip-markdown/v2"
|
2018-11-08 07:19:03 +01:00
|
|
|
"github.com/writeas/web-core/log"
|
|
|
|
)
|
|
|
|
|
2019-05-12 22:55:30 +02:00
|
|
|
func ViewFeed(app *App, w http.ResponseWriter, req *http.Request) error {
|
2018-11-08 07:19:03 +01:00
|
|
|
alias := collectionAliasFromReq(req)
|
|
|
|
|
|
|
|
// Display collection if this is a collection
|
|
|
|
var c *Collection
|
|
|
|
var err error
|
|
|
|
if app.cfg.App.SingleUser {
|
|
|
|
c, err = app.db.GetCollectionByID(1)
|
2018-11-18 03:51:14 +01:00
|
|
|
} else {
|
|
|
|
c, err = app.db.GetCollection(alias)
|
2018-11-08 07:19:03 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-28 21:37:45 +02:00
|
|
|
|
2019-11-12 00:21:45 +01:00
|
|
|
silenced, err := app.db.IsUserSilenced(c.OwnerID)
|
2019-08-28 21:37:45 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error("view feed: get user: %v", err)
|
|
|
|
return ErrInternalGeneral
|
|
|
|
}
|
2019-11-12 00:21:45 +01:00
|
|
|
if silenced {
|
2019-08-28 21:37:45 +02:00
|
|
|
return ErrCollectionNotFound
|
|
|
|
}
|
2019-06-15 00:54:04 +02:00
|
|
|
c.hostName = app.cfg.App.Host
|
2018-11-08 07:19:03 +01:00
|
|
|
|
|
|
|
if c.IsPrivate() || c.IsProtected() {
|
|
|
|
return ErrCollectionNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch extra data about the Collection
|
|
|
|
// TODO: refactor out this logic, shared in collection.go:fetchCollection()
|
|
|
|
coll := &DisplayCollection{CollectionObj: &CollectionObj{Collection: *c}}
|
|
|
|
if c.PublicOwner {
|
|
|
|
u, err := app.db.GetUserByID(coll.OwnerID)
|
|
|
|
if err != nil {
|
|
|
|
// Log the error and just continue
|
|
|
|
log.Error("Error getting user for collection: %v", err)
|
|
|
|
} else {
|
|
|
|
coll.Owner = u
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tag := mux.Vars(req)["tag"]
|
|
|
|
if tag != "" {
|
2019-08-07 15:26:07 +02:00
|
|
|
coll.Posts, _ = app.db.GetPostsTagged(app.cfg, c, tag, 1, false)
|
2018-11-08 07:19:03 +01:00
|
|
|
} else {
|
2019-08-07 15:26:07 +02:00
|
|
|
coll.Posts, _ = app.db.GetPosts(app.cfg, c, 1, false, true, false)
|
2018-11-08 07:19:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
author := ""
|
|
|
|
if coll.Owner != nil {
|
|
|
|
author = coll.Owner.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
collectionTitle := coll.DisplayTitle()
|
|
|
|
if tag != "" {
|
|
|
|
collectionTitle = tag + " — " + collectionTitle
|
|
|
|
}
|
|
|
|
|
|
|
|
baseUrl := coll.CanonicalURL()
|
|
|
|
basePermalinkUrl := baseUrl
|
|
|
|
siteURL := baseUrl
|
|
|
|
if tag != "" {
|
|
|
|
siteURL += "tag:" + tag
|
|
|
|
}
|
|
|
|
|
2021-09-29 18:48:50 +02:00
|
|
|
feed := &feeds.Feed{
|
2018-11-08 07:19:03 +01:00
|
|
|
Title: collectionTitle,
|
2021-09-29 18:48:50 +02:00
|
|
|
Link: &feeds.Link{Href: siteURL},
|
2018-11-08 07:19:03 +01:00
|
|
|
Description: coll.Description,
|
2021-09-29 18:48:50 +02:00
|
|
|
Author: &feeds.Author{author, ""},
|
2018-11-08 07:19:03 +01:00
|
|
|
Created: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
var title, permalink string
|
|
|
|
for _, p := range *coll.Posts {
|
2021-06-07 21:52:24 +02:00
|
|
|
// Add necessary path back to the web browser for Web Monetization if needed
|
|
|
|
p.Collection = coll.CollectionObj // augmentReadingDestination requires a populated Collection field
|
|
|
|
p.augmentReadingDestination()
|
|
|
|
// Create the item for the feed
|
2018-11-08 07:19:03 +01:00
|
|
|
title = p.PlainDisplayTitle()
|
|
|
|
permalink = fmt.Sprintf("%s%s", baseUrl, p.Slug.String)
|
2021-09-29 18:48:50 +02:00
|
|
|
feed.Items = append(feed.Items, &feeds.Item{
|
2018-11-08 07:19:03 +01:00
|
|
|
Id: fmt.Sprintf("%s%s", basePermalinkUrl, p.Slug.String),
|
|
|
|
Title: title,
|
2021-09-29 18:48:50 +02:00
|
|
|
Link: &feeds.Link{Href: permalink},
|
2018-11-08 07:19:03 +01:00
|
|
|
Description: "<![CDATA[" + stripmd.Strip(p.Content) + "]]>",
|
2020-12-15 15:33:14 +01:00
|
|
|
Content: string(p.HTMLContent),
|
2021-09-29 18:48:50 +02:00
|
|
|
Author: &feeds.Author{author, ""},
|
2018-11-08 07:19:03 +01:00
|
|
|
Created: p.Created,
|
|
|
|
Updated: p.Updated,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
rss, err := feed.ToRss()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprint(w, rss)
|
|
|
|
return nil
|
|
|
|
}
|