upd: convertAccount now fetches info from the DB

This commit is contained in:
Mar0xy 2023-10-29 02:59:36 +01:00
parent 95bcfd8ef3
commit 549bcf70db
No known key found for this signature in database
GPG Key ID: 56569BBE47D2C828
4 changed files with 59 additions and 29 deletions

View File

@ -305,9 +305,8 @@ export class MastodonApiServerService {
try { try {
const sharkId = _request.params.id; const sharkId = _request.params.id;
const data = await client.getAccount(sharkId); const data = await client.getAccount(sharkId);
const profile = await this.userProfilesRepository.findOneBy({ userId: sharkId }); const account = await this.mastoConverter.convertAccount(data.data);
data.data.fields = profile?.fields.map(f => ({ ...f, verified_at: null })) || []; reply.send(account);
reply.send(convertAccount(data.data));
} catch (e: any) { } catch (e: any) {
/* console.error(e); /* console.error(e);
console.error(e.response.data); */ console.error(e.response.data); */

View File

@ -1,15 +1,16 @@
import type { Config } from '@/config.js';
import { MfmService } from '@/core/MfmService.js';
import { DI } from '@/di-symbols.js';
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import { Entity } from 'megalodon'; import { Entity } from 'megalodon';
import mfm from 'mfm-js'; import mfm from 'mfm-js';
import { GetterService } from '../GetterService.js'; import { DI } from '@/di-symbols.js';
import { MfmService } from '@/core/MfmService.js';
import type { Config } from '@/config.js';
import type { IMentionedRemoteUsers } from '@/models/Note.js'; import type { IMentionedRemoteUsers } from '@/models/Note.js';
import type { MiUser } from '@/models/User.js'; import type { MiUser } from '@/models/User.js';
import type { NoteEditRepository, NotesRepository, UsersRepository } from '@/models/_.js'; import type { NoteEditRepository, NotesRepository, UserProfilesRepository, UsersRepository } from '@/models/_.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js'; import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { awaitAll } from '@/misc/prelude/await-all.js'; import { awaitAll } from '@/misc/prelude/await-all.js';
import { GetterService } from '../GetterService.js';
import { CustomEmojiService } from '@/core/CustomEmojiService.js';
export enum IdConvertType { export enum IdConvertType {
MastodonId, MastodonId,
@ -17,13 +18,13 @@ export enum IdConvertType {
} }
export const escapeMFM = (text: string): string => text export const escapeMFM = (text: string): string => text
.replace(/&/g, "&") .replace(/&/g, '&')
.replace(/</g, "&lt;") .replace(/</g, '&lt;')
.replace(/>/g, "&gt;") .replace(/>/g, '&gt;')
.replace(/"/g, "&quot;") .replace(/"/g, '&quot;')
.replace(/'/g, "&#39;") .replace(/'/g, '&#39;')
.replace(/`/g, "&#x60;") .replace(/`/g, '&#x60;')
.replace(/\r?\n/g, "<br>"); .replace(/\r?\n/g, '<br>');
@Injectable() @Injectable()
export class MastoConverters { export class MastoConverters {
@ -31,8 +32,15 @@ export class MastoConverters {
@Inject(DI.config) @Inject(DI.config)
private config: Config, private config: Config,
@Inject(DI.usersRepository)
private usersRepository: UsersRepository,
@Inject(DI.userProfilesRepository)
private userProfilesRepository: UserProfilesRepository,
private mfmService: MfmService, private mfmService: MfmService,
private getterService: GetterService, private getterService: GetterService,
private customEmojiService: CustomEmojiService,
) { ) {
} }
@ -60,29 +68,51 @@ export class MastoConverters {
}); });
} }
private async encodeField(f: Entity.Field): Promise<Entity.Field> {
return {
name: f.name,
value: await this.mfmService.toMastoHtml(mfm.parse(f.value), [], true) ?? escapeMFM(f.value),
verified_at: null,
};
}
public async convertAccount(account: Entity.Account) { public async convertAccount(account: Entity.Account) {
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);
const emoji: Entity.Emoji[] = [];
Object.entries(emojis).forEach(entry => {
const [key, value] = entry;
emoji.push({
shortcode: key,
static_url: value,
url: value,
visible_in_picker: true,
category: undefined,
});
});
return awaitAll({ return awaitAll({
id: account.id, id: account.id,
username: account.username, username: account.username,
acct: account.acct, acct: account.acct,
fqn: account.fqn, fqn: account.fqn,
display_name: account.display_name || account.username, display_name: account.display_name || account.username,
locked: account.locked, locked: user.isLocked,
created_at: account.created_at, created_at: account.created_at,
followers_count: account.followers_count, followers_count: user.followersCount,
following_count: account.following_count, following_count: user.followingCount,
statuses_count: account.statuses_count, statuses_count: user.notesCount,
note: account.note, note: profile?.description ?? account.note,
url: account.url, url: account.url,
avatar: account.avatar, avatar: user.avatarUrl ? user.avatarUrl : 'https://dev.joinsharkey.org/static-assets/avatar.png',
avatar_static: account.avatar, avatar_static: user.avatarUrl ? user.avatarUrl : 'https://dev.joinsharkey.org/static-assets/avatar.png',
header: account.header, header: user.bannerUrl ? user.bannerUrl : 'https://dev.joinsharkey.org/static-assets/transparent.png',
header_static: account.header, header_static: user.bannerUrl ? user.bannerUrl : 'https://dev.joinsharkey.org/static-assets/transparent.png',
emojis: account.emojis, emojis: emoji,
moved: null, //FIXME moved: null, //FIXME
fields: [], fields: Promise.all(profile?.fields.map(async p => this.encodeField(p)) ?? []),
bot: false, bot: user.isBot,
discoverable: true, discoverable: user.isExplorable,
}); });
} }

View File

@ -2,6 +2,6 @@ namespace Entity {
export type Field = { export type Field = {
name: string name: string
value: string value: string
verified_at: string | null verified_at?: string | null
} }
} }

View File

@ -3,5 +3,6 @@ namespace MisskeyEntity {
name: string; name: string;
value: string; value: string;
verified?: string; verified?: string;
verified_at?: string;
}; };
} }