/
syndicator.go
86 lines (69 loc) 路 1.84 KB
/
syndicator.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package syndicator
import (
"fmt"
"github.com/hacdias/eagle/v3/entry"
"github.com/hashicorp/go-multierror"
"github.com/thoas/go-funk"
)
type Syndicator interface {
Syndicate(entry *entry.Entry) (url string, err error)
IsByContext(entry *entry.Entry) bool
Name() string
Identifier() string
}
type Config struct {
UID string
Name string
}
type Manager struct {
syndicators map[string]Syndicator
}
func NewManager() *Manager {
return &Manager{
syndicators: map[string]Syndicator{},
}
}
func (m *Manager) Add(s Syndicator) {
m.syndicators[s.Identifier()] = s
}
func (m *Manager) Syndicate(entry *entry.Entry, syndicators []string) ([]string, error) {
// TODO: detect that this is a reply/like/repost to a post on my own
// website. If so, fetch the syndications to syndicate the replies directly
// there. For example, if I reply to a post on my website that is syndicated
// on Twitter, I will want to syndicate that on Twitter. For now, I have to
// directly reply to the Twitter version.
for id, syndicator := range m.syndicators {
if syndicator.IsByContext(entry) {
syndicators = append(syndicators, id)
}
}
syndicators = funk.UniqString(syndicators)
var (
errors *multierror.Error
syndications []string
)
for _, id := range syndicators {
syndicator, ok := m.syndicators[id]
if !ok {
errors = multierror.Append(errors, fmt.Errorf("unknown syndication service: %s", id))
continue
}
url, err := syndicator.Syndicate(entry)
if err != nil {
errors = multierror.Append(errors, err)
} else {
syndications = append(syndications, url)
}
}
return syndications, errors.ErrorOrNil()
}
func (m *Manager) Config() []*Config {
cfg := []*Config{}
for _, syndicator := range m.syndicators {
cfg = append(cfg, &Config{
UID: syndicator.Identifier(),
Name: syndicator.Name(),
})
}
return cfg
}