rm: mastodon api

This commit is contained in:
Mar0xy 2023-12-01 12:04:41 +01:00
parent dcce9ba70b
commit 4cfc110e00
No known key found for this signature in database
GPG key ID: 56569BBE47D2C828
242 changed files with 41 additions and 25789 deletions

View file

@ -117,7 +117,6 @@
"json5": "2.2.3",
"jsonld": "8.3.1",
"jsrsasign": "10.8.6",
"megalodon": "workspace:*",
"meilisearch": "0.35.0",
"mfm-js": "0.23.3",
"microformats-parser": "1.5.2",

View file

@ -23,7 +23,6 @@ import { SigninService } from './api/SigninService.js';
import { SignupApiService } from './api/SignupApiService.js';
import { StreamingApiServerService } from './api/StreamingApiServerService.js';
import { ClientServerService } from './web/ClientServerService.js';
import { MastoConverters } from './api/mastodon/converters.js';
import { FeedService } from './web/FeedService.js';
import { UrlPreviewService } from './web/UrlPreviewService.js';
import { MainChannelService } from './api/stream/channels/main.js';
@ -40,7 +39,6 @@ import { QueueStatsChannelService } from './api/stream/channels/queue-stats.js';
import { ServerStatsChannelService } from './api/stream/channels/server-stats.js';
import { UserListChannelService } from './api/stream/channels/user-list.js';
import { OpenApiServerService } from './api/openapi/OpenApiServerService.js';
import { MastodonApiServerService } from './api/mastodon/MastodonApiServerService.js';
import { ClientLoggerService } from './web/ClientLoggerService.js';
import { RoleTimelineChannelService } from './api/stream/channels/role-timeline.js';
import { OAuth2ProviderService } from './oauth/OAuth2ProviderService.js';
@ -86,9 +84,7 @@ import { OAuth2ProviderService } from './oauth/OAuth2ProviderService.js';
ServerStatsChannelService,
UserListChannelService,
OpenApiServerService,
MastodonApiServerService,
OAuth2ProviderService,
MastoConverters,
],
exports: [
ServerService,

View file

@ -31,7 +31,6 @@ import { WellKnownServerService } from './WellKnownServerService.js';
import { FileServerService } from './FileServerService.js';
import { ClientServerService } from './web/ClientServerService.js';
import { OpenApiServerService } from './api/openapi/OpenApiServerService.js';
import { MastodonApiServerService } from './api/mastodon/MastodonApiServerService.js';
import { OAuth2ProviderService } from './oauth/OAuth2ProviderService.js';
const _dirname = fileURLToPath(new URL('.', import.meta.url));
@ -105,7 +104,6 @@ export class ServerService implements OnApplicationShutdown {
fastify.register(this.apiServerService.createServer, { prefix: '/api' });
fastify.register(this.openApiServerService.createServer);
fastify.register(this.mastodonApiServerService.createServer, { prefix: '/api' });
fastify.register(this.fileServerService.createServer);
fastify.register(this.activityPubServerService.createServer);
fastify.register(this.nodeinfoServerService.createServer);

View file

@ -1,906 +0,0 @@
import { Inject, Injectable } from '@nestjs/common';
import megalodon, { Entity, MegalodonInterface } from 'megalodon';
import querystring from 'querystring';
import { IsNull } from 'typeorm';
import multer from 'fastify-multer';
import type { AccessTokensRepository, NoteEditRepository, NotesRepository, UserProfilesRepository, UsersRepository } from '@/models/_.js';
import { DI } from '@/di-symbols.js';
import { bindThis } from '@/decorators.js';
import type { Config } from '@/config.js';
import { MetaService } from '@/core/MetaService.js';
import { convertAnnouncement, convertFilter, convertAttachment, convertFeaturedTag, convertList, MastoConverters } from './converters.js';
import { getInstance } from './endpoints/meta.js';
import { ApiAuthMastodon, ApiAccountMastodon, ApiFilterMastodon, ApiNotifyMastodon, ApiSearchMastodon, ApiTimelineMastodon, ApiStatusMastodon } from './endpoints.js';
import type { FastifyInstance, FastifyPluginOptions } from 'fastify';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { DriveService } from '@/core/DriveService.js';
export function getClient(BASE_URL: string, authorization: string | undefined): MegalodonInterface {
const accessTokenArr = authorization?.split(' ') ?? [null];
const accessToken = accessTokenArr[accessTokenArr.length - 1];
const generator = (megalodon as any).default;
const client = generator('misskey', BASE_URL, accessToken) as MegalodonInterface;
return client;
}
@Injectable()
export class MastodonApiServerService {
constructor(
@Inject(DI.usersRepository)
private usersRepository: UsersRepository,
@Inject(DI.notesRepository)
private notesRepository: NotesRepository,
@Inject(DI.userProfilesRepository)
private userProfilesRepository: UserProfilesRepository,
@Inject(DI.noteEditRepository)
private noteEditRepository: NoteEditRepository,
@Inject(DI.accessTokensRepository)
private accessTokensRepository: AccessTokensRepository,
@Inject(DI.config)
private config: Config,
private metaService: MetaService,
private userEntityService: UserEntityService,
private driveService: DriveService,
private mastoConverter: MastoConverters,
) { }
@bindThis
public createServer(fastify: FastifyInstance, _options: FastifyPluginOptions, done: (err?: Error) => void) {
const upload = multer({
storage: multer.diskStorage({}),
limits: {
fileSize: this.config.maxFileSize || 262144000,
files: 1,
},
});
fastify.addHook('onRequest', (request, reply, done) => {
reply.header('Access-Control-Allow-Origin', '*');
done();
});
fastify.addContentTypeParser('application/x-www-form-urlencoded', (request, payload, done) => {
let body = '';
payload.on('data', (data) => {
body += data;
});
payload.on('end', () => {
try {
const parsed = querystring.parse(body);
done(null, parsed);
} catch (e: any) {
done(e);
}
});
payload.on('error', done);
});
fastify.register(multer.contentParser);
fastify.get('/v1/custom_emojis', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getInstanceCustomEmojis();
reply.send(data.data);
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
});
fastify.get('/v1/instance', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
// displayed without being logged in
try {
const data = await client.getInstance();
const admin = await this.usersRepository.findOne({
where: {
host: IsNull(),
isRoot: true,
isDeleted: false,
isSuspended: false,
},
order: { id: 'ASC' },
});
const contact = admin == null ? null : await this.mastoConverter.convertAccount((await client.getAccount(admin.id)).data);
reply.send(await getInstance(data.data, contact as Entity.Account, this.config, await this.metaService.fetch()));
} catch (e: any) {
/* console.error(e); */
reply.code(401).send(e.response.data);
}
});
fastify.get('/v1/announcements', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getInstanceAnnouncements();
reply.send(data.data.map((announcement) => convertAnnouncement(announcement)));
} catch (e: any) {
/* console.error(e); */
reply.code(401).send(e.response.data);
}
});
fastify.post<{ Body: { id: string } }>('/v1/announcements/:id/dismiss', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.dismissInstanceAnnouncement(
_request.body['id'],
);
reply.send(data.data);
} catch (e: any) {
/* console.error(e); */
reply.code(401).send(e.response.data);
}
},
);
fastify.post('/v1/media', { preHandler: upload.single('file') }, async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const multipartData = await _request.file;
if (!multipartData) {
reply.code(401).send({ error: 'No image' });
return;
}
const data = await client.uploadMedia(multipartData);
reply.send(convertAttachment(data.data as Entity.Attachment));
} catch (e: any) {
/* console.error(e); */
reply.code(401).send(e.response.data);
}
});
fastify.post('/v2/media', { preHandler: upload.single('file') }, async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const multipartData = await _request.file;
if (!multipartData) {
reply.code(401).send({ error: 'No image' });
return;
}
const data = await client.uploadMedia(multipartData, _request.body!);
reply.send(convertAttachment(data.data as Entity.Attachment));
} catch (e: any) {
/* console.error(e); */
reply.code(401).send(e.response.data);
}
});
fastify.get('/v1/filters', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
// displayed without being logged in
try {
const data = await client.getFilters();
reply.send(data.data.map((filter) => convertFilter(filter)));
} catch (e: any) {
/* console.error(e); */
reply.code(401).send(e.response.data);
}
});
fastify.get('/v1/trends', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
// displayed without being logged in
try {
const data = await client.getInstanceTrends();
reply.send(data.data);
} catch (e: any) {
/* console.error(e); */
reply.code(401).send(e.response.data);
}
});
fastify.get('/v1/trends/tags', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
// displayed without being logged in
try {
const data = await client.getInstanceTrends();
reply.send(data.data);
} catch (e: any) {
/* console.error(e); */
reply.code(401).send(e.response.data);
}
});
fastify.get('/v1/trends/links', async (_request, reply) => {
// As we do not have any system for news/links this will just return empty
reply.send([]);
});
fastify.post('/v1/apps', { preHandler: upload.single('none') }, async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const client = getClient(BASE_URL, ''); // we are using this here, because in private mode some info isnt
// displayed without being logged in
try {
const data = await ApiAuthMastodon(_request, client);
reply.send(data);
} catch (e: any) {
/* console.error(e); */
reply.code(401).send(e.response.data);
}
});
fastify.get('/v1/preferences', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
// displayed without being logged in
try {
const data = await client.getPreferences();
reply.send(data.data);
} catch (e: any) {
/* console.error(e); */
reply.code(401).send(e.response.data);
}
});
//#region Accounts
fastify.get('/v1/accounts/verify_credentials', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
// displayed without being logged in
try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.verifyCredentials());
} catch (e: any) {
/* console.error(e); */
reply.code(401).send(e.response.data);
}
});
fastify.patch('/v1/accounts/update_credentials', { preHandler: upload.any() }, async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
// displayed without being logged in
try {
// Check if there is an Header or Avatar being uploaded, if there is proceed to upload it to the drive of the user and then set it.
if (_request.files.length > 0 && accessTokens) {
const tokeninfo = await this.accessTokensRepository.findOneBy({ token: accessTokens.replace('Bearer ', '') });
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const avatar = (_request.files as any).find((obj: any) => {
return obj.fieldname === 'avatar';
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const header = (_request.files as any).find((obj: any) => {
return obj.fieldname === 'header';
});
if (tokeninfo && avatar) {
const upload = await this.driveService.addFile({
user: { id: tokeninfo.userId, host: null },
path: avatar.path,
name: avatar.originalname !== null && avatar.originalname !== 'file' ? avatar.originalname : undefined,
sensitive: false,
});
if (upload.type.startsWith('image/')) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(_request.body as any).avatar = upload.id;
}
} else if (tokeninfo && header) {
const upload = await this.driveService.addFile({
user: { id: tokeninfo.userId, host: null },
path: header.path,
name: header.originalname !== null && header.originalname !== 'file' ? header.originalname : undefined,
sensitive: false,
});
if (upload.type.startsWith('image/')) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(_request.body as any).header = upload.id;
}
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((_request.body as any).fields_attributes) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const fields = (_request.body as any).fields_attributes.map((field: any) => {
if (!(field.name.trim() === '' && field.value.trim() === '')) {
if (field.name.trim() === '') return reply.code(400).send('Field name can not be empty');
if (field.value.trim() === '') return reply.code(400).send('Field value can not be empty');
}
return {
...field,
};
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(_request.body as any).fields_attributes = fields.filter((field: any) => field.name.trim().length > 0 && field.value.length > 0);
}
const data = await client.updateCredentials(_request.body!);
reply.send(await this.mastoConverter.convertAccount(data.data));
} catch (e: any) {
//console.error(e);
reply.code(401).send(e.response.data);
}
});
fastify.get('/v1/accounts/lookup', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
// displayed without being logged in
try {
const data = await client.search((_request.query as any).acct, { type: 'accounts' });
const profile = await this.userProfilesRepository.findOneBy({ userId: data.data.accounts[0].id });
data.data.accounts[0].fields = profile?.fields.map(f => ({ ...f, verified_at: null })) || [];
reply.send(await this.mastoConverter.convertAccount(data.data.accounts[0]));
} catch (e: any) {
/* console.error(e); */
reply.code(401).send(e.response.data);
}
});
fastify.get('/v1/accounts/relationships', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
// displayed without being logged in
let users;
try {
let ids = _request.query ? (_request.query as any)['id[]'] ?? (_request.query as any)['id'] : null;
if (typeof ids === 'string') {
ids = [ids];
}
users = ids;
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.getRelationships(users));
} catch (e: any) {
/* console.error(e); */
const data = e.response.data;
data.users = users;
console.error(data);
reply.code(401).send(data);
}
});
fastify.get<{ Params: { id: string } }>('/v1/accounts/:id', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const sharkId = _request.params.id;
const data = await client.getAccount(sharkId);
const account = await this.mastoConverter.convertAccount(data.data);
reply.send(account);
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.get<{ Params: { id: string } }>('/v1/accounts/:id/statuses', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.getStatuses());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.get<{ Params: { id: string } }>('/v1/accounts/:id/featured_tags', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getFeaturedTags();
reply.send(data.data.map((tag) => convertFeaturedTag(tag)));
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.get<{ Params: { id: string } }>('/v1/accounts/:id/followers', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.getFollowers());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.get<{ Params: { id: string } }>('/v1/accounts/:id/following', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.getFollowing());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.get<{ Params: { id: string } }>('/v1/accounts/:id/lists', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getAccountLists(_request.params.id);
reply.send(data.data.map((list) => convertList(list)));
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.post<{ Params: { id: string } }>('/v1/accounts/:id/follow', { preHandler: upload.single('none') }, async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.addFollow());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.post<{ Params: { id: string } }>('/v1/accounts/:id/unfollow', { preHandler: upload.single('none') }, async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.rmFollow());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.post<{ Params: { id: string } }>('/v1/accounts/:id/block', { preHandler: upload.single('none') }, async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.addBlock());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.post<{ Params: { id: string } }>('/v1/accounts/:id/unblock', { preHandler: upload.single('none') }, async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.rmBlock());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.post<{ Params: { id: string } }>('/v1/accounts/:id/mute', { preHandler: upload.single('none') }, async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.addMute());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.post<{ Params: { id: string } }>('/v1/accounts/:id/unmute', { preHandler: upload.single('none') }, async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.rmMute());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.get('/v1/followed_tags', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getFollowedTags();
reply.send(data.data);
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.get('/v1/bookmarks', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.getBookmarks());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.get('/v1/favourites', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.getFavourites());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.get('/v1/mutes', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.getMutes());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.get('/v1/blocks', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.getBlocks());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.get('/v1/follow_requests', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getFollowRequests( ((_request.query as any) || { limit: 20 }).limit );
reply.send(await Promise.all(data.data.map(async (account) => await this.mastoConverter.convertAccount(account as Entity.Account))));
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.post<{ Params: { id: string } }>('/v1/follow_requests/:id/authorize', { preHandler: upload.single('none') }, async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.acceptFollow());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.post<{ Params: { id: string } }>('/v1/follow_requests/:id/reject', { preHandler: upload.single('none') }, async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const account = new ApiAccountMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await account.rejectFollow());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
//#endregion
//#region Search
fastify.get('/v1/search', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const search = new ApiSearchMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await search.SearchV1());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.get('/v2/search', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const search = new ApiSearchMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await search.SearchV2());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.get('/v1/trends/statuses', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const search = new ApiSearchMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await search.getStatusTrends());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.get('/v2/suggestions', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const search = new ApiSearchMastodon(_request, client, BASE_URL, this.mastoConverter);
reply.send(await search.getSuggestions());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
//#endregion
//#region Notifications
fastify.get('/v1/notifications', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const notify = new ApiNotifyMastodon(_request, client);
reply.send(await notify.getNotifications());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.get<{ Params: { id: string } }>('/v1/notification/:id', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const notify = new ApiNotifyMastodon(_request, client);
reply.send(await notify.getNotification());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.post<{ Params: { id: string } }>('/v1/notification/:id/dismiss', { preHandler: upload.single('none') }, async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const notify = new ApiNotifyMastodon(_request, client);
reply.send(await notify.rmNotification());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
fastify.post('/v1/notifications/clear', { preHandler: upload.single('none') }, async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const notify = new ApiNotifyMastodon(_request, client);
reply.send(await notify.rmNotifications());
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
reply.code(401).send(e.response.data);
}
});
//#endregion
//#region Filters
fastify.get<{ Params: { id: string } }>('/v1/filters/:id', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const filter = new ApiFilterMastodon(_request, client);
!_request.params.id ? reply.send(await filter.getFilters()) : reply.send(await filter.getFilter());
} catch (e: any) {
console.error(e);
console.error(e.response.data);
reply.code(401).send(e.response.data);
}
});
fastify.post('/v1/filters', { preHandler: upload.single('none') }, async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const filter = new ApiFilterMastodon(_request, client);
reply.send(await filter.createFilter());
} catch (e: any) {
console.error(e);
console.error(e.response.data);
reply.code(401).send(e.response.data);
}
});
fastify.post<{ Params: { id: string } }>('/v1/filters/:id', { preHandler: upload.single('none') }, async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const filter = new ApiFilterMastodon(_request, client);
reply.send(await filter.updateFilter());
} catch (e: any) {
console.error(e);
console.error(e.response.data);
reply.code(401).send(e.response.data);
}
});
fastify.delete<{ Params: { id: string } }>('/v1/filters/:id', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const filter = new ApiFilterMastodon(_request, client);
reply.send(await filter.rmFilter());
} catch (e: any) {
console.error(e);
console.error(e.response.data);
reply.code(401).send(e.response.data);
}
});
//#endregion
//#region Timelines
const TLEndpoint = new ApiTimelineMastodon(fastify, this.config, this.mastoConverter);
// GET Endpoints
TLEndpoint.getTL();
TLEndpoint.getHomeTl();
TLEndpoint.getListTL();
TLEndpoint.getTagTl();
TLEndpoint.getConversations();
TLEndpoint.getList();
TLEndpoint.getLists();
TLEndpoint.getListAccounts();
// POST Endpoints
TLEndpoint.createList();
TLEndpoint.addListAccount();
// PUT Endpoint
TLEndpoint.updateList();
// DELETE Endpoints
TLEndpoint.deleteList();
TLEndpoint.rmListAccount();
//#endregion
//#region Status
const NoteEndpoint = new ApiStatusMastodon(fastify, this.mastoConverter);
// GET Endpoints
NoteEndpoint.getStatus();
NoteEndpoint.getStatusSource();
NoteEndpoint.getContext();
NoteEndpoint.getHistory();
NoteEndpoint.getReblogged();
NoteEndpoint.getFavourites();
NoteEndpoint.getMedia();
NoteEndpoint.getPoll();
//POST Endpoints
NoteEndpoint.postStatus();
NoteEndpoint.addFavourite();
NoteEndpoint.rmFavourite();
NoteEndpoint.reblogStatus();
NoteEndpoint.unreblogStatus();
NoteEndpoint.bookmarkStatus();
NoteEndpoint.unbookmarkStatus();
NoteEndpoint.pinStatus();
NoteEndpoint.unpinStatus();
NoteEndpoint.reactStatus();
NoteEndpoint.unreactStatus();
NoteEndpoint.votePoll();
// PUT Endpoint
fastify.put<{ Params: { id: string } }>('/v1/media/:id', { preHandler: upload.none() }, async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.updateMedia(_request.params.id, _request.body!);
reply.send(convertAttachment(data.data));
} catch (e: any) {
/* console.error(e); */
reply.code(401).send(e.response.data);
}
});
NoteEndpoint.updateStatus();
// DELETE Endpoint
NoteEndpoint.deleteStatus();
//#endregion
done();
}
}

