/
watcher.ts
146 lines (131 loc) · 4.08 KB
/
watcher.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import $ from "jquery";
import { getThreadJSON } from "../api/threads";
import { currentThread } from "../postinfo";
import { getJsonStorageVal, getNumberStorageVal, setStorageVal } from "../storage";
import "./menu";
const subjectCuttoff = 24;
let watcherInterval = -1; // eslint-disable-line prefer-const
export function updateWatchedThreads() {
const watched = getJsonStorageVal<any>("watched", {});
const boards = Object.keys(watched);
const currentPage = currentThread();
for(const board of boards) {
if(!(watched[board] instanceof Array)) {
console.error(`Invalid data for board ${board}: expected Array object, deleting.`);
delete watched[board];
continue;
}
$(document).trigger("beginNewPostsCheck");
for(const t in watched[board]) {
const thread = watched[board][t];
if(thread.err !== undefined) continue;
getThreadJSON(thread.id, board).then(data => {
if(data.posts.length > thread.posts) {
// watched thread has new posts, trigger a menu update
if(currentPage.board === board && currentPage.id === thread.id) {
// we're currently in the thread, update the cookie
watched[board][t].posts = data.posts.length;
watched[board][t].latest = data.posts[data.posts.length - 1].no;
setStorageVal("watched", watched, true);
}
$(document).trigger("watcherNewPosts", {
newPosts: data.posts.slice(thread.posts),
newNumPosts: data.posts.length,
op: thread.id,
board: board
});
}
}).catch(e => {
if(e.status === 404) {
watched[board][t].err = e.statusText;
setStorageVal("watched", watched, true);
}
});
}
}
}
export interface WatchedThreadsListJSON {
[board: string]: WatchedThreadJSON[]
}
export interface WatchedThreadJSON {
id: number;
board?: string;
posts?: number;
op?: string;
latest?: string;
subject?: string;
newNumPosts?: number;
err?: string;
newPosts?: any[];
}
export function isThreadWatched(threadID: number, board: string) {
const watched = getJsonStorageVal<WatchedThreadsListJSON>("watched", {});
const threads = watched[board];
if(threads === undefined) return false;
for(const thread of threads) {
if(thread.id === threadID) return true;
}
return false;
}
export function watchThread(threadID: string|number, board: string) {
const watched = getJsonStorageVal<WatchedThreadsListJSON>("watched", {});
if(typeof threadID === "string") {
threadID = parseInt(threadID);
}
if(!(watched[board] instanceof Array))
watched[board] = [];
for(const t in watched[board]) {
let thread = watched[board][t];
if(typeof thread === "number") {
thread = watched[board][t] = {id: thread};
}
if(thread.id === threadID) return; // thread is already in the watched list
}
getThreadJSON(threadID, board).then(data => {
const op = data.posts[0];
const threadObj: WatchedThreadJSON = {
id: threadID as number,
board: board,
posts: data.posts.length,
op: op.name,
latest: data.posts[data.posts.length-1].no
};
if(op.trip !== "") threadObj.op += "!" + op.trip;
if(op.sub !== "") {
if(op.sub.length > subjectCuttoff)
threadObj.subject = op.sub.slice(0, subjectCuttoff) + "...";
else
threadObj.subject = op.sub;
}
watched[board].push(threadObj);
setStorageVal("watched", watched, true);
$(document).trigger("watchThread", threadObj);
});
}
export function unwatchThread(threadID: number, board: string) {
const watched = getJsonStorageVal<WatchedThreadsListJSON>("watched", {});
if(!(watched[board] instanceof Array))
return;
for(const i in watched[board]) {
if(watched[board][i].id === threadID) {
watched[board].splice(i as any, 1);
setStorageVal("watched", watched, true);
$(document).trigger("unwatchThread", threadID);
return;
}
}
}
export function stopThreadWatcher() {
clearInterval(watcherInterval);
}
export function resetThreadWatcherInterval() {
stopThreadWatcher();
(watcherInterval as unknown as NodeJS.Timer) = setInterval(
updateWatchedThreads,
getNumberStorageVal("watcherseconds", 10) * 1000);
}
export function initWatcher() {
updateWatchedThreads();
resetThreadWatcherInterval();
}
$(initWatcher);