upd: simplify importing of mastoconverter, fix bug

Lets you import stuff into mastoconverter without needing to also import them everywhere else
Fixes not being able to get statuses on accounts
This commit is contained in:
Mar0xy 2023-10-29 02:06:19 +02:00
parent f92773d995
commit 43f27a639f
No known key found for this signature in database
GPG key ID: 56569BBE47D2C828
6 changed files with 39 additions and 54 deletions

View file

@ -23,6 +23,7 @@ import { SigninService } from './api/SigninService.js';
import { SignupApiService } from './api/SignupApiService.js'; import { SignupApiService } from './api/SignupApiService.js';
import { StreamingApiServerService } from './api/StreamingApiServerService.js'; import { StreamingApiServerService } from './api/StreamingApiServerService.js';
import { ClientServerService } from './web/ClientServerService.js'; import { ClientServerService } from './web/ClientServerService.js';
import { MastoConverters } from './api/mastodon/converters.js';
import { FeedService } from './web/FeedService.js'; import { FeedService } from './web/FeedService.js';
import { UrlPreviewService } from './web/UrlPreviewService.js'; import { UrlPreviewService } from './web/UrlPreviewService.js';
import { MainChannelService } from './api/stream/channels/main.js'; import { MainChannelService } from './api/stream/channels/main.js';
@ -87,6 +88,7 @@ import { OAuth2ProviderService } from './oauth/OAuth2ProviderService.js';
OpenApiServerService, OpenApiServerService,
MastodonApiServerService, MastodonApiServerService,
OAuth2ProviderService, OAuth2ProviderService,
MastoConverters,
], ],
exports: [ exports: [
ServerService, ServerService,

View file

@ -8,7 +8,7 @@ import { DI } from '@/di-symbols.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
import type { Config } from '@/config.js'; import type { Config } from '@/config.js';
import { MetaService } from '@/core/MetaService.js'; import { MetaService } from '@/core/MetaService.js';
import { convertAccount, convertAnnouncement, convertFilter, convertAttachment, convertFeaturedTag, convertList } from './converters.js'; import { convertAccount, convertAnnouncement, convertFilter, convertAttachment, convertFeaturedTag, convertList, MastoConverters } from './converters.js';
import { getInstance } from './endpoints/meta.js'; import { getInstance } from './endpoints/meta.js';
import { ApiAuthMastodon, ApiAccountMastodon, ApiFilterMastodon, ApiNotifyMastodon, ApiSearchMastodon, ApiTimelineMastodon, ApiStatusMastodon } from './endpoints.js'; import { ApiAuthMastodon, ApiAccountMastodon, ApiFilterMastodon, ApiNotifyMastodon, ApiSearchMastodon, ApiTimelineMastodon, ApiStatusMastodon } from './endpoints.js';
import type { FastifyInstance, FastifyPluginOptions } from 'fastify'; import type { FastifyInstance, FastifyPluginOptions } from 'fastify';
@ -37,6 +37,7 @@ export class MastodonApiServerService {
private config: Config, private config: Config,
private metaService: MetaService, private metaService: MetaService,
private userEntityService: UserEntityService, private userEntityService: UserEntityService,
private mastoConverter: MastoConverters,
) { } ) { }
@bindThis @bindThis
@ -236,7 +237,7 @@ export class MastodonApiServerService {
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
// displayed without being logged in // displayed without being logged in
try { try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.config, this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService); const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.verifyCredentials()); reply.send(await account.verifyCredentials());
} catch (e: any) { } catch (e: any) {
/* console.error(e); */ /* console.error(e); */
@ -286,7 +287,7 @@ export class MastodonApiServerService {
ids = [ids]; ids = [ids];
} }
users = ids; users = ids;
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.config, this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService); const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.getRelationships(users)); reply.send(await account.getRelationships(users));
} catch (e: any) { } catch (e: any) {
/* console.error(e); */ /* console.error(e); */
@ -319,7 +320,7 @@ export class MastodonApiServerService {
const accessTokens = _request.headers.authorization; const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.config, this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService); const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.getStatuses()); reply.send(await account.getStatuses());
} catch (e: any) { } catch (e: any) {
/* console.error(e); /* console.error(e);
@ -347,7 +348,7 @@ export class MastodonApiServerService {
const accessTokens = _request.headers.authorization; const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.config, this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService); const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.getFollowers()); reply.send(await account.getFollowers());
} catch (e: any) { } catch (e: any) {
/* console.error(e); /* console.error(e);
@ -361,7 +362,7 @@ export class MastodonApiServerService {
const accessTokens = _request.headers.authorization; const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.config, this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService); const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.getFollowing()); reply.send(await account.getFollowing());
} catch (e: any) { } catch (e: any) {
/* console.error(e); /* console.error(e);
@ -389,7 +390,7 @@ export class MastodonApiServerService {
const accessTokens = _request.headers.authorization; const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.config, this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService); const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.addFollow()); reply.send(await account.addFollow());
} catch (e: any) { } catch (e: any) {
/* console.error(e); /* console.error(e);
@ -403,7 +404,7 @@ export class MastodonApiServerService {
const accessTokens = _request.headers.authorization; const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.config, this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService); const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.rmFollow()); reply.send(await account.rmFollow());
} catch (e: any) { } catch (e: any) {
/* console.error(e); /* console.error(e);
@ -417,7 +418,7 @@ export class MastodonApiServerService {
const accessTokens = _request.headers.authorization; const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.config, this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService); const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.addBlock()); reply.send(await account.addBlock());
} catch (e: any) { } catch (e: any) {
/* console.error(e); /* console.error(e);
@ -431,7 +432,7 @@ export class MastodonApiServerService {
const accessTokens = _request.headers.authorization; const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.config, this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService); const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.rmBlock()); reply.send(await account.rmBlock());
} catch (e: any) { } catch (e: any) {
/* console.error(e); /* console.error(e);
@ -445,7 +446,7 @@ export class MastodonApiServerService {
const accessTokens = _request.headers.authorization; const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.config, this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService); const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.addMute()); reply.send(await account.addMute());
} catch (e: any) { } catch (e: any) {
/* console.error(e); /* console.error(e);
@ -459,7 +460,7 @@ export class MastodonApiServerService {
const accessTokens = _request.headers.authorization; const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.config, this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService); const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.rmMute()); reply.send(await account.rmMute());
} catch (e: any) { } catch (e: any) {
/* console.error(e); /* console.error(e);
@ -487,7 +488,7 @@ export class MastodonApiServerService {
const accessTokens = _request.headers.authorization; const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.config, this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService); const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.getBookmarks()); reply.send(await account.getBookmarks());
} catch (e: any) { } catch (e: any) {
/* console.error(e); /* console.error(e);
@ -501,7 +502,7 @@ export class MastodonApiServerService {
const accessTokens = _request.headers.authorization; const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.config, this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService); const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.getFavourites()); reply.send(await account.getFavourites());
} catch (e: any) { } catch (e: any) {
/* console.error(e); /* console.error(e);
@ -515,7 +516,7 @@ export class MastodonApiServerService {
const accessTokens = _request.headers.authorization; const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.config, this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService); const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.getMutes()); reply.send(await account.getMutes());
} catch (e: any) { } catch (e: any) {
/* console.error(e); /* console.error(e);
@ -529,7 +530,7 @@ export class MastodonApiServerService {
const accessTokens = _request.headers.authorization; const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.config, this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService); const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.getBlocks()); reply.send(await account.getBlocks());
} catch (e: any) { } catch (e: any) {
/* console.error(e); /* console.error(e);
@ -557,7 +558,7 @@ export class MastodonApiServerService {
const accessTokens = _request.headers.authorization; const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.config, this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService); const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.acceptFollow()); reply.send(await account.acceptFollow());
} catch (e: any) { } catch (e: any) {
/* console.error(e); /* console.error(e);
@ -571,7 +572,7 @@ export class MastodonApiServerService {
const accessTokens = _request.headers.authorization; const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.config, this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService); const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.rejectFollow()); reply.send(await account.rejectFollow());
} catch (e: any) { } catch (e: any) {
/* console.error(e); /* console.error(e);
@ -756,7 +757,7 @@ export class MastodonApiServerService {
//#endregion //#endregion
//#region Timelines //#region Timelines
const TLEndpoint = new ApiTimelineMastodon(fastify, this.config, this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService); const TLEndpoint = new ApiTimelineMastodon(fastify, this.config, this.mastoConverter);
// GET Endpoints // GET Endpoints
TLEndpoint.getTL(); TLEndpoint.getTL();
@ -781,7 +782,7 @@ export class MastodonApiServerService {
//#endregion //#endregion
//#region Status //#region Status
const NoteEndpoint = new ApiStatusMastodon(fastify, this.config, this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService); const NoteEndpoint = new ApiStatusMastodon(fastify, this.mastoConverter);
// GET Endpoints // GET Endpoints
NoteEndpoint.getStatus(); NoteEndpoint.getStatus();

View file

@ -1,7 +1,7 @@
import type { Config } from '@/config.js'; import type { Config } from '@/config.js';
import { MfmService } from '@/core/MfmService.js'; import { MfmService } from '@/core/MfmService.js';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import { Inject } 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 { GetterService } from '../GetterService.js';
@ -25,27 +25,15 @@ export const escapeMFM = (text: string): string => text
.replace(/`/g, "`") .replace(/`/g, "`")
.replace(/\r?\n/g, "<br>"); .replace(/\r?\n/g, "<br>");
@Injectable()
export class MastoConverters { export class MastoConverters {
private MfmService: MfmService;
private GetterService: GetterService;
constructor( constructor(
@Inject(DI.config) @Inject(DI.config)
private config: Config, private config: Config,
@Inject(DI.usersRepository) private mfmService: MfmService,
private usersRepository: UsersRepository, private getterService: GetterService,
@Inject(DI.notesRepository)
private notesRepository: NotesRepository,
@Inject(DI.noteEditRepository)
private noteEditRepository: NoteEditRepository,
private userEntityService: UserEntityService
) { ) {
this.MfmService = new MfmService(this.config);
this.GetterService = new GetterService(this.usersRepository, this.notesRepository, this.noteEditRepository, this.userEntityService);
} }
private encode(u: MiUser, m: IMentionedRemoteUsers): Entity.Mention { private encode(u: MiUser, m: IMentionedRemoteUsers): Entity.Mention {
@ -67,7 +55,7 @@ export class MastoConverters {
} }
public async getUser(id: string): Promise<MiUser> { public async getUser(id: string): Promise<MiUser> {
return this.GetterService.getUser(id).then(p => { return this.getterService.getUser(id).then(p => {
return p; return p;
}); });
} }
@ -100,7 +88,7 @@ export class MastoConverters {
public async convertStatus(status: Entity.Status) { public async convertStatus(status: Entity.Status) {
const convertedAccount = this.convertAccount(status.account); const convertedAccount = this.convertAccount(status.account);
const note = await this.GetterService.getNote(status.id); const note = await this.getterService.getNote(status.id);
const mentions = Promise.all(note.mentions.map(p => const mentions = Promise.all(note.mentions.map(p =>
this.getUser(p) this.getUser(p)
@ -109,7 +97,7 @@ export class MastoConverters {
.then(p => p.filter(m => m)) as Promise<Entity.Mention[]>; .then(p => p.filter(m => m)) as Promise<Entity.Mention[]>;
const content = note.text !== null const content = note.text !== null
? this.MfmService.toMastoHtml(mfm.parse(note.text!), JSON.parse(note.mentionedRemoteUsers), false, null) ? this.mfmService.toMastoHtml(mfm.parse(note.text!), JSON.parse(note.mentionedRemoteUsers), false, null)
.then(p => p ?? escapeMFM(note.text!)) .then(p => p ?? escapeMFM(note.text!))
: ''; : '';

View file

@ -5,6 +5,7 @@ import type { FastifyRequest } from 'fastify';
import { NoteEditRepository, NotesRepository, UsersRepository } from '@/models/_.js'; import { NoteEditRepository, NotesRepository, UsersRepository } from '@/models/_.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js'; import { UserEntityService } from '@/core/entities/UserEntityService.js';
import type { Config } from '@/config.js'; import type { Config } from '@/config.js';
import { Injectable } from '@nestjs/common';
const relationshipModel = { const relationshipModel = {
id: '', id: '',
@ -23,23 +24,16 @@ const relationshipModel = {
note: '', note: '',
}; };
@Injectable()
export class ApiAccountMastodon { export class ApiAccountMastodon {
private request: FastifyRequest; private request: FastifyRequest;
private client: MegalodonInterface; private client: MegalodonInterface;
private BASE_URL: string; private BASE_URL: string;
private mastoconverter: MastoConverters;
constructor(request: FastifyRequest, client: MegalodonInterface, BASE_URL: string, constructor(request: FastifyRequest, client: MegalodonInterface, BASE_URL: string, private mastoconverter: MastoConverters) {
config: Config,
usersrepo: UsersRepository,
notesrepo: NotesRepository,
noteeditrepo: NoteEditRepository,
userentity: UserEntityService,
) {
this.request = request; this.request = request;
this.client = client; this.client = client;
this.BASE_URL = BASE_URL; this.BASE_URL = BASE_URL;
this.mastoconverter = new MastoConverters(config, usersrepo, notesrepo, noteeditrepo, userentity);
} }
public async verifyCredentials() { public async verifyCredentials() {
@ -104,7 +98,9 @@ export class ApiAccountMastodon {
public async getStatuses() { public async getStatuses() {
try { try {
const data = await this.client.getAccountStatuses((this.request.params as any).id, argsToBools(limitToInt(this.request.query as any))); const data = await this.client.getAccountStatuses((this.request.params as any).id, argsToBools(limitToInt(this.request.query as any)));
return data.data.map((status) => this.mastoconverter.convertStatus(status)); const a = await Promise.all(data.data.map(async (status) => await this.mastoconverter.convertStatus(status)));
console.error(a);
return a;
} catch (e: any) { } catch (e: any) {
console.error(e); console.error(e);
console.error(e.response.data); console.error(e.response.data);

View file

@ -18,9 +18,9 @@ export class ApiStatusMastodon {
private fastify: FastifyInstance; private fastify: FastifyInstance;
private mastoconverter: MastoConverters; private mastoconverter: MastoConverters;
constructor(fastify: FastifyInstance, config: Config, usersrepo: UsersRepository, notesrepo: NotesRepository, noteeditrepo: NoteEditRepository, userentity: UserEntityService) { constructor(fastify: FastifyInstance, mastoconverter: MastoConverters) {
this.fastify = fastify; this.fastify = fastify;
this.mastoconverter = new MastoConverters(config, usersrepo, notesrepo, noteeditrepo, userentity); this.mastoconverter = mastoconverter;
} }
public async getStatus() { public async getStatus() {

View file

@ -34,11 +34,9 @@ export function argsToBools(q: ParsedUrlQuery) {
export class ApiTimelineMastodon { export class ApiTimelineMastodon {
private fastify: FastifyInstance; private fastify: FastifyInstance;
private mastoconverter: MastoConverters;
constructor(fastify: FastifyInstance, config: Config, usersRepository: UsersRepository, notesRepository: NotesRepository, noteEditRepository: NoteEditRepository, userEntityService: UserEntityService) { constructor(fastify: FastifyInstance, config: Config, private mastoconverter: MastoConverters) {
this.fastify = fastify; this.fastify = fastify;
this.mastoconverter = new MastoConverters(config, usersRepository, notesRepository, noteEditRepository, userEntityService);
} }
public async getTL() { public async getTL() {