View file

@ -1,353 +0,0 @@
import { Inject, Injectable } from '@nestjs/common';
import { Entity } from 'megalodon';
import mfm from 'mfm-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 { MiUser } from '@/models/User.js';
import type { NoteEditRepository, NotesRepository, UserProfilesRepository, UsersRepository } from '@/models/_.js';
import { awaitAll } from '@/misc/prelude/await-all.js';
import { CustomEmojiService } from '@/core/CustomEmojiService.js';
import { GetterService } from '../GetterService.js';
import { DriveFileEntityService } from '@/core/entities/DriveFileEntityService.js';
import { IdService } from '@/core/IdService.js';
export enum IdConvertType {
MastodonId,
SharkeyId,
}
export const escapeMFM = (text: string): string => text
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;')
.replace(/`/g, '&#x60;')
.replace(/\r?\n/g, '<br>');
@Injectable()
export class MastoConverters {
constructor(
@Inject(DI.config)
private config: Config,
@Inject(DI.usersRepository)
private usersRepository: UsersRepository,
@Inject(DI.userProfilesRepository)
private userProfilesRepository: UserProfilesRepository,
@Inject(DI.noteEditRepository)
private noteEditRepository: NoteEditRepository,
private mfmService: MfmService,
private getterService: GetterService,
private customEmojiService: CustomEmojiService,
private idService: IdService,
private driveFileEntityService: DriveFileEntityService,
) {
}
private encode(u: MiUser, m: IMentionedRemoteUsers): Entity.Mention {
let acct = u.username;
let acctUrl = `https://${u.host || this.config.host}/@${u.username}`;
let url: string | null = null;
if (u.host) {
const info = m.find(r => r.username === u.username && r.host === u.host);
acct = `${u.username}@${u.host}`;
acctUrl = `https://${u.host}/@${u.username}`;
if (info) url = info.url ?? info.uri;
}
return {
id: u.id,
username: u.username,
acct: acct,
url: url ?? acctUrl,
};
}
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;
});
}
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 | 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);
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,
});
});
const fqn = `${user.username}@${user.host ?? this.config.hostname}`;
let acct = user.username;
let acctUrl = `https://${user.host || this.config.host}/@${user.username}`;
const acctUri = `https://${this.config.host}/users/${user.id}`;
if (user.host) {
acct = `${user.username}@${user.host}`;
acctUrl = `https://${user.host}/@${user.username}`;
}
return awaitAll({
id: account.id,
username: user.username,
acct: acct,
fqn: fqn,
display_name: user.name ?? user.username,
locked: user.isLocked,
created_at: this.idService.parse(user.id).date.toISOString(),
followers_count: user.followersCount,
following_count: user.followingCount,
statuses_count: user.notesCount,
note: profile?.description ?? '',
url: user.uri ?? acctUrl,
uri: user.uri ?? acctUri,
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',
header_static: user.bannerUrl ? user.bannerUrl : 'https://dev.joinsharkey.org/static-assets/transparent.png',
emojis: emoji,
moved: null, //FIXME
fields: Promise.all(profile?.fields.map(async p => this.encodeField(p)) ?? []),
bot: user.isBot,
discoverable: user.isExplorable,
});
}
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);
}
private async convertReblog(status: Entity.Status | null): Promise<any> {
if (!status) return null;
return await this.convertStatus(status);
}
public async convertStatus(status: Entity.Status) {
const convertedAccount = this.convertAccount(status.account);
const note = await this.getterService.getNote(status.id);
const noteUser = await this.getUser(status.account.id);
const emojis = await this.customEmojiService.populateEmojis(note.emojis, noteUser.host ? noteUser.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,
});
});
const mentions = Promise.all(note.mentions.map(p =>
this.getUser(p)
.then(u => this.encode(u, JSON.parse(note.mentionedRemoteUsers)))
.catch(() => null)))
.then(p => p.filter(m => m)) as Promise<Entity.Mention[]>;
const tags = note.tags.map(tag => {
return {
name: tag,
url: `${this.config.url}/tags/${tag}`,
} as Entity.Tag;
});
const isQuote = note.renoteId && note.text ? true : false;
const renote = note.renoteId ? this.getterService.getNote(note.renoteId) : null;
const quoteUri = Promise.resolve(renote).then(renote => {
if (!renote || !isQuote) return null;
return renote.url ?? renote.uri ?? `${this.config.url}/notes/${renote.id}`;
});
const content = note.text !== null
? quoteUri.then(quoteUri => this.mfmService.toMastoHtml(mfm.parse(note.text!), JSON.parse(note.mentionedRemoteUsers), false, quoteUri))
.then(p => p ?? escapeMFM(note.text!))
: '';
// noinspection ES6MissingAwait
return await awaitAll({
id: note.id,
uri: note.uri ?? `https://${this.config.host}/notes/${note.id}`,
url: note.url ?? note.uri ?? `https://${this.config.host}/notes/${note.id}`,
account: convertedAccount,
in_reply_to_id: note.replyId,
in_reply_to_account_id: note.replyUserId,
reblog: !isQuote ? await this.convertReblog(status.reblog) : null,
content: content,
content_type: 'text/x.misskeymarkdown',
text: note.text,
created_at: status.created_at,
emojis: emoji,
replies_count: note.repliesCount,
reblogs_count: note.renoteCount,
favourites_count: status.favourites_count,
reblogged: false,
favourited: status.favourited,
muted: status.muted,
sensitive: status.sensitive,
spoiler_text: note.cw ? note.cw : '',
visibility: status.visibility,
media_attachments: status.media_attachments,
mentions: mentions,
tags: tags,
card: null, //FIXME
poll: status.poll ?? null,
application: null, //FIXME
language: null, //FIXME
pinned: false,
reactions: status.emoji_reactions,
emoji_reactions: status.emoji_reactions,
bookmarked: false,
quote: isQuote ? await this.convertReblog(status.reblog) : null,
edited_at: note.updatedAt?.toISOString(),
});
}
}
function simpleConvert(data: any) {
// copy the object to bypass weird pass by reference bugs
const result = Object.assign({}, data);
return result;
}
export function convertAccount(account: Entity.Account) {
return simpleConvert(account);
}
export function convertAnnouncement(announcement: Entity.Announcement) {
return simpleConvert(announcement);
}
export function convertAttachment(attachment: Entity.Attachment) {
return simpleConvert(attachment);
}
export function convertFilter(filter: Entity.Filter) {
return simpleConvert(filter);
}
export function convertList(list: Entity.List) {
return simpleConvert(list);
}
export function convertFeaturedTag(tag: Entity.FeaturedTag) {
return simpleConvert(tag);
}
export function convertNotification(notification: Entity.Notification) {
notification.account = convertAccount(notification.account);
if (notification.status) notification.status = convertStatus(notification.status);
return notification;
}
export function convertPoll(poll: Entity.Poll) {
return simpleConvert(poll);
}
export function convertReaction(reaction: Entity.Reaction) {
if (reaction.accounts) {
reaction.accounts = reaction.accounts.map(convertAccount);
}
return reaction;
}
export function convertRelationship(relationship: Entity.Relationship) {
return simpleConvert(relationship);
}
export function convertStatus(status: Entity.Status) {
status.account = convertAccount(status.account);
status.media_attachments = status.media_attachments.map((attachment) =>
convertAttachment(attachment),
);
if (status.poll) status.poll = convertPoll(status.poll);
if (status.reblog) status.reblog = convertStatus(status.reblog);
return status;
}
export function convertStatusSource(status: Entity.StatusSource) {
return simpleConvert(status);
}
export function convertConversation(conversation: Entity.Conversation) {
conversation.accounts = conversation.accounts.map(convertAccount);
if (conversation.last_status) {
conversation.last_status = convertStatus(conversation.last_status);
}
return conversation;
}

