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 { load_new_subreddit_posts } from "https://esm.town/v/bnorick/load_new_subreddit_posts";
export let reddit_matches = async ({
username,
last_run_at_utc,
filters,
current_state,
fetch_limit,
enable_toggle,
force,
}) => {
let matches = {};
let new_state = current_state ? current_state : {};
current_state = current_state || {};
// TODO: handle >10 subreddits by raising and exception?
let fetch_budget = Math.floor(fetch_limit / Object.keys(filters).length);
let additional_fetches_required = {};
let fetch_total = 0;
for (const subreddit in filters) {
console.log(`processing subreddit ${subreddit}`);
let subreddit_filters = filters[subreddit];
if (!subreddit_filters) {
console.log(`no patterns for subreddit ${subreddit}, skipping`);
continue;
}
let state = current_state[subreddit] ? current_state[subreddit] : {};
if (state.disabled) {
if (!force) {
console.log(`subreddit ${subreddit} is disabled by state, skipping`);
continue;
} else {
console.log(
`subreddit ${subreddit} is disabled by state, processing because options.force == true`
);
}
}
let result = await load_new_subreddit_posts({
subreddit: subreddit,
filters: subreddit_filters,
after_utc: last_run_at_utc,
fetch_limit: fetch_budget,
});
fetch_total += result.fetches;
if (result.next_fetch_from) {
additional_fetches_required[subreddit] = result.next_fetch_from;
}
let extra_message = additional_fetches_required[subreddit]
? `, reached fetch limit (${fetch_budget}), loading more posts after other subreddits if possible`
: "";
console.log(`processed ${result.new_posts} posts${extra_message}`);
new_state[subreddit] = {
disabled: state.disabled,
last_run_at_utc: last_run_at_utc,
};
if (result.matches.length) {
matches[subreddit] = result.matches;
}
}
// TODO: handle additional_fetches_required
let text = [];
let total_matches = 0;
let first = true;
for (let subreddit in matches) {
if (!first) {
text.push("\n\n");
}
text.push(`${subreddit}`);
text.push(`found ${matches[subreddit].length} matches`);
if (enable_toggle) {
text.push(
`toggle: https://api.val.town/eval/@${username}.toggleSubreddits(%22${subreddit}%22)`
);
}
text.push("");
for (let match of matches[subreddit]) {
text.push(`${match.title}\n${match.url}\n`);
}
total_matches += matches[subreddit].length;
first = false;
}
return {
state: new_state,
matches: matches,
email: text.length
? {
text: text.join("\n"),
subject: `Reddit Notifier (${total_matches} matches)`,
1
Next