forked from pkreissel/fedialgo
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtoot.ts
More file actions
1234 lines (1110 loc) · 55.6 KB
/
toot.ts
File metadata and controls
1234 lines (1110 loc) · 55.6 KB
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
/**
* @fileoverview {@linkcode Toot} class and helper methods for dealing with Mastodon
* {@linkcode https://docs.joinmastodon.org/entities/Status/ Status} objects.
* Includes methods for scoring, filtering, deduplication, and property repair.
*/
import { capitalCase } from "change-case";
import { isEmpty, isFinite } from "lodash";
import { mastodon } from "masto";
import { QuoteApproval } from "masto/dist/esm/mastodon/entities/v1/quote-approval";
import { Type } from 'class-transformer';
import Account from "./account";
import MastoApi from "../api";
import MastodonServer from "../mastodon_server";
import Scorer from "../../scorer/scorer";
import UserData from "../user_data";
import { AgeIn, ageString, timelineCutoffAt, toISOFormat } from "../../helpers/time_helpers";
import { config } from "../../config";
import { FILTERABLE_SCORES } from "../../filters/numeric_filter";
import { FOREIGN_SCRIPTS, LANGUAGE_NAMES, detectForeignScriptLanguage, detectLanguage } from "../../helpers/language_helper";
import { isProduction } from "../../helpers/environment_helpers";
import { isValidForSubstringSearch, repairTag } from "./tag";
import { LoadAction, MediaCategory, ScoreName, TypeFilterName } from '../../enums';
import { Logger } from '../../helpers/logger';
import {
asOptionalArray,
batchMap,
filterWithLog,
groupBy,
sortObjsByProps,
split,
sumArray,
uniquify,
uniquifyByProp
} from "../../helpers/collection_helpers";
import {
DEFAULT_FONT_SIZE,
MEDIA_TYPES,
VIDEO_TYPES,
at,
collapseWhitespace,
determineMediaCategory,
extractDomain,
htmlToParagraphs,
htmlToText,
optionalSuffix,
removeDiacritics,
removeEmojis,
removeLinks,
removeMentions,
removeTags,
replaceEmojiShortcodesWithImgTags,
replaceHttpsLinks,
wordRegex,
} from "../../helpers/string_helpers";
import {
type AccountLike,
type FeedFilterSettings,
type KeysOfValueType,
type Hashtag,
type ScoreType,
type TagWithUsageCounts,
type TootLike,
type TootNumberProp,
type TootScore,
type TootSource,
type TrendingLink,
} from "../../types";
enum TootCacheKey {
CONTENT_STRIPPED = "contentStripped",
CONTENT_WITH_EMOJIS = "contentWithEmojis",
CONTENT_WITH_CARD = "contentWithCard",
};
// https://docs.joinmastodon.org/entities/Status/#visibility
enum TootVisibility {
DIRECT_MSG = "direct",
PUBLIC = "public",
PRIVATE = "private",
UNLISTED = "unlisted",
};
// Cache for methods that build strings from the toot content.
type TootCacheStrings = {[key in TootCacheKey]?: string};
type TootCache = TootCacheStrings & {tagNames?: Set<string>};
const UNKNOWN = "unknown";
const BSKY_BRIDGY = 'bsky.brid.gy';
const HASHTAG_LINK_REGEX = /<a href="https:\/\/[\w.]+\/tags\/[\w]+" class="[-\w_ ]*hashtag[-\w_ ]*" rel="[a-z ]+"( target="_blank")?>#<span>[\w]+<\/span><\/a>/i;
const HASHTAG_PARAGRAPH_REGEX = new RegExp(`^<p>(?:${HASHTAG_LINK_REGEX.source} ?)+</p>`, "i");
const PROPS_THAT_CHANGE = FILTERABLE_SCORES.concat("numTimesShown");
const tootLogger = new Logger("Toot");
const repairLogger = tootLogger.tempLogger("repairToot");
/**
* Extension of mastodon.v1.Status data object with additional properties used by fedialgo
* that should be serialized to storage.
*/
export interface SerializableToot extends mastodon.v1.Status {
completedAt?: string; // Timestamp a full deep inspection of the toot was completed
followedTags?: Hashtag[]; // Array of tags that the user follows that exist in this toot
numTimesShown?: number; // Managed in client app. # of times the Toot has been shown to the user.
participatedTags?: TagWithUsageCounts[]; // Tags that the user has participated in that exist in this toot
reblog?: SerializableToot | null, // The toot that was retooted (if any)
reblogsBy?: AccountLike[]; // The accounts that retooted this toot (if any)
resolvedID?: string; // This Toot with URLs resolved to homeserver versions
scoreInfo?: TootScore; // Scoring info for weighting/sorting this toot
sources?: string[]; // Source of the toot (e.g. trending tag toots, home timeline, etc.)
trendingLinks?: TrendingLink[]; // Links that are trending in this toot
trendingRank?: number; // Most trending on a server gets a 10, next is a 9, etc.
trendingTags?: TagWithUsageCounts[]; // Tags that are trending in this toot
audioAttachments?: mastodon.v1.MediaAttachment[];
imageAttachments?: mastodon.v1.MediaAttachment[];
videoAttachments?: mastodon.v1.MediaAttachment[];
};
/**
* Interface for mastodon.v1.Status object with additional helper methods.
* @interface
*/
interface TootObj extends SerializableToot {
// Getters
accounts: Account[];
attachmentType: MediaCategory | undefined;
author: Account;
contentTagsParagraph: string | undefined;
description: string;
isDM: boolean;
isFollowed: boolean;
isPrivate: boolean;
isTrending: boolean;
popularity: number;
realToot: Toot;
realURI: string;
realURL: string;
score: number;
withRetoot: Toot[];
// Methods
containsString: (str: string) => boolean;
containsTag: (tag: TagWithUsageCounts, fullScan?: boolean) => boolean;
containsTagsMsg: () => string | undefined;
contentNonTagsParagraphs: (fontSize?: number) => string;
contentParagraphs: (fontSize?: number) => string[];
contentShortened: (maxChars?: number) => string;
contentWithEmojis: (fontSize?: number) => string;
localServerUrl: () => Promise<string>;
isInTimeline: (filters: FeedFilterSettings) => boolean;
isValidForFeed: (mutedKeywordRegex: RegExp, blockedDomains:Set<string>) => boolean;
resolve: () => Promise<Toot>;
resolveID: () => Promise<string>;
tagNames: () => Set<string>;
};
/**
* Class representing a Mastodon {@linkcode Toot} with helper methods for scoring, filtering, and more.
* Extends the base Mastodon {@linkcode https://docs.joinmastodon.org/entities/Status/ Status} object.
* Note: the base {@linkcode https://docs.joinmastodon.org/entities/Status/ Status} class's
* properties are not documented here.
*
* @implements {TootObj}
* @extends {mastodon.v1.Status}
* @property {Account[]} accounts - Array with the author of the toot and (if it exists) the account that retooted it.
* @property {number} ageInHours - Age of this toot in hours.
* @property {mastodon.v1.CustomEmoji[]} allEmojis - All custom emojis in the toot, including the author's.
* @property {MediaAttachmentType} [attachmentType] - The type of media in the toot (image, video, audio, etc.).
* @property {Account} author - The account that posted this toot, not the account that reblogged it.
* @property {string} [completedAt] - Timestamp when a full deep inspection of the toot was last completed.
* @property {string} [contentTagsParagraph] - If the last paragraph is 100% hashtag this is the HTML for that paragraph.
* @property {string} description - A string describing the toot, including author, content, and createdAt.
* @property {MastodonTag[]} [followedTags] - Array of tags that the user follows that exist in this toot.
* @property {string} homeserver - The homeserver of the author of the toot.
* @property {boolean} isDM - True if the toot is a direct message (DM) to the user.
* @property {boolean} isFollowed - True if this toot is from a followed account or contains a followed tag.
* @property {boolean} isLocal - True if this toot is from the FediAlgo user's home server.
* @property {boolean} isPrivate - True if it's for followers only.
* @property {boolean} isTrending - True if it's a trending toot or contains any trending hashtags or links.
* @property {string} lastEditedAt - The date when the toot was last edited, or createdAt if never edited.
* @property {number} [numTimesShown] - Managed in client app. # of times the Toot has been shown to the user.
* @property {TagWithUsageCounts[]} [participatedTags] - Tags that the user has participated in that exist in this toot
* @property {number} popularity - Sum of the trendingRank, numReblogs, replies, and local server favourites. Currently unused.
* @property {Toot} realToot - The toot that was reblogged if it's a reblog, otherwise this toot.
* @property {string} realURI - URI for the realToot.
* @property {string} realURL - Default to this.realURI if url property is empty.
* @property {SerializableToot | null} [reblog] - The toot that was retooted (if any).
* @property {AccountLike[]} [reblogsBy] - The accounts that retooted this toot (if any)
* @property {string[]} replyMentions - The webfinger URIs of the accounts mentioned in the toot + the author prepended with @.
* @property {string} [resolvedID] - This Toot with URLs resolved to homeserver versions
* @property {number} score - Current overall score for this toot.
* @property {TootScore} [scoreInfo] - Scoring info for weighting/sorting this toot
* @property {string[]} [sources] - Source of the toot (e.g. trending tag toots, home timeline, etc.)
* @property {Date} tootedAt - Timestamp of toot's createdAt.
* @property {TrendingLink[]} [trendingLinks] - Links that are trending in this toot
* @property {number} [trendingRank] - Most trending on a server gets a 10, next is a 9, etc.
* @property {TagWithUsageCounts[]} [trendingTags] - Tags that are trending in this toot
* @property {Toot[]} withRetoot - Returns the toot and the retoot, if it exists, as an array.
* @property {mastodon.v1.MediaAttachment[]} [audioAttachments]
* @property {mastodon.v1.MediaAttachment[]} [imageAttachments]
* @property {mastodon.v1.MediaAttachment[]} [videoAttachments]
*/
export default class Toot implements TootObj {
// Props from mastodon.v1.Status
id!: string;
uri!: string;
application!: mastodon.v1.Application;
@Type(() => Account) account!: Account;
content!: string;
createdAt!: string;
editedAt: string | null = null;
emojis!: mastodon.v1.CustomEmoji[];
favouritesCount!: number;
mediaAttachments!: mastodon.v1.MediaAttachment[];
mentions!: mastodon.v1.StatusMention[];
quotesCount!: number;
quoteApproval!: QuoteApproval;
reblogsCount!: number;
repliesCount!: number;
sensitive!: boolean;
spoilerText!: string;
tags!: TagWithUsageCounts[];
visibility!: mastodon.v1.StatusVisibility;
// Optional fields
bookmarked?: boolean | null;
card?: mastodon.v1.PreviewCard | null;
favourited?: boolean | null;
filtered?: mastodon.v1.FilterResult[];
language?: string | null;
inReplyToAccountId?: string | null;
inReplyToId?: string | null;
muted?: boolean | null;
pinned?: boolean | null;
poll?: mastodon.v1.Poll | null;
@Type(() => Toot) reblog?: Toot | null;
reblogged?: boolean | null;
text?: string | null;
url?: string | null;
// extensions to mastodon.v1.Status. Most of these are set in completeProperties()
completedAt?: string;
followedTags?: mastodon.v1.Tag[]; // Array of tags that the user follows that exist in this toot
numTimesShown!: number;
participatedTags?: TagWithUsageCounts[]; // Array of tags that the user has participated in that exist in this toot
@Type(() => Account) reblogsBy!: Account[]; // The accounts that retooted this toot
resolvedID?: string; // This Toot with URLs resolved to homeserver versions
scoreInfo?: TootScore; // Scoring info for weighting/sorting this toot
sources?: string[]; // Source of the toot (e.g. trending tag toots, home timeline, etc.)
trendingLinks?: TrendingLink[]; // Links that are trending in this toot
trendingRank?: number; // Most trending on a server gets a 10, next is a 9, etc.
trendingTags?: TagWithUsageCounts[]; // Tags that are trending that appear in this toot
audioAttachments!: mastodon.v1.MediaAttachment[];
imageAttachments!: mastodon.v1.MediaAttachment[];
videoAttachments!: mastodon.v1.MediaAttachment[];
// See JSDoc comment for explanations of the various getters
get accounts(): Account[] { return this.withRetoot.map((toot) => toot.account)};
get ageInHours(): number { return AgeIn.hours(this.createdAt) };
get allEmojis(): mastodon.v1.CustomEmoji[] { return (this.emojis || []).concat(this.account.emojis || []) };
get author(): Account { return this.realToot.account };
get homeserver(): string { return this.author.homeserver };
get isDM(): boolean { return this.visibility === TootVisibility.DIRECT_MSG };
get isFollowed(): boolean { return !!(this.accounts.some(a => a.isFollowed) || this.realToot.followedTags?.length) };
get isLocal(): boolean { return MastoApi.instance.isLocalUrl(this.realURI) };
get isPrivate(): boolean { return this.visibility === TootVisibility.PRIVATE };
get isTrending(): boolean { return !!(this.trendingRank || this.trendingLinks?.length || this.trendingTags?.length) };
get lastEditedAt(): string { return this.editedAt || this.createdAt };
get popularity() { return sumArray([this.favouritesCount, this.reblogsCount, this.repliesCount, this.trendingRank]) };
get realToot(): Toot { return this.reblog ?? this };
get realURI(): string { return this.realToot.uri };
get realURL(): string { return this.realToot.url || this.realURI };
get replyMentions(): string[] { return [...this.accounts, ...(this.mentions || [])].map((m) => at(m.acct)) };
get score(): number { return this.scoreInfo?.score || 0 };
get tootedAt(): Date { return new Date(this.createdAt) }; // TODO: should this consider the values in reblogsBy?
get withRetoot(): Toot[] { return [this, ...asOptionalArray(this.reblog)] };
get attachmentType(): MediaCategory | undefined {
if (this.imageAttachments.length) {
return MediaCategory.IMAGE;
} else if (this.videoAttachments.length) {
return MediaCategory.VIDEO;
} else if (this.audioAttachments.length) {
return MediaCategory.AUDIO;
}
}
// TODO: should this take a fontSize argument like contentParagraphs()?
get contentTagsParagraph(): string | undefined {
const finalParagraph = this.contentParagraphs().slice(-1)[0];
return HASHTAG_PARAGRAPH_REGEX.test(finalParagraph) ? finalParagraph : undefined;
}
get description(): string {
const msg = `${this.account.description} [url="${this.url || this.uri}"`;
return `${msg}, createdAt="${toISOFormat(this.createdAt)}"]: "${this.contentShortened()}"`;
}
// Temporary caches for performance (profiler said contentWithCard() was using a lot of runtime)
private contentCache: TootCache = {};
/**
* Alternate constructor because {@linkcode https://www.npmjs.com/package/class-transformer class-transformer}
* doesn't work with constructor arguments.
* @static
* @param {SerializableToot} toot - The toot data to build from.
* @returns {Toot} The constructed Toot instance.
*/
static build(toot: SerializableToot | Toot): Toot {
if (toot instanceof Toot) {
// Clear the cache if the toot was edited // TODO: Probably not the ideal time to clear the cache
if (toot.editedAt) toot.contentCache = {};
return toot;
}
const tootObj = new Toot();
tootObj.id = toot.id;
tootObj.uri = toot.uri;
tootObj.account = Account.build(toot.account);
tootObj.application = toot.application;
tootObj.bookmarked = toot.bookmarked;
tootObj.card = toot.card;
tootObj.content = toot.content;
tootObj.createdAt = toot.createdAt;
tootObj.editedAt = toot.editedAt;
tootObj.emojis = toot.emojis;
tootObj.favourited = toot.favourited;
tootObj.favouritesCount = toot.favouritesCount;
tootObj.filtered = toot.filtered;
tootObj.inReplyToAccountId = toot.inReplyToAccountId;
tootObj.inReplyToId = toot.inReplyToId;
tootObj.language = toot.language;
tootObj.mediaAttachments = toot.mediaAttachments || [];
tootObj.mentions = toot.mentions;
tootObj.muted = toot.muted;
tootObj.pinned = toot.pinned;
tootObj.poll = toot.poll;
tootObj.quotesCount = toot.quotesCount;
tootObj.quoteApproval = toot.quoteApproval;
tootObj.reblogged = toot.reblogged;
tootObj.reblogsCount = toot.reblogsCount;
tootObj.repliesCount = toot.repliesCount;
tootObj.sensitive = toot.sensitive;
tootObj.spoilerText = toot.spoilerText;
tootObj.tags = toot.tags;
tootObj.text = toot.text;
tootObj.url = toot.url;
tootObj.visibility = toot.visibility;
// Unique to fedialgo
tootObj.numTimesShown = toot.numTimesShown || 0;
tootObj.completedAt = toot.completedAt;
tootObj.followedTags = toot.followedTags;
tootObj.reblog = toot.reblog ? Toot.build(toot.reblog) : undefined;
// TODO: the reblogsBy don't necessarily have the isFollowed flag set correctly
tootObj.reblogsBy = (toot.reblogsBy ?? []).map(account => Account.build(account));
tootObj.resolvedID = toot.resolvedID;
tootObj.scoreInfo = toot.scoreInfo;
tootObj.sources = toot.sources;
tootObj.trendingLinks = toot.trendingLinks;
tootObj.trendingRank = toot.trendingRank;
tootObj.trendingTags = toot.trendingTags;
tootObj.repair();
// These must be set after repair() has a chance to fix any broken media types
tootObj.audioAttachments = tootObj.attachmentsOfType(MediaCategory.AUDIO);
tootObj.imageAttachments = tootObj.attachmentsOfType(MediaCategory.IMAGE);
tootObj.videoAttachments = VIDEO_TYPES.flatMap((videoType) => tootObj.attachmentsOfType(videoType));
if (tootObj.account.suspended) {
tootLogger.warn(`Toot from suspended account:`, tootObj);
} else if (tootObj.account.limited) {
tootLogger.trace(`Toot from limited account:`, tootObj);
}
return tootObj;
}
/**
* True if toot contains {@linkcode pattern} in the tags, the content, or the link preview card description.
* @param {string} pattern - The string to search for.
* @returns {boolean}
*/
containsString(pattern: string): boolean {
return this.matchesRegex(wordRegex(pattern));
}
/**
* Return true if the toot contains the tag or hashtag. If fullScan is true uses containsString() to search.
* @param {TagWithUsageCounts} tag - The tag to search for.
* @param {boolean} [fullScan] - Whether to use full scan.
* @returns {boolean}
*/
containsTag(tag: TagWithUsageCounts, fullScan?: boolean): boolean {
if (fullScan && isValidForSubstringSearch(tag)) {
if (!tag.regex) {
tootLogger.warn(`containsTag() called on tag without regex:`, tag);
tag.regex = wordRegex(tag.name);
}
return this.matchesRegex(tag.regex);
} else {
try {
return this.tagNames().has(tag.name);
} catch (err) {
tootLogger.error(`Error in containsTag("${tag.name}"), current cache:`, this.contentCache, err);
this.contentCache.tagNames = new Set<string>((this.tags || []).map((tag) => tag.name))
return this.contentCache.tagNames!.has(tag.name);
}
}
}
/**
* Generate a string describing the followed, trending, and participated tags in the toot.
* TODO: add favourited tags?
* @returns {string | undefined}
*/
containsTagsMsg(): string | undefined {
let msgs = [
this.containsTagsOfTypeMsg(TypeFilterName.FOLLOWED_HASHTAGS),
this.containsTagsOfTypeMsg(TypeFilterName.TRENDING_TAGS),
this.containsTagsOfTypeMsg(TypeFilterName.PARTICIPATED_TAGS),
];
msgs = msgs.filter((msg) => msg);
return msgs.length ? `Contains ${msgs.join("; ")}` : undefined;
}
/**
* Returns {@linkcode true} if the fedialgo user is mentioned in this {@linkcode Toot}.
* @returns {boolean}
*/
containsUserMention(): boolean {
return this.mentions.some((mention) => mention.acct == MastoApi.instance.user.webfingerURI);
}
/**
* Return all but the last paragraph if that last paragraph is just hashtag links.
* @param {number} [fontSize=DEFAULT_FONT_SIZE] - Size in pixels of any emoji <img> tags. Should match surrounding txt.
* @returns {string}
*/
contentNonTagsParagraphs(fontSize: number = DEFAULT_FONT_SIZE): string {
const paragraphs = this.contentParagraphs(fontSize);
if (this.contentTagsParagraph) paragraphs.pop(); // Remove the last paragraph if it's just hashtags
return paragraphs.join("\n");
}
/**
* Break up the content into paragraphs and add <img> tags for custom emojis.
* @param {number} [fontSize=DEFAULT_FONT_SIZE] - Size in pixels of any emoji <img> tags. Should match surrounding txt.
* @returns {string[]}
*/
contentParagraphs(fontSize: number = DEFAULT_FONT_SIZE): string[] {
return htmlToParagraphs(this.contentWithEmojis(fontSize));
}
/**
* Shortened string of content property stripped of HTML tags.
* @param {number} [maxChars]
* @returns {string}
*/
contentShortened(maxChars?: number): string {
maxChars ||= config.toots.maxContentPreviewChars;
let content = replaceHttpsLinks(this.contentString());
// Fill in placeholders if content string is empty, truncate it if it's too long
if (content.length == 0) {
content = `<${capitalCase(this.attachmentType || 'empty')} post by ${this.author.description}>`;
} else if (content.length > maxChars) {
content = `${content.slice(0, maxChars)}...`;
}
return content;
}
/**
* Replace custom emoji shortcodes (e.g. ":myemoji:") with image tags.
* @param {number} [fontSize=DEFAULT_FONT_SIZE] - Size in pixels of any emoji imgs. Should match surrounding text.
* @returns {string}
*/
contentWithEmojis(fontSize: number = DEFAULT_FONT_SIZE): string {
if (!this.contentCache[TootCacheKey.CONTENT_WITH_EMOJIS]) {
this.contentCache[TootCacheKey.CONTENT_WITH_EMOJIS] = this.addEmojiHtmlTags(this.content, fontSize);
}
return this.contentCache[TootCacheKey.CONTENT_WITH_EMOJIS];
}
/**
* Fetch the conversation for this toot (Mastodon API calls this a
* {@linkcode https://docs.joinmastodon.org/entities/Context/ Context}).
* @returns {Promise<Toot[]>}
*/
async getConversation(): Promise<Toot[]> {
const action = LoadAction.GET_CONVERSATION;
const logger = tootLogger.tempLogger(action);
logger.debug(`Fetching conversation for toot:`, this.description);
const startTime = new Date();
const context = await MastoApi.instance.api.v1.statuses.$select(await this.resolveID()).context.fetch();
const toots = await Toot.buildToots([...context.ancestors, this, ...context.descendants], action);
logger.trace(`Fetched ${toots.length} toots ${ageString(startTime)}`, toots.map(t => t.description));
return toots;
}
/**
* Get an individual score for this {@linkcode Toot}.
* @param {ScoreType} scoreType - The score type.
* @param {ScoreName} name - The score name.
* @returns {number}
*/
getIndividualScore(scoreType: ScoreType, name: ScoreName): number {
if (isFinite(this.scoreInfo?.scores?.[name]?.[scoreType])) {
return this.scoreInfo!.scores[name][scoreType];
} else {
tootLogger.trace(`no score available for ${scoreType}/${name}:`, this);
return 0;
}
}
/**
* Return true if the {@linkcode Toot} should not be filtered out of the feed by the current filters.
* @param {FeedFilterSettings} filters - The feed filter settings.
* @returns {boolean}
*/
isInTimeline(filters: FeedFilterSettings): boolean {
const isOK = Object.values(filters.booleanFilters).every((section) => section.isAllowed(this));
return isOK && Object.values(filters.numericFilters).every((filter) => filter.isAllowed(this));
}
/**
* Return false if {@linkcode Toot} should be discarded from feed altogether and permanently.
* @param {mastodon.v2.Filter[]} serverSideFilters - Server-side filters.
* @returns {boolean}
*/
isValidForFeed(mutedKeywordRegex: RegExp, blockedDomains: Set<string>): boolean {
if (this.reblog?.muted || this.muted) {
tootLogger.trace(`Removing toot from muted account (${this.author.description}):`, this);
return false;
} else if (Date.now() < this.tootedAt.getTime()) {
// Sometimes there are wonky statuses that are like years in the future so we filter them out.
tootLogger.warn(`Removing toot with future timestamp:`, this);
return false;
} else if (this.filtered?.length || this.reblog?.filtered?.length) {
// The user can configure suppression filters through a Mastodon GUI (webapp or whatever)
const filterMatches = (this.filtered || []).concat(this.reblog?.filtered || []);
const filterMatchStr = filterMatches[0].keywordMatches?.join(' ');
tootLogger.trace(`Removing toot matching server filter (${filterMatchStr}): ${this.description}`);
return false;
} else if (this.tootedAt < timelineCutoffAt()) {
tootLogger.trace(`Removing toot older than ${timelineCutoffAt()}:`, this.tootedAt);
return false;
} else if (blockedDomains.has(this.author.homeserver)) {
tootLogger.trace(`Removing toot from blocked domain:`, this);
return false;
} else if (this.matchesRegex(mutedKeywordRegex)) {
tootLogger.trace(`Removing toot matching muted keyword regex:`, this);
return false;
}
return true;
}
/**
* Make an API call to get this {@linkcode Toot}'s URL on the FediAlgo user's home server instead of on
* the {@linkcode Toot}'s home server.
* @example "https://fosstodon.org/@kate/114360290341300577" => "https://universeodon.com/@kate@fosstodon.org/114360290578867339"
* @returns {Promise<string>} The home server URL.
*/
async localServerUrl(): Promise<string> {
const homeURL = `${this.account.localServerUrl}/${await this.resolveID()}`;
tootLogger.debug(`<homeserverURL()> converted '${this.realURL}' to '${homeURL}'`);
return homeURL;
}
/**
* True if {@linkcode Toot} matches {@linkcode regex} in the tags, the content, or the link preview card description.
* @param {RegExp} regex - The string to search for.
* @returns {boolean}
*/
matchesRegex(regex: RegExp): boolean {
return regex.test(this.contentWithCard());
}
/**
* Get {@linkcode https://docs.joinmastodon.org/entities/Status/ Status} obj for this {@linkcode Toot}
* from user's home server so the property URLs point to the home server.
* @returns {Promise<Toot>}
*/
async resolve(): Promise<Toot> {
try {
tootLogger.trace(`Resolving local toot ID for`, this);
const resolvedToot = await MastoApi.instance.resolveToot(this);
this.resolvedID = resolvedToot.id; // Cache the resolved ID for future calls
return resolvedToot;
} catch (error) {
tootLogger.error(`Error resolving a toot:`, error, `\nThis was the toot:`, this);
throw error;
}
}
/**
* Get {@linkcode https://docs.joinmastodon.org/entities/Status/ Status} ID for {@linkcode Toot} from
* user's home server so the property URLs point to the home server.
* @returns {Promise<string>}
*/
async resolveID(): Promise<string> {
this.resolvedID ||= (await this.resolve()).id;
return this.resolvedID;
}
/**
* Get the {@linkcode Toot}'s tags as a {@linkcode Set} of strings. Caches results for future calls.
* @returns {Set<string>} Set of the names of the tags in this toot.
*/
tagNames(): Set<string> {
// TODO: class-transformer doesn't serialize Sets correctly so we have to check if it's an array
// See https://github.com/typestack/class-transformer/issues/54
if (!this.contentCache.tagNames || Array.isArray(this.contentCache.tagNames)) {
this.contentCache.tagNames = new Set((this.tags || []).map((tag) => tag.name));
}
return this.contentCache.tagNames;
}
//////////////////////////////
// Private methods //
//////////////////////////////
/**
* Replace custom emoji shortcodes (e.g. ":myemoji:") with HTML image tags in a string.
* @private
*/
private addEmojiHtmlTags(str: string, fontSize: number = DEFAULT_FONT_SIZE): string {
return replaceEmojiShortcodesWithImgTags(str, this.allEmojis, fontSize);
}
/**
* Return {@linkcode MediaAttachmentType} objects with type == {@linkcode attachmentType}
* @private
* @param {MediaAttachmentType} attachmentType - The attachment type to filter for.
* @returns {mastodon.v1.MediaAttachment[]}
*/
private attachmentsOfType(attachmentType: mastodon.v1.MediaAttachmentType): mastodon.v1.MediaAttachment[] {
return this.realToot.mediaAttachments.filter(attachment => attachment.type == attachmentType);
}
/**
* Some properties cannot be repaired and/or set until info about the user is available.
* Also some properties are very slow - in particular all the tag and trendingLink calcs.
* {@linkcode isDeepInspect} argument is used to determine if we should do the slow
* calculations or quick ones.
* @private
* @param {UserData} userData - The user data.
* @param {TrendingLink[]} trendingLinks - The trending links.
* @param {TagWithUsageCounts[]} trendingTags - The trending tags.
* @param {TootSource} [source] - The source of the toot (e.g. REFRESH_HOME_TIMELINE).
*/
private completeProperties(
userData: UserData,
trendingLinks: TrendingLink[],
trendingTags: TagWithUsageCounts[],
source?: TootSource
): void {
if (source) {
this.sources ??= [];
// REFRESH_MUTED_ACCOUNTS isn't a sources for toots even if it's a reason for invoking this method.
if (source != LoadAction.REFRESH_MUTED_ACCOUNTS && !this.sources.includes(source)) {
this.sources?.push(source);
}
}
const isDeepInspect = !source;
this.muted ||= (this.author.webfingerURI in userData.mutedAccounts);
this.account.isFollowed ||= (this.account.webfingerURI in userData.followedAccounts);
if (this.reblog) {
this.reblog.account.isFollowed ||= (this.reblog.account.webfingerURI in userData.followedAccounts);
}
// TODO: We handled muted/followed before checking if complete so we can refresh mutes & follows which sucks
if (this.isComplete()) return;
const toot = this.realToot; // Retoots never have their own tags, etc.
// containsString() matched way too many toots so we use containsTag() for participated tags
// TODO: things might be fast enough to try this again
toot.participatedTags = userData.participatedTags.filter(tag => toot.containsTag(tag)).objs;
// With all the containsString() calls it takes ~1.1 seconds to build 40 toots
// Without them it's ~0.1 seconds. In particular the trendingLinks are slow! maybe 90% of that time.
toot.followedTags = userData.followedTags.filter(tag => toot.containsTag(tag, isDeepInspect)).objs;
toot.trendingTags = trendingTags.filter(tag => toot.containsTag(tag, isDeepInspect));
// Only set the completedAt field if isDeepInspect is true // TODO: might be fast enough to try this again?
if (isDeepInspect) {
toot.trendingLinks = trendingLinks.filter(link => toot.matchesRegex(link.regex!));
this.completedAt = toot.completedAt = new Date().toISOString(); // Note the multiple assignmnet!
} else {
toot.trendingLinks ??= []; // Very slow to calculate so skip it unless isDeepInspect is true
}
}
// Generate a string describing the followed and trending tags in the toot
private containsTagsOfTypeMsg(tagType: TypeFilterName): string | undefined {
let tags: Hashtag[] = [];
if (tagType == TypeFilterName.FOLLOWED_HASHTAGS) {
tags = this.followedTags || [];
} else if (tagType == TypeFilterName.PARTICIPATED_TAGS) {
tags = this.participatedTags || [];
} else if (tagType == TypeFilterName.TRENDING_TAGS) {
tags = this.trendingTags || [];
} else {
tootLogger.warn(`containsTagsOfTypeMsg() called with invalid tagType: ${tagType}`);
return;
}
if (!tags.length) return;
const tagTypeStr = capitalCase(tagType).replace(/ Tag/, " Hashtag");
return `${tagTypeStr}: ${tags.map(t => `#${t.name}`).join(", ")}`;
}
/**
* Return the toot's 'content' field stripped of HTML tags and emojis.
* @private
* @returns {string}
*/
private contentString(): string {
return htmlToText(this.realToot.contentWithEmojis());
}
/**
* Return the toot's content + link description stripped of everything (links, mentions, tags, etc.)
* Used for inferring the Toot's language.
* @private
* @returns {string}
*/
private contentStripped(): string {
if (!this.contentCache[TootCacheKey.CONTENT_STRIPPED]) {
const str = removeEmojis(removeTags(removeLinks(this.contentWithCard())));
this.contentCache[TootCacheKey.CONTENT_STRIPPED] = collapseWhitespace(removeMentions(str));
}
return this.contentCache[TootCacheKey.CONTENT_STRIPPED];
}
/**
* Return the content with the card title and description added in parentheses, stripped of diacritics for
* matching tags. Returned string is cached for future calls to {@linkcode containsString()} and
* {@linkcode containsTag()} etc.
* @private
* @returns {string}
*/
private contentWithCard(): string {
if (!this.contentCache[TootCacheKey.CONTENT_WITH_CARD]) {
const cardContent = [this.card?.title || "", this.card?.description || ""].join(" ").trim();
const suffix = optionalSuffix(cardContent, htmlToText);
const txt = `${this.contentString()} ${this.imageAltText()} ${suffix}`.trim();
this.contentCache[TootCacheKey.CONTENT_WITH_CARD] = removeDiacritics(txt);
}
return this.contentCache[TootCacheKey.CONTENT_WITH_CARD];
}
// Figure out an appropriate language for the toot based on the content.
private determineLanguage(): void {
const text = this.contentStripped();
// if (this.isUsersOwnToot() || text.length < config.toots.minCharsForLanguageDetect) {
if (text.length < config.toots.minCharsForLanguageDetect) {
this.language ??= config.locale.defaultLanguage;
return;
}
const langDetectInfo = detectLanguage(text);
const { chosenLanguage, langDetector, tinyLD } = langDetectInfo;
const langLogObj = {...langDetectInfo, text, toot: this, tootLanguage: this.language};
const logTrace = (msg: string) => repairLogger.trace(`${msg} for "${text}"`, langLogObj);
// If there's nothing detected log a warning (if text is long enough) and set language to default
if ((tinyLD.languageAccuracies.length + langDetector.languageAccuracies.length) == 0) {
// Last ditch effort with detectHashtagLanguage() for foreign scripts
const foreignScript = detectForeignScriptLanguage(text);
if (foreignScript) {
logTrace(`Falling back to foreign script "${foreignScript}" as language`);
this.language = foreignScript;
} else if (text.length > (config.toots.minCharsForLanguageDetect * 2)) {
repairLogger.warn(`no language detected`, langLogObj);
}
this.language ??= config.locale.defaultLanguage;
return;
}
// If either language detection matches this.language return
if (this.language && (tinyLD.chosenLang == this.language || langDetector.chosenLang == this.language)) {
return;
}
// Or if we have successfully detected a language assign it to this.language and return
if (chosenLanguage) {
// Don't overwrite "zh-TW" with "zh"
if (this.language?.startsWith(chosenLanguage)) {
return;
} else if (this.language && this.language != UNKNOWN) {
logTrace(`Using chosenLanguage "${chosenLanguage}" to replace "${this.language}"`);
}
this.language = chosenLanguage;
return;
}
if (FOREIGN_SCRIPTS.has(tinyLD.chosenLang) && this.language?.startsWith(tinyLD.chosenLang!)) {
logTrace(`Using existing foreign lang "${this.language}" even with low accuracy`);
return;
}
// Prioritize English in edge cases with low tinyLD accuracy but "en" either in toot or in LangDetector result
if (!tinyLD.isAccurate && langDetector.isAccurate && langDetector.chosenLang == LANGUAGE_NAMES.english) {
logTrace(`Accepting "en" from langDetector.detectedLang`);
this.language = LANGUAGE_NAMES.english;
return;
}
if (this.language) {
if (text.length > (2 * config.toots.minCharsForLanguageDetect)) {
logTrace(`No guess good enough to override language "${this.language}" for "${text}"`);
}
} else {
logTrace(`Defaulting language prop to "en"`);
this.language ??= config.locale.defaultLanguage;
}
// If this is the user's own toot and we have a language set, log it
// TODO: remove this eventually
if (this.isUsersOwnToot() && this.language != config.locale.defaultLanguage) {
repairLogger.warn(`User's own toot language set to "${this.language}"`, langLogObj);
}
}
/**
* @private
* @returns {string} Alt text from any included multimedia objects.
*/
private imageAltText(): string {
return this.mediaAttachments.map((media) => media.description || "").join(" ");
}
/**
* Returns true if the {@linkcode Toot} needs to be (re-)evaluated for trending tags, links, etc.
* @private
*/
private isComplete(): boolean {
if (!this.completedAt || (this.completedAt < this.lastEditedAt) || !this.trendingLinks) {
return false;
}
// If we have completed it, check if we need to re-evaluate for newer trending tags, links, etc.
return (
// Check if toot was completed long enough ago that we might want to re-evaluate it
AgeIn.minutes(this.completedAt) < config.minTrendingMinutesUntilStale()
// But not tooted so long ago that there's little chance of new data
|| AgeIn.minutes(this.createdAt) > config.toots.completeAfterMinutes
);
}
/**
* Returns true if this toot is by the fedialgo user.
* @private
*/
private isUsersOwnToot(): boolean {
return this.accounts.some((account) => account.webfingerURI == MastoApi.instance.user.webfingerURI)
}
/**
* Repair toot properties:
* 1. Set {@linkcode Toot.application.name} to UNKNOWN if missing
* 2. Call {@linkcode Toot.determineLanguage} to set the language
* 3. Lowercase all tags
* 4. Repair {@linkcode mediaAttachment} types if reparable based on URL file extension
* 5. Repair {@linkcode https://docs.joinmastodon.org/entities/StatusMention/ StatusMention} objects for users on home server
* @private
*/
private repair(): void {
this.application ??= {name: UNKNOWN};
this.application.name ??= UNKNOWN;
this.tags.forEach(repairTag); // Repair Tags
this.determineLanguage(); // Determine language
if (this.reblog) {
this.trendingRank ||= this.reblog.trendingRank;
const reblogsByAccts = this.reblogsBy.map((account) => account.webfingerURI);
if (!reblogsByAccts.includes(this.account.webfingerURI)) {
this.reblog.reblogsBy.push(this.account);
this.reblog.reblogsBy = sortObjsByProps(this.reblog.reblogsBy, ["displayName"], true, true);
}
}
// Check for weird media types
this.mediaAttachments.forEach((media) => {
if (media.type == UNKNOWN) {
const category = determineMediaCategory(media.remoteUrl);
if (category) {
repairLogger.trace(`Repaired broken ${category} attachment in toot:`, this);
media.type = category;
} else if (this.uri?.includes(BSKY_BRIDGY) && media.previewUrl?.endsWith("/small") && !media.previewRemoteUrl) {
// Special handling for Bluesky bridge images
repairLogger.debug(`Repairing broken bluesky bridge image attachment in toot:`, this);
media.type = MediaCategory.IMAGE;
} else {
repairLogger.warn(`Unknown media type for URL: '${media.remoteUrl}' for toot:`, this);
}
} else if (!MEDIA_TYPES.includes(media.type)) {
repairLogger.warn(`Unknown media of type: '${media.type}' for toot:`, this);
}
if (isEmpty(media?.url)) {
repairLogger.warn(`Media attachment URL is empty for toot:`, this);
}
});
// Repair StatusMention.acct field for users on the home server by appending @serverDomain
this.mentions.forEach((mention) => {
if (mention.acct && !mention.acct.includes("@")) {
mention.acct += at(extractDomain(mention.url));
}
})
}
////////////////////////////////
// Static methods //
////////////////////////////////
/**
* Build array of new {@linkcode Toot} objects from an array of
* {@linkcode https://docs.joinmastodon.org/entities/Status/ Status} objects (or {@linkcode Toot}s).
* {@linkcode Toot}s returned are sorted by score and should have most of their properties set correctly.
* @param {TootLike[]} statuses - Array of status objects or Toots.
* @param {TootSource} source - The source label for logging.
* @returns {Promise<Toot[]>}
*/
static async buildToots(statuses: TootLike[], source: TootSource): Promise<Toot[]> {
if (!statuses.length) return []; // Avoid the data fetching if we don't to build anything
const logger = tootLogger.tempLogger(source, `buildToots`);
const startedAt = new Date();
let toots = await this.completeToots(statuses, logger, source);
toots = await this.removeInvalidToots(toots, logger);
toots = Toot.dedupeToots(toots, logger);
// "Best effort" scoring. Note scoreToots() does not sort 'toots' in place but the return value is sorted.
const tootsSortedByScore = await Scorer.scoreToots(toots, false);
if (source != LoadAction.GET_CONVERSATION) {
toots = this.removeUsersOwnToots(tootsSortedByScore, logger);
}
logger.trace(`${toots.length} toots built in ${ageString(startedAt)}`);
return toots;
}
/**
* Fetch all the data we need to set dependent properties and set them on the toots.
* If {@linkcode source} arg is provided we set it as the {@linkcode Toot.source} prop and avoid doing an
* {@linkcode Toot.isDeepInspect} completion.
* @param {TootLike[]} toots - Array of toots to complete.
* @param {Logger} logger - Logger for logging.
* @param {string} [source] - Optional source label.
* @returns {Promise<Toot[]>}
*/
static async completeToots(toots: TootLike[], logger: Logger, source?: TootSource): Promise<Toot[]> {
logger = logger.tempLogger(`completeToots(${source || ''})`);
const isDeepInspect = !source;
const startedAt = new Date();
const userData = await MastoApi.instance.getUserData();
const trendingTags = (await MastodonServer.fediverseTrendingTags()).topObjs();
const trendingLinks = isDeepInspect ? (await MastodonServer.fediverseTrendingLinks()) : []; // Skip trending links
let completedToots: TootLike[] = [];
let incompleteToots = toots;
// If isDeepInspect separate toots that need completing bc it's slow to rely on isComplete() + batching
if (isDeepInspect) {
[completedToots, incompleteToots] = (split(toots, (t) => t instanceof Toot && t.isComplete()));
}
const newCompleteToots: Toot[] = await batchMap(
incompleteToots,
async (tootLike: TootLike) => {
const toot = (tootLike instanceof Toot) ? tootLike : Toot.build(tootLike);
toot.completeProperties(userData, trendingLinks, trendingTags, source);
return toot as Toot;
},
{
batchSize: config.toots.batchCompleteSize,
logger,
sleepBetweenMS: isDeepInspect ? config.toots.batchCompleteSleepBetweenMS : 0
}
);