View file

@ -1,17 +0,0 @@
import { ApiAuthMastodon } from './endpoints/auth.js';
import { ApiAccountMastodon } from './endpoints/account.js';
import { ApiSearchMastodon } from './endpoints/search.js';
import { ApiNotifyMastodon } from './endpoints/notifications.js';
import { ApiFilterMastodon } from './endpoints/filter.js';
import { ApiTimelineMastodon } from './endpoints/timeline.js';
import { ApiStatusMastodon } from './endpoints/status.js';
export {
ApiAccountMastodon,
ApiAuthMastodon,
ApiSearchMastodon,
ApiNotifyMastodon,
ApiFilterMastodon,
ApiTimelineMastodon,
ApiStatusMastodon,
};

View file

@ -1,270 +0,0 @@
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';
import { Injectable } from '@nestjs/common';
const relationshipModel = {
id: '',
following: false,
followed_by: false,
delivery_following: false,
blocking: false,
blocked_by: false,
muting: false,
muting_notifications: false,
requested: false,
domain_blocking: false,
showing_reblogs: false,
endorsed: false,
notifying: false,
note: '',
};
@Injectable()
export class ApiAccountMastodon {
private request: FastifyRequest;
private client: MegalodonInterface;
private BASE_URL: string;
constructor(request: FastifyRequest, client: MegalodonInterface, BASE_URL: string, private mastoconverter: MastoConverters) {
this.request = request;
this.client = client;
this.BASE_URL = BASE_URL;
}
public async verifyCredentials() {
try {
const data = await this.client.verifyAccountCredentials();
const acct = await this.mastoconverter.convertAccount(data.data);
const newAcct = Object.assign({}, acct, {
source: {
note: acct.note,
fields: acct.fields,
privacy: '',
sensitive: false,
language: '',
},
});
return newAcct;
} catch (e: any) {
/* console.error(e);
console.error(e.response.data); */
return e.response;
}
}
public async lookup() {
try {
const data = await this.client.search((this.request.query as any).acct, { type: 'accounts' });
return this.mastoconverter.convertAccount(data.data.accounts[0]);
} catch (e: any) {
/* console.error(e)
console.error(e.response.data); */
return e.response;
}
}
public async getRelationships(users: [string]) {
try {
relationshipModel.id = users.toString() || '1';
if (!(users.length > 0)) {
return [relationshipModel];
}
const reqIds = [];
for (let i = 0; i < users.length; i++) {
reqIds.push(users[i]);
}
const data = await this.client.getRelationships(reqIds);
return data.data.map((relationship) => convertRelationship(relationship));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
return e.response.data;
}
}
public async getStatuses() {
try {
const data = await this.client.getAccountStatuses((this.request.params as any).id, argsToBools(limitToInt(this.request.query as any)));
return await Promise.all(data.data.map(async (status) => await this.mastoconverter.convertStatus(status)));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
return e.response.data;
}
}
public async getFollowers() {
try {
const data = await this.client.getAccountFollowers(
(this.request.params as any).id,
limitToInt(this.request.query as any),
);
return await Promise.all(data.data.map(async (account) => await this.mastoconverter.convertAccount(account)));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
return e.response.data;
}
}
public async getFollowing() {
try {
const data = await this.client.getAccountFollowing(
(this.request.params as any).id,
limitToInt(this.request.query as any),
);
return await Promise.all(data.data.map(async (account) => await this.mastoconverter.convertAccount(account)));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
return e.response.data;
}
}
public async addFollow() {
try {
const data = await this.client.followAccount( (this.request.params as any).id );
const acct = convertRelationship(data.data);
acct.following = true;
return acct;
} catch (e: any) {
console.error(e);
console.error(e.response.data);
return e.response.data;
}
}
public async rmFollow() {
try {
const data = await this.client.unfollowAccount( (this.request.params as any).id );
const acct = convertRelationship(data.data);
acct.following = false;
return acct;
} catch (e: any) {
console.error(e);
console.error(e.response.data);
return e.response.data;
}
}
public async addBlock() {
try {
const data = await this.client.blockAccount( (this.request.params as any).id );
return convertRelationship(data.data);
} catch (e: any) {
console.error(e);
console.error(e.response.data);
return e.response.data;
}
}
public async rmBlock() {
try {
const data = await this.client.unblockAccount( (this.request.params as any).id );
return convertRelationship(data.data);
} catch (e: any) {
console.error(e);
console.error(e.response.data);
return e.response.data;
}
}
public async addMute() {
try {
const data = await this.client.muteAccount(
(this.request.params as any).id,
this.request.body as any,
);
return convertRelationship(data.data);
} catch (e: any) {
console.error(e);
console.error(e.response.data);
return e.response.data;
}
}
public async rmMute() {
try {
const data = await this.client.unmuteAccount( (this.request.params as any).id );
return convertRelationship(data.data);
} catch (e: any) {
console.error(e);
console.error(e.response.data);
return e.response.data;
}
}
public async getBookmarks() {
try {
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);
return e.response.data;
}
}
public async getFavourites() {
try {
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);
return e.response.data;
}
}
public async getMutes() {
try {
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);
return e.response.data;
}
}
public async getBlocks() {
try {
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);
return e.response.data;
}
}
public async acceptFollow() {
try {
const data = await this.client.acceptFollowRequest( (this.request.params as any).id );
return convertRelationship(data.data);
} catch (e: any) {
console.error(e);
console.error(e.response.data);
return e.response.data;
}
}
public async rejectFollow() {
try {
const data = await this.client.rejectFollowRequest( (this.request.params as any).id );
return convertRelationship(data.data);
} catch (e: any) {
console.error(e);
console.error(e.response.data);
return e.response.data;
}
}
}

