upd: add history endpoint, make sure all areas use new convertAccount

This commit is contained in:
Mar0xy 2023-10-29 12:18:18 +01:00
parent 8fd669ff7d
commit c53323d237
No known key found for this signature in database
GPG key ID: 56569BBE47D2C828
6 changed files with 104 additions and 28 deletions

View file

@ -7,11 +7,11 @@ import type { Config } from '@/config.js';
import type { IMentionedRemoteUsers } from '@/models/Note.js';
import type { MiUser } from '@/models/User.js';
import type { NoteEditRepository, NotesRepository, UserProfilesRepository, UsersRepository } from '@/models/_.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { awaitAll } from '@/misc/prelude/await-all.js';
import { CustomEmojiService } from '@/core/CustomEmojiService.js';
import { GetterService } from '../GetterService.js';
import { ReactionService } from '@/core/ReactionService.js';
import { DriveFileEntityService } from '@/core/entities/DriveFileEntityService.js';
import { IdService } from '@/core/IdService.js';
export enum IdConvertType {
MastodonId,
@ -39,10 +39,14 @@ export class MastoConverters {
@Inject(DI.userProfilesRepository)
private userProfilesRepository: UserProfilesRepository,
@Inject(DI.noteEditRepository)
private noteEditRepository: NoteEditRepository,
private mfmService: MfmService,
private getterService: GetterService,
private customEmojiService: CustomEmojiService,
private reactionService: ReactionService,
private idService: IdService,
private driveFileEntityService: DriveFileEntityService,
) {
}
@ -64,6 +68,39 @@ export class MastoConverters {
};
}
public fileType(s: string): 'unknown' | 'image' | 'gifv' | 'video' | 'audio' {
if (s === 'image/gif') {
return 'gifv';
}
if (s.includes('image')) {
return 'image';
}
if (s.includes('video')) {
return 'video';
}
if (s.includes('audio')) {
return 'audio';
}
return 'unknown';
}
public encodeFile(f: any): Entity.Attachment {
return {
id: f.id,
type: this.fileType(f.type),
url: f.url,
remote_url: f.url,
preview_url: f.thumbnailUrl,
text_url: f.url,
meta: {
width: f.properties.width,
height: f.properties.height
},
description: f.comment ? f.comment : null,
blurhash: f.blurhash ? f.blurhash : null
};
}
public async getUser(id: string): Promise<MiUser> {
return this.getterService.getUser(id).then(p => {
return p;
@ -78,7 +115,7 @@ export class MastoConverters {
};
}
public async convertAccount(account: Entity.Account) {
public async convertAccount(account: Entity.Account | MiUser) {
const user = await this.getUser(account.id);
const profile = await this.userProfilesRepository.findOneBy({ userId: user.id });
const emojis = await this.customEmojiService.populateEmojis(user.emojis, user.host ? user.host : this.config.host);
@ -93,19 +130,26 @@ export class MastoConverters {
category: undefined,
});
});
const fqn = `${user.username}@${user.host ?? this.config.hostname}`;
let acct = user.username;
let acctUrl = `https://${user.host || this.config.host}/@${user.username}`;
if (user.host) {
acct = `${user.username}@${user.host}`;
acctUrl = `https://${user.host}/@${user.username}`;
}
return awaitAll({
id: account.id,
username: account.username,
acct: account.acct,
fqn: account.fqn,
display_name: account.display_name || account.username,
username: user.username,
acct: acct,
fqn: fqn,
display_name: user.name ?? user.username,
locked: user.isLocked,
created_at: account.created_at,
created_at: this.idService.parse(user.id).date.toISOString(),
followers_count: user.followersCount,
following_count: user.followingCount,
statuses_count: user.notesCount,
note: profile?.description ?? account.note,
url: account.url,
note: profile?.description ?? '',
url: user.uri ?? acctUrl,
avatar: user.avatarUrl ? user.avatarUrl : 'https://dev.joinsharkey.org/static-assets/avatar.png',
avatar_static: user.avatarUrl ? user.avatarUrl : 'https://dev.joinsharkey.org/static-assets/avatar.png',
header: user.bannerUrl ? user.bannerUrl : 'https://dev.joinsharkey.org/static-assets/transparent.png',
@ -118,6 +162,36 @@ export class MastoConverters {
});
}
public async getEdits(id: string) {
const note = await this.getterService.getNote(id);
if (!note) {
return {};
}
const noteUser = await this.getUser(note.userId).then(async (p) => await this.convertAccount(p));
const edits = await this.noteEditRepository.find({ where: { noteId: note.id }, order: { id: 'ASC' } });
const history: Promise<any>[] = [];
let lastDate = this.idService.parse(note.id).date;
for (const edit of edits) {
const files = this.driveFileEntityService.packManyByIds(edit.fileIds);
const item = {
account: noteUser,
content: this.mfmService.toMastoHtml(mfm.parse(edit.newText ?? ''), JSON.parse(note.mentionedRemoteUsers)).then(p => p ?? ''),
created_at: lastDate.toISOString(),
emojis: [],
sensitive: files.then(files => files.length > 0 ? files.some((f) => f.isSensitive) : false),
spoiler_text: edit.cw ?? '',
poll: null,
media_attachments: files.then(files => files.length > 0 ? files.map((f) => this.encodeFile(f)) : [])
};
lastDate = edit.updatedAt;
history.push(awaitAll(item));
}
return await Promise.all(history);
}
public async convertStatus(status: Entity.Status) {
const convertedAccount = this.convertAccount(status.account);
const note = await this.getterService.getNote(status.id);