-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatomrepository.ts
100 lines (88 loc) · 3.5 KB
/
atomrepository.ts
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
98
99
100
import { NDKArticle, NDKEvent, NDKUser } from "@nostr-dev-kit/ndk";
import { NostrRepository } from "./nostrrepo.ts";
import { Feed } from "feed";
import * as nostr from "npm:nostr-tools";
import { passes_reply, text_filter } from "./filters.ts";
class AtomRepository {
private nostrRepo: NostrRepository;
private pendingEvents: Set<NDKEvent>;
constructor(nostrRepo: NostrRepository) {
this.nostrRepo = nostrRepo;
}
getFeedPromise(filter): Promise<Feed> {
const p: Promise<Feed> = new Promise((resolve, reject) => {
//Tells to nostr repo to start fetchin events, and tell when it stopped.
//All fetched events will be slowly added to a place specified here
const nostrEvents: Set<NDKEvent> = new Set<NDKEvent>();
console.log(`ATOM repo: filter is: ${filter.userids}`);
const nostrEventsOpenEded = this.nostrRepo.getOpenEndedEvents(
filter,
nostrEvents,
);
//When all events are avaiable, then create a feed (no need to optimiye further, because from here on its pretty quick)
nostrEventsOpenEded.then(() => {
const eventListFiltered = Array.from(nostrEvents).filter((event) =>
passes_reply(event.tags, filter.replies) &&
text_filter(event.content, filter.whitelist, filter.blacklist)
);
const pass_ratio: number = eventListFiltered.length / nostrEvents.size;
console.log(
`Userlist: ${pass_ratio * 100} pct of events passed filters`,
);
const atomFeed = AtomRepository.createAtomFeed(eventListFiltered, []);
resolve(atomFeed);
});
});
return p;
}
static createAtomFeed(events: NDKEvent[], ndkUsers: NDKUser[]): Feed {
const feed = new Feed({
title: `Nostr RSS feed: ${
(ndkUsers.map((user) => user.profile?.name)).join(", ")
}`,
description: "Creates RSS feed from nostr",
id: "github.com/gustavonmartins/nostr-to-rss",
link: "github.com/gustavonmartins/nostr-to-rss",
updated: new Date(),
ttl: 1440,
copyright:
"https://njump.me/npub1auwq2edy2tahk58uepwyvjjmdvkxdvmrv492xts8m2s030gla0msruxp7s",
});
function resumeString(inputString: string): string {
return inputString.substring(0, 75) + " (...)";
}
function resumenpub(npub: string): string {
const firstPart = npub.slice(4, 8);
const lastPart = npub.slice(-4);
return `${firstPart}:${lastPart}`;
}
console.log(`Atom feed will have ${events.length} events`);
for (const event of events) {
if (event.kind === 30023) {
//const result2 = event.tags.find(subList => subList[0] === "summary");
feed.addItem({
title: NDKArticle.from(event).title,
date: new Date(event.created_at * 1000),
published: new Date(event.created_at * 1000),
id: event.id,
link: `https://njump.me/${event.id}`,
content: event.content,
author: [{ name: resumenpub(nostr.nip19.npubEncode(event.pubkey)) }],
});
} else {
//const result2 = event.tags.find(subList => subList[0] === "summary");
feed.addItem({
title: resumeString(event.content),
date: new Date(event.created_at * 1000),
published: new Date(event.created_at * 1000),
id: event.id,
link: `https://njump.me/${event.id}`,
content: event.content,
author: [{ name: resumenpub(nostr.nip19.npubEncode(event.pubkey)) }],
});
}
}
return feed;
}
}
export { AtomRepository };