View file

@ -1,74 +0,0 @@
import type { MegalodonInterface } from 'megalodon';
import type { FastifyRequest } from 'fastify';
const readScope = [
'read:account',
'read:drive',
'read:blocks',
'read:favorites',
'read:following',
'read:messaging',
'read:mutes',
'read:notifications',
'read:reactions',
'read:pages',
'read:page-likes',
'read:user-groups',
'read:channels',
'read:gallery',
'read:gallery-likes',
];
const writeScope = [
'write:account',
'write:drive',
'write:blocks',
'write:favorites',
'write:following',
'write:messaging',
'write:mutes',
'write:notes',
'write:notifications',
'write:reactions',
'write:votes',
'write:pages',
'write:page-likes',
'write:user-groups',
'write:channels',
'write:gallery',
'write:gallery-likes',
];
export async function ApiAuthMastodon(request: FastifyRequest, client: MegalodonInterface) {
const body: any = request.body || request.query;
try {
let scope = body.scopes;
if (typeof scope === 'string') scope = scope.split(' ') || scope.split('+');
const pushScope = new Set<string>();
for (const s of scope) {
if (s.match(/^read/)) for (const r of readScope) pushScope.add(r);
if (s.match(/^write/)) for (const r of writeScope) pushScope.add(r);
}
const scopeArr = Array.from(pushScope);
const red = body.redirect_uris;
const appData = await client.registerApp(body.client_name, {
scopes: scopeArr,
redirect_uris: red,
website: body.website,
});
const returns = {
id: Math.floor(Math.random() * 100).toString(),
name: appData.name,
website: body.website,
redirect_uri: red,
client_id: Buffer.from(appData.url || '').toString('base64'),
client_secret: appData.clientSecret,
};
return returns;
} catch (e: any) {
console.error(e);
return e.response.data;
}
}

