1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package logic

import (
	"rss-kakapo/dal"
	"strings"
)

type IBlockedFeeds interface {
	IsBlocked(feedUrl string) (bool, error)
}

// blockedFeeds is the list of feeds whose sites have opted out, kept in the
// database (dal.BlockedFeeds) and edited with `rss-kakapo block`. It is looked
// up on every request, so a change applies at once, to a running service too.
//
// A request whose lookup fails fails too, rather than let through a site that
// opted out.
type blockedFeeds struct {
	repo dal.IRepo
}

func NewBlockedFeeds(repo dal.IRepo) IBlockedFeeds {
	return &blockedFeeds{repo}
}

func (bf *blockedFeeds) IsBlocked(feedUrl string) (bool, error) {
	return bf.repo.IsInList(dal.BlockedFeeds, NormaliseFeedUrl(feedUrl))
}

// NormaliseFeedUrl is how a feed URL is kept in, and looked up in, the
// blocklist: without surrounding space, in lower case and without scheme, so
// that http:// and https:// are the same feed.
func NormaliseFeedUrl(s string) string {
	s = strings.ToLower(strings.TrimSpace(s))
	s = strings.TrimPrefix(s, "https://")
	s = strings.TrimPrefix(s, "http://")
	return s
}