2020-08-10 18:50:40 -04:00
|
|
|
package feeds
|
|
|
|
|
|
|
|
import (
|
2020-08-16 17:42:45 -04:00
|
|
|
"crypto/sha256"
|
2020-08-10 18:50:40 -04:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-08-16 17:42:45 -04:00
|
|
|
"mime"
|
2020-08-17 15:33:53 -04:00
|
|
|
urlPkg "net/url"
|
2020-08-10 18:50:40 -04:00
|
|
|
"os"
|
2020-08-16 17:42:45 -04:00
|
|
|
"path"
|
2020-11-17 20:56:15 -05:00
|
|
|
"reflect"
|
2020-08-10 18:50:40 -04:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2020-08-16 17:42:45 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
2020-08-10 18:50:40 -04:00
|
|
|
|
2020-08-16 17:42:45 -04:00
|
|
|
"github.com/makeworld-the-better-one/amfora/client"
|
2020-08-10 18:50:40 -04:00
|
|
|
"github.com/makeworld-the-better-one/amfora/config"
|
2020-11-17 20:56:15 -05:00
|
|
|
"github.com/makeworld-the-better-one/amfora/logger"
|
2020-08-16 17:42:45 -04:00
|
|
|
"github.com/makeworld-the-better-one/go-gemini"
|
2020-08-10 18:50:40 -04:00
|
|
|
"github.com/mmcdole/gofeed"
|
|
|
|
)
|
|
|
|
|
2020-08-17 13:31:45 -04:00
|
|
|
// TODO: Test for deadlocks and whether there should be more
|
|
|
|
// goroutines for file writing or other things.
|
|
|
|
|
2020-08-16 17:42:45 -04:00
|
|
|
var (
|
|
|
|
ErrSaving = errors.New("couldn't save JSON to disk")
|
|
|
|
ErrNotSuccess = errors.New("status 20 not returned")
|
|
|
|
ErrNotFeed = errors.New("not a valid feed")
|
|
|
|
)
|
2020-08-10 18:50:40 -04:00
|
|
|
|
2020-08-28 19:33:37 -04:00
|
|
|
var writeMu = sync.Mutex{} // Prevent concurrent writes to feeds.json file
|
|
|
|
|
|
|
|
// LastUpdated is the time when the in-memory data was last updated.
|
|
|
|
// It can be used to know if the feed page should be regenerated.
|
|
|
|
var LastUpdated time.Time
|
2020-08-10 18:50:40 -04:00
|
|
|
|
|
|
|
// Init should be called after config.Init.
|
|
|
|
func Init() error {
|
2020-11-17 20:56:15 -05:00
|
|
|
f, err := os.Open(config.FeedPath)
|
|
|
|
if err == nil {
|
2020-11-18 16:10:22 -05:00
|
|
|
// File exists and could be opened
|
2020-11-17 20:56:15 -05:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err == nil && fi.Size() > 0 {
|
2020-11-18 16:10:22 -05:00
|
|
|
// File is not empty
|
2020-11-17 20:56:15 -05:00
|
|
|
dec := json.NewDecoder(f)
|
|
|
|
err = dec.Decode(&data)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return fmt.Errorf("feeds.json is corrupted: %w", err) //nolint:goerr113
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
// There's an error opening the file, but it's not bc is doesn't exist
|
|
|
|
return fmt.Errorf("open feeds.json error: %w", err) //nolint:goerr113
|
2020-08-10 18:50:40 -04:00
|
|
|
}
|
2020-08-16 17:42:45 -04:00
|
|
|
|
2020-08-28 19:33:37 -04:00
|
|
|
LastUpdated = time.Now()
|
2020-08-17 15:33:53 -04:00
|
|
|
go updateAll()
|
|
|
|
return nil
|
2020-08-10 18:50:40 -04:00
|
|
|
}
|
|
|
|
|
2020-08-16 17:42:45 -04:00
|
|
|
// IsTracked returns true if the feed/page URL is already being tracked.
|
2020-08-10 18:50:40 -04:00
|
|
|
func IsTracked(url string) bool {
|
2020-11-17 20:56:15 -05:00
|
|
|
logger.Log.Println("feeds.IsTracked called")
|
|
|
|
|
2020-08-16 17:42:45 -04:00
|
|
|
data.feedMu.RLock()
|
2020-08-10 18:50:40 -04:00
|
|
|
for u := range data.Feeds {
|
|
|
|
if url == u {
|
2020-08-16 17:42:45 -04:00
|
|
|
data.feedMu.RUnlock()
|
2020-08-10 18:50:40 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2020-08-16 17:42:45 -04:00
|
|
|
data.feedMu.RUnlock()
|
|
|
|
data.pageMu.RLock()
|
2020-08-10 18:50:40 -04:00
|
|
|
for u := range data.Pages {
|
|
|
|
if url == u {
|
2020-08-16 17:42:45 -04:00
|
|
|
data.pageMu.RUnlock()
|
2020-08-10 18:50:40 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2020-08-16 17:42:45 -04:00
|
|
|
data.pageMu.RUnlock()
|
2020-08-10 18:50:40 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFeed returns a Feed object and a bool indicating whether the passed
|
|
|
|
// content was actually recognized as a feed.
|
|
|
|
func GetFeed(mediatype, filename string, r io.Reader) (*gofeed.Feed, bool) {
|
2020-11-17 20:56:15 -05:00
|
|
|
logger.Log.Println("feeds.GetFeed called")
|
|
|
|
|
2020-08-28 19:33:37 -04:00
|
|
|
if r == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2020-08-10 18:50:40 -04:00
|
|
|
// Check mediatype and filename
|
2020-11-17 11:26:49 -05:00
|
|
|
if mediatype != "application/atom+xml" && mediatype != "application/rss+xml" && mediatype != "application/json+feed" &&
|
|
|
|
filename != "atom.xml" && filename != "feed.xml" && filename != "feed.json" &&
|
2020-08-10 18:50:40 -04:00
|
|
|
!strings.HasSuffix(filename, ".atom") && !strings.HasSuffix(filename, ".rss") {
|
|
|
|
// No part of the above is true
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
feed, err := gofeed.NewParser().Parse(r)
|
2020-08-16 17:42:45 -04:00
|
|
|
if feed == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
2020-08-10 18:50:40 -04:00
|
|
|
return feed, err == nil
|
|
|
|
}
|
|
|
|
|
2020-08-28 12:18:30 -04:00
|
|
|
func writeJSON() error {
|
2020-11-17 20:56:15 -05:00
|
|
|
logger.Log.Println("feeds.writeJSON called")
|
|
|
|
|
2020-08-16 17:42:45 -04:00
|
|
|
writeMu.Lock()
|
|
|
|
defer writeMu.Unlock()
|
|
|
|
|
2020-08-10 18:50:40 -04:00
|
|
|
f, err := os.OpenFile(config.FeedPath, os.O_WRONLY|os.O_CREATE, 0666)
|
|
|
|
if err != nil {
|
2020-11-17 20:56:15 -05:00
|
|
|
logger.Log.Println("feeds.writeJSON error", err)
|
2020-08-10 18:50:40 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
enc := json.NewEncoder(f)
|
|
|
|
enc.SetEscapeHTML(false)
|
|
|
|
enc.SetIndent("", " ")
|
2020-08-16 17:42:45 -04:00
|
|
|
|
2020-08-17 13:31:45 -04:00
|
|
|
data.Lock()
|
2020-11-17 20:56:15 -05:00
|
|
|
logger.Log.Println("feeds.writeJSON acquired data lock")
|
2020-08-10 18:50:40 -04:00
|
|
|
err = enc.Encode(&data)
|
2020-08-17 13:31:45 -04:00
|
|
|
data.Unlock()
|
2020-08-16 17:42:45 -04:00
|
|
|
|
2020-11-17 20:56:15 -05:00
|
|
|
if err != nil {
|
|
|
|
logger.Log.Println("feeds.writeJSON error", err)
|
|
|
|
}
|
|
|
|
|
2020-08-10 18:50:40 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddFeed stores a feed.
|
2020-08-16 17:42:45 -04:00
|
|
|
// It can be used to update a feed for a URL, although the package
|
|
|
|
// will handle that on its own.
|
2020-08-10 18:50:40 -04:00
|
|
|
func AddFeed(url string, feed *gofeed.Feed) error {
|
2020-11-17 20:56:15 -05:00
|
|
|
logger.Log.Println("feeds.AddFeed called")
|
|
|
|
|
2020-08-16 17:42:45 -04:00
|
|
|
if feed == nil {
|
|
|
|
panic("feed is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove any content to save memory and disk space
|
|
|
|
for _, item := range feed.Items {
|
|
|
|
item.Content = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
data.feedMu.Lock()
|
2020-11-17 20:56:15 -05:00
|
|
|
oldFeed, ok := data.Feeds[url]
|
|
|
|
if !ok || !reflect.DeepEqual(feed, oldFeed) {
|
|
|
|
// Feeds are different, or there was never an old one
|
|
|
|
|
2020-11-18 16:10:22 -05:00
|
|
|
LastUpdated = time.Now()
|
2020-11-17 20:56:15 -05:00
|
|
|
data.Feeds[url] = feed
|
|
|
|
data.feedMu.Unlock()
|
|
|
|
err := writeJSON()
|
|
|
|
if err != nil {
|
|
|
|
return ErrSaving
|
|
|
|
}
|
|
|
|
} else {
|
2020-08-16 17:42:45 -04:00
|
|
|
data.feedMu.Unlock()
|
2020-08-10 18:50:40 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-28 19:33:37 -04:00
|
|
|
// AddPage stores a page to track for changes.
|
|
|
|
// It can be used to update the page as well, although the package
|
|
|
|
// will handle that on its own.
|
|
|
|
func AddPage(url string, r io.Reader) error {
|
2020-11-17 20:56:15 -05:00
|
|
|
logger.Log.Println("feeds.AddPage called")
|
|
|
|
|
2020-08-28 19:33:37 -04:00
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
h := sha256.New()
|
|
|
|
if _, err := io.Copy(h, r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newHash := fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
|
2020-08-16 17:42:45 -04:00
|
|
|
data.pageMu.Lock()
|
2020-08-28 19:33:37 -04:00
|
|
|
_, ok := data.Pages[url]
|
|
|
|
if !ok || data.Pages[url].Hash != newHash {
|
|
|
|
// Page content is different, or it didn't exist
|
2020-11-18 16:10:22 -05:00
|
|
|
|
|
|
|
LastUpdated = time.Now()
|
2020-08-28 19:33:37 -04:00
|
|
|
data.Pages[url] = &pageJSON{
|
|
|
|
Hash: newHash,
|
|
|
|
Changed: time.Now().UTC(),
|
|
|
|
}
|
2020-08-16 17:42:45 -04:00
|
|
|
|
|
|
|
data.pageMu.Unlock()
|
2020-11-17 20:56:15 -05:00
|
|
|
err := writeJSON()
|
|
|
|
if err != nil {
|
|
|
|
return ErrSaving
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data.pageMu.Unlock()
|
2020-08-10 18:50:40 -04:00
|
|
|
}
|
2020-08-28 19:33:37 -04:00
|
|
|
|
2020-08-10 18:50:40 -04:00
|
|
|
return nil
|
|
|
|
}
|
2020-08-16 17:42:45 -04:00
|
|
|
|
|
|
|
func updateFeed(url string) error {
|
2020-11-17 20:56:15 -05:00
|
|
|
logger.Log.Println("feeds.updateFeed called")
|
|
|
|
|
2020-08-16 17:42:45 -04:00
|
|
|
res, err := client.Fetch(url)
|
|
|
|
if err != nil {
|
|
|
|
if res != nil {
|
|
|
|
res.Body.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
if res.Status != gemini.StatusSuccess {
|
|
|
|
return ErrNotSuccess
|
|
|
|
}
|
|
|
|
mediatype, _, err := mime.ParseMediaType(res.Meta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
filename := path.Base(url)
|
|
|
|
feed, ok := GetFeed(mediatype, filename, res.Body)
|
|
|
|
if !ok {
|
|
|
|
return ErrNotFeed
|
|
|
|
}
|
|
|
|
return AddFeed(url, feed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updatePage(url string) error {
|
2020-11-17 20:56:15 -05:00
|
|
|
logger.Log.Println("feeds.updatePage called")
|
|
|
|
|
2020-08-16 17:42:45 -04:00
|
|
|
res, err := client.Fetch(url)
|
|
|
|
if err != nil {
|
|
|
|
if res != nil {
|
|
|
|
res.Body.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
if res.Status != gemini.StatusSuccess {
|
|
|
|
return ErrNotSuccess
|
|
|
|
}
|
2020-08-17 13:31:45 -04:00
|
|
|
|
2020-08-28 19:33:37 -04:00
|
|
|
return AddPage(url, res.Body)
|
2020-08-16 17:42:45 -04:00
|
|
|
}
|
|
|
|
|
2020-11-17 11:26:49 -05:00
|
|
|
// updateAll updates all feeds and pages using workers.
|
|
|
|
// It only returns once all the workers are done.
|
2020-08-16 17:42:45 -04:00
|
|
|
func updateAll() {
|
2020-11-17 20:56:15 -05:00
|
|
|
logger.Log.Println("feeds.updateAll called")
|
|
|
|
|
2020-08-17 13:31:45 -04:00
|
|
|
// TODO: Is two goroutines the right amount?
|
|
|
|
|
|
|
|
worker := func(jobs <-chan [2]string, wg *sync.WaitGroup) {
|
2020-08-28 19:33:37 -04:00
|
|
|
// Each job is: [2]string{<type>, "url"}
|
2020-08-17 13:31:45 -04:00
|
|
|
// where <type> is "feed" or "page"
|
|
|
|
|
|
|
|
defer wg.Done()
|
|
|
|
for j := range jobs {
|
|
|
|
if j[0] == "feed" {
|
2020-08-28 12:18:30 -04:00
|
|
|
updateFeed(j[1]) //nolint:errcheck
|
2020-08-28 19:33:37 -04:00
|
|
|
} else if j[0] == "page" {
|
2020-08-28 12:18:30 -04:00
|
|
|
updatePage(j[1]) //nolint:errcheck
|
2020-08-17 13:31:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
data.RLock()
|
|
|
|
numJobs := len(data.Feeds) + len(data.Pages)
|
|
|
|
jobs := make(chan [2]string, numJobs)
|
|
|
|
|
2020-11-17 20:56:15 -05:00
|
|
|
if numJobs == 0 {
|
|
|
|
data.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-17 13:31:45 -04:00
|
|
|
// Start 2 workers, waiting for jobs
|
|
|
|
for w := 0; w < 2; w++ {
|
|
|
|
wg.Add(1)
|
2020-11-17 20:56:15 -05:00
|
|
|
go func(i int) {
|
|
|
|
logger.Log.Println("started worker", i)
|
|
|
|
worker(jobs, &wg)
|
|
|
|
logger.Log.Println("ended worker", i)
|
|
|
|
}(w)
|
2020-08-17 13:31:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get map keys in a slice
|
|
|
|
|
|
|
|
feedKeys := make([]string, len(data.Feeds))
|
|
|
|
i := 0
|
|
|
|
for k := range data.Feeds {
|
|
|
|
feedKeys[i] = k
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
pageKeys := make([]string, len(data.Pages))
|
|
|
|
i = 0
|
|
|
|
for k := range data.Pages {
|
|
|
|
pageKeys[i] = k
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
data.RUnlock()
|
|
|
|
|
|
|
|
for j := 0; j < numJobs; j++ {
|
|
|
|
if j < len(feedKeys) {
|
|
|
|
jobs <- [2]string{"feed", feedKeys[j]}
|
|
|
|
} else {
|
|
|
|
// In the Pages
|
|
|
|
jobs <- [2]string{"page", pageKeys[j-len(feedKeys)]}
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 20:56:15 -05:00
|
|
|
close(jobs)
|
2020-08-16 17:42:45 -04:00
|
|
|
|
2020-08-17 13:31:45 -04:00
|
|
|
wg.Wait()
|
2020-08-16 17:42:45 -04:00
|
|
|
}
|
2020-08-17 15:33:53 -04:00
|
|
|
|
|
|
|
// GetPageEntries returns the current list of PageEntries
|
|
|
|
// for use in rendering a page.
|
2020-08-28 19:33:37 -04:00
|
|
|
// The contents of the returned entries will never change,
|
|
|
|
// so this function needs to be called again to get updates.
|
|
|
|
// It always returns sorted entries - by post time, from newest to oldest.
|
2020-08-17 15:33:53 -04:00
|
|
|
func GetPageEntries() *PageEntries {
|
2020-11-17 20:56:15 -05:00
|
|
|
logger.Log.Println("feeds.GetPageEntries called")
|
|
|
|
|
2020-08-17 15:33:53 -04:00
|
|
|
var pe PageEntries
|
|
|
|
|
|
|
|
data.RLock()
|
|
|
|
|
|
|
|
for _, feed := range data.Feeds {
|
|
|
|
for _, item := range feed.Items {
|
|
|
|
|
|
|
|
var pub time.Time
|
|
|
|
|
2020-08-28 19:33:37 -04:00
|
|
|
// Try to use updated time first, then published
|
|
|
|
|
2020-08-17 15:33:53 -04:00
|
|
|
if !item.UpdatedParsed.IsZero() {
|
|
|
|
pub = *item.UpdatedParsed
|
|
|
|
} else if !item.PublishedParsed.IsZero() {
|
|
|
|
pub = *item.PublishedParsed
|
|
|
|
} else {
|
|
|
|
// No time on the post
|
|
|
|
pub = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
pe.Entries = append(pe.Entries, &PageEntry{
|
|
|
|
Author: feed.Author.Name,
|
|
|
|
Title: item.Title,
|
|
|
|
URL: item.Link,
|
|
|
|
Published: pub,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for url, page := range data.Pages {
|
|
|
|
parsed, _ := urlPkg.Parse(url)
|
|
|
|
pe.Entries = append(pe.Entries, &PageEntry{
|
|
|
|
Author: parsed.Host, // Domain is author
|
|
|
|
Title: path.Base(parsed.Path), // Filename is title
|
|
|
|
URL: url,
|
|
|
|
Published: page.Changed,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
data.RUnlock()
|
|
|
|
|
|
|
|
sort.Sort(&pe)
|
|
|
|
return &pe
|
|
|
|
}
|