View file

@ -1,65 +0,0 @@
import { convertFilter } from '../converters.js';
import type { MegalodonInterface } from 'megalodon';
import type { FastifyRequest } from 'fastify';
export class ApiFilterMastodon {
private request: FastifyRequest;
private client: MegalodonInterface;
constructor(request: FastifyRequest, client: MegalodonInterface) {
this.request = request;
this.client = client;
}
public async getFilters() {
try {
const data = await this.client.getFilters();
return data.data.map((filter) => convertFilter(filter));
} catch (e: any) {
console.error(e);
return e.response.data;
}
}
public async getFilter() {
try {
const data = await this.client.getFilter( (this.request.params as any).id );
return convertFilter(data.data);
} catch (e: any) {
console.error(e);
return e.response.data;
}
}
public async createFilter() {
try {
const body: any = this.request.body;
const data = await this.client.createFilter(body.pharse, body.context, body);
return convertFilter(data.data);
} catch (e: any) {
console.error(e);
return e.response.data;
}
}
public async updateFilter() {
try {
const body: any = this.request.body;
const data = await this.client.updateFilter((this.request.params as any).id, body.pharse, body.context);
return convertFilter(data.data);
} catch (e: any) {
console.error(e);
return e.response.data;
}
}
public async rmFilter() {
try {
const data = await this.client.deleteFilter( (this.request.params as any).id );
return data.data;
} catch (e: any) {
console.error(e);
return e.response.data;
}
}
}

View file

