-
Notifications
You must be signed in to change notification settings - Fork 0
/
activityFeed.js
94 lines (82 loc) · 2.31 KB
/
activityFeed.js
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
import { YouTubeUserActivity } from "./src/YouTubeUserActivity.js";
import { AtomActivity } from "./src/AtomActivity.js";
import { PixelfedActivity } from "./src/PixelfedActivity.js";
import { RssActivity } from "./src/RssActivity.js";
import pluginRss from "@11ty/eleventy-plugin-rss";
class ActivityFeed {
constructor() {
this.sources = [];
}
setCacheDuration(duration) {
this.cacheDuration = duration;
}
addSource(type, label, ...args) {
let cls;
if(type === "youtubeUser") {
cls = YouTubeUserActivity;
} else if (type === 'pixelfed') {
cls = PixelfedActivity;
} else if(type === "atom") {
cls = AtomActivity;
} else if(type === "rss") {
cls = RssActivity;
} else {
throw new Error(`${type} is not a supported activity type for addSource`);
}
let source = new cls(...args);
source.setLabel(label);
if(this.cacheDuration) {
source.setCacheDuration(this.cacheDuration);
}
this.sources.push(source);
}
async getEntries() {
let entries = [];
for(let source of this.sources) {
entries = [
...entries,
...await source.getEntries(),
]
}
entries = entries.map(entry => {
entry.published = new Date(Date.parse(entry.published));
return entry;
});
return entries.sort((a, b) => {
if(a.published < b.published) {
return 1;
}
if(a.published > b.published) {
return -1;
}
return 0;
});
}
async toRssFeed(metadata) {
let entries = await this.getEntries();
let url = metadata.url?.home || metadata.url;
let feedUrl = metadata.url?.feed || url;
return `<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xml:base="${url}" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>${metadata.title}</title>
<link>${url}</link>
<atom:link href="${feedUrl}" rel="self" type="application/rss+xml" />
<description>${metadata.subtitle}</description>
<language>${metadata.language}</language>
${entries.map(entry => {
return `
<item>
<title>${entry.title}</title>
<link>${entry.url}</link>
<description><![CDATA[${entry.content || ""}]]></description>
<pubDate>${pluginRss.dateToRfc822(entry.published)}</pubDate>
<dc:creator>${entry.author.name}</dc:creator>
<guid>${entry.url}</guid>
</item>`;
}).join("\n")}
</channel>
</rss>`
}
}
export {ActivityFeed};