mirror of
https://codeberg.org/yeentown/barkey.git
synced 2025-07-07 20:44:34 +00:00
fixes to CW and quote conversion for mastodon
This commit is contained in:
parent
1fa290c3eb
commit
81f7346f80
2 changed files with 34 additions and 11 deletions
|
@ -14,10 +14,13 @@
|
||||||
* @param additional Content warning to append
|
* @param additional Content warning to append
|
||||||
* @param reverse If true, then the additional CW will be prepended instead of appended.
|
* @param reverse If true, then the additional CW will be prepended instead of appended.
|
||||||
*/
|
*/
|
||||||
export function appendContentWarning(original: string | null | undefined, additional: string, reverse = false): string {
|
export function appendContentWarning(original: string | null | undefined, additional: string, reverse?: boolean): string;
|
||||||
|
export function appendContentWarning(original: string, additional: string | null | undefined, reverse?: boolean): string;
|
||||||
|
export function appendContentWarning(original: string | null | undefined, additional: string | null | undefined, reverse?: boolean): string | null;
|
||||||
|
export function appendContentWarning(original: string | null | undefined, additional: string | null | undefined, reverse = false): string | null {
|
||||||
// Easy case - if original is empty, then additional replaces it.
|
// Easy case - if original is empty, then additional replaces it.
|
||||||
if (!original) {
|
if (!original) {
|
||||||
return additional;
|
return additional ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Easy case - if the additional CW is empty, then don't append it.
|
// Easy case - if the additional CW is empty, then don't append it.
|
||||||
|
|
|
@ -19,6 +19,8 @@ import { IdService } from '@/core/IdService.js';
|
||||||
import type { Packed } from '@/misc/json-schema.js';
|
import type { Packed } from '@/misc/json-schema.js';
|
||||||
import { MastodonDataService } from '@/server/api/mastodon/MastodonDataService.js';
|
import { MastodonDataService } from '@/server/api/mastodon/MastodonDataService.js';
|
||||||
import { GetterService } from '@/server/api/GetterService.js';
|
import { GetterService } from '@/server/api/GetterService.js';
|
||||||
|
import { appendContentWarning } from '@/misc/append-content-warning.js';
|
||||||
|
import { isRenote } from '@/misc/is-renote.js';
|
||||||
|
|
||||||
// Missing from Megalodon apparently
|
// Missing from Megalodon apparently
|
||||||
// https://docs.joinmastodon.org/entities/StatusEdit/
|
// https://docs.joinmastodon.org/entities/StatusEdit/
|
||||||
|
@ -201,21 +203,37 @@ export class MastodonConverters {
|
||||||
if (!note) {
|
if (!note) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
const noteUser = await this.getUser(note.userId).then(async (p) => await this.convertAccount(p));
|
|
||||||
|
const noteUser = await this.getUser(note.userId);
|
||||||
|
const account = await this.convertAccount(noteUser);
|
||||||
const edits = await this.noteEditRepository.find({ where: { noteId: note.id }, order: { id: 'ASC' } });
|
const edits = await this.noteEditRepository.find({ where: { noteId: note.id }, order: { id: 'ASC' } });
|
||||||
const history: StatusEdit[] = [];
|
const history: StatusEdit[] = [];
|
||||||
|
|
||||||
|
const mentionedRemoteUsers = JSON.parse(note.mentionedRemoteUsers);
|
||||||
|
const renote = isRenote(note) ? await this.mastodonDataService.requireNote(note.renoteId, me) : null;
|
||||||
|
|
||||||
// TODO this looks wrong, according to mastodon docs
|
// TODO this looks wrong, according to mastodon docs
|
||||||
let lastDate = this.idService.parse(note.id).date;
|
let lastDate = this.idService.parse(note.id).date;
|
||||||
|
|
||||||
for (const edit of edits) {
|
for (const edit of edits) {
|
||||||
|
// TODO avoid re-packing files for each edit
|
||||||
const files = await this.driveFileEntityService.packManyByIds(edit.fileIds);
|
const files = await this.driveFileEntityService.packManyByIds(edit.fileIds);
|
||||||
|
|
||||||
|
const cwMFM = appendContentWarning(edit.cw, noteUser.mandatoryCW);
|
||||||
|
const cw = (cwMFM && this.mfmService.toMastoApiHtml(mfm.parse(cwMFM), mentionedRemoteUsers, true)) ?? '';
|
||||||
|
|
||||||
|
const isQuote = renote && (edit.cw || edit.newText || edit.fileIds.length > 0 || note.replyId);
|
||||||
|
const quoteUri = isQuote
|
||||||
|
? renote.url ?? renote.uri ?? `${this.config.url}/notes/${renote.id}`
|
||||||
|
: null;
|
||||||
|
|
||||||
const item = {
|
const item = {
|
||||||
account: noteUser,
|
account: account,
|
||||||
content: this.mfmService.toMastoApiHtml(mfm.parse(edit.newText ?? ''), JSON.parse(note.mentionedRemoteUsers)) ?? '',
|
content: this.mfmService.toMastoApiHtml(mfm.parse(edit.newText ?? ''), mentionedRemoteUsers, false, quoteUri) ?? '',
|
||||||
created_at: lastDate.toISOString(),
|
created_at: lastDate.toISOString(),
|
||||||
emojis: [], //FIXME
|
emojis: [], //FIXME
|
||||||
sensitive: edit.cw != null && edit.cw.length > 0,
|
sensitive: !!cw,
|
||||||
spoiler_text: edit.cw ?? '',
|
spoiler_text: cw,
|
||||||
media_attachments: files.length > 0 ? files.map((f) => this.encodeFile(f)) : [],
|
media_attachments: files.length > 0 ? files.map((f) => this.encodeFile(f)) : [],
|
||||||
};
|
};
|
||||||
lastDate = edit.updatedAt;
|
lastDate = edit.updatedAt;
|
||||||
|
@ -274,10 +292,12 @@ export class MastodonConverters {
|
||||||
|
|
||||||
const text = note.text;
|
const text = note.text;
|
||||||
const content = text !== null
|
const content = text !== null
|
||||||
? quoteUri
|
? quoteUri.then(quote => this.mfmService.toMastoApiHtml(mfm.parse(text), mentionedRemoteUsers, false, quote) ?? escapeMFM(text))
|
||||||
.then(quoteUri => this.mfmService.toMastoApiHtml(mfm.parse(text), mentionedRemoteUsers, false, quoteUri) ?? escapeMFM(text))
|
|
||||||
: '';
|
: '';
|
||||||
|
|
||||||
|
const cwMFM = appendContentWarning(note.cw, noteUser.mandatoryCW);
|
||||||
|
const cw = (cwMFM && this.mfmService.toMastoApiHtml(mfm.parse(cwMFM), mentionedRemoteUsers, true)) ?? '';
|
||||||
|
|
||||||
const reblogged = await this.mastodonDataService.hasReblog(note.id, me);
|
const reblogged = await this.mastodonDataService.hasReblog(note.id, me);
|
||||||
|
|
||||||
// noinspection ES6MissingAwait
|
// noinspection ES6MissingAwait
|
||||||
|
@ -301,8 +321,8 @@ export class MastodonConverters {
|
||||||
reblogged,
|
reblogged,
|
||||||
favourited: status.favourited,
|
favourited: status.favourited,
|
||||||
muted: status.muted,
|
muted: status.muted,
|
||||||
sensitive: status.sensitive || !!note.cw,
|
sensitive: status.sensitive || !!cw,
|
||||||
spoiler_text: note.cw ?? '',
|
spoiler_text: cw,
|
||||||
visibility: status.visibility,
|
visibility: status.visibility,
|
||||||
media_attachments: status.media_attachments.map(a => convertAttachment(a)),
|
media_attachments: status.media_attachments.map(a => convertAttachment(a)),
|
||||||
mentions: mentions,
|
mentions: mentions,
|
||||||
|
|
Loading…
Add table
Reference in a new issue