@ -1,63 +0,0 @@
import { Entity } from 'megalodon';
import { MAX_NOTE_TEXT_LENGTH, FILE_TYPE_BROWSERSAFE } from '@/const.js';
import type { Config } from '@/config.js';
import type { MiMeta } from '@/models/Meta.js';
export async function getInstance(
response: Entity.Instance,
contact: Entity.Account,
config: Config,
meta: MiMeta,
) {
return {
uri: config.url,
title: meta.name || 'Sharkey',
short_description:
meta.description || 'This is a vanilla Sharkey Instance. It doesn\'t seem to have a description.',
description:
meta.description ||
'This is a vanilla Sharkey Instance. It doesn\'t seem to have a description.',
email: response.email || '',
version: `3.0.0 (compatible; Sharkey ${config.version})`,
urls: response.urls,
stats: {
user_count: response.stats.user_count,
status_count: response.stats.status_count,
domain_count: response.stats.domain_count,
},
thumbnail: meta.backgroundImageUrl || '/static-assets/transparent.png',
languages: meta.langs,
registrations: !meta.disableRegistration || response.registrations,
approval_required: meta.approvalRequiredForSignup,
invites_enabled: response.registrations,
configuration: {
accounts: {
max_featured_tags: 20,
},
statuses: {
max_characters: MAX_NOTE_TEXT_LENGTH,
max_media_attachments: 16,
characters_reserved_per_url: response.uri.length,
},
media_attachments: {
supported_mime_types: FILE_TYPE_BROWSERSAFE,
image_size_limit: 10485760,
image_matrix_limit: 16777216,
video_size_limit: 41943040,
video_frame_rate_limit: 60,
video_matrix_limit: 2304000,
},
polls: {
max_options: 10,
max_characters_per_option: 50,
min_expiration: 50,
max_expiration: 2629746,
},
reactions: {
max_reactions: 1,
},
},
contact_account: contact,
rules: [],
};
}

View file

@ -1,70 +0,0 @@
import { convertNotification } from '../converters.js';
import type { MegalodonInterface, Entity } from 'megalodon';
import type { FastifyRequest } from 'fastify';
function toLimitToInt(q: any) {
if (q.limit) if (typeof q.limit === 'string') q.limit = parseInt(q.limit, 10);
return q;
}
export class ApiNotifyMastodon {
private request: FastifyRequest;
private client: MegalodonInterface;
constructor(request: FastifyRequest, client: MegalodonInterface) {
this.request = request;
this.client = client;
}
public async getNotifications() {
try {
const data = await this.client.getNotifications( toLimitToInt(this.request.query) );
const notifs = data.data;
const processed = notifs.map((n: Entity.Notification) => {
const convertedn = convertNotification(n);
if (convertedn.type !== 'follow' && convertedn.type !== 'follow_request') {
if (convertedn.type === 'reaction') convertedn.type = 'favourite';
return convertedn;
} else {
return convertedn;
}
});
return processed;
} catch (e: any) {
console.error(e);
return e.response.data;
}
}
public async getNotification() {
try {
const data = await this.client.getNotification( (this.request.params as any).id );
const notif = convertNotification(data.data);
if (notif.type !== 'follow' && notif.type !== 'follow_request' && notif.type === 'reaction') notif.type = 'favourite';
return notif;
} catch (e: any) {
console.error(e);
return e.response.data;
}
}
public async rmNotification() {
try {
const data = await this.client.dismissNotification( (this.request.params as any).id );
return data.data;
} catch (e: any) {
console.error(e);
return e.response.data;
}
}
public async rmNotifications() {
try {
const data = await this.client.dismissNotifications();
return data.data;
} catch (e: any) {
console.error(e);
return e.response.data;
}
}
}

View file

@ -1,85 +0,0 @@
import { MastoConverters } from '../converters.js';
import { limitToInt } from './timeline.js';
import type { MegalodonInterface } from 'megalodon';
import type { FastifyRequest } from 'fastify';
export class ApiSearchMastodon {
private request: FastifyRequest;
private client: MegalodonInterface;
private BASE_URL: string;
constructor(request: FastifyRequest, client: MegalodonInterface, BASE_URL: string, private mastoConverter: MastoConverters) {
this.request = request;
this.client = client;
this.BASE_URL = BASE_URL;
}
public async SearchV1() {
try {
const query: any = limitToInt(this.request.query as any);
const type = query.type || '';
const data = await this.client.search(query.q, { type: type, ...query });
return data.data;
} catch (e: any) {
console.error(e);
return e.response.data;
}
}
public async SearchV2() {
try {
const query: any = limitToInt(this.request.query as any);
const type = query.type;
const acct = !type || type === 'accounts' ? await this.client.search(query.q, { type: 'accounts', ...query }) : null;
const stat = !type || type === 'statuses' ? await this.client.search(query.q, { type: 'statuses', ...query }) : null;
const tags = !type || type === 'hashtags' ? await this.client.search(query.q, { type: 'hashtags', ...query }) : null;
const data = {
accounts: await Promise.all(acct?.data.accounts.map(async (account: any) => await this.mastoConverter.convertAccount(account)) ?? []),
statuses: await Promise.all(stat?.data.statuses.map(async (status: any) => await this.mastoConverter.convertStatus(status)) ?? []),
hashtags: tags?.data.hashtags ?? [],
};
return data;
} catch (e: any) {
console.error(e);
return e.response.data;
}
}
public async getStatusTrends() {
try {
const data = await fetch(`${this.BASE_URL}/api/notes/featured`,
{
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({}),
})
.then(res => res.json())
.then(data => data.map((status: any) => this.mastoConverter.convertStatus(status)));
return data;
} catch (e: any) {
console.error(e);
return [];
}
}
public async getSuggestions() {
try {
const data = await fetch(`${this.BASE_URL}/api/users`,
{
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ i: this.request.headers.authorization?.replace('Bearer ', ''), limit: parseInt((this.request.query as any).limit) || 20, origin: 'local', sort: '+follower', state: 'alive' }),
}).then((res) => res.json()).then(data => data.map(((entry: any) => { return { source: 'global', account: entry }; })));
return Promise.all(data.map(async (suggestion: any) => { suggestion.account = await this.mastoConverter.convertAccount(suggestion.account); return suggestion; }));
} catch (e: any) {
console.error(e);
return [];
}
}
}

View file

