-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.ts
139 lines (122 loc) · 6.06 KB
/
index.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
import { api, logstr, AddToHistoryAction, ErrorStatus, IsVerifiedAction, ReasonExternal, RemoveFromHistoryAction, SoupcanExtensionId, SuccessStatus, DefaultOptions, AddToQueueAction, PopFromQueueAction, IntegrationStateSendAndReceive, IntegrationStateDisabled, IntegrationStateSendOnly } from '../constants';
import { abbreviate, RefId } from '../utilities';
import { AddUserToHistory, AddUserToQueue, CheckDbIsUserLegacyVerified, ConnectDb, PopUserFromQueue, PopulateVerifiedDb, RemoveUserFromHistory } from './db';
api.action.setBadgeBackgroundColor({ color: "#666" });
if (api.action.hasOwnProperty("setBadgeTextColor")) {
// setBadgeTextColor requires chrome 110+
api.action.setBadgeTextColor({ color: "#fff" });
}
// TODO: change to message listener ?
api.storage.local.onChanged.addListener((items) => {
if (items.hasOwnProperty("BlockCounter")) {
api.action.setBadgeText({
text: abbreviate(items.BlockCounter.newValue),
});
}
});
api.storage.sync.get(DefaultOptions).then(async items => {
// set initial extension state
api.action.setIcon({ path: items.suspendedBlockCollection ? "/icon/icon-128-greyscale.png" : "/icon/icon-128.png" });
if (items.skipVerified) {
await PopulateVerifiedDb();
}
});
// populate verified db
api.storage.sync.onChanged.addListener(async items => {
if (
items.hasOwnProperty('skipVerified') &&
items.skipVerified.oldValue === false &&
items.skipVerified.newValue === true
) {
await PopulateVerifiedDb();
}
});
ConnectDb();
api.runtime.onMessage.addListener((m, s, r) => { let response: MessageResponse; (async (message: RuntimeMessage, sender) => {
const refid = RefId();
console.debug(logstr, refid, "recv:", message, sender);
// messages are ALWAYS expected to be:
// 1. objects
// 2. contain a string value stored under message.action. should be one defined above
// other message contents change based on the defined action
try {
switch (message?.action) {
case IsVerifiedAction:
const verifiedMessage = message.data as { user_id: string, handle: string };
const isVerified = await CheckDbIsUserLegacyVerified(verifiedMessage.user_id, verifiedMessage.handle);
response = { status: SuccessStatus, result: isVerified } as SuccessResponse;
break;
case AddToHistoryAction:
const historyMessage = message.data as BlockedUser;
await AddUserToHistory(historyMessage);
response = { status: SuccessStatus, result: null } as SuccessResponse;
break;
case RemoveFromHistoryAction:
const removeMessage = message.data as { user_id: string };
await RemoveUserFromHistory(removeMessage.user_id);
response = { status: SuccessStatus, result: null } as SuccessResponse;
break;
case AddToQueueAction:
const addToQueueMessage = message.data as BlockUser;
await AddUserToQueue(addToQueueMessage);
response = { status: SuccessStatus, result: null } as SuccessResponse;
break;
case PopFromQueueAction:
// no payload with this request
const user = await PopUserFromQueue();
response = { status: SuccessStatus, result: user } as SuccessResponse;
break;
default:
console.error(logstr, refid, "got a message that couldn't be handled from sender:", sender, message);
response = { status: ErrorStatus, message: "unknown action" } as ErrorResponse;
}
} catch (_e) {
const e = _e as Error;
console.error(logstr, refid, "unexpected error caught during", message?.action, "action", e);
response = { status: ErrorStatus, message: e.message ?? "unknown error" } as ErrorResponse;
}
console.debug(logstr, refid, "respond:", response);
})(m, s).finally(() => r(response)); return true });
////////////////////////////////////////////////// EXTERNAL MESSAGE HANDLING //////////////////////////////////////////////////
const [blockActionV1, blockAction] = ["BLOCK", "block_user"];
api.runtime.onMessageExternal.addListener((m, s, r) => { let response: MessageResponse; (async (message, sender) => {
const refid = RefId();
console.debug(logstr, refid, "ext recv:", message, sender);
const integrations = await api.storage.local.get({ soupcanIntegration: false, integrations: { } }) as { [id: string]: Integration };
const senderId = sender.id ?? "";
if (!integrations.hasOwnProperty(senderId)) {
response = { status: ErrorStatus, message: "extension not allowed" } as ErrorResponse;
return;
}
if (integrations[senderId].state === IntegrationStateDisabled || integrations[senderId].state === IntegrationStateSendOnly) {
response = { status: ErrorStatus, message: "extension disabled or not allowed to send messages" } as ErrorResponse;
return;
}
// messages are ALWAYS expected to be:
// 1. objects
// 2. contain a string value stored under message.action. should be one defined above
// other message contents change based on the defined action
try {
switch (message?.action) {
case blockActionV1:
const blockV1Message = message as { user_id: string, name: string, screen_name: string, reason: string };
const userV1: BlockUser = { user_id: blockV1Message.user_id, user: { name: blockV1Message.name, screen_name: blockV1Message.screen_name }, reason: ReasonExternal, external_reason: blockV1Message.reason };
await AddUserToQueue(userV1).catch(() => AddUserToQueue(userV1));
response = { status: SuccessStatus, result: "user queued for blocking" } as SuccessResponse;
break;
case blockAction:
const blockMessage = message.data as BlockUser;
await AddUserToQueue(blockMessage).catch(() => AddUserToQueue(blockMessage));
response = { status: SuccessStatus, result: "user queued for blocking" } as SuccessResponse;
break;
default:
console.error(logstr, refid, "got a message that couldn't be handled from sender:", sender, message);
response = { status: ErrorStatus, message: "unknown action" } as ErrorResponse;
}
} catch (_e) {
const e = _e as Error;
console.error(logstr, refid, "unexpected error caught during", message?.action, "action", e);
response = { status: ErrorStatus, message: e.message ?? "unknown error" } as ErrorResponse;
}
console.debug(logstr, refid, "ext respond:", response);
})(m, s).finally(() => r(response)); return true });