-
Notifications
You must be signed in to change notification settings - Fork 1
/
feeds.go
97 lines (77 loc) · 2.57 KB
/
feeds.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
87
88
89
90
91
92
93
94
95
96
97
package mastodon
import (
"github.com/Crossbell-Box/OperatorSync/app/worker/global"
"github.com/Crossbell-Box/OperatorSync/app/worker/types"
"github.com/Crossbell-Box/OperatorSync/app/worker/utils"
commonConsts "github.com/Crossbell-Box/OperatorSync/common/consts"
commonTypes "github.com/Crossbell-Box/OperatorSync/common/types"
commonUtils "github.com/Crossbell-Box/OperatorSync/common/utils"
"regexp"
"strings"
)
var (
customEmojisRegex *regexp.Regexp
)
func init() {
// Medium regex
customEmojisRegex = regexp.MustCompile(`<img[^>]+\bsrc=["']([^"']+)["']`)
}
func Feeds(cccs *types.ConcurrencyChannels, work *commonTypes.WorkDispatched, collectLink string) (
bool, []commonTypes.RawFeed, uint, string,
) {
// Refer to https://eihei.net/@candinya.rss
// Concurrency control
cccs.Direct.Request()
defer cccs.Direct.Done()
global.Logger.Debug("New feeds request for mastodon")
username, instance, err := commonUtils.SplitFediverseUsernameInstance(work.Username)
if err != nil {
return false, nil, commonConsts.ERROR_CODE_INVALID_FORMAT, err.Error()
}
collectLink = strings.Replace(collectLink, "{{instance}}", instance, 1)
collectLink = strings.Replace(collectLink, "{{username}}", username, 1)
rawFeed, errCode, err := utils.RSSFeedRequest(
collectLink,
true,
)
if err != nil {
return false, nil, errCode, err.Error()
}
var feeds []commonTypes.RawFeed
for _, item := range rawFeed.Items {
if item.PublishedParsed.After(work.DropBefore) && item.PublishedParsed.Before(work.DropAfter) {
feed := commonTypes.RawFeed{
Link: item.Link,
GUID: item.GUID,
PublishedAt: *item.PublishedParsed,
}
// Process content: upload custom emojis
rawContent := item.Description
var customEmojis []string
ces := customEmojisRegex.FindAllStringSubmatch(rawContent, -1)
for _, ce := range ces {
customEmojis = append(customEmojis, ce[1])
}
ceUploadeds := utils.UploadAllMedia(customEmojis)
for _, ceu := range ceUploadeds {
rawContent = strings.ReplaceAll(rawContent, ceu.OriginalURI, ceu.IPFSUri)
}
feed.Content = rawContent
// Process medias
if attachedMedias, ok := item.Extensions["media"]["content"]; ok {
// Upload media with order
for _, aMedia := range attachedMedias {
media, err := utils.UploadOneMedia(aMedia.Attrs["url"])
if err != nil {
// Fail to upload, oops
return false, nil, commonConsts.ERROR_CODE_FAILED_TO_UPLOAD, err.Error()
} else {
feed.Media = append(feed.Media, *media)
}
}
}
feeds = append(feeds, feed)
}
}
return true, feeds, 0, ""
}