@ -1,410 +0,0 @@
import querystring from 'querystring';
import { emojiRegexAtStartToEnd } from '@/misc/emoji-regex.js';
import { convertAttachment, convertPoll, convertStatusSource, MastoConverters } from '../converters.js';
import { getClient } from '../MastodonApiServerService.js';
import { limitToInt } from './timeline.js';
import type { Entity } from 'megalodon';
import type { FastifyInstance } from 'fastify';
import type { Config } from '@/config.js';
import { NoteEditRepository, NotesRepository, UsersRepository } from '@/models/_.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
function normalizeQuery(data: any) {
const str = querystring.stringify(data);
return querystring.parse(str);
}
export class ApiStatusMastodon {
private fastify: FastifyInstance;
private mastoconverter: MastoConverters;
constructor(fastify: FastifyInstance, mastoconverter: MastoConverters) {
this.fastify = fastify;
this.mastoconverter = mastoconverter;
}
public async getStatus() {
this.fastify.get<{ Params: { id: string } }>('/v1/statuses/:id', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getStatus(_request.params.id);
reply.send(await this.mastoconverter.convertStatus(data.data));
} catch (e: any) {
console.error(e);
reply.code(_request.is404 ? 404 : 401).send(e.response.data);
}
});
}
public async getStatusSource() {
this.fastify.get<{ Params: { id: string } }>('/v1/statuses/:id/source', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getStatusSource(_request.params.id);
reply.send(data.data);
} catch (e: any) {
console.error(e);
reply.code(_request.is404 ? 404 : 401).send(e.response.data);
}
});
}
public async getContext() {
this.fastify.get<{ Params: { id: string } }>('/v1/statuses/:id/context', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
const query: any = _request.query;
try {
const data = await client.getStatusContext(_request.params.id, limitToInt(query));
data.data.ancestors = await Promise.all(data.data.ancestors.map(async (status: Entity.Status) => await this.mastoconverter.convertStatus(status)));
data.data.descendants = await Promise.all(data.data.descendants.map(async (status: Entity.Status) => await this.mastoconverter.convertStatus(status)));
reply.send(data.data);
} catch (e: any) {
console.error(e);
reply.code(_request.is404 ? 404 : 401).send(e.response.data);
}
});
}
public async getHistory() {
this.fastify.get<{ Params: { id: string } }>('/v1/statuses/:id/history', async (_request, reply) => {
try {
const edits = await this.mastoconverter.getEdits(_request.params.id);
reply.send(edits);
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
});
}
public async getReblogged() {
this.fastify.get<{ Params: { id: string } }>('/v1/statuses/:id/reblogged_by', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getStatusRebloggedBy(_request.params.id);
reply.send(await Promise.all(data.data.map(async (account: Entity.Account) => await this.mastoconverter.convertAccount(account))));
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
});
}
public async getFavourites() {
this.fastify.get<{ Params: { id: string } }>('/v1/statuses/:id/favourited_by', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getStatusFavouritedBy(_request.params.id);
reply.send(await Promise.all(data.data.map(async (account: Entity.Account) => await this.mastoconverter.convertAccount(account))));
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
});
}
public async getMedia() {
this.fastify.get<{ Params: { id: string } }>('/v1/media/:id', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getMedia(_request.params.id);
reply.send(convertAttachment(data.data));
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
});
}
public async getPoll() {
this.fastify.get<{ Params: { id: string } }>('/v1/polls/:id', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getPoll(_request.params.id);
reply.send(convertPoll(data.data));
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
});
}
public async votePoll() {
this.fastify.post<{ Params: { id: string } }>('/v1/polls/:id/votes', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
const body: any = _request.body;
try {
const data = await client.votePoll(_request.params.id, body.choices);
reply.send(convertPoll(data.data));
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
});
}
public async postStatus() {
this.fastify.post('/v1/statuses', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
let body: any = _request.body;
try {
if (
(!body.poll && body['poll[options][]']) ||
(!body.media_ids && body['media_ids[]'])
) {
body = normalizeQuery(body);
}
const text = body.status ? body.status : ' ';
const removed = text.replace(/@\S+/g, '').replace(/\s|/g, '');
const isDefaultEmoji = emojiRegexAtStartToEnd.test(removed);
const isCustomEmoji = /^:[a-zA-Z0-9@_]+:$/.test(removed);
if ((body.in_reply_to_id && isDefaultEmoji) || (body.in_reply_to_id && isCustomEmoji)) {
const a = await client.createEmojiReaction(
body.in_reply_to_id,
removed,
);
reply.send(a.data);
}
if (body.in_reply_to_id && removed === '/unreact') {
try {
const id = body.in_reply_to_id;
const post = await client.getStatus(id);
const react = post.data.emoji_reactions.filter((e: any) => e.me)[0].name;
const data = await client.deleteEmojiReaction(id, react);
reply.send(data.data);
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
}
if (!body.media_ids) body.media_ids = undefined;
if (body.media_ids && !body.media_ids.length) body.media_ids = undefined;
const { sensitive } = body;
body.sensitive = typeof sensitive === 'string' ? sensitive === 'true' : sensitive;
if (body.poll) {
if (
body.poll.expires_in != null &&
typeof body.poll.expires_in === 'string'
) body.poll.expires_in = parseInt(body.poll.expires_in);
if (
body.poll.multiple != null &&
typeof body.poll.multiple === 'string'
) body.poll.multiple = body.poll.multiple === 'true';
if (
body.poll.hide_totals != null &&
typeof body.poll.hide_totals === 'string'
) body.poll.hide_totals = body.poll.hide_totals === 'true';
}
const data = await client.postStatus(text, body);
reply.send(await this.mastoconverter.convertStatus(data.data as Entity.Status));
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
});
}
public async updateStatus() {
this.fastify.put<{ Params: { id: string } }>('/v1/statuses/:id', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
const body: any = _request.body;
try {
if (!body.media_ids) body.media_ids = undefined;
if (body.media_ids && !body.media_ids.length) body.media_ids = undefined;
const data = await client.editStatus(_request.params.id, body);
reply.send(await this.mastoconverter.convertStatus(data.data));
} catch (e: any) {
console.error(e);
reply.code(_request.is404 ? 404 : 401).send(e.response.data);
}
});
}
public async addFavourite() {
this.fastify.post<{ Params: { id: string } }>('/v1/statuses/:id/favourite', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = (await client.createEmojiReaction(_request.params.id, '❤')) as any;
reply.send(await this.mastoconverter.convertStatus(data.data));
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
});
}
public async rmFavourite() {
this.fastify.post<{ Params: { id: string } }>('/v1/statuses/:id/unfavourite', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.deleteEmojiReaction(_request.params.id, '❤');
reply.send(await this.mastoconverter.convertStatus(data.data));
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
});
}
public async reblogStatus() {
this.fastify.post<{ Params: { id: string } }>('/v1/statuses/:id/reblog', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.reblogStatus(_request.params.id);
reply.send(await this.mastoconverter.convertStatus(data.data));
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
});
}
public async unreblogStatus() {
this.fastify.post<{ Params: { id: string } }>('/v1/statuses/:id/unreblog', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.unreblogStatus(_request.params.id);
reply.send(await this.mastoconverter.convertStatus(data.data));
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
});
}
public async bookmarkStatus() {
this.fastify.post<{ Params: { id: string } }>('/v1/statuses/:id/bookmark', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.bookmarkStatus(_request.params.id);
reply.send(await this.mastoconverter.convertStatus(data.data));
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
});
}
public async unbookmarkStatus() {
this.fastify.post<{ Params: { id: string } }>('/v1/statuses/:id/unbookmark', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.unbookmarkStatus(_request.params.id);
reply.send(await this.mastoconverter.convertStatus(data.data));
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
});
}
public async pinStatus() {
this.fastify.post<{ Params: { id: string } }>('/v1/statuses/:id/pin', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.pinStatus(_request.params.id);
reply.send(await this.mastoconverter.convertStatus(data.data));
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
});
}
public async unpinStatus() {
this.fastify.post<{ Params: { id: string } }>('/v1/statuses/:id/unpin', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.unpinStatus(_request.params.id);
reply.send(await this.mastoconverter.convertStatus(data.data));
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
});
}
public async reactStatus() {
this.fastify.post<{ Params: { id: string, name: string } }>('/v1/statuses/:id/react/:name', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.createEmojiReaction(_request.params.id, _request.params.name);
reply.send(await this.mastoconverter.convertStatus(data.data));
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
});
}
public async unreactStatus() {
this.fastify.post<{ Params: { id: string, name: string } }>('/v1/statuses/:id/unreact/:name', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.deleteEmojiReaction(_request.params.id, _request.params.name);
reply.send(await this.mastoconverter.convertStatus(data.data));
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
});
}
public async deleteStatus() {
this.fastify.delete<{ Params: { id: string } }>('/v1/statuses/:id', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.deleteStatus(_request.params.id);
reply.send(data.data);
} catch (e: any) {
console.error(e);
reply.code(401).send(e.response.data);
}
});
}
}

