forked from jtgi/automod
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcast-content.ts
474 lines (431 loc) · 13.4 KB
/
cast-content.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import { Cast, CastId } from "@neynar/nodejs-sdk/build/neynar-api/v2";
import RE2 from "re2";
import emojiRegex from "emoji-regex";
import { detect } from "tinyld";
import { CheckFunction, CheckFunctionArgs, Rule, RuleDefinition } from "~/rules/rules.type";
import mimeType from "mime-types";
import { languages } from "~/lib/languages";
import { neynar } from "~/lib/neynar.server";
function tryParseUrl(url: string) {
try {
return new URL(url);
} catch (e) {
return undefined;
}
}
// Rule: contains text, option to ignore case
function containsText(args: CheckFunctionArgs) {
const { cast, rule } = args as { cast: Cast; rule: Rule };
const { searchText, caseSensitive } = rule.args;
const text = caseSensitive ? cast.text : cast.text.toLowerCase();
const search = caseSensitive ? searchText : searchText.toLowerCase();
const result = text.includes(search);
return {
result,
message: result ? `Cast contains "${searchText}"` : `Cast does not contain "${searchText}"`,
};
}
async function containsEmbeds(args: CheckFunctionArgs) {
const { cast, rule } = args as { cast: Cast; rule: Rule };
const { images, videos, frames, links, casts, domain } = rule.args;
const checkForEmbeds: string[] = [];
images && checkForEmbeds.push("image");
videos && checkForEmbeds.push("video");
frames && checkForEmbeds.push("frame");
links && checkForEmbeds.push("link");
casts && checkForEmbeds.push("casts");
let embedsFound: string[] = [];
const embedTypesFound: string[] = [];
const foundCasts = cast.embeds.filter((embed): embed is { cast_id: CastId } => {
return "cast_id" in embed;
});
if (foundCasts.length > 0) {
embedTypesFound.push("casts");
embedsFound = embedsFound.concat(foundCasts.map((c) => c.cast_id.hash));
}
const knownImageCdnHostnames = ["imagedelivery.net", "imgur.com"];
// even if not specified in args we always search for
// images and videos because they may only be filtering
// for `link` embeds in which case these need to be
// ruled out. its also free and fast.
const foundImages = cast.embeds.filter((embed): embed is { url: string } => {
if ("url" in embed) {
const url = tryParseUrl(embed.url);
if (!url) {
return false;
}
if (domain && !embed.url.includes(domain)) {
return false;
}
const mime = mimeType.lookup(embed.url);
return (mime && mime.startsWith("image")) || knownImageCdnHostnames.includes(url.hostname);
} else {
return false;
}
});
if (foundImages.length > 0) {
embedTypesFound.push("image");
embedsFound = embedsFound.concat(foundImages.map((i) => i.url));
}
const foundVideos = cast.embeds.filter((embed): embed is { url: string } => {
if ("url" in embed) {
if (domain && !embed.url.includes(domain)) {
return false;
}
const mime = mimeType.lookup(embed.url);
return !!mime && (mime.startsWith("video") || mime.startsWith("application/vnd.apple.mpegurl"));
} else {
return false;
}
});
if (foundVideos.length > 0) {
embedTypesFound.push("video");
embedsFound = embedsFound.concat(foundVideos.map((i) => i.url));
}
// if either is specified we need to fetch the cast
// since a frame_url looks just like a link url.
// its debatable whether you'd ever want to filter
// on this but so it goes..
if (links || frames) {
const rsp = await neynar.fetchBulkCasts([cast.hash]);
const castWithInteractions = rsp.result.casts[0];
if (!castWithInteractions) {
throw new Error(`Cast not found. Should be impossible. hash: ${cast.hash}`);
}
if (castWithInteractions.frames && castWithInteractions.frames.length > 0) {
let frames = [...castWithInteractions.frames];
if (domain) {
frames = frames.filter((f) => f.frames_url.includes(domain));
}
if (frames.length > 0) {
embedTypesFound.push("frame");
embedsFound = embedsFound.concat(frames.map((f) => f.frames_url) || []);
}
}
const remainingUrls = castWithInteractions.embeds.filter((e): e is { url: string } => {
if ("url" in e) {
if (domain && !e.url.includes(domain)) {
return false;
}
return !embedsFound.includes(e.url);
} else {
return false;
}
});
if (remainingUrls.length > 0) {
embedTypesFound.push("link");
embedsFound = embedsFound.concat(remainingUrls.map((i) => i.url));
}
}
const violatingEmbeds = checkForEmbeds.filter((embedType) => embedTypesFound.includes(embedType));
const result = violatingEmbeds.length > 0;
const domainMessage = domain ? ` from ${domain}` : "";
return {
result,
message: result
? `Cast contains ${violatingEmbeds.join(", ")}` + domainMessage
: `Cast doesn't contain ${checkForEmbeds.join(", ")}` + domainMessage,
};
}
function castLength(args: CheckFunctionArgs) {
const { cast, rule } = args as { cast: Cast; rule: Rule };
const { min, max } = rule.args as { min?: number; max?: number };
if (min) {
if (cast.text.length > min) {
return {
result: false,
message: `Cast is greater than ${min} characters`,
};
}
}
if (max) {
if (cast.text.length < max) {
return {
result: false,
message: `Cast is less than ${max} characters`,
};
}
}
return {
result: true,
message: "Cast is within length limits",
};
}
function textMatchesPattern(args: CheckFunctionArgs) {
const { cast, rule } = args as { cast: Cast; rule: Rule };
const { pattern, caseInsensitive } = rule.args;
const re2 = new RE2(pattern, caseInsensitive ? "i" : "");
const isMatch = re2.test(cast.text);
return {
result: isMatch,
message: isMatch ? `Cast matches pattern ${pattern}` : `Cast does not match pattern ${pattern}`,
};
}
function textMatchesLanguage(args: CheckFunctionArgs) {
const { cast, rule } = args as { cast: Cast; rule: Rule };
const { language } = rule.args;
if (!cast.text.length) {
return {
result: false,
message: "Language detection is not available for empty casts.",
};
}
try {
new URL(cast.text);
return {
result: false,
message: "URLs are not supported for language detection.",
};
} catch (e) {
// not a url
}
const regex = emojiRegex();
const withoutEmojis = cast.text.replaceAll(regex, "");
if (cast.text.length < 20) {
// model not reliable here
return {
result: true,
message: "Language detection is not reliable for short casts.",
};
}
const isLanguage = detect(withoutEmojis, { only: [language] }) !== "";
return {
result: isLanguage,
message: isLanguage ? `Cast is in ${language}` : `Cast is not in ${language}`,
};
}
function containsTooManyMentions(args: CheckFunctionArgs) {
const { cast, rule } = args as { cast: Cast; rule: Rule };
const { maxMentions } = rule.args;
const mentions = cast.text.match(/@\w+/g) || [];
const result = mentions.length > maxMentions;
return {
result,
message: result ? `Too many mentions. Max is ${maxMentions}` : `Mentions are within limits`,
};
}
function containsLinks(args: CheckFunctionArgs) {
const { cast, rule } = args as { cast: Cast; rule: Rule };
const maxLinks = rule.args.maxLinks || 0;
const regex = /https?:\/\/\S+/gi;
const matches = cast.text.match(regex) || [];
const result = matches.length > maxLinks;
return {
result,
message: result ? `Too many links. Max is ${maxLinks}` : `Links are within limits`,
};
}
type RuleName =
| "containsText"
| "containsEmbeds"
| "textMatchesPattern"
| "textMatchesLanguage"
| "castLength"
| "containsTooManyMentions"
| "containsLinks";
export const castContentRulesFunction: Record<RuleName, CheckFunction> = {
containsText: containsText,
containsEmbeds: containsEmbeds,
textMatchesPattern: textMatchesPattern,
textMatchesLanguage: textMatchesLanguage,
castLength: castLength,
containsTooManyMentions: containsTooManyMentions,
containsLinks: containsLinks,
};
export const castContentRulesDefinitions: Record<RuleName, RuleDefinition> = {
containsText: {
name: "containsText",
author: "modbot",
authorUrl: "https://modbot.sh",
authorIcon: `/icons/modbot.png`,
allowMultiple: true,
category: "cast",
friendlyName: "Contains Text",
checkType: "cast",
description: "Check if the text contains a specific string",
hidden: false,
invertable: true,
args: {
searchText: {
type: "string",
friendlyName: "Search Text",
description: "The text to search for",
},
caseSensitive: {
type: "boolean",
friendlyName: "Case Sensitive",
description: "If checked, 'abc' is different from 'ABC'",
},
},
},
containsEmbeds: {
name: "containsEmbeds",
author: "modbot",
authorUrl: "https://modbot.sh",
authorIcon: `/icons/modbot.png`,
allowMultiple: true,
category: "cast",
friendlyName: "Contains Embedded Content",
checkType: "cast",
description: "Check if the cast contains images, gifs, videos, frames or links",
hidden: false,
invertable: true,
args: {
images: {
type: "boolean",
defaultValue: true,
friendlyName: "Images",
description: "Check for images or gifs",
},
videos: {
type: "boolean",
defaultValue: true,
friendlyName: "Videos",
description: "Check for videos",
},
frames: {
type: "boolean",
defaultValue: true,
friendlyName: "Frames",
description: "Check for frames",
},
links: {
type: "boolean",
defaultValue: true,
friendlyName: "Links",
description: "Check for links",
},
casts: {
type: "boolean",
defaultValue: true,
friendlyName: "Casts",
description: "Check for quote casts",
},
domain: {
type: "string",
friendlyName: "Domain",
placeholder: "e.g. glass.com",
description:
"Check for embeds from a specific domain. Example: if you check 'Frames' and add glass.com, this check will trigger for frames from glass.com.",
},
},
},
textMatchesPattern: {
name: "textMatchesPattern",
author: "modbot",
authorUrl: "https://modbot.sh",
authorIcon: `/icons/modbot.png`,
allowMultiple: true,
category: "cast",
friendlyName: "Matches Pattern (Regex)",
checkType: "cast",
description: "Check if the text matches a specific pattern",
hidden: false,
invertable: true,
args: {
pattern: {
type: "string",
friendlyName: "Pattern",
required: true,
description: "The regular expression to match against. No leading or trailing slashes.",
},
caseInsensitive: {
type: "boolean",
friendlyName: "Ignore Case",
description: "If checked, 'abc' is the same as 'ABC'",
},
},
},
textMatchesLanguage: {
name: "textMatchesLanguage",
author: "modbot",
authorUrl: "https://modbot.sh",
authorIcon: `/icons/modbot.png`,
allowMultiple: true,
category: "cast",
friendlyName: "Matches Language",
checkType: "cast",
description: "Check if the text matches a specific language",
invertedDescription: "Check if the text is any language *but* the one specified.",
hidden: true,
invertable: true,
args: {
language: {
type: "select",
friendlyName: "Language",
description: "The language to check for",
options: languages.map((l) => ({
label: l.name,
value: l.code,
})),
},
},
},
castLength: {
name: "castLength",
author: "modbot",
authorUrl: "https://modbot.sh",
authorIcon: `/icons/modbot.png`,
allowMultiple: false,
category: "cast",
friendlyName: "Cast Length",
checkType: "cast",
description: "Check if the cast length is within a range",
hidden: false,
invertable: false,
args: {
min: {
type: "number",
friendlyName: "Less than",
description: "Setting a value of 5 would trigger this rule if the length was 0 to 4 characters.",
},
max: {
type: "number",
friendlyName: "More than",
description: "Setting a value of 10 would trigger this rule if the length was 11 or more characters.",
},
},
},
containsTooManyMentions: {
name: "containsTooManyMentions",
author: "modbot",
authorUrl: "https://modbot.sh",
authorIcon: `/icons/modbot.png`,
allowMultiple: false,
category: "cast",
friendlyName: "Contains Mentions",
checkType: "cast",
description: "Check if the text contains a certain amount of mentions",
invertable: true,
hidden: false,
args: {
maxMentions: {
type: "number",
required: true,
friendlyName: "Max Mentions",
placeholder: "0",
description: "The maximum number of mentions allowed",
},
},
},
containsLinks: {
name: "containsLinks",
author: "modbot",
authorUrl: "https://modbot.sh",
authorIcon: `/icons/modbot.png`,
allowMultiple: false,
category: "cast",
friendlyName: "Contains Links",
checkType: "cast",
description: "Check if the text contains any links",
hidden: false,
invertable: true,
args: {
maxLinks: {
type: "number",
friendlyName: "Max Links",
description: "The maximum number of links allowed",
},
},
},
};