-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.mjs
1349 lines (1163 loc) · 52.5 KB
/
index.mjs
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import axios from "axios";
import { retry } from "@ultraq/promise-utils";
import to from "await-to-js";
import * as dotenv from 'dotenv';
import 'websocket-polyfill';
import { verifySignature, getPublicKey, getEventHash, getSignature, validateEvent, SimplePool } from 'nostr-tools';
import { RelayPool } from 'nostr';
import { LRUCache } from 'lru-cache';
import {
extractUrl,
getUrlType,
handleFatalError,
nMinutesAgo,
truncateString
} from "./util.mjs";
import { extractHashtags, isActivityPubUser, hasContentWarning, hasNsfwHashtag } from "./nostr-util.mjs";
import { isProbablyNSFWContent } from "./classification.mjs";
import fs from "node:fs/promises";
import { connectAsync } from "mqtt";
import { v4 as uuidv4 } from 'uuid';
import pLimit from 'p-limit';
import { exit } from "node:process";
import { TokenizerEn, NormalizerEn } from "@nlpjs/lang-en";
import {
MentionNostrEntityRegex,
unnecessaryCharRegex,
fullUnnecessaryCharRegex,
commonEmojiRegex,
ordinalPatternRegex,
zapPatternRegex,
hexStringRegex,
separateCamelCaseWordsHashtag,
reduceRepeatingCharacters,
normalizedNonGoodWordsPattern
} from "./nlp-util.mjs"
// Load env variable from .env
dotenv.config();
const NODE_ENV = process.env.NODE_ENV || "production";
const ENABLE_NIP_32_CLASSIFICATION_EVENT = process.env.ENABLE_NIP_32_CLASSIFICATION_EVENT ? process.env.ENABLE_NIP_32_CLASSIFICATION_EVENT === 'true' : true;
const ENABLE_LEGACY_CLASSIFICATION_EVENT = process.env.ENABLE_LEGACY_CLASSIFICATION_EVENT ? process.env.ENABLE_LEGACY_CLASSIFICATION_EVENT === 'true' : true;
const ENABLE_NSFW_CLASSIFICATION = process.env.ENABLE_NSFW_CLASSIFICATION ? process.env.ENABLE_NSFW_CLASSIFICATION === 'true' : false;
const NSFW_DETECTOR_ENDPOINT = process.env.NSFW_DETECTOR_ENDPOINT || "";
const NSFW_DETECTOR_TOKEN = process.env.NSFW_DETECTOR_TOKEN || "";
const ENABLE_LANGUAGE_DETECTION = process.env.ENABLE_LANGUAGE_DETECTION ? process.env.ENABLE_LANGUAGE_DETECTION === 'true' : false;
const LANGUAGE_DETECTOR_ENDPOINT = process.env.LANGUAGE_DETECTOR_ENDPOINT || "";
const LANGUAGE_DETECTOR_TOKEN = process.env.LANGUAGE_DETECTOR_TOKEN || "";
const LANGUAGE_DETECTOR_TRUNCATE_LENGTH = parseInt(process.env.LANGUAGE_DETECTOR_TRUNCATE_LENGTH || "350");
if (Number.isNaN(LANGUAGE_DETECTOR_TRUNCATE_LENGTH) || LANGUAGE_DETECTOR_TRUNCATE_LENGTH < 0) {
handleFatalError(new Error("Invalid LANGUAGE_DETECTOR_TRUNCATE_LENGTH"));
}
const ENABLE_HATE_SPEECH_DETECTION = process.env.ENABLE_HATE_SPEECH_DETECTION ? process.env.ENABLE_HATE_SPEECH_DETECTION === 'true' : false;
const HATE_SPEECH_DETECTOR_ENDPOINT = process.env.HATE_SPEECH_DETECTOR_ENDPOINT || "";
const HATE_SPEECH_DETECTOR_TOKEN = process.env.HATE_SPEECH_DETECTOR_TOKEN || "";
const HATE_SPEECH_DETECTOR_TRUNCATE_LENGTH = parseInt(process.env.HATE_SPEECH_DETECTOR_TRUNCATE_LENGTH || "350");
if (Number.isNaN(HATE_SPEECH_DETECTOR_TRUNCATE_LENGTH) || HATE_SPEECH_DETECTOR_TRUNCATE_LENGTH < 0) {
handleFatalError(new Error("Invalid HATE_SPEECH_DETECTOR_TRUNCATE_LENGTH"));
}
const ENABLE_SENTIMENT_ANALYSIS = process.env.ENABLE_SENTIMENT_ANALYSIS ? process.env.ENABLE_SENTIMENT_ANALYSIS === 'true' : false;
const SENTIMENT_ANALYSIS_ENDPOINT = process.env.SENTIMENT_ANALYSIS_ENDPOINT || "";
const SENTIMENT_ANALYSIS_TOKEN = process.env.SENTIMENT_ANALYSIS_TOKEN || "";
const SENTIMENT_ANALYSIS_TRUNCATE_LENGTH = parseInt(process.env.SENTIMENT_ANALYSIS_TRUNCATE_LENGTH || "350");
if (Number.isNaN(SENTIMENT_ANALYSIS_TRUNCATE_LENGTH) || SENTIMENT_ANALYSIS_TRUNCATE_LENGTH < 0) {
handleFatalError(new Error("Invalid SENTIMENT_ANALYSIS_TRUNCATE_LENGTH"));
}
const ENABLE_TOPIC_CLASSIFICATION = process.env.ENABLE_TOPIC_CLASSIFICATION ? process.env.ENABLE_TOPIC_CLASSIFICATION === 'true' : false;
const TOPIC_CLASSIFICATION_ENDPOINT = process.env.TOPIC_CLASSIFICATION_ENDPOINT || "";
const TOPIC_CLASSIFICATION_TOKEN = process.env.TOPIC_CLASSIFICATION_TOKEN || "";
const TOPIC_CLASSIFICATION_TRUNCATE_LENGTH = parseInt(process.env.TOPIC_CLASSIFICATION_TRUNCATE_LENGTH || "350");
if (Number.isNaN(TOPIC_CLASSIFICATION_TRUNCATE_LENGTH) || TOPIC_CLASSIFICATION_TRUNCATE_LENGTH < 0) {
handleFatalError(new Error("Invalid TOPIC_CLASSIFICATION_TRUNCATE_LENGTH"));
}
const NOSTR_MONITORING_BOT_PRIVATE_KEY = process.env.NOSTR_MONITORING_BOT_PRIVATE_KEY || handleFatalError(new Error("NOSTR_MONITORING_BOT_PRIVATE_KEY is required"));
const RELAYS_SOURCE =
(typeof process.env.RELAYS_SOURCE !== "undefined" &&
process.env.RELAYS_SOURCE !== "")
? process.env.RELAYS_SOURCE.split(",").map((relay) => relay.trim())
: [];
const RELAYS_TO_PUBLISH =
(typeof process.env.RELAYS_TO_PUBLISH !== "undefined" &&
process.env.RELAYS_TO_PUBLISH !== "")
? process.env.RELAYS_TO_PUBLISH.split(",").map((relay) => relay.trim())
: [];
if (RELAYS_SOURCE.length === 0) handleFatalError(new Error("RELAYS_SOURCE is required"));
if (RELAYS_TO_PUBLISH.length === 0) handleFatalError(new Error("RELAYS_TO_PUBLISH is required"));
const DELAYS_BEFORE_PUBLISHING_NOTES = parseInt(process.env.DELAYS_BEFORE_PUBLISHING_NOTES || "1000");
const ALLOW_EVENTS_NOT_OLDER_THAN_MINUTES = parseInt(process.env.ALLOW_EVENTS_NOT_OLDER_THAN_MINUTES || "10");
const ENABLE_MQTT_PUBLISH = process.env.ENABLE_MQTT_PUBLISH ? process.env.ENABLE_MQTT_PUBLISH === 'true' : false;
const MQTT_BROKER_TO_PUBLISH =
(typeof process.env.MQTT_BROKER_TO_PUBLISH !== "undefined" &&
process.env.MQTT_BROKER_TO_PUBLISH !== "")
? process.env.MQTT_BROKER_TO_PUBLISH.split(",").map((broker) => broker.trim())
: [];
if (ENABLE_MQTT_PUBLISH === true && MQTT_BROKER_TO_PUBLISH.length === 0) {
handleFatalError(new Error("MQTT_BROKER_TO_PUBLISH is required when ENABLE_MQTT_PUBLISH == true"));
}
// Additional image url regular expression pattern to be used for image classification requests.
// User can add environment variable like this: IMAGE_URL_PATTERN_0=hostname1.tld IMAGE_URL_PATTERN_1=hostname2.tld IMAGE_URL_PATTERN_2=or_any_pattern
const IMAGE_URL_PATTERN_LIST = Object.keys(process.env)
.filter(key => key.startsWith('IMAGE_URL_PATTERN_'))
.map(key => {
const pattern = process.env[key];
const match = pattern.match(/^\/(.+)\/([gimy]*)$/);
if (!match) {
return new RegExp(pattern);
} else {
return new RegExp(match[1], match[2]);
}
});
// Override log and debug functions for production environment. Use console.info, console.warn, console.error instead if needed.
if (NODE_ENV === "production") {
console.log = (...data) => {
};
console.debug = (...data) => {
};
}
const eventCache = new LRUCache(
{
max: 5000,
// how long to live in ms
ttl: 300000,
}
);
const requestLimiter = pLimit(10);
const stringNormalizer = new NormalizerEn();
const stringTokenizer = new TokenizerEn();
let mqttBroker = (ENABLE_MQTT_PUBLISH) ? MQTT_BROKER_TO_PUBLISH : [];
let mqttClient = (await Promise.allSettled(mqttBroker.map(url => connectAsync(url))))
.filter(res => res.status === "fulfilled").map(res => res.value);
let isInRelayPollFunction = false;
let relayPool;
const pool = new SimplePool();
let relays = RELAYS_SOURCE;
let relaysToPublish = RELAYS_TO_PUBLISH;
function axiosRetryStrategy(result, error, attempts) {
return !!error && attempts < 2 ? attempts * 5000 : -1;
}
const detectLanguagePromiseGenerator = function (text) {
const reqHeaders = {
'Content-Type': 'application/json',
'Accept': 'application/json',
// 'Authorization': `Bearer ${LANGUAGE_DETECTOR_TOKEN}`
};
return retry(() => axios({
url: LANGUAGE_DETECTOR_ENDPOINT,
method: 'POST',
headers: reqHeaders,
data: { "q": text, "api_key": LANGUAGE_DETECTOR_TOKEN }
}), axiosRetryStrategy);
};
const detectLanguage = async function (text) {
const result = await detectLanguagePromiseGenerator(text);
return result;
}
// (Deprecated) This function will be removed and replaced with NIP-32 event generator. Consider to use NIP-32 Label event.
const createLanguageClassificationEvent = (detectedLanguage, privateKey, taggedId, taggedAuthor, createdAt) => {
let languageClassificationEvent = {
id: "",
pubkey: getPublicKey(privateKey),
kind: 9978,
created_at: (createdAt !== undefined) ? createdAt : Math.floor(Date.now() / 1000),
tags: [
["d", "nostr-language-classification"],
["t", "nostr-language-classification"],
["e", taggedId],
["p", taggedAuthor],
],
content: JSON.stringify(detectedLanguage),
sig: ""
}
languageClassificationEvent.id = getEventHash(languageClassificationEvent);
languageClassificationEvent.sig = getSignature(languageClassificationEvent, privateKey);
let ok = validateEvent(languageClassificationEvent);
if (!ok) return undefined;
let veryOk = verifySignature(languageClassificationEvent);
if (!veryOk) return undefined;
return languageClassificationEvent;
};
const createLanguageClassificationNip32Event = (detectedLanguage, privateKey, taggedId, taggedAuthor, createdAt) => {
let labelNamespace = "app.nfrelay.language";
let labelISONamespace = "ISO-639-1";
let labelModelName = "atrifat/language-detector-api";
let labelModelUrl = "https://github.com/atrifat/language-detector-api";
let labelMinimumScore = 35;
let labelScoreType = "float";
let relaySource = "wss://nfrelay.app";
let languageClassificationEvent = {
id: "",
pubkey: getPublicKey(privateKey),
kind: 1985,
created_at: (createdAt !== undefined) ? createdAt : Math.floor(Date.now() / 1000),
tags: [
["e", taggedId, relaySource],
["p", taggedAuthor],
["L", labelISONamespace],
["L", labelNamespace],
["label_score_type", labelNamespace, labelScoreType],
["label_model", labelNamespace, labelModelName, labelModelUrl],
["label_minimum_score", labelNamespace, String(labelMinimumScore)],
],
content: "",
sig: ""
};
for (const language of detectedLanguage) {
let languageLabel = language.language.split("-").at(0);
let languageScore = String(language.confidence);
// Ensure only ISO-639-1 language code
if (languageLabel.length > 2) continue;
if (language.confidence >= labelMinimumScore) {
languageClassificationEvent.tags.push(["l", languageLabel, labelISONamespace]);
languageClassificationEvent.tags.push(["l", languageLabel, labelNamespace]);
}
languageClassificationEvent.tags.push(["label_score", languageLabel, labelNamespace, languageScore]);
}
languageClassificationEvent.id = getEventHash(languageClassificationEvent);
languageClassificationEvent.sig = getSignature(languageClassificationEvent, privateKey);
let ok = validateEvent(languageClassificationEvent);
if (!ok) return undefined;
let veryOk = verifySignature(languageClassificationEvent);
if (!veryOk) return undefined;
return languageClassificationEvent;
};
const classifyUrlNsfwDetectorPromiseGenerator = function (url) {
const reqHeaders = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${NSFW_DETECTOR_TOKEN}`
};
return requestLimiter(() => retry(() => axios({
url: NSFW_DETECTOR_ENDPOINT,
method: 'POST',
headers: reqHeaders,
data: { "url": url }
}), axiosRetryStrategy));
};
const classifyUrlNsfwDetector = async (mediaUrl, metadata) => {
const classifyUrlNsfwDetectorList = mediaUrl.map((url) => classifyUrlNsfwDetectorPromiseGenerator(url));
const rawOutput = await Promise.allSettled(classifyUrlNsfwDetectorList);
const classificationOutput = rawOutput.map((item) => {
let output = {};
output.id = metadata.id;
output.author = metadata.author;
output.is_activitypub_user = metadata.is_activitypub_user;
output.has_content_warning = metadata.has_content_warning;
output.has_nsfw_hashtag = metadata.has_nsfw_hashtag;
if (item.status === 'fulfilled') {
const nsfwProbability = 1 - parseFloat(item.value.data.data.neutral);
output.status = true;
output.data = item.value.data.data;
output.probably_nsfw = nsfwProbability >= 0.75;
output.high_probably_nsfw = nsfwProbability >= 0.85;
output.responsible_nsfw = (!output.probably_nsfw) ? true : output.probably_nsfw && (output.has_content_warning || output.has_nsfw_hashtag);
}
else {
output.status = false;
output.data = item.reason.message;
output.probably_nsfw = false;
output.high_probably_nsfw = false;
output.responsible_nsfw = true;
}
return output;
});
for (let index = 0; index < mediaUrl.length; index++) {
const element = mediaUrl[index];
classificationOutput[index].url = element;
}
const classificationData = classificationOutput.filter(m => m.status === true).map(m => {
return {
id: m.id,
author: m.author,
is_activitypub_user: m.is_activitypub_user,
has_content_warning: m.has_content_warning,
has_nsfw_hashtag: m.has_nsfw_hashtag,
probably_nsfw: m.probably_nsfw,
high_probably_nsfw: m.high_probably_nsfw,
responsible_nsfw: m.responsible_nsfw,
data: m.data,
url: m.url,
};
});
// const classificationData = classificationOutput.filter(m => m.status === true);
return classificationData;
};
// (Deprecated) This function will be removed and replaced with NIP-32 event generator. Consider to use NIP-32 Label event.
const createNsfwClassificationEvent = (nsfwClassificationData, privateKey, taggedId, taggedAuthor, createdAt) => {
let nsfwClassificationEvent = {
id: "",
pubkey: getPublicKey(privateKey),
kind: 9978,
created_at: (createdAt !== undefined) ? createdAt : Math.floor(Date.now() / 1000),
tags: [
["d", "nostr-nsfw-classification"],
["t", "nostr-nsfw-classification"],
["e", taggedId],
["p", taggedAuthor],
],
content: JSON.stringify(nsfwClassificationData),
sig: ""
}
nsfwClassificationEvent.id = getEventHash(nsfwClassificationEvent);
nsfwClassificationEvent.sig = getSignature(nsfwClassificationEvent, privateKey);
let ok = validateEvent(nsfwClassificationEvent);
if (!ok) return undefined;
let veryOk = verifySignature(nsfwClassificationEvent);
if (!veryOk) return undefined;
return nsfwClassificationEvent;
};
const createNsfwClassificationNip32Event = (nsfwClassificationData, privateKey, taggedId, taggedAuthor, createdAt) => {
let labelNamespace = "app.nfrelay.content-safety";
let labelModelName = "atrifat/nsfw-detector-api";
let labelModelUrl = "https://github.com/atrifat/nsfw-detector-api";
let labelScoreType = "float";
let labelMinimumScore = 0.5;
let labelSchema = ["sfw", "nsfw"];
let labelSchemaOriginal = ["hentai", "neutral", "pornography", "sexy"];
let relaySource = "wss://nfrelay.app";
let nsfwClassificationEvent = {
id: "",
pubkey: getPublicKey(privateKey),
kind: 1985,
created_at: (createdAt !== undefined) ? createdAt : Math.floor(Date.now() / 1000),
tags: [
["e", taggedId, relaySource],
["p", taggedAuthor],
["L", labelNamespace],
["label_score_type", labelNamespace, labelScoreType],
["label_model", labelNamespace, labelModelName, labelModelUrl],
["label_minimum_score", labelNamespace, String(labelMinimumScore)],
["label_schema", labelNamespace].concat(labelSchema),
["label_schema_original", labelNamespace].concat(labelSchemaOriginal),
],
content: "",
sig: ""
}
for (const item of nsfwClassificationData) {
let rawClassificationData = item.data;
let sourceClassificationData = item.url;
let nsfwScore = 1.0 - item.data.neutral;
let sfwScore = item.data.neutral;
let label = (nsfwScore >= labelMinimumScore) ? "nsfw" : "sfw";
let score = (nsfwScore >= labelMinimumScore) ? nsfwScore : sfwScore;
nsfwClassificationEvent.tags.push(["l", label, labelNamespace]);
nsfwClassificationEvent.tags.push(["label_score", label, labelNamespace, String(score), sourceClassificationData]);
for (const labelOriginal of labelSchemaOriginal) {
if (rawClassificationData.hasOwnProperty(labelOriginal)) {
nsfwClassificationEvent.tags.push(["label_score", labelOriginal, labelNamespace, String(rawClassificationData[labelOriginal]), sourceClassificationData]);
}
}
}
nsfwClassificationEvent.id = getEventHash(nsfwClassificationEvent);
nsfwClassificationEvent.sig = getSignature(nsfwClassificationEvent, privateKey);
let ok = validateEvent(nsfwClassificationEvent);
if (!ok) return undefined;
let veryOk = verifySignature(nsfwClassificationEvent);
if (!veryOk) return undefined;
return nsfwClassificationEvent;
};
const detectHateSpeechPromiseGenerator = function (text) {
const reqHeaders = {
'Content-Type': 'application/json',
'Accept': 'application/json',
// 'Authorization': `Bearer ${LANGUAGE_DETECTOR_TOKEN}`
};
return retry(() => axios({
url: HATE_SPEECH_DETECTOR_ENDPOINT,
method: 'POST',
headers: reqHeaders,
data: { "q": text, "api_key": HATE_SPEECH_DETECTOR_TOKEN }
}), axiosRetryStrategy);
};
const detectHateSpeech = async function (text) {
const result = await detectHateSpeechPromiseGenerator(text);
return result;
}
// (Deprecated) This function will be removed and replaced with NIP-32 event generator. Consider to use NIP-32 Label event.
const createHateSpeechClassificationEvent = (detectedHateSpeech, privateKey, taggedId, taggedAuthor, createdAt) => {
let hateSpeechClassificationEvent = {
id: "",
pubkey: getPublicKey(privateKey),
kind: 9978,
created_at: (createdAt !== undefined) ? createdAt : Math.floor(Date.now() / 1000),
tags: [
["d", "nostr-hate-speech-classification"],
["t", "nostr-hate-speech-classification"],
["e", taggedId],
["p", taggedAuthor],
],
content: JSON.stringify(detectedHateSpeech),
sig: ""
}
hateSpeechClassificationEvent.id = getEventHash(hateSpeechClassificationEvent);
hateSpeechClassificationEvent.sig = getSignature(hateSpeechClassificationEvent, privateKey);
let ok = validateEvent(hateSpeechClassificationEvent);
if (!ok) return undefined;
let veryOk = verifySignature(hateSpeechClassificationEvent);
if (!veryOk) return undefined;
return hateSpeechClassificationEvent;
};
const createHateSpeechClassificationNip32Event = (detectedHateSpeech, privateKey, taggedId, taggedAuthor, createdAt) => {
let labelNamespace = "app.nfrelay.toxicity";
let labelModelName = "atrifat/hate-speech-detector-api";
let labelModelUrl = "https://github.com/atrifat/hate-speech-detector-api";
let labelScoreType = "float";
let labelMinimumScore = 0.5;
let labelSchema = ["toxic", "non-toxic"];
let labelSchemaOriginal = ["identity_attack", "insult", "obscene", "severe_toxicity", "sexual_explicit", "threat", "toxicity"];
let relaySource = "wss://nfrelay.app";
let hateSpeechClassificationEvent = {
id: "",
pubkey: getPublicKey(privateKey),
kind: 1985,
created_at: (createdAt !== undefined) ? createdAt : Math.floor(Date.now() / 1000),
tags: [
["e", taggedId, relaySource],
["p", taggedAuthor],
["L", labelNamespace],
["label_score_type", labelNamespace, labelScoreType],
["label_model", labelNamespace, labelModelName, labelModelUrl],
["label_minimum_score", labelNamespace, String(labelMinimumScore)],
["label_schema", labelNamespace].concat(labelSchema),
["label_schema_original", labelNamespace].concat(labelSchemaOriginal),
],
content: "",
sig: ""
}
// Get maximum probability of all classification label
const maxScoreHateSpeechDetection = Math.max(...Object.values(detectedHateSpeech).map((score) => parseFloat(score)));
let toxicScore = maxScoreHateSpeechDetection;
let nonToxicScore = 1.0 - toxicScore;
let label = (toxicScore >= labelMinimumScore) ? "toxic" : "non-toxic";
let score = (toxicScore >= labelMinimumScore) ? toxicScore : nonToxicScore;
hateSpeechClassificationEvent.tags.push(["l", label, labelNamespace]);
hateSpeechClassificationEvent.tags.push(["label_score", label, labelNamespace, String(score)]);
for (const labelOriginal of labelSchemaOriginal) {
if (detectedHateSpeech.hasOwnProperty(labelOriginal)) {
hateSpeechClassificationEvent.tags.push(["label_score", labelOriginal, labelNamespace, String(detectedHateSpeech[labelOriginal])]);
}
}
hateSpeechClassificationEvent.id = getEventHash(hateSpeechClassificationEvent);
hateSpeechClassificationEvent.sig = getSignature(hateSpeechClassificationEvent, privateKey);
let ok = validateEvent(hateSpeechClassificationEvent);
if (!ok) return undefined;
let veryOk = verifySignature(hateSpeechClassificationEvent);
if (!veryOk) return undefined;
return hateSpeechClassificationEvent;
};
const detectSentimentPromiseGenerator = function (text) {
const reqHeaders = {
'Content-Type': 'application/json',
'Accept': 'application/json',
};
return retry(() => axios({
url: SENTIMENT_ANALYSIS_ENDPOINT,
method: 'POST',
headers: reqHeaders,
data: { "q": text, "api_key": SENTIMENT_ANALYSIS_TOKEN }
}), axiosRetryStrategy);
};
const detectSentiment = async function (text) {
const result = await detectSentimentPromiseGenerator(text);
return result;
}
// (Deprecated) This function will be removed and replaced with NIP-32 event generator. Consider to use NIP-32 Label event.
const createSentimentClassificationEvent = (detectedSentiment, privateKey, taggedId, taggedAuthor, createdAt) => {
let sentimentClassificationEvent = {
id: "",
pubkey: getPublicKey(privateKey),
kind: 9978,
created_at: (createdAt !== undefined) ? createdAt : Math.floor(Date.now() / 1000),
tags: [
["d", "nostr-sentiment-classification"],
["t", "nostr-sentiment-classification"],
["e", taggedId],
["p", taggedAuthor],
],
content: JSON.stringify(detectedSentiment),
sig: ""
}
sentimentClassificationEvent.id = getEventHash(sentimentClassificationEvent);
sentimentClassificationEvent.sig = getSignature(sentimentClassificationEvent, privateKey);
let ok = validateEvent(sentimentClassificationEvent);
if (!ok) return undefined;
let veryOk = verifySignature(sentimentClassificationEvent);
if (!veryOk) return undefined;
return sentimentClassificationEvent;
};
const createSentimentClassificationNip32Event = (detectedSentiment, privateKey, taggedId, taggedAuthor, createdAt) => {
let labelNamespace = "app.nfrelay.sentiment";
let labelModelName = "atrifat/sentiment-analysis-api";
let labelModelUrl = "https://github.com/atrifat/sentiment-analysis-api";
let labelScoreType = "float";
let labelMinimumScore = 0.35;
let labelSchema = ["negative", "neutral", "positive"];
let labelSchemaOriginal = ["negative", "neutral", "positive"];
let relaySource = "wss://nfrelay.app";
let sentimentClassificationEvent = {
id: "",
pubkey: getPublicKey(privateKey),
kind: 1985,
created_at: (createdAt !== undefined) ? createdAt : Math.floor(Date.now() / 1000),
tags: [
["e", taggedId, relaySource],
["p", taggedAuthor],
["L", labelNamespace],
["label_score_type", labelNamespace, labelScoreType],
["label_model", labelNamespace, labelModelName, labelModelUrl],
["label_minimum_score", labelNamespace, String(labelMinimumScore)],
["label_schema", labelNamespace].concat(labelSchema),
["label_schema_original", labelNamespace].concat(labelSchemaOriginal),
],
content: "",
sig: ""
}
let sentimentLabel = "";
let sentimentMaxScore = 0.0;
for (const label in detectedSentiment) {
if (detectedSentiment[label] > sentimentMaxScore) {
sentimentMaxScore = parseFloat(detectedSentiment[label] ?? 0.0);
sentimentLabel = label;
}
}
if (sentimentLabel === "") {
return undefined;
}
if (sentimentMaxScore >= labelMinimumScore) {
sentimentClassificationEvent.tags.push(["l", sentimentLabel, labelNamespace]);
}
for (const labelOriginal of labelSchemaOriginal) {
if (detectedSentiment.hasOwnProperty(labelOriginal)) {
sentimentClassificationEvent.tags.push(["label_score", labelOriginal, labelNamespace, String(detectedSentiment[labelOriginal])]);
}
}
sentimentClassificationEvent.id = getEventHash(sentimentClassificationEvent);
sentimentClassificationEvent.sig = getSignature(sentimentClassificationEvent, privateKey);
let ok = validateEvent(sentimentClassificationEvent);
if (!ok) return undefined;
let veryOk = verifySignature(sentimentClassificationEvent);
if (!veryOk) return undefined;
return sentimentClassificationEvent;
};
const classifyTopicPromiseGenerator = function (text) {
const reqHeaders = {
'Content-Type': 'application/json',
'Accept': 'application/json',
};
return retry(() => axios({
url: TOPIC_CLASSIFICATION_ENDPOINT,
method: 'POST',
headers: reqHeaders,
data: { "q": text, "api_key": TOPIC_CLASSIFICATION_TOKEN }
}), axiosRetryStrategy);
};
const classifyTopic = async function (text) {
try {
const rawResult = await classifyTopicPromiseGenerator(text);
let result = rawResult.data.map(item => { return { label: item.label.replace("&", "and"), score: item.score }; });
return { data: result };
} catch (error) {
throw error;
}
}
// (Deprecated) This function will be removed and replaced with NIP-32 event generator. Consider to use NIP-32 Label event.
const createTopicClassificationEvent = (topicClassification, privateKey, taggedId, taggedAuthor, createdAt) => {
let topicClassificationEvent = {
id: "",
pubkey: getPublicKey(privateKey),
kind: 9978,
created_at: (createdAt !== undefined) ? createdAt : Math.floor(Date.now() / 1000),
tags: [
["d", "nostr-topic-classification"],
["t", "nostr-topic-classification"],
["e", taggedId],
["p", taggedAuthor],
],
content: JSON.stringify(topicClassification),
sig: ""
}
topicClassificationEvent.id = getEventHash(topicClassificationEvent);
topicClassificationEvent.sig = getSignature(topicClassificationEvent, privateKey);
let ok = validateEvent(topicClassificationEvent);
if (!ok) return undefined;
let veryOk = verifySignature(topicClassificationEvent);
if (!veryOk) return undefined;
return topicClassificationEvent;
};
const createTopicClassificationNip32Event = (topicClassification, privateKey, taggedId, taggedAuthor, createdAt) => {
let labelNamespace = "app.nfrelay.topic";
let labelModelName = "atrifat/topic-classification-api";
let labelModelUrl = "https://github.com/atrifat/topic-classification-api";
let labelScoreType = "float";
let labelMinimumScore = 0.35;
let labelSchema = ["arts_and_culture", "business_and_entrepreneurs", "celebrity_and_pop_culture", "diaries_and_daily_life", "family", "fashion_and_style", "film_tv_and_video", "fitness_and_health", "food_and_dining", "gaming", "learning_and_educational", "music", "news_and_social_concern", "other_hobbies", "relationships", "science_and_technology", "sports", "travel_and_adventure", "youth_and_student_life"];
let labelSchemaOriginal = ["arts_&_culture", "business_&_entrepreneurs", "celebrity_&_pop_culture", "diaries_&_daily_life", "family", "fashion_&_style", "film_tv_&_video", "fitness_&_health", "food_&_dining", "gaming", "learning_&_educational", "music", "news_&_social_concern", "other_hobbies", "relationships", "science_&_technology", "sports", "travel_&_adventure", "youth_&_student_life"];
let relaySource = "wss://nfrelay.app";
let topicClassificationEvent = {
id: "",
pubkey: getPublicKey(privateKey),
kind: 1985,
created_at: (createdAt !== undefined) ? createdAt : Math.floor(Date.now() / 1000),
tags: [
["e", taggedId, relaySource],
["p", taggedAuthor],
["L", labelNamespace],
["label_score_type", labelNamespace, labelScoreType],
["label_model", labelNamespace, labelModelName, labelModelUrl],
["label_minimum_score", labelNamespace, String(labelMinimumScore)],
["label_schema", labelNamespace].concat(labelSchema),
["label_schema_original", labelNamespace].concat(labelSchemaOriginal),
],
content: "",
sig: ""
}
for (const classification of topicClassification ?? []) {
let label = classification.label;
let score = parseFloat(classification.score ?? "0.0");
if (score >= labelMinimumScore) {
topicClassificationEvent.tags.push(["l", label, labelNamespace]);
}
topicClassificationEvent.tags.push(["label_score", label, labelNamespace, String(score)]);
}
topicClassificationEvent.id = getEventHash(topicClassificationEvent);
topicClassificationEvent.sig = getSignature(topicClassificationEvent, privateKey);
let ok = validateEvent(topicClassificationEvent);
if (!ok) return undefined;
let veryOk = verifySignature(topicClassificationEvent);
if (!veryOk) return undefined;
return topicClassificationEvent;
};
const publishNostrEvent = async (pool, relaysToPublish, event) => {
try {
let pubs = pool.publish(relaysToPublish, event);
const joinResult = await Promise.all(pubs);
console.debug("Event published", event.id);
return true;
} catch (error) {
if (error === undefined) {
console.error("Error publishing", "undefined error");
return false;
}
if (error.message.trim() === "") {
console.error("Error publishing", "empty message error");
return true;
}
console.error("Error publishing", error.message);
return false;
}
};
const preprocessText = async (inputText) => {
let text;
// Preprocess to replace any NIP-19 mentions (nostr:npub1, nevent1, note1, etc.)
text = inputText.replace(MentionNostrEntityRegex, ' ');
// Extract URL
const extractedUrl = [... new Set(extractUrl(inputText + ' ') ?? [])];
// Preprocess to remove links
for (let index = 0; index < extractedUrl.length; index++) {
const url = extractedUrl[index];
text = text.replaceAll(url, ' ');
}
// Preprocess to remove ordinal pattern such as: 2nd, 3rd, etc.
text = text.replace(ordinalPatternRegex, '');
text = reduceRepeatingCharacters(text);
text = separateCamelCaseWordsHashtag(text);
text = normalizedNonGoodWordsPattern(text);
// Transform zap/zapathon into "tip" to reduce false positive
text = text.replace(zapPatternRegex, 'tip');
// Preprocess to remove hex string characters
text = text.replace(hexStringRegex, ' ');
// Preprocess to remove unnecessary characters (excluding single quote or double quote)
text = text.replace(unnecessaryCharRegex, ' ');
text = stringTokenizer.tokenize(text).join(' ');
text = stringNormalizer.normalize(text);
// Remove unicode emojis (disabled by default, since emoji is important in sentiment analysis and hate speech detection)
// text = text.replace(commonEmojiRegex, ' ');
// Preprocess to remove full unnecessary characters (including single quote or double quote)
text = text.replace(fullUnnecessaryCharRegex, '');
// Replace common greeting patten (good morning)
text = text.replace(/(^gm\s|\sgm|gm\s|\sgm$)/gm, 'good morning');
// Replace common greeting patten (good night)
text = text.replace(/(^gn\s|\sgn|gn\s|\sgn$)/gm, 'good night');
// Replace common greeting patten (pura vida)
text = text.replace(/(^pv\s|\spv|pv\s|\spv$)/gm, 'pura vida gratitude happy life');
// Replace multiple newline character into single space character
text = text.replace(/\n+/gm, ' ');
// Replace multiple spaces character into single space character
text = text.replace(/\s+/gm, ' ');
text = text.trim().toLowerCase();
return text;
}
const handleNotesEvent = async (relay, sub_id, ev) => {
const event = ev;
const id = ev.id;
const author = ev.pubkey;
const kind = parseInt(ev.kind) || 0;
// Only accept event kind 1 for now
if (kind !== 1) {
console.debug("Not kind 1");
console.debug(relay);
console.debug(ev);
// exit(1);
return;
}
const content = ev.content;
const created_at = ev.created_at;
const tags = ev.tags;
const hashtags = extractHashtags(tags);
const _hasContentWarning = hasContentWarning(tags);
const _hasNsfwHashtag = hasNsfwHashtag(hashtags);
const _isActivityPubUser = isActivityPubUser(tags);
const extractedUrl = [... new Set(extractUrl(content + ' ') ?? [])];
console.debug('======================================');
console.debug('relay = ', relay.url);
console.debug(`id = ${id}, created_at = ${created_at}, kinds = ${kind}`);
console.debug('author = ', author);
console.debug('_isActivityPubUser = ', _isActivityPubUser);
console.debug('content = ', content);
console.debug('tags = ', JSON.stringify(tags));
console.debug('hashtags = ', JSON.stringify(hashtags));
console.debug('_hasContentWarning = ', _hasContentWarning);
console.debug('_hasNsfwHashtag = ', _hasNsfwHashtag);
console.debug('Extracted url = ', extractedUrl.join(', '));
// Extract only image/video url
const mediaUrl = extractedUrl.filter((url) => {
let match = false;
for (const filter of IMAGE_URL_PATTERN_LIST) {
if (filter.test(url)) {
// console.debug(url, "match", filter)
match = true;
break;
}
}
return match || getUrlType(url) === 'image' || getUrlType(url) === 'video';
}) ?? [];
console.debug('Img/video url = ', mediaUrl.join(', '));
// NSFW classification event processing
if (ENABLE_NSFW_CLASSIFICATION && mediaUrl.length > 0) {
let metadata = {};
metadata.id = id;
metadata.author = author;
metadata.has_content_warning = _hasContentWarning;
metadata.has_nsfw_hashtag = _hasNsfwHashtag;
metadata.is_activitypub_user = _isActivityPubUser;
const nsfwClassificationData = await classifyUrlNsfwDetector(mediaUrl, metadata);
const _isProbablyNSFW = isProbablyNSFWContent(nsfwClassificationData);
// Mark as reponsible nsfw content if it has content warning or nsfw hashtag
const _isResponsibleNSFW = (!_isProbablyNSFW) ? true : _isProbablyNSFW && (metadata.has_content_warning || metadata.has_nsfw_hashtag);
console.debug('_isResponsibleNSFW = ', _isResponsibleNSFW);
console.debug('_isProbablyNSFW = ', _isProbablyNSFW);
console.debug('nsfwClassificationData = ', JSON.stringify(nsfwClassificationData));
const nsfwClassificationEvent = createNsfwClassificationEvent(nsfwClassificationData, NOSTR_MONITORING_BOT_PRIVATE_KEY,
metadata.id, metadata.author, created_at);
const nsfwClassificationNip32Event = createNsfwClassificationNip32Event(nsfwClassificationData, NOSTR_MONITORING_BOT_PRIVATE_KEY,
metadata.id, metadata.author, created_at);
// Publish classification event
if (nsfwClassificationData.length > 0) {
const publishEventResult = (ENABLE_LEGACY_CLASSIFICATION_EVENT) ? await publishNostrEvent(pool, relaysToPublish, nsfwClassificationEvent) : true;
if (!publishEventResult) {
console.info("Fail to publish nsfwClassificationEvent event, try again for the last time");
await publishNostrEvent(pool, relaysToPublish, nsfwClassificationEvent);
}
const publishNip32EventResult = (ENABLE_NIP_32_CLASSIFICATION_EVENT) ? await publishNostrEvent(pool, relaysToPublish, nsfwClassificationNip32Event) : true;
if (!publishNip32EventResult) {
console.info("Fail to publish nsfwClassificationNip32Event event, try again for the last time");
await publishNostrEvent(pool, relaysToPublish, nsfwClassificationNip32Event);
}
}
if (NODE_ENV !== 'production') fs.appendFile('classification.txt', JSON.stringify(nsfwClassificationData) + "\n");
if (_isProbablyNSFW && !_isResponsibleNSFW) {
if (NODE_ENV !== 'production') fs.appendFile('classification_not_responsible_nsfw.txt', JSON.stringify(nsfwClassificationData) + "\n");
}
if (_isProbablyNSFW) {
if (NODE_ENV !== 'production') fs.appendFile('classification_probably_nsfw.txt', JSON.stringify(nsfwClassificationData) + "\n");
mqttClient.forEach((client) => {
if (ENABLE_MQTT_PUBLISH) {
client.publishAsync('nostr-nsfw-classification/1', JSON.stringify(nsfwClassificationEvent)).then(() => {
console.log(client.options.host, "nostr-nsfw-classification/1", "Published");
});
}
});
}
else {
if (NODE_ENV !== 'production') fs.appendFile('classification_not_probably_nsfw.txt', JSON.stringify(nsfwClassificationData) + "\n");
mqttClient.forEach((client) => {
if (ENABLE_MQTT_PUBLISH) {
client.publishAsync('nostr-nsfw-classification/0', JSON.stringify(nsfwClassificationEvent)).then(() => {
console.log(client.options.host, "nostr-nsfw-classification/0", "Published");
});
}
});
}
}
// Text Preprocessing for Classification
let processedText = await preprocessText(content);
// Detext empty text
const isEmptyText = content.replace(/(\n|\s)+/gm, '').trim() === '' && extractedUrl.length === 0;
// Language detection event processing
let isEnglish = false;
if (ENABLE_LANGUAGE_DETECTION && !isEmptyText) {
const startTime = performance.now();
let err, detectedLanguageResponse;
let text = processedText;
const preprocessStartTime = performance.now();
// Remove unicode emojis
text = text.replace(commonEmojiRegex, '');
// Replace multiple spaces character into single space character
text = text.replace(/\s+/g, ' ').trim();
// Truncate text if needed
if (LANGUAGE_DETECTOR_TRUNCATE_LENGTH > 0) {
text = truncateString(text, LANGUAGE_DETECTOR_TRUNCATE_LENGTH);
}
const finalText = text;
const preprocessElapsedTime = performance.now() - preprocessStartTime;
if (finalText !== '') {
[err, detectedLanguageResponse] = await to.default(detectLanguage(finalText));
if (err) {
console.error("Error:", err.message);
}
}
else {
console.debug("Empty text after preprocessing, original text = ", content);
err = new Error("Empty text");
}
const elapsedTime = performance.now() - startTime;
const defaultResult = [{ confidence: 0, language: 'en' }];
const detectedLanguage = (!err) ? detectedLanguageResponse.data : defaultResult;
for (let i = 0; i < detectedLanguage.length; i++) {
const languageData = detectedLanguage[i];
if (languageData.confidence >= 50 && languageData.language === 'en') {
isEnglish = true;
break;
}
}
// console.debug(text);
console.debug("detectedLanguage", JSON.stringify(detectedLanguage), elapsedTime);
const languageClassificationEvent = createLanguageClassificationEvent(detectedLanguage, NOSTR_MONITORING_BOT_PRIVATE_KEY, id, author, created_at);
const languageClassificationNip32Event = createLanguageClassificationNip32Event(detectedLanguage, NOSTR_MONITORING_BOT_PRIVATE_KEY, id, author, created_at);
// Publish languageClassificationEvent
const publishEventResult = (ENABLE_LEGACY_CLASSIFICATION_EVENT) ? await publishNostrEvent(pool, relaysToPublish, languageClassificationEvent) : true;
if (!publishEventResult) {
console.info("Fail to publish languageClassificationEvent event, try again for the last time");
await publishNostrEvent(pool, relaysToPublish, languageClassificationEvent);
}
// Publish languageClassificationNip32Event
const publishNip32EventResult = (ENABLE_NIP_32_CLASSIFICATION_EVENT) ? await publishNostrEvent(pool, relaysToPublish, languageClassificationNip32Event) : true;
if (!publishNip32EventResult) {
console.info("Fail to publish languageClassificationNip32Event event, try again for the last time");
await publishNostrEvent(pool, relaysToPublish, languageClassificationNip32Event);
}
mqttClient.forEach((client) => {
if (ENABLE_MQTT_PUBLISH) {
client.publishAsync('nostr-language-classification', JSON.stringify(languageClassificationEvent)).then(() => {
console.log(client.options.host, "nostr-language-classification", "Published");
});
}
});
}
// Hate speech detection event processing
if (ENABLE_HATE_SPEECH_DETECTION && isEnglish && !isEmptyText) {
const startTime = performance.now();
let err, detectedHateSpeechResponse;
let text = processedText;
const preprocessStartTime = performance.now();
// Truncate text if needed
if (HATE_SPEECH_DETECTOR_TRUNCATE_LENGTH > 0) {
text = truncateString(text, HATE_SPEECH_DETECTOR_TRUNCATE_LENGTH);
}
const finalText = text;
const preprocessElapsedTime = performance.now() - preprocessStartTime;
if (finalText !== '') {
[err, detectedHateSpeechResponse] = await to.default(detectHateSpeech(finalText));
if (err) {
console.error("Error:", err.message);