View file

@ -1,268 +0,0 @@
import { ParsedUrlQuery } from 'querystring';
import { convertConversation, convertList, MastoConverters } from '../converters.js';
import { getClient } from '../MastodonApiServerService.js';
import type { Entity } from 'megalodon';
import type { FastifyInstance } from 'fastify';
import type { Config } from '@/config.js';
import { NoteEditRepository, NotesRepository, UsersRepository } from '@/models/_.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
export function limitToInt(q: ParsedUrlQuery) {
const object: any = q;
if (q.limit) if (typeof q.limit === 'string') object.limit = parseInt(q.limit, 10);
if (q.offset) if (typeof q.offset === 'string') object.offset = parseInt(q.offset, 10);
return object;
}
export function argsToBools(q: ParsedUrlQuery) {
// Values taken from https://docs.joinmastodon.org/client/intro/#boolean
const toBoolean = (value: string) =>
!['0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].includes(value);
// Keys taken from:
// - https://docs.joinmastodon.org/methods/accounts/#statuses
// - https://docs.joinmastodon.org/methods/timelines/#public
// - https://docs.joinmastodon.org/methods/timelines/#tag
const object: any = q;
if (q.only_media) if (typeof q.only_media === 'string') object.only_media = toBoolean(q.only_media);
if (q.exclude_replies) if (typeof q.exclude_replies === 'string') object.exclude_replies = toBoolean(q.exclude_replies);
if (q.exclude_reblogs) if (typeof q.exclude_reblogs === 'string') object.exclude_reblogs = toBoolean(q.exclude_reblogs);
if (q.pinned) if (typeof q.pinned === 'string') object.pinned = toBoolean(q.pinned);
if (q.local) if (typeof q.local === 'string') object.local = toBoolean(q.local);
return q;
}
export class ApiTimelineMastodon {
private fastify: FastifyInstance;
constructor(fastify: FastifyInstance, config: Config, private mastoconverter: MastoConverters) {
this.fastify = fastify;
}
public async getTL() {
this.fastify.get('/v1/timelines/public', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const query: any = _request.query;
const data = query.local === 'true'
? await client.getLocalTimeline(argsToBools(limitToInt(query)))
: await client.getPublicTimeline(argsToBools(limitToInt(query)));
reply.send(await Promise.all(data.data.map(async (status: Entity.Status) => await this.mastoconverter.convertStatus(status))));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
reply.code(401).send(e.response.data);
}
});
}
public async getHomeTl() {
this.fastify.get('/v1/timelines/home', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const query: any = _request.query;
const data = await client.getHomeTimeline(limitToInt(query));
reply.send(await Promise.all(data.data.map(async (status: Entity.Status) => await this.mastoconverter.convertStatus(status))));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
reply.code(401).send(e.response.data);
}
});
}
public async getTagTl() {
this.fastify.get<{ Params: { hashtag: string } }>('/v1/timelines/tag/:hashtag', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const query: any = _request.query;
const params: any = _request.params;
const data = await client.getTagTimeline(params.hashtag, limitToInt(query));
reply.send(await Promise.all(data.data.map(async (status: Entity.Status) => await this.mastoconverter.convertStatus(status))));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
reply.code(401).send(e.response.data);
}
});
}
public async getListTL() {
this.fastify.get<{ Params: { id: string } }>('/v1/timelines/list/:id', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const query: any = _request.query;
const params: any = _request.params;
const data = await client.getListTimeline(params.id, limitToInt(query));
reply.send(await Promise.all(data.data.map(async (status: Entity.Status) => await this.mastoconverter.convertStatus(status))));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
reply.code(401).send(e.response.data);
}
});
}
public async getConversations() {
this.fastify.get('/v1/conversations', async (_request, reply) => {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const query: any = _request.query;
const data = await client.getConversationTimeline(limitToInt(query));
reply.send(data.data.map((conversation: Entity.Conversation) => convertConversation(conversation)));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
reply.code(401).send(e.response.data);
}
});
}
public async getList() {
this.fastify.get<{ Params: { id: string } }>('/v1/lists/:id', async (_request, reply) => {
try {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
const params: any = _request.params;
const data = await client.getList(params.id);
reply.send(convertList(data.data));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
reply.code(401).send(e.response.data);
}
});
}
public async getLists() {
this.fastify.get('/v1/lists', async (_request, reply) => {
try {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
const data = await client.getLists();
reply.send(data.data.map((list: Entity.List) => convertList(list)));
} catch (e: any) {
console.error(e);
return e.response.data;
}
});
}
public async getListAccounts() {
this.fastify.get<{ Params: { id: string } }>('/v1/lists/:id/accounts', async (_request, reply) => {
try {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
const params: any = _request.params;
const query: any = _request.query;
const data = await client.getAccountsInList(params.id, query);
reply.send(data.data.map((account: Entity.Account) => this.mastoconverter.convertAccount(account)));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
reply.code(401).send(e.response.data);
}
});
}
public async addListAccount() {
this.fastify.post<{ Params: { id: string } }>('/v1/lists/:id/accounts', async (_request, reply) => {
try {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
const params: any = _request.params;
const query: any = _request.query;
const data = await client.addAccountsToList(params.id, query.accounts_id);
reply.send(data.data);
} catch (e: any) {
console.error(e);
console.error(e.response.data);
reply.code(401).send(e.response.data);
}
});
}
public async rmListAccount() {
this.fastify.delete<{ Params: { id: string } }>('/v1/lists/:id/accounts', async (_request, reply) => {
try {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
const params: any = _request.params;
const query: any = _request.query;
const data = await client.deleteAccountsFromList(params.id, query.accounts_id);
reply.send(data.data);
} catch (e: any) {
console.error(e);
console.error(e.response.data);
reply.code(401).send(e.response.data);
}
});
}
public async createList() {
this.fastify.post('/v1/lists', async (_request, reply) => {
try {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
const body: any = _request.body;
const data = await client.createList(body.title);
reply.send(convertList(data.data));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
reply.code(401).send(e.response.data);
}
});
}
public async updateList() {
this.fastify.put<{ Params: { id: string } }>('/v1/lists/:id', async (_request, reply) => {
try {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
const body: any = _request.body;
const params: any = _request.params;
const data = await client.updateList(params.id, body.title);
reply.send(convertList(data.data));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
reply.code(401).send(e.response.data);
}
});
}
public async deleteList() {
this.fastify.delete<{ Params: { id: string } }>('/v1/lists/:id', async (_request, reply) => {
try {
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
const params: any = _request.params;
const data = await client.deleteList(params.id);
reply.send({});
} catch (e: any) {
console.error(e);
console.error(e.response.data);
reply.code(401).send(e.response.data);
}
});
}
}