-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
53 lines (50 loc) · 1.35 KB
/
index.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
import * as sdk from "matrix-js-sdk";
import {
sleep,
newRedditPosts,
getNewestRedditPosts,
announceNewRedditPost,
removeMessage,
} from "./lib.js";
function must(x, msg) {
if (!x) {
console.error(msg);
process.exit(1);
}
return x;
}
const clientConfig = {
baseUrl: must(process.env.BASE_URL, "specify BASE_URL"),
accessToken: must(process.env.ACCESS_TOKEN, "specify ACCESS_TOKEN"),
};
const feedInterval = parseInt(process.env.FEED_INTERVAL) || 60 * 1000;
const feedRoom = must(process.env.FEED_ROOM, "specify FEED_ROOM");
const subreddit = must(process.env.SUBREDDIT, "specify SUBREDDIT");
// post.id => [event_id]
const postToEvents = {};
const client = sdk.createClient(clientConfig);
for await (const { deletedPosts, newPosts } of newRedditPosts(
subreddit,
getNewestRedditPosts
)) {
const imagePosts = newPosts.filter((post) => post.post_hint === "image");
for (const post of imagePosts) {
console.log("announcing post", post.permalink);
const { sentMessages } = await announceNewRedditPost(
client,
feedRoom,
post
);
postToEvents[post.id] = sentMessages;
}
for (const post of deletedPosts) {
const events = postToEvents[post.id];
if (!events) {
break;
}
for (const event of events) {
await removeMessage(client, feedRoom, event);
}
}
await sleep(feedInterval);
}