upd: change deps, fix a few bugs, update converter

Fixes User and Notes count bug (transfem-org/Sharkey#113)
Fixes build issues due to types (transfem-org/Sharkey#111)
Return accounts and notes like Iceshrimp
Use MFM class from Iceshrimp to fix HTML output for mastodon
This commit is contained in:
Mar0xy 2023-10-29 00:50:00 +02:00
parent b0a7fd6ddb
commit 82c10de265
No known key found for this signature in database
GPG key ID: 56569BBE47D2C828
14 changed files with 421 additions and 242 deletions

View file

@ -1,7 +1,10 @@
import { convertId, IdConvertType as IdType, convertAccount, convertRelationship, convertStatus } from '../converters.js';
import { argsToBools, convertTimelinesArgsId, limitToInt } from './timeline.js';
import { MastoConverters, convertRelationship } from '../converters.js';
import { argsToBools, limitToInt } from './timeline.js';
import type { MegalodonInterface } from 'megalodon';
import type { FastifyRequest } from 'fastify';
import { NoteEditRepository, NotesRepository, UsersRepository } from '@/models/_.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import type { Config } from '@/config.js';
const relationshipModel = {
id: '',
@ -24,18 +27,25 @@ export class ApiAccountMastodon {
private request: FastifyRequest;
private client: MegalodonInterface;
private BASE_URL: string;
private mastoconverter: MastoConverters;
constructor(request: FastifyRequest, client: MegalodonInterface, BASE_URL: string) {
constructor(request: FastifyRequest, client: MegalodonInterface, BASE_URL: string,
config: Config,
usersrepo: UsersRepository,
notesrepo: NotesRepository,
noteeditrepo: NoteEditRepository,
userentity: UserEntityService,
) {
this.request = request;
this.client = client;
this.BASE_URL = BASE_URL;
this.mastoconverter = new MastoConverters(config, usersrepo, notesrepo, noteeditrepo, userentity);
}
public async verifyCredentials() {
try {
const data = await this.client.verifyAccountCredentials();
const acct = data.data;
acct.id = convertId(acct.id, IdType.MastodonId);
acct.display_name = acct.display_name || acct.username;
acct.url = `${this.BASE_URL}/@${acct.url}`;
acct.note = acct.note || '';
@ -61,7 +71,7 @@ export class ApiAccountMastodon {
public async lookup() {
try {
const data = await this.client.search((this.request.query as any).acct, { type: 'accounts' });
return convertAccount(data.data.accounts[0]);
return this.mastoconverter.convertAccount(data.data.accounts[0]);
} catch (e: any) {
/* console.error(e)
console.error(e.response.data); */
@ -79,7 +89,7 @@ export class ApiAccountMastodon {
const reqIds = [];
for (let i = 0; i < users.length; i++) {
reqIds.push(convertId(users[i], IdType.SharkeyId));
reqIds.push(users[i]);
}
const data = await this.client.getRelationships(reqIds);
@ -93,11 +103,8 @@ export class ApiAccountMastodon {
public async getStatuses() {
try {
const data = await this.client.getAccountStatuses(
convertId((this.request.params as any).id, IdType.SharkeyId),
convertTimelinesArgsId(argsToBools(limitToInt(this.request.query as any)))
);
return data.data.map((status) => convertStatus(status));
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));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
@ -108,10 +115,10 @@ export class ApiAccountMastodon {
public async getFollowers() {
try {
const data = await this.client.getAccountFollowers(
convertId((this.request.params as any).id, IdType.SharkeyId),
convertTimelinesArgsId(limitToInt(this.request.query as any)),
(this.request.params as any).id,
limitToInt(this.request.query as any),
);
return data.data.map((account) => convertAccount(account));
return data.data.map((account) => this.mastoconverter.convertAccount(account));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
@ -122,10 +129,10 @@ export class ApiAccountMastodon {
public async getFollowing() {
try {
const data = await this.client.getAccountFollowing(
convertId((this.request.params as any).id, IdType.SharkeyId),
convertTimelinesArgsId(limitToInt(this.request.query as any)),
(this.request.params as any).id,
limitToInt(this.request.query as any),
);
return data.data.map((account) => convertAccount(account));
return data.data.map((account) => this.mastoconverter.convertAccount(account));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
@ -135,7 +142,7 @@ export class ApiAccountMastodon {
public async addFollow() {
try {
const data = await this.client.followAccount( convertId((this.request.params as any).id, IdType.SharkeyId) );
const data = await this.client.followAccount( (this.request.params as any).id );
const acct = convertRelationship(data.data);
acct.following = true;
return acct;
@ -148,7 +155,7 @@ export class ApiAccountMastodon {
public async rmFollow() {
try {
const data = await this.client.unfollowAccount( convertId((this.request.params as any).id, IdType.SharkeyId) );
const data = await this.client.unfollowAccount( (this.request.params as any).id );
const acct = convertRelationship(data.data);
acct.following = false;
return acct;
@ -161,7 +168,7 @@ export class ApiAccountMastodon {
public async addBlock() {
try {
const data = await this.client.blockAccount( convertId((this.request.params as any).id, IdType.SharkeyId) );
const data = await this.client.blockAccount( (this.request.params as any).id );
return convertRelationship(data.data);
} catch (e: any) {
console.error(e);
@ -172,7 +179,7 @@ export class ApiAccountMastodon {
public async rmBlock() {
try {
const data = await this.client.unblockAccount( convertId((this.request.params as any).id, IdType.SharkeyId) );
const data = await this.client.unblockAccount( (this.request.params as any).id );
return convertRelationship(data.data);
} catch (e: any) {
console.error(e);
@ -184,7 +191,7 @@ export class ApiAccountMastodon {
public async addMute() {
try {
const data = await this.client.muteAccount(
convertId((this.request.params as any).id, IdType.SharkeyId),
(this.request.params as any).id,
this.request.body as any,
);
return convertRelationship(data.data);
@ -197,7 +204,7 @@ export class ApiAccountMastodon {
public async rmMute() {
try {
const data = await this.client.unmuteAccount( convertId((this.request.params as any).id, IdType.SharkeyId) );
const data = await this.client.unmuteAccount( (this.request.params as any).id );
return convertRelationship(data.data);
} catch (e: any) {
console.error(e);
@ -208,8 +215,8 @@ export class ApiAccountMastodon {
public async getBookmarks() {
try {
const data = await this.client.getBookmarks( convertTimelinesArgsId(limitToInt(this.request.query as any)) );
return data.data.map((status) => convertStatus(status));
const data = await this.client.getBookmarks( limitToInt(this.request.query as any) );
return data.data.map((status) => this.mastoconverter.convertStatus(status));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
@ -219,8 +226,8 @@ export class ApiAccountMastodon {
public async getFavourites() {
try {
const data = await this.client.getFavourites( convertTimelinesArgsId(limitToInt(this.request.query as any)) );
return data.data.map((status) => convertStatus(status));
const data = await this.client.getFavourites( limitToInt(this.request.query as any) );
return data.data.map((status) => this.mastoconverter.convertStatus(status));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
@ -230,8 +237,8 @@ export class ApiAccountMastodon {
public async getMutes() {
try {
const data = await this.client.getMutes( convertTimelinesArgsId(limitToInt(this.request.query as any)) );
return data.data.map((account) => convertAccount(account));
const data = await this.client.getMutes( limitToInt(this.request.query as any) );
return data.data.map((account) => this.mastoconverter.convertAccount(account));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
@ -241,8 +248,8 @@ export class ApiAccountMastodon {
public async getBlocks() {
try {
const data = await this.client.getBlocks( convertTimelinesArgsId(limitToInt(this.request.query as any)) );
return data.data.map((account) => convertAccount(account));
const data = await this.client.getBlocks( limitToInt(this.request.query as any) );
return data.data.map((account) => this.mastoconverter.convertAccount(account));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
@ -252,7 +259,7 @@ export class ApiAccountMastodon {
public async acceptFollow() {
try {
const data = await this.client.acceptFollowRequest( convertId((this.request.params as any).id, IdType.SharkeyId) );
const data = await this.client.acceptFollowRequest( (this.request.params as any).id );
return convertRelationship(data.data);
} catch (e: any) {
console.error(e);
@ -263,7 +270,7 @@ export class ApiAccountMastodon {
public async rejectFollow() {
try {
const data = await this.client.rejectFollowRequest( convertId((this.request.params as any).id, IdType.SharkeyId) );
const data = await this.client.rejectFollowRequest( (this.request.params as any).id );
return convertRelationship(data.data);
} catch (e: any) {
console.error(e);