mirror of
https://activitypub.software/TransFem-org/Sharkey.git
synced 2025-04-13 09:44:40 +00:00
View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/954 Closes #405, #471, and #984 Approved-by: Marie <github@yuugi.dev> Approved-by: dakkar <dakkar@thenautilus.net>
This commit is contained in:
commit
920bf71eb5
133 changed files with 2182 additions and 16735 deletions
|
@ -179,7 +179,7 @@ export class MfmService {
|
|||
break;
|
||||
}
|
||||
|
||||
// this is here only to catch upstream changes!
|
||||
// this is here only to catch upstream changes!
|
||||
case 'ruby--': {
|
||||
let ruby: [string, string][] = [];
|
||||
for (const child of node.childNodes) {
|
||||
|
@ -584,9 +584,10 @@ export class MfmService {
|
|||
}
|
||||
|
||||
// the toMastoApiHtml function was taken from Iceshrimp and written by zotan and modified by marie to work with the current MK version
|
||||
// additionally modified by hazelnoot to remove async
|
||||
|
||||
@bindThis
|
||||
public async toMastoApiHtml(nodes: mfm.MfmNode[] | null, mentionedRemoteUsers: IMentionedRemoteUsers = [], inline = false, quoteUri: string | null = null) {
|
||||
public toMastoApiHtml(nodes: mfm.MfmNode[] | null, mentionedRemoteUsers: IMentionedRemoteUsers = [], inline = false, quoteUri: string | null = null) {
|
||||
if (nodes == null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -597,50 +598,50 @@ export class MfmService {
|
|||
|
||||
const body = doc.createElement('p');
|
||||
|
||||
async function appendChildren(children: mfm.MfmNode[], targetElement: any): Promise<void> {
|
||||
function appendChildren(children: mfm.MfmNode[], targetElement: any): void {
|
||||
if (children) {
|
||||
for (const child of await Promise.all(children.map(async (x) => await (handlers as any)[x.type](x)))) targetElement.appendChild(child);
|
||||
for (const child of children.map((x) => (handlers as any)[x.type](x))) targetElement.appendChild(child);
|
||||
}
|
||||
}
|
||||
|
||||
const handlers: {
|
||||
[K in mfm.MfmNode['type']]: (node: mfm.NodeType<K>) => any;
|
||||
} = {
|
||||
async bold(node) {
|
||||
bold(node) {
|
||||
const el = doc.createElement('span');
|
||||
el.textContent = '**';
|
||||
await appendChildren(node.children, el);
|
||||
appendChildren(node.children, el);
|
||||
el.textContent += '**';
|
||||
return el;
|
||||
},
|
||||
|
||||
async small(node) {
|
||||
small(node) {
|
||||
const el = doc.createElement('small');
|
||||
await appendChildren(node.children, el);
|
||||
appendChildren(node.children, el);
|
||||
return el;
|
||||
},
|
||||
|
||||
async strike(node) {
|
||||
strike(node) {
|
||||
const el = doc.createElement('span');
|
||||
el.textContent = '~~';
|
||||
await appendChildren(node.children, el);
|
||||
appendChildren(node.children, el);
|
||||
el.textContent += '~~';
|
||||
return el;
|
||||
},
|
||||
|
||||
async italic(node) {
|
||||
italic(node) {
|
||||
const el = doc.createElement('span');
|
||||
el.textContent = '*';
|
||||
await appendChildren(node.children, el);
|
||||
appendChildren(node.children, el);
|
||||
el.textContent += '*';
|
||||
return el;
|
||||
},
|
||||
|
||||
async fn(node) {
|
||||
fn(node) {
|
||||
switch (node.props.name) {
|
||||
case 'group': { // hack for ruby
|
||||
const el = doc.createElement('span');
|
||||
await appendChildren(node.children, el);
|
||||
appendChildren(node.children, el);
|
||||
return el;
|
||||
}
|
||||
case 'ruby': {
|
||||
|
@ -666,7 +667,7 @@ export class MfmService {
|
|||
|
||||
if (!rt) {
|
||||
const el = doc.createElement('span');
|
||||
await appendChildren(node.children, el);
|
||||
appendChildren(node.children, el);
|
||||
return el;
|
||||
}
|
||||
|
||||
|
@ -679,7 +680,7 @@ export class MfmService {
|
|||
const rpEndEl = doc.createElement('rp');
|
||||
rpEndEl.appendChild(doc.createTextNode(')'));
|
||||
|
||||
await appendChildren(node.children.slice(0, node.children.length - 1), rubyEl);
|
||||
appendChildren(node.children.slice(0, node.children.length - 1), rubyEl);
|
||||
rtEl.appendChild(doc.createTextNode(text.trim()));
|
||||
rubyEl.appendChild(rpStartEl);
|
||||
rubyEl.appendChild(rtEl);
|
||||
|
@ -691,7 +692,7 @@ export class MfmService {
|
|||
default: {
|
||||
const el = doc.createElement('span');
|
||||
el.textContent = '*';
|
||||
await appendChildren(node.children, el);
|
||||
appendChildren(node.children, el);
|
||||
el.textContent += '*';
|
||||
return el;
|
||||
}
|
||||
|
@ -714,9 +715,9 @@ export class MfmService {
|
|||
return pre;
|
||||
},
|
||||
|
||||
async center(node) {
|
||||
center(node) {
|
||||
const el = doc.createElement('div');
|
||||
await appendChildren(node.children, el);
|
||||
appendChildren(node.children, el);
|
||||
return el;
|
||||
},
|
||||
|
||||
|
@ -755,16 +756,16 @@ export class MfmService {
|
|||
return el;
|
||||
},
|
||||
|
||||
async link(node) {
|
||||
link(node) {
|
||||
const a = doc.createElement('a');
|
||||
a.setAttribute('rel', 'nofollow noopener noreferrer');
|
||||
a.setAttribute('target', '_blank');
|
||||
a.setAttribute('href', node.props.url);
|
||||
await appendChildren(node.children, a);
|
||||
appendChildren(node.children, a);
|
||||
return a;
|
||||
},
|
||||
|
||||
async mention(node) {
|
||||
mention(node) {
|
||||
const { username, host, acct } = node.props;
|
||||
const resolved = mentionedRemoteUsers.find(remoteUser => remoteUser.username === username && remoteUser.host === host);
|
||||
|
||||
|
@ -787,9 +788,9 @@ export class MfmService {
|
|||
return el;
|
||||
},
|
||||
|
||||
async quote(node) {
|
||||
quote(node) {
|
||||
const el = doc.createElement('blockquote');
|
||||
await appendChildren(node.children, el);
|
||||
appendChildren(node.children, el);
|
||||
return el;
|
||||
},
|
||||
|
||||
|
@ -822,14 +823,14 @@ export class MfmService {
|
|||
return a;
|
||||
},
|
||||
|
||||
async plain(node) {
|
||||
plain(node) {
|
||||
const el = doc.createElement('span');
|
||||
await appendChildren(node.children, el);
|
||||
appendChildren(node.children, el);
|
||||
return el;
|
||||
},
|
||||
};
|
||||
|
||||
await appendChildren(nodes, body);
|
||||
appendChildren(nodes, body);
|
||||
|
||||
if (quoteUri !== null) {
|
||||
const a = doc.createElement('a');
|
||||
|
|
|
@ -19,7 +19,9 @@ type Context = {
|
|||
type Level = 'error' | 'success' | 'warning' | 'debug' | 'info';
|
||||
|
||||
export type Data = DataElement | DataElement[];
|
||||
export type DataElement = Record<string, unknown> | Error | string | null;
|
||||
export type DataElement = DataObject | Error | string | null;
|
||||
// https://stackoverflow.com/questions/61148466/typescript-type-that-matches-any-object-but-not-arrays
|
||||
export type DataObject = Record<string, unknown> | (object & { length?: never; });
|
||||
|
||||
// eslint-disable-next-line import/no-default-export
|
||||
export default class Logger {
|
||||
|
|
|
@ -14,10 +14,13 @@
|
|||
* @param additional Content warning to append
|
||||
* @param reverse If true, then the additional CW will be prepended instead of appended.
|
||||
*/
|
||||
export function appendContentWarning(original: string | null | undefined, additional: string, reverse = false): string {
|
||||
export function appendContentWarning(original: string | null | undefined, additional: string, reverse?: boolean): string;
|
||||
export function appendContentWarning(original: string, additional: string | null | undefined, reverse?: boolean): string;
|
||||
export function appendContentWarning(original: string | null | undefined, additional: string | null | undefined, reverse?: boolean): string | null;
|
||||
export function appendContentWarning(original: string | null | undefined, additional: string | null | undefined, reverse = false): string | null {
|
||||
// Easy case - if original is empty, then additional replaces it.
|
||||
if (!original) {
|
||||
return additional;
|
||||
return additional ?? null;
|
||||
}
|
||||
|
||||
// Easy case - if the additional CW is empty, then don't append it.
|
||||
|
|
|
@ -7,6 +7,15 @@ import { Module } from '@nestjs/common';
|
|||
import { EndpointsModule } from '@/server/api/EndpointsModule.js';
|
||||
import { CoreModule } from '@/core/CoreModule.js';
|
||||
import { SkRateLimiterService } from '@/server/SkRateLimiterService.js';
|
||||
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
|
||||
import { ApiNotificationsMastodon } from '@/server/api/mastodon/endpoints/notifications.js';
|
||||
import { ApiAccountMastodon } from '@/server/api/mastodon/endpoints/account.js';
|
||||
import { ApiFilterMastodon } from '@/server/api/mastodon/endpoints/filter.js';
|
||||
import { ApiSearchMastodon } from '@/server/api/mastodon/endpoints/search.js';
|
||||
import { ApiTimelineMastodon } from '@/server/api/mastodon/endpoints/timeline.js';
|
||||
import { ApiAppsMastodon } from '@/server/api/mastodon/endpoints/apps.js';
|
||||
import { ApiInstanceMastodon } from '@/server/api/mastodon/endpoints/instance.js';
|
||||
import { ApiStatusMastodon } from '@/server/api/mastodon/endpoints/status.js';
|
||||
import { ApiCallService } from './api/ApiCallService.js';
|
||||
import { FileServerService } from './FileServerService.js';
|
||||
import { HealthServerService } from './HealthServerService.js';
|
||||
|
@ -26,7 +35,7 @@ import { SignupApiService } from './api/SignupApiService.js';
|
|||
import { StreamingApiServerService } from './api/StreamingApiServerService.js';
|
||||
import { OpenApiServerService } from './api/openapi/OpenApiServerService.js';
|
||||
import { ClientServerService } from './web/ClientServerService.js';
|
||||
import { MastoConverters } from './api/mastodon/converters.js';
|
||||
import { MastodonConverters } from './api/mastodon/MastodonConverters.js';
|
||||
import { MastodonLogger } from './api/mastodon/MastodonLogger.js';
|
||||
import { MastodonDataService } from './api/mastodon/MastodonDataService.js';
|
||||
import { FeedService } from './web/FeedService.js';
|
||||
|
@ -104,9 +113,18 @@ import { SigninWithPasskeyApiService } from './api/SigninWithPasskeyApiService.j
|
|||
OpenApiServerService,
|
||||
MastodonApiServerService,
|
||||
OAuth2ProviderService,
|
||||
MastoConverters,
|
||||
MastodonConverters,
|
||||
MastodonLogger,
|
||||
MastodonDataService,
|
||||
MastodonClientService,
|
||||
ApiAccountMastodon,
|
||||
ApiAppsMastodon,
|
||||
ApiFilterMastodon,
|
||||
ApiInstanceMastodon,
|
||||
ApiNotificationsMastodon,
|
||||
ApiSearchMastodon,
|
||||
ApiStatusMastodon,
|
||||
ApiTimelineMastodon,
|
||||
],
|
||||
exports: [
|
||||
ServerService,
|
||||
|
|
|
@ -84,8 +84,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
|||
const includeTypes = ps.includeTypes && ps.includeTypes.filter(type => !(obsoleteNotificationTypes).includes(type as any)) as typeof notificationTypes[number][];
|
||||
const excludeTypes = ps.excludeTypes && ps.excludeTypes.filter(type => !(obsoleteNotificationTypes).includes(type as any)) as typeof notificationTypes[number][];
|
||||
|
||||
let sinceTime = ps.sinceId ? this.idService.parse(ps.sinceId).date.getTime().toString() : null;
|
||||
let untilTime = ps.untilId ? this.idService.parse(ps.untilId).date.getTime().toString() : null;
|
||||
let sinceTime = ps.sinceId ? (this.idService.parse(ps.sinceId).date.getTime() + 1).toString() : null;
|
||||
let untilTime = ps.untilId ? (this.idService.parse(ps.untilId).date.getTime() - 1).toString() : null;
|
||||
|
||||
let notifications: MiNotification[];
|
||||
for (;;) {
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { Misskey } from 'megalodon';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { MiLocalUser } from '@/models/User.js';
|
||||
import { AuthenticateService } from '@/server/api/AuthenticateService.js';
|
||||
import type { FastifyRequest } from 'fastify';
|
||||
|
||||
@Injectable()
|
||||
export class MastodonClientService {
|
||||
constructor(
|
||||
private readonly authenticateService: AuthenticateService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Gets the authenticated user and API client for a request.
|
||||
*/
|
||||
public async getAuthClient(request: FastifyRequest, accessToken?: string | null): Promise<{ client: Misskey, me: MiLocalUser | null }> {
|
||||
const authorization = request.headers.authorization;
|
||||
accessToken = accessToken !== undefined ? accessToken : getAccessToken(authorization);
|
||||
|
||||
const me = await this.getAuth(request, accessToken);
|
||||
const client = this.getClient(request, accessToken);
|
||||
|
||||
return { client, me };
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the authenticated client user for a request.
|
||||
*/
|
||||
public async getAuth(request: FastifyRequest, accessToken?: string | null): Promise<MiLocalUser | null> {
|
||||
const authorization = request.headers.authorization;
|
||||
accessToken = accessToken !== undefined ? accessToken : getAccessToken(authorization);
|
||||
const [me] = await this.authenticateService.authenticate(accessToken);
|
||||
return me;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an authenticated API client for a request.
|
||||
*/
|
||||
public getClient(request: FastifyRequest, accessToken?: string | null): Misskey {
|
||||
const authorization = request.headers.authorization;
|
||||
accessToken = accessToken !== undefined ? accessToken : getAccessToken(authorization);
|
||||
|
||||
// TODO pass agent?
|
||||
const baseUrl = this.getBaseUrl(request);
|
||||
const userAgent = request.headers['user-agent'];
|
||||
return new Misskey(baseUrl, accessToken, userAgent);
|
||||
}
|
||||
|
||||
readonly getBaseUrl = getBaseUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the base URL (origin) of the incoming request
|
||||
*/
|
||||
export function getBaseUrl(request: FastifyRequest): string {
|
||||
return `${request.protocol}://${request.host}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the first access token from an authorization header
|
||||
* Returns null if none were found.
|
||||
*/
|
||||
function getAccessToken(authorization: string | undefined): string | null {
|
||||
const accessTokenArr = authorization?.split(' ') ?? [null];
|
||||
return accessTokenArr[accessTokenArr.length - 1];
|
||||
}
|
|
@ -6,6 +6,8 @@
|
|||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Entity } from 'megalodon';
|
||||
import mfm from '@transfem-org/sfm-js';
|
||||
import { MastodonNotificationType } from 'megalodon/lib/src/mastodon/notification.js';
|
||||
import { NotificationType } from 'megalodon/lib/src/notification.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import { MfmService } from '@/core/MfmService.js';
|
||||
import type { Config } from '@/config.js';
|
||||
|
@ -19,6 +21,8 @@ import { IdService } from '@/core/IdService.js';
|
|||
import type { Packed } from '@/misc/json-schema.js';
|
||||
import { MastodonDataService } from '@/server/api/mastodon/MastodonDataService.js';
|
||||
import { GetterService } from '@/server/api/GetterService.js';
|
||||
import { appendContentWarning } from '@/misc/append-content-warning.js';
|
||||
import { isRenote } from '@/misc/is-renote.js';
|
||||
|
||||
// Missing from Megalodon apparently
|
||||
// https://docs.joinmastodon.org/entities/StatusEdit/
|
||||
|
@ -47,7 +51,7 @@ export const escapeMFM = (text: string): string => text
|
|||
.replace(/\r?\n/g, '<br>');
|
||||
|
||||
@Injectable()
|
||||
export class MastoConverters {
|
||||
export class MastodonConverters {
|
||||
constructor(
|
||||
@Inject(DI.config)
|
||||
private readonly config: Config,
|
||||
|
@ -68,7 +72,6 @@ export class MastoConverters {
|
|||
|
||||
private encode(u: MiUser, m: IMentionedRemoteUsers): MastodonEntity.Mention {
|
||||
let acct = u.username;
|
||||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
||||
let acctUrl = `https://${u.host || this.config.host}/@${u.username}`;
|
||||
let url: string | null = null;
|
||||
if (u.host) {
|
||||
|
@ -136,10 +139,10 @@ export class MastoConverters {
|
|||
});
|
||||
}
|
||||
|
||||
private async encodeField(f: Entity.Field): Promise<MastodonEntity.Field> {
|
||||
private encodeField(f: Entity.Field): MastodonEntity.Field {
|
||||
return {
|
||||
name: f.name,
|
||||
value: await this.mfmService.toMastoApiHtml(mfm.parse(f.value), [], true) ?? escapeMFM(f.value),
|
||||
value: this.mfmService.toMastoApiHtml(mfm.parse(f.value), [], true) ?? escapeMFM(f.value),
|
||||
verified_at: null,
|
||||
};
|
||||
}
|
||||
|
@ -161,13 +164,15 @@ export class MastoConverters {
|
|||
});
|
||||
const fqn = `${user.username}@${user.host ?? this.config.hostname}`;
|
||||
let acct = user.username;
|
||||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
||||
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}`;
|
||||
}
|
||||
|
||||
const bioText = profile?.description && this.mfmService.toMastoApiHtml(mfm.parse(profile.description));
|
||||
|
||||
return awaitAll({
|
||||
id: account.id,
|
||||
username: user.username,
|
||||
|
@ -179,16 +184,16 @@ export class MastoConverters {
|
|||
followers_count: profile?.followersVisibility === 'public' ? user.followersCount : 0,
|
||||
following_count: profile?.followingVisibility === 'public' ? user.followingCount : 0,
|
||||
statuses_count: user.notesCount,
|
||||
note: profile?.description ?? '',
|
||||
note: bioText ?? '',
|
||||
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',
|
||||
avatar: user.avatarUrl ?? 'https://dev.joinsharkey.org/static-assets/avatar.png',
|
||||
avatar_static: user.avatarUrl ?? 'https://dev.joinsharkey.org/static-assets/avatar.png',
|
||||
header: user.bannerUrl ?? 'https://dev.joinsharkey.org/static-assets/transparent.png',
|
||||
header_static: 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)) ?? []),
|
||||
fields: profile?.fields.map(p => this.encodeField(p)) ?? [],
|
||||
bot: user.isBot,
|
||||
discoverable: user.isExplorable,
|
||||
noindex: user.noindex,
|
||||
|
@ -198,41 +203,56 @@ export class MastoConverters {
|
|||
});
|
||||
}
|
||||
|
||||
public async getEdits(id: string, me?: MiLocalUser | null) {
|
||||
public async getEdits(id: string, me: MiLocalUser | null): Promise<StatusEdit[]> {
|
||||
const note = await this.mastodonDataService.getNote(id, me);
|
||||
if (!note) {
|
||||
return [];
|
||||
}
|
||||
const noteUser = await this.getUser(note.userId).then(async (p) => await this.convertAccount(p));
|
||||
|
||||
const noteUser = await this.getUser(note.userId);
|
||||
const account = await this.convertAccount(noteUser);
|
||||
const edits = await this.noteEditRepository.find({ where: { noteId: note.id }, order: { id: 'ASC' } });
|
||||
const history: Promise<StatusEdit>[] = [];
|
||||
const history: StatusEdit[] = [];
|
||||
|
||||
const mentionedRemoteUsers = JSON.parse(note.mentionedRemoteUsers);
|
||||
const renote = isRenote(note) ? await this.mastodonDataService.requireNote(note.renoteId, me) : null;
|
||||
|
||||
// TODO this looks wrong, according to mastodon docs
|
||||
let lastDate = this.idService.parse(note.id).date;
|
||||
|
||||
for (const edit of edits) {
|
||||
const files = this.driveFileEntityService.packManyByIds(edit.fileIds);
|
||||
// TODO avoid re-packing files for each edit
|
||||
const files = await this.driveFileEntityService.packManyByIds(edit.fileIds);
|
||||
|
||||
const cw = appendContentWarning(edit.cw, noteUser.mandatoryCW) ?? '';
|
||||
|
||||
const isQuote = renote && (edit.cw || edit.newText || edit.fileIds.length > 0 || note.replyId);
|
||||
const quoteUri = isQuote
|
||||
? renote.url ?? renote.uri ?? `${this.config.url}/notes/${renote.id}`
|
||||
: null;
|
||||
|
||||
const item = {
|
||||
account: noteUser,
|
||||
content: this.mfmService.toMastoApiHtml(mfm.parse(edit.newText ?? ''), JSON.parse(note.mentionedRemoteUsers)).then(p => p ?? ''),
|
||||
account: account,
|
||||
content: this.mfmService.toMastoApiHtml(mfm.parse(edit.newText ?? ''), mentionedRemoteUsers, false, quoteUri) ?? '',
|
||||
created_at: lastDate.toISOString(),
|
||||
emojis: [],
|
||||
sensitive: edit.cw != null && edit.cw.length > 0,
|
||||
spoiler_text: edit.cw ?? '',
|
||||
media_attachments: files.then(files => files.length > 0 ? files.map((f) => this.encodeFile(f)) : []),
|
||||
emojis: [], //FIXME
|
||||
sensitive: !!cw,
|
||||
spoiler_text: cw,
|
||||
media_attachments: files.length > 0 ? files.map((f) => this.encodeFile(f)) : [],
|
||||
};
|
||||
lastDate = edit.updatedAt;
|
||||
history.push(awaitAll(item));
|
||||
history.push(item);
|
||||
}
|
||||
|
||||
return await Promise.all(history);
|
||||
return history;
|
||||
}
|
||||
|
||||
private async convertReblog(status: Entity.Status | null, me?: MiLocalUser | null): Promise<MastodonEntity.Status | null> {
|
||||
private async convertReblog(status: Entity.Status | null, me: MiLocalUser | null): Promise<MastodonEntity.Status | null> {
|
||||
if (!status) return null;
|
||||
return await this.convertStatus(status, me);
|
||||
}
|
||||
|
||||
public async convertStatus(status: Entity.Status, me?: MiLocalUser | null): Promise<MastodonEntity.Status> {
|
||||
public async convertStatus(status: Entity.Status, me: MiLocalUser | null): Promise<MastodonEntity.Status> {
|
||||
const convertedAccount = this.convertAccount(status.account);
|
||||
const note = await this.mastodonDataService.requireNote(status.id, me);
|
||||
const noteUser = await this.getUser(status.account.id);
|
||||
|
@ -265,7 +285,6 @@ export class MastoConverters {
|
|||
});
|
||||
|
||||
// This must mirror the usual isQuote / isPureRenote logic used elsewhere.
|
||||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
||||
const isQuote = note.renoteId && (note.text || note.cw || note.fileIds.length > 0 || note.hasPoll || note.replyId);
|
||||
|
||||
const renote: Promise<MiNote> | null = note.renoteId ? this.mastodonDataService.requireNote(note.renoteId, me) : null;
|
||||
|
@ -277,11 +296,11 @@ export class MastoConverters {
|
|||
|
||||
const text = note.text;
|
||||
const content = text !== null
|
||||
? quoteUri
|
||||
.then(quoteUri => this.mfmService.toMastoApiHtml(mfm.parse(text), mentionedRemoteUsers, false, quoteUri))
|
||||
.then(p => p ?? escapeMFM(text))
|
||||
? quoteUri.then(quote => this.mfmService.toMastoApiHtml(mfm.parse(text), mentionedRemoteUsers, false, quote) ?? escapeMFM(text))
|
||||
: '';
|
||||
|
||||
const cw = appendContentWarning(note.cw, noteUser.mandatoryCW) ?? '';
|
||||
|
||||
const reblogged = await this.mastodonDataService.hasReblog(note.id, me);
|
||||
|
||||
// noinspection ES6MissingAwait
|
||||
|
@ -292,11 +311,12 @@ export class MastoConverters {
|
|||
account: convertedAccount,
|
||||
in_reply_to_id: note.replyId,
|
||||
in_reply_to_account_id: note.replyUserId,
|
||||
reblog: !isQuote ? await this.convertReblog(status.reblog, me) : null,
|
||||
reblog: !isQuote ? this.convertReblog(status.reblog, me) : null,
|
||||
content: content,
|
||||
content_type: 'text/x.misskeymarkdown',
|
||||
text: note.text,
|
||||
created_at: status.created_at,
|
||||
edited_at: note.updatedAt?.toISOString() ?? null,
|
||||
emojis: emoji,
|
||||
replies_count: note.repliesCount,
|
||||
reblogs_count: note.renoteCount,
|
||||
|
@ -304,8 +324,8 @@ export class MastoConverters {
|
|||
reblogged,
|
||||
favourited: status.favourited,
|
||||
muted: status.muted,
|
||||
sensitive: status.sensitive,
|
||||
spoiler_text: note.cw ?? '',
|
||||
sensitive: status.sensitive || !!cw,
|
||||
spoiler_text: cw,
|
||||
visibility: status.visibility,
|
||||
media_attachments: status.media_attachments.map(a => convertAttachment(a)),
|
||||
mentions: mentions,
|
||||
|
@ -315,15 +335,14 @@ export class MastoConverters {
|
|||
application: null, //FIXME
|
||||
language: null, //FIXME
|
||||
pinned: false, //FIXME
|
||||
reactions: status.emoji_reactions,
|
||||
emoji_reactions: status.emoji_reactions,
|
||||
bookmarked: false, //FIXME
|
||||
quote: isQuote ? await this.convertReblog(status.reblog, me) : null,
|
||||
edited_at: note.updatedAt?.toISOString() ?? null,
|
||||
quote_id: isQuote ? status.reblog?.id : undefined,
|
||||
quote: isQuote ? this.convertReblog(status.reblog, me) : null,
|
||||
reactions: status.emoji_reactions,
|
||||
});
|
||||
}
|
||||
|
||||
public async convertConversation(conversation: Entity.Conversation, me?: MiLocalUser | null): Promise<MastodonEntity.Conversation> {
|
||||
public async convertConversation(conversation: Entity.Conversation, me: MiLocalUser | null): Promise<MastodonEntity.Conversation> {
|
||||
return {
|
||||
id: conversation.id,
|
||||
accounts: await Promise.all(conversation.accounts.map(a => this.convertAccount(a))),
|
||||
|
@ -332,13 +351,22 @@ export class MastoConverters {
|
|||
};
|
||||
}
|
||||
|
||||
public async convertNotification(notification: Entity.Notification, me?: MiLocalUser | null): Promise<MastodonEntity.Notification> {
|
||||
public async convertNotification(notification: Entity.Notification, me: MiLocalUser | null): Promise<MastodonEntity.Notification | null> {
|
||||
const status = notification.status
|
||||
? await this.convertStatus(notification.status, me).catch(() => null)
|
||||
: null;
|
||||
|
||||
// We sometimes get notifications for inaccessible notes, these should be ignored.
|
||||
if (!status) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
account: await this.convertAccount(notification.account),
|
||||
created_at: notification.created_at,
|
||||
id: notification.id,
|
||||
status: notification.status ? await this.convertStatus(notification.status, me) : undefined,
|
||||
type: notification.type,
|
||||
status,
|
||||
type: convertNotificationType(notification.type as NotificationType),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -348,12 +376,26 @@ function simpleConvert<T>(data: T): T {
|
|||
return Object.assign({}, data);
|
||||
}
|
||||
|
||||
export function convertAccount(account: Entity.Account) {
|
||||
return simpleConvert(account);
|
||||
function convertNotificationType(type: NotificationType): MastodonNotificationType {
|
||||
switch (type) {
|
||||
case 'emoji_reaction': return 'reaction';
|
||||
case 'poll_vote':
|
||||
case 'poll_expired':
|
||||
return 'poll';
|
||||
// Not supported by mastodon
|
||||
case 'move':
|
||||
return type as MastodonNotificationType;
|
||||
default: return type;
|
||||
}
|
||||
}
|
||||
export function convertAnnouncement(announcement: Entity.Announcement) {
|
||||
return simpleConvert(announcement);
|
||||
|
||||
export function convertAnnouncement(announcement: Entity.Announcement): MastodonEntity.Announcement {
|
||||
return {
|
||||
...announcement,
|
||||
updated_at: announcement.updated_at ?? announcement.published_at,
|
||||
};
|
||||
}
|
||||
|
||||
export function convertAttachment(attachment: Entity.Attachment): MastodonEntity.Attachment {
|
||||
const { width, height } = attachment.meta?.original ?? attachment.meta ?? {};
|
||||
const size = (width && height) ? `${width}x${height}` : undefined;
|
||||
|
@ -379,28 +421,24 @@ export function convertAttachment(attachment: Entity.Attachment): MastodonEntity
|
|||
} : null,
|
||||
};
|
||||
}
|
||||
export function convertFilter(filter: Entity.Filter) {
|
||||
export function convertFilter(filter: Entity.Filter): MastodonEntity.Filter {
|
||||
return simpleConvert(filter);
|
||||
}
|
||||
export function convertList(list: Entity.List) {
|
||||
return simpleConvert(list);
|
||||
export function convertList(list: Entity.List): MastodonEntity.List {
|
||||
return {
|
||||
id: list.id,
|
||||
title: list.title,
|
||||
replies_policy: list.replies_policy ?? 'followed',
|
||||
};
|
||||
}
|
||||
export function convertFeaturedTag(tag: Entity.FeaturedTag) {
|
||||
export function convertFeaturedTag(tag: Entity.FeaturedTag): MastodonEntity.FeaturedTag {
|
||||
return simpleConvert(tag);
|
||||
}
|
||||
|
||||
export function convertPoll(poll: Entity.Poll) {
|
||||
export function convertPoll(poll: Entity.Poll): MastodonEntity.Poll {
|
||||
return simpleConvert(poll);
|
||||
}
|
||||
|
||||
// noinspection JSUnusedGlobalSymbols
|
||||
export function convertReaction(reaction: Entity.Reaction) {
|
||||
if (reaction.accounts) {
|
||||
reaction.accounts = reaction.accounts.map(convertAccount);
|
||||
}
|
||||
return reaction;
|
||||
}
|
||||
|
||||
// Megalodon sometimes returns broken / stubbed relationship data
|
||||
export function convertRelationship(relationship: Partial<Entity.Relationship> & { id: string }): MastodonEntity.Relationship {
|
||||
return {
|
||||
|
@ -422,7 +460,3 @@ export function convertRelationship(relationship: Partial<Entity.Relationship> &
|
|||
};
|
||||
}
|
||||
|
||||
// noinspection JSUnusedGlobalSymbols
|
||||
export function convertStatusSource(status: Entity.StatusSource) {
|
||||
return simpleConvert(status);
|
||||
}
|
|
@ -3,37 +3,138 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import Logger, { Data } from '@/logger.js';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { FastifyRequest } from 'fastify';
|
||||
import Logger from '@/logger.js';
|
||||
import { LoggerService } from '@/core/LoggerService.js';
|
||||
import { ApiError } from '@/server/api/error.js';
|
||||
import { EnvService } from '@/core/EnvService.js';
|
||||
import { getBaseUrl } from '@/server/api/mastodon/MastodonClientService.js';
|
||||
|
||||
@Injectable()
|
||||
export class MastodonLogger {
|
||||
public readonly logger: Logger;
|
||||
|
||||
constructor(loggerService: LoggerService) {
|
||||
constructor(
|
||||
@Inject(EnvService)
|
||||
private readonly envService: EnvService,
|
||||
|
||||
loggerService: LoggerService,
|
||||
) {
|
||||
this.logger = loggerService.getLogger('masto-api');
|
||||
}
|
||||
|
||||
public error(endpoint: string, error: Data): void {
|
||||
this.logger.error(`Error in mastodon API endpoint ${endpoint}:`, error);
|
||||
public error(request: FastifyRequest, error: MastodonError, status: number): void {
|
||||
if ((status < 400 && status > 499) || this.envService.env.NODE_ENV === 'development') {
|
||||
const path = new URL(request.url, getBaseUrl(request)).pathname;
|
||||
this.logger.error(`Error in mastodon endpoint ${request.method} ${path}:`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function getErrorData(error: unknown): Data {
|
||||
if (error == null) return {};
|
||||
if (typeof(error) === 'string') return error;
|
||||
if (typeof(error) === 'object') {
|
||||
if ('response' in error) {
|
||||
if (typeof(error.response) === 'object' && error.response) {
|
||||
if ('data' in error.response) {
|
||||
if (typeof(error.response.data) === 'object' && error.response.data) {
|
||||
return error.response.data as Record<string, unknown>;
|
||||
}
|
||||
// TODO move elsewhere
|
||||
export interface MastodonError {
|
||||
error: string;
|
||||
error_description?: string;
|
||||
}
|
||||
|
||||
export function getErrorData(error: unknown): MastodonError {
|
||||
// Axios wraps errors from the backend
|
||||
error = unpackAxiosError(error);
|
||||
|
||||
if (!error || typeof(error) !== 'object') {
|
||||
return {
|
||||
error: 'UNKNOWN_ERROR',
|
||||
error_description: String(error),
|
||||
};
|
||||
}
|
||||
|
||||
if (error instanceof ApiError) {
|
||||
return convertApiError(error);
|
||||
}
|
||||
|
||||
if ('code' in error && typeof (error.code) === 'string') {
|
||||
if ('message' in error && typeof (error.message) === 'string') {
|
||||
return convertApiError(error as ApiError);
|
||||
}
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
return convertGenericError(error);
|
||||
}
|
||||
|
||||
return convertUnknownError(error);
|
||||
}
|
||||
|
||||
function unpackAxiosError(error: unknown): unknown {
|
||||
if (error && typeof(error) === 'object') {
|
||||
if ('response' in error && error.response && typeof (error.response) === 'object') {
|
||||
if ('data' in error.response && error.response.data && typeof (error.response.data) === 'object') {
|
||||
if ('error' in error.response.data && error.response.data.error && typeof(error.response.data.error) === 'object') {
|
||||
return error.response.data.error;
|
||||
}
|
||||
|
||||
return error.response.data;
|
||||
}
|
||||
|
||||
// No data - this is a fallback to avoid leaking request/response details in the error
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
function convertApiError(apiError: ApiError): MastodonError {
|
||||
const mastoError: MastodonError & Partial<ApiError> = {
|
||||
error: apiError.code,
|
||||
error_description: apiError.message,
|
||||
...apiError,
|
||||
};
|
||||
|
||||
delete mastoError.code;
|
||||
delete mastoError.message;
|
||||
delete mastoError.httpStatusCode;
|
||||
|
||||
return mastoError;
|
||||
}
|
||||
|
||||
function convertUnknownError(data: object = {}): MastodonError {
|
||||
return Object.assign({}, data, {
|
||||
error: 'INTERNAL_ERROR',
|
||||
error_description: 'Internal error occurred. Please contact us if the error persists.',
|
||||
id: '5d37dbcb-891e-41ca-a3d6-e690c97775ac',
|
||||
kind: 'server',
|
||||
});
|
||||
}
|
||||
|
||||
function convertGenericError(error: Error): MastodonError {
|
||||
const mastoError: MastodonError & Partial<Error> = {
|
||||
error: 'INTERNAL_ERROR',
|
||||
error_description: String(error),
|
||||
...error,
|
||||
};
|
||||
|
||||
delete mastoError.name;
|
||||
delete mastoError.message;
|
||||
delete mastoError.stack;
|
||||
|
||||
return mastoError;
|
||||
}
|
||||
|
||||
export function getErrorStatus(error: unknown): number {
|
||||
if (error && typeof(error) === 'object') {
|
||||
// Axios wraps errors from the backend
|
||||
if ('response' in error && typeof (error.response) === 'object' && error.response) {
|
||||
if ('status' in error.response && typeof(error.response.status) === 'number') {
|
||||
return error.response.status;
|
||||
}
|
||||
}
|
||||
return error as Record<string, unknown>;
|
||||
|
||||
if ('httpStatusCode' in error && typeof(error.httpStatusCode) === 'number') {
|
||||
return error.httpStatusCode;
|
||||
}
|
||||
}
|
||||
return { error };
|
||||
|
||||
return 500;
|
||||
}
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: marie and other Sharkey contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
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,
|
||||
};
|
|
@ -3,14 +3,18 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { parseTimelineArgs, TimelineArgs } from '@/server/api/mastodon/timelineArgs.js';
|
||||
import { MiLocalUser } from '@/models/User.js';
|
||||
import { MastoConverters, convertRelationship } from '../converters.js';
|
||||
import type { MegalodonInterface } from 'megalodon';
|
||||
import type { FastifyRequest } from 'fastify';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { parseTimelineArgs, TimelineArgs, toBoolean } from '@/server/api/mastodon/argsUtils.js';
|
||||
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
|
||||
import { DriveService } from '@/core/DriveService.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import type { AccessTokensRepository, UserProfilesRepository } from '@/models/_.js';
|
||||
import { attachMinMaxPagination } from '@/server/api/mastodon/pagination.js';
|
||||
import { MastodonConverters, convertRelationship, convertFeaturedTag, convertList } from '../MastodonConverters.js';
|
||||
import type multer from 'fastify-multer';
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
|
||||
export interface ApiAccountMastodonRoute {
|
||||
interface ApiAccountMastodonRoute {
|
||||
Params: { id?: string },
|
||||
Querystring: TimelineArgs & { acct?: string },
|
||||
Body: { notifications?: boolean }
|
||||
|
@ -19,133 +23,280 @@ export interface ApiAccountMastodonRoute {
|
|||
@Injectable()
|
||||
export class ApiAccountMastodon {
|
||||
constructor(
|
||||
private readonly request: FastifyRequest<ApiAccountMastodonRoute>,
|
||||
private readonly client: MegalodonInterface,
|
||||
private readonly me: MiLocalUser | null,
|
||||
private readonly mastoConverters: MastoConverters,
|
||||
@Inject(DI.userProfilesRepository)
|
||||
private readonly userProfilesRepository: UserProfilesRepository,
|
||||
|
||||
@Inject(DI.accessTokensRepository)
|
||||
private readonly accessTokensRepository: AccessTokensRepository,
|
||||
|
||||
private readonly clientService: MastodonClientService,
|
||||
private readonly mastoConverters: MastodonConverters,
|
||||
private readonly driveService: DriveService,
|
||||
) {}
|
||||
|
||||
public async verifyCredentials() {
|
||||
const data = await this.client.verifyAccountCredentials();
|
||||
const acct = await this.mastoConverters.convertAccount(data.data);
|
||||
return Object.assign({}, acct, {
|
||||
source: {
|
||||
note: acct.note,
|
||||
fields: acct.fields,
|
||||
privacy: '',
|
||||
sensitive: false,
|
||||
language: '',
|
||||
public register(fastify: FastifyInstance, upload: ReturnType<typeof multer>): void {
|
||||
fastify.get<ApiAccountMastodonRoute>('/v1/accounts/verify_credentials', async (_request, reply) => {
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.verifyAccountCredentials();
|
||||
const acct = await this.mastoConverters.convertAccount(data.data);
|
||||
const response = Object.assign({}, acct, {
|
||||
source: {
|
||||
note: acct.note,
|
||||
fields: acct.fields,
|
||||
privacy: 'public',
|
||||
sensitive: false,
|
||||
language: '',
|
||||
},
|
||||
});
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.patch<{
|
||||
Body: {
|
||||
discoverable?: string,
|
||||
bot?: string,
|
||||
display_name?: string,
|
||||
note?: string,
|
||||
avatar?: string,
|
||||
header?: string,
|
||||
locked?: string,
|
||||
source?: {
|
||||
privacy?: string,
|
||||
sensitive?: string,
|
||||
language?: string,
|
||||
},
|
||||
fields_attributes?: {
|
||||
name: string,
|
||||
value: string,
|
||||
}[],
|
||||
},
|
||||
}>('/v1/accounts/update_credentials', { preHandler: upload.any() }, async (_request, reply) => {
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = this.clientService.getClient(_request);
|
||||
// Check if there is a 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 options = {
|
||||
..._request.body,
|
||||
discoverable: toBoolean(_request.body.discoverable),
|
||||
bot: toBoolean(_request.body.bot),
|
||||
locked: toBoolean(_request.body.locked),
|
||||
source: _request.body.source ? {
|
||||
..._request.body.source,
|
||||
sensitive: toBoolean(_request.body.source.sensitive),
|
||||
} : undefined,
|
||||
};
|
||||
const data = await client.updateCredentials(options);
|
||||
const response = await this.mastoConverters.convertAccount(data.data);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.get<{ Querystring: { acct?: string }}>('/v1/accounts/lookup', async (_request, reply) => {
|
||||
if (!_request.query.acct) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required property "acct"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.search(_request.query.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 })) ?? [];
|
||||
const response = await this.mastoConverters.convertAccount(data.data.accounts[0]);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.get<ApiAccountMastodonRoute & { Querystring: { id?: string | string[] }}>('/v1/accounts/relationships', async (_request, reply) => {
|
||||
if (!_request.query.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required property "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.getRelationships(_request.query.id);
|
||||
const response = data.data.map(relationship => convertRelationship(relationship));
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.get<{ Params: { id?: string } }>('/v1/accounts/:id', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.getAccount(_request.params.id);
|
||||
const account = await this.mastoConverters.convertAccount(data.data);
|
||||
|
||||
reply.send(account);
|
||||
});
|
||||
|
||||
fastify.get<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/statuses', async (request, reply) => {
|
||||
if (!request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const { client, me } = await this.clientService.getAuthClient(request);
|
||||
const args = parseTimelineArgs(request.query);
|
||||
const data = await client.getAccountStatuses(request.params.id, args);
|
||||
const response = await Promise.all(data.data.map(async (status) => await this.mastoConverters.convertStatus(status, me)));
|
||||
|
||||
attachMinMaxPagination(request, reply, response);
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.get<{ Params: { id?: string } }>('/v1/accounts/:id/featured_tags', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.getFeaturedTags();
|
||||
const response = data.data.map((tag) => convertFeaturedTag(tag));
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.get<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/followers', async (request, reply) => {
|
||||
if (!request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(request);
|
||||
const data = await client.getAccountFollowers(
|
||||
request.params.id,
|
||||
parseTimelineArgs(request.query),
|
||||
);
|
||||
const response = await Promise.all(data.data.map(async (account) => await this.mastoConverters.convertAccount(account)));
|
||||
|
||||
attachMinMaxPagination(request, reply, response);
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.get<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/following', async (request, reply) => {
|
||||
if (!request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(request);
|
||||
const data = await client.getAccountFollowing(
|
||||
request.params.id,
|
||||
parseTimelineArgs(request.query),
|
||||
);
|
||||
const response = await Promise.all(data.data.map(async (account) => await this.mastoConverters.convertAccount(account)));
|
||||
|
||||
attachMinMaxPagination(request, reply, response);
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.get<{ Params: { id?: string } }>('/v1/accounts/:id/lists', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.getAccountLists(_request.params.id);
|
||||
const response = data.data.map((list) => convertList(list));
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.post<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/follow', { preHandler: upload.single('none') }, async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.followAccount(_request.params.id);
|
||||
const acct = convertRelationship(data.data);
|
||||
acct.following = true; // TODO this is wrong, follow may not have processed immediately
|
||||
|
||||
reply.send(acct);
|
||||
});
|
||||
|
||||
fastify.post<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/unfollow', { preHandler: upload.single('none') }, async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.unfollowAccount(_request.params.id);
|
||||
const acct = convertRelationship(data.data);
|
||||
acct.following = false;
|
||||
|
||||
reply.send(acct);
|
||||
});
|
||||
|
||||
fastify.post<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/block', { preHandler: upload.single('none') }, async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.blockAccount(_request.params.id);
|
||||
const response = convertRelationship(data.data);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.post<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/unblock', { preHandler: upload.single('none') }, async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.unblockAccount(_request.params.id);
|
||||
const response = convertRelationship(data.data);
|
||||
|
||||
return reply.send(response);
|
||||
});
|
||||
|
||||
fastify.post<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/mute', { preHandler: upload.single('none') }, async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.muteAccount(
|
||||
_request.params.id,
|
||||
_request.body.notifications ?? true,
|
||||
);
|
||||
const response = convertRelationship(data.data);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.post<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/unmute', { preHandler: upload.single('none') }, async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.unmuteAccount(_request.params.id);
|
||||
const response = convertRelationship(data.data);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public async lookup() {
|
||||
if (!this.request.query.acct) throw new Error('Missing required property "acct"');
|
||||
const data = await this.client.search(this.request.query.acct, { type: 'accounts' });
|
||||
return this.mastoConverters.convertAccount(data.data.accounts[0]);
|
||||
}
|
||||
|
||||
public async getRelationships(reqIds: string[]) {
|
||||
const data = await this.client.getRelationships(reqIds);
|
||||
return data.data.map(relationship => convertRelationship(relationship));
|
||||
}
|
||||
|
||||
public async getStatuses() {
|
||||
if (!this.request.params.id) throw new Error('Missing required parameter "id"');
|
||||
const data = await this.client.getAccountStatuses(this.request.params.id, parseTimelineArgs(this.request.query));
|
||||
return await Promise.all(data.data.map(async (status) => await this.mastoConverters.convertStatus(status, this.me)));
|
||||
}
|
||||
|
||||
public async getFollowers() {
|
||||
if (!this.request.params.id) throw new Error('Missing required parameter "id"');
|
||||
const data = await this.client.getAccountFollowers(
|
||||
this.request.params.id,
|
||||
parseTimelineArgs(this.request.query),
|
||||
);
|
||||
return await Promise.all(data.data.map(async (account) => await this.mastoConverters.convertAccount(account)));
|
||||
}
|
||||
|
||||
public async getFollowing() {
|
||||
if (!this.request.params.id) throw new Error('Missing required parameter "id"');
|
||||
const data = await this.client.getAccountFollowing(
|
||||
this.request.params.id,
|
||||
parseTimelineArgs(this.request.query),
|
||||
);
|
||||
return await Promise.all(data.data.map(async (account) => await this.mastoConverters.convertAccount(account)));
|
||||
}
|
||||
|
||||
public async addFollow() {
|
||||
if (!this.request.params.id) throw new Error('Missing required parameter "id"');
|
||||
const data = await this.client.followAccount(this.request.params.id);
|
||||
const acct = convertRelationship(data.data);
|
||||
acct.following = true;
|
||||
return acct;
|
||||
}
|
||||
|
||||
public async rmFollow() {
|
||||
if (!this.request.params.id) throw new Error('Missing required parameter "id"');
|
||||
const data = await this.client.unfollowAccount(this.request.params.id);
|
||||
const acct = convertRelationship(data.data);
|
||||
acct.following = false;
|
||||
return acct;
|
||||
}
|
||||
|
||||
public async addBlock() {
|
||||
if (!this.request.params.id) throw new Error('Missing required parameter "id"');
|
||||
const data = await this.client.blockAccount(this.request.params.id);
|
||||
return convertRelationship(data.data);
|
||||
}
|
||||
|
||||
public async rmBlock() {
|
||||
if (!this.request.params.id) throw new Error('Missing required parameter "id"');
|
||||
const data = await this.client.unblockAccount(this.request.params.id);
|
||||
return convertRelationship(data.data);
|
||||
}
|
||||
|
||||
public async addMute() {
|
||||
if (!this.request.params.id) throw new Error('Missing required parameter "id"');
|
||||
const data = await this.client.muteAccount(
|
||||
this.request.params.id,
|
||||
this.request.body.notifications ?? true,
|
||||
);
|
||||
return convertRelationship(data.data);
|
||||
}
|
||||
|
||||
public async rmMute() {
|
||||
if (!this.request.params.id) throw new Error('Missing required parameter "id"');
|
||||
const data = await this.client.unmuteAccount(this.request.params.id);
|
||||
return convertRelationship(data.data);
|
||||
}
|
||||
|
||||
public async getBookmarks() {
|
||||
const data = await this.client.getBookmarks(parseTimelineArgs(this.request.query));
|
||||
return Promise.all(data.data.map((status) => this.mastoConverters.convertStatus(status, this.me)));
|
||||
}
|
||||
|
||||
public async getFavourites() {
|
||||
const data = await this.client.getFavourites(parseTimelineArgs(this.request.query));
|
||||
return Promise.all(data.data.map((status) => this.mastoConverters.convertStatus(status, this.me)));
|
||||
}
|
||||
|
||||
public async getMutes() {
|
||||
const data = await this.client.getMutes(parseTimelineArgs(this.request.query));
|
||||
return Promise.all(data.data.map((account) => this.mastoConverters.convertAccount(account)));
|
||||
}
|
||||
|
||||
public async getBlocks() {
|
||||
const data = await this.client.getBlocks(parseTimelineArgs(this.request.query));
|
||||
return Promise.all(data.data.map((account) => this.mastoConverters.convertAccount(account)));
|
||||
}
|
||||
|
||||
public async acceptFollow() {
|
||||
if (!this.request.params.id) throw new Error('Missing required parameter "id"');
|
||||
const data = await this.client.acceptFollowRequest(this.request.params.id);
|
||||
return convertRelationship(data.data);
|
||||
}
|
||||
|
||||
public async rejectFollow() {
|
||||
if (!this.request.params.id) throw new Error('Missing required parameter "id"');
|
||||
const data = await this.client.rejectFollowRequest(this.request.params.id);
|
||||
return convertRelationship(data.data);
|
||||
}
|
||||
}
|
||||
|
|
113
packages/backend/src/server/api/mastodon/endpoints/apps.ts
Normal file
113
packages/backend/src/server/api/mastodon/endpoints/apps.ts
Normal file
|
@ -0,0 +1,113 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: marie and other Sharkey contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import type multer from 'fastify-multer';
|
||||
|
||||
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 interface AuthPayload {
|
||||
scopes?: string | string[],
|
||||
redirect_uris?: string,
|
||||
client_name?: string,
|
||||
website?: string,
|
||||
}
|
||||
|
||||
// Not entirely right, but it gets TypeScript to work so *shrug*
|
||||
type AuthMastodonRoute = { Body?: AuthPayload, Querystring: AuthPayload };
|
||||
|
||||
@Injectable()
|
||||
export class ApiAppsMastodon {
|
||||
constructor(
|
||||
private readonly clientService: MastodonClientService,
|
||||
) {}
|
||||
|
||||
public register(fastify: FastifyInstance, upload: ReturnType<typeof multer>): void {
|
||||
fastify.post<AuthMastodonRoute>('/v1/apps', { preHandler: upload.single('none') }, async (_request, reply) => {
|
||||
const body = _request.body ?? _request.query;
|
||||
if (!body.scopes) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required payload "scopes"' });
|
||||
if (!body.redirect_uris) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required payload "redirect_uris"' });
|
||||
if (!body.client_name) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required payload "client_name"' });
|
||||
|
||||
let scope = body.scopes;
|
||||
if (typeof scope === 'string') {
|
||||
scope = scope.split(/[ +]/g);
|
||||
}
|
||||
|
||||
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 red = body.redirect_uris;
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const appData = await client.registerApp(body.client_name, {
|
||||
scopes: Array.from(pushScope),
|
||||
redirect_uris: red,
|
||||
website: body.website,
|
||||
});
|
||||
|
||||
const response = {
|
||||
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,
|
||||
};
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -1,97 +0,0 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: marie and other Sharkey contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
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 interface AuthPayload {
|
||||
scopes?: string | string[],
|
||||
redirect_uris?: string,
|
||||
client_name?: string,
|
||||
website?: string,
|
||||
}
|
||||
|
||||
// Not entirely right, but it gets TypeScript to work so *shrug*
|
||||
export type AuthMastodonRoute = { Body?: AuthPayload, Querystring: AuthPayload };
|
||||
|
||||
export async function ApiAuthMastodon(request: FastifyRequest<AuthMastodonRoute>, client: MegalodonInterface) {
|
||||
const body = request.body ?? request.query;
|
||||
if (!body.scopes) throw new Error('Missing required payload "scopes"');
|
||||
if (!body.redirect_uris) throw new Error('Missing required payload "redirect_uris"');
|
||||
if (!body.client_name) throw new Error('Missing required payload "client_name"');
|
||||
|
||||
let scope = body.scopes;
|
||||
if (typeof scope === 'string') {
|
||||
scope = scope.split(/[ +]/g);
|
||||
}
|
||||
|
||||
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 red = body.redirect_uris;
|
||||
const appData = await client.registerApp(body.client_name, {
|
||||
scopes: Array.from(pushScope),
|
||||
redirect_uris: red,
|
||||
website: body.website,
|
||||
});
|
||||
|
||||
return {
|
||||
id: Math.floor(Math.random() * 100).toString(),
|
||||
name: appData.name,
|
||||
website: body.website,
|
||||
redirect_uri: red,
|
||||
client_id: Buffer.from(appData.url || '').toString('base64'), // eslint-disable-line @typescript-eslint/prefer-nullish-coalescing
|
||||
client_secret: appData.clientSecret,
|
||||
};
|
||||
}
|
|
@ -3,12 +3,14 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { toBoolean } from '@/server/api/mastodon/timelineArgs.js';
|
||||
import { convertFilter } from '../converters.js';
|
||||
import type { MegalodonInterface } from 'megalodon';
|
||||
import type { FastifyRequest } from 'fastify';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { toBoolean } from '@/server/api/mastodon/argsUtils.js';
|
||||
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
|
||||
import { convertFilter } from '../MastodonConverters.js';
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import type multer from 'fastify-multer';
|
||||
|
||||
export interface ApiFilterMastodonRoute {
|
||||
interface ApiFilterMastodonRoute {
|
||||
Params: {
|
||||
id?: string,
|
||||
},
|
||||
|
@ -21,55 +23,78 @@ export interface ApiFilterMastodonRoute {
|
|||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class ApiFilterMastodon {
|
||||
constructor(
|
||||
private readonly request: FastifyRequest<ApiFilterMastodonRoute>,
|
||||
private readonly client: MegalodonInterface,
|
||||
private readonly clientService: MastodonClientService,
|
||||
) {}
|
||||
|
||||
public async getFilters() {
|
||||
const data = await this.client.getFilters();
|
||||
return data.data.map((filter) => convertFilter(filter));
|
||||
}
|
||||
public register(fastify: FastifyInstance, upload: ReturnType<typeof multer>): void {
|
||||
fastify.get('/v1/filters', async (_request, reply) => {
|
||||
const client = this.clientService.getClient(_request);
|
||||
|
||||
public async getFilter() {
|
||||
if (!this.request.params.id) throw new Error('Missing required parameter "id"');
|
||||
const data = await this.client.getFilter(this.request.params.id);
|
||||
return convertFilter(data.data);
|
||||
}
|
||||
const data = await client.getFilters();
|
||||
const response = data.data.map((filter) => convertFilter(filter));
|
||||
|
||||
public async createFilter() {
|
||||
if (!this.request.body.phrase) throw new Error('Missing required payload "phrase"');
|
||||
if (!this.request.body.context) throw new Error('Missing required payload "context"');
|
||||
const options = {
|
||||
phrase: this.request.body.phrase,
|
||||
context: this.request.body.context,
|
||||
irreversible: toBoolean(this.request.body.irreversible),
|
||||
whole_word: toBoolean(this.request.body.whole_word),
|
||||
expires_in: this.request.body.expires_in,
|
||||
};
|
||||
const data = await this.client.createFilter(this.request.body.phrase, this.request.body.context, options);
|
||||
return convertFilter(data.data);
|
||||
}
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
public async updateFilter() {
|
||||
if (!this.request.params.id) throw new Error('Missing required parameter "id"');
|
||||
if (!this.request.body.phrase) throw new Error('Missing required payload "phrase"');
|
||||
if (!this.request.body.context) throw new Error('Missing required payload "context"');
|
||||
const options = {
|
||||
phrase: this.request.body.phrase,
|
||||
context: this.request.body.context,
|
||||
irreversible: toBoolean(this.request.body.irreversible),
|
||||
whole_word: toBoolean(this.request.body.whole_word),
|
||||
expires_in: this.request.body.expires_in,
|
||||
};
|
||||
const data = await this.client.updateFilter(this.request.params.id, this.request.body.phrase, this.request.body.context, options);
|
||||
return convertFilter(data.data);
|
||||
}
|
||||
fastify.get<ApiFilterMastodonRoute & { Params: { id?: string } }>('/v1/filters/:id', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
public async rmFilter() {
|
||||
if (!this.request.params.id) throw new Error('Missing required parameter "id"');
|
||||
const data = await this.client.deleteFilter(this.request.params.id);
|
||||
return data.data;
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.getFilter(_request.params.id);
|
||||
const response = convertFilter(data.data);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.post<ApiFilterMastodonRoute>('/v1/filters', { preHandler: upload.single('none') }, async (_request, reply) => {
|
||||
if (!_request.body.phrase) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required payload "phrase"' });
|
||||
if (!_request.body.context) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required payload "context"' });
|
||||
|
||||
const options = {
|
||||
phrase: _request.body.phrase,
|
||||
context: _request.body.context,
|
||||
irreversible: toBoolean(_request.body.irreversible),
|
||||
whole_word: toBoolean(_request.body.whole_word),
|
||||
expires_in: _request.body.expires_in,
|
||||
};
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.createFilter(_request.body.phrase, _request.body.context, options);
|
||||
const response = convertFilter(data.data);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.post<ApiFilterMastodonRoute & { Params: { id?: string } }>('/v1/filters/:id', { preHandler: upload.single('none') }, async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
if (!_request.body.phrase) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required payload "phrase"' });
|
||||
if (!_request.body.context) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required payload "context"' });
|
||||
|
||||
const options = {
|
||||
phrase: _request.body.phrase,
|
||||
context: _request.body.context,
|
||||
irreversible: toBoolean(_request.body.irreversible),
|
||||
whole_word: toBoolean(_request.body.whole_word),
|
||||
expires_in: _request.body.expires_in,
|
||||
};
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.updateFilter(_request.params.id, _request.body.phrase, _request.body.context, options);
|
||||
const response = convertFilter(data.data);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.delete<ApiFilterMastodonRoute & { Params: { id?: string } }>('/v1/filters/:id', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.deleteFilter(_request.params.id);
|
||||
|
||||
reply.send(data.data);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
104
packages/backend/src/server/api/mastodon/endpoints/instance.ts
Normal file
104
packages/backend/src/server/api/mastodon/endpoints/instance.ts
Normal file
|
@ -0,0 +1,104 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: marie and other Sharkey contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { IsNull } from 'typeorm';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { FILE_TYPE_BROWSERSAFE } from '@/const.js';
|
||||
import type { Config } from '@/config.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import type { MiMeta, UsersRepository } from '@/models/_.js';
|
||||
import { MastodonConverters } from '@/server/api/mastodon/MastodonConverters.js';
|
||||
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
|
||||
import { RoleService } from '@/core/RoleService.js';
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import type { MastodonEntity } from 'megalodon';
|
||||
|
||||
@Injectable()
|
||||
export class ApiInstanceMastodon {
|
||||
constructor(
|
||||
@Inject(DI.meta)
|
||||
private readonly meta: MiMeta,
|
||||
|
||||
@Inject(DI.usersRepository)
|
||||
private readonly usersRepository: UsersRepository,
|
||||
|
||||
@Inject(DI.config)
|
||||
private readonly config: Config,
|
||||
|
||||
private readonly mastoConverters: MastodonConverters,
|
||||
private readonly clientService: MastodonClientService,
|
||||
private readonly roleService: RoleService,
|
||||
) {}
|
||||
|
||||
public register(fastify: FastifyInstance): void {
|
||||
fastify.get('/v1/instance', async (_request, reply) => {
|
||||
const { client, me } = await this.clientService.getAuthClient(_request);
|
||||
const data = await client.getInstance();
|
||||
const instance = data.data;
|
||||
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.mastoConverters.convertAccount((await client.getAccount(admin.id)).data);
|
||||
const roles = await this.roleService.getUserPolicies(me?.id ?? null);
|
||||
|
||||
const response: MastodonEntity.Instance = {
|
||||
uri: this.config.url,
|
||||
title: this.meta.name || 'Sharkey',
|
||||
description: this.meta.description || 'This is a vanilla Sharkey Instance. It doesn\'t seem to have a description.',
|
||||
email: instance.email || '',
|
||||
version: `3.0.0 (compatible; Sharkey ${this.config.version}; like Akkoma)`,
|
||||
urls: instance.urls,
|
||||
stats: {
|
||||
user_count: instance.stats.user_count,
|
||||
status_count: instance.stats.status_count,
|
||||
domain_count: instance.stats.domain_count,
|
||||
},
|
||||
thumbnail: this.meta.backgroundImageUrl || '/static-assets/transparent.png',
|
||||
languages: this.meta.langs,
|
||||
registrations: !this.meta.disableRegistration || instance.registrations,
|
||||
approval_required: this.meta.approvalRequiredForSignup,
|
||||
invites_enabled: instance.registrations,
|
||||
configuration: {
|
||||
accounts: {
|
||||
max_featured_tags: 20,
|
||||
max_pinned_statuses: roles.pinLimit,
|
||||
},
|
||||
statuses: {
|
||||
max_characters: this.config.maxNoteLength,
|
||||
max_media_attachments: 16,
|
||||
characters_reserved_per_url: instance.uri.length,
|
||||
},
|
||||
media_attachments: {
|
||||
supported_mime_types: FILE_TYPE_BROWSERSAFE,
|
||||
image_size_limit: 10485760,
|
||||
image_matrix_limit: 16777216,
|
||||
video_size_limit: 41943040,
|
||||
video_frame_limit: 60,
|
||||
video_matrix_limit: 2304000,
|
||||
},
|
||||
polls: {
|
||||
max_options: 10,
|
||||
max_characters_per_option: 150,
|
||||
min_expiration: 50,
|
||||
max_expiration: 2629746,
|
||||
},
|
||||
reactions: {
|
||||
max_reactions: 1,
|
||||
},
|
||||
},
|
||||
contact_account: contact,
|
||||
rules: instance.rules ?? [],
|
||||
};
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: marie and other Sharkey contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { Entity } from 'megalodon';
|
||||
import { FILE_TYPE_BROWSERSAFE } from '@/const.js';
|
||||
import type { Config } from '@/config.js';
|
||||
import type { MiMeta } from '@/models/Meta.js';
|
||||
|
||||
/* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
|
||||
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: config.maxNoteLength,
|
||||
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: 150,
|
||||
min_expiration: 50,
|
||||
max_expiration: 2629746,
|
||||
},
|
||||
reactions: {
|
||||
max_reactions: 1,
|
||||
},
|
||||
},
|
||||
contact_account: contact,
|
||||
rules: [],
|
||||
};
|
||||
}
|
|
@ -3,56 +3,82 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { parseTimelineArgs, TimelineArgs } from '@/server/api/mastodon/timelineArgs.js';
|
||||
import { MiLocalUser } from '@/models/User.js';
|
||||
import { MastoConverters } from '@/server/api/mastodon/converters.js';
|
||||
import type { MegalodonInterface } from 'megalodon';
|
||||
import type { FastifyRequest } from 'fastify';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { parseTimelineArgs, TimelineArgs } from '@/server/api/mastodon/argsUtils.js';
|
||||
import { MastodonConverters } from '@/server/api/mastodon/MastodonConverters.js';
|
||||
import { attachMinMaxPagination } from '@/server/api/mastodon/pagination.js';
|
||||
import { MastodonClientService } from '../MastodonClientService.js';
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import type multer from 'fastify-multer';
|
||||
|
||||
export interface ApiNotifyMastodonRoute {
|
||||
interface ApiNotifyMastodonRoute {
|
||||
Params: {
|
||||
id?: string,
|
||||
},
|
||||
Querystring: TimelineArgs,
|
||||
}
|
||||
|
||||
export class ApiNotifyMastodon {
|
||||
@Injectable()
|
||||
export class ApiNotificationsMastodon {
|
||||
constructor(
|
||||
private readonly request: FastifyRequest<ApiNotifyMastodonRoute>,
|
||||
private readonly client: MegalodonInterface,
|
||||
private readonly me: MiLocalUser | null,
|
||||
private readonly mastoConverters: MastoConverters,
|
||||
private readonly mastoConverters: MastodonConverters,
|
||||
private readonly clientService: MastodonClientService,
|
||||
) {}
|
||||
|
||||
public async getNotifications() {
|
||||
const data = await this.client.getNotifications(parseTimelineArgs(this.request.query));
|
||||
return Promise.all(data.data.map(async n => {
|
||||
const converted = await this.mastoConverters.convertNotification(n, this.me);
|
||||
if (converted.type === 'reaction') {
|
||||
converted.type = 'favourite';
|
||||
public register(fastify: FastifyInstance, upload: ReturnType<typeof multer>): void {
|
||||
fastify.get<ApiNotifyMastodonRoute>('/v1/notifications', async (request, reply) => {
|
||||
const { client, me } = await this.clientService.getAuthClient(request);
|
||||
const data = await client.getNotifications(parseTimelineArgs(request.query));
|
||||
const notifications = await Promise.all(data.data.map(n => this.mastoConverters.convertNotification(n, me)));
|
||||
const response: MastodonEntity.Notification[] = [];
|
||||
for (const notification of notifications) {
|
||||
// Notifications for inaccessible notes will be null and should be ignored
|
||||
if (!notification) continue;
|
||||
|
||||
response.push(notification);
|
||||
if (notification.type === 'reaction') {
|
||||
response.push({
|
||||
...notification,
|
||||
type: 'favourite',
|
||||
});
|
||||
}
|
||||
}
|
||||
return converted;
|
||||
}));
|
||||
}
|
||||
|
||||
public async getNotification() {
|
||||
if (!this.request.params.id) throw new Error('Missing required parameter "id"');
|
||||
const data = await this.client.getNotification(this.request.params.id);
|
||||
const converted = await this.mastoConverters.convertNotification(data.data, this.me);
|
||||
if (converted.type === 'reaction') {
|
||||
converted.type = 'favourite';
|
||||
}
|
||||
return converted;
|
||||
}
|
||||
attachMinMaxPagination(request, reply, response);
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
public async rmNotification() {
|
||||
if (!this.request.params.id) throw new Error('Missing required parameter "id"');
|
||||
const data = await this.client.dismissNotification(this.request.params.id);
|
||||
return data.data;
|
||||
}
|
||||
fastify.get<ApiNotifyMastodonRoute & { Params: { id?: string } }>('/v1/notification/:id', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
public async rmNotifications() {
|
||||
const data = await this.client.dismissNotifications();
|
||||
return data.data;
|
||||
const { client, me } = await this.clientService.getAuthClient(_request);
|
||||
const data = await client.getNotification(_request.params.id);
|
||||
const response = await this.mastoConverters.convertNotification(data.data, me);
|
||||
|
||||
// Notifications for inaccessible notes will be null and should be ignored
|
||||
if (!response) {
|
||||
return reply.code(404).send({
|
||||
error: 'NOT_FOUND',
|
||||
});
|
||||
}
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.post<ApiNotifyMastodonRoute & { Params: { id?: string } }>('/v1/notification/:id/dismiss', { preHandler: upload.single('none') }, async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.dismissNotification(_request.params.id);
|
||||
|
||||
reply.send(data.data);
|
||||
});
|
||||
|
||||
fastify.post<ApiNotifyMastodonRoute>('/v1/notifications/clear', { preHandler: upload.single('none') }, async (_request, reply) => {
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.dismissNotifications();
|
||||
|
||||
reply.send(data.data);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,92 +3,189 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { MiLocalUser } from '@/models/User.js';
|
||||
import { MastoConverters } from '../converters.js';
|
||||
import { parseTimelineArgs, TimelineArgs } from '../timelineArgs.js';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
|
||||
import { attachMinMaxPagination, attachOffsetPagination } from '@/server/api/mastodon/pagination.js';
|
||||
import { MastodonConverters } from '../MastodonConverters.js';
|
||||
import { parseTimelineArgs, TimelineArgs, toBoolean, toInt } from '../argsUtils.js';
|
||||
import { ApiError } from '../../error.js';
|
||||
import Account = Entity.Account;
|
||||
import Status = Entity.Status;
|
||||
import type { MegalodonInterface } from 'megalodon';
|
||||
import type { FastifyRequest } from 'fastify';
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
|
||||
export interface ApiSearchMastodonRoute {
|
||||
interface ApiSearchMastodonRoute {
|
||||
Querystring: TimelineArgs & {
|
||||
type?: 'accounts' | 'hashtags' | 'statuses';
|
||||
type?: string;
|
||||
q?: string;
|
||||
resolve?: string;
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class ApiSearchMastodon {
|
||||
constructor(
|
||||
private readonly request: FastifyRequest<ApiSearchMastodonRoute>,
|
||||
private readonly client: MegalodonInterface,
|
||||
private readonly me: MiLocalUser | null,
|
||||
private readonly BASE_URL: string,
|
||||
private readonly mastoConverters: MastoConverters,
|
||||
private readonly mastoConverters: MastodonConverters,
|
||||
private readonly clientService: MastodonClientService,
|
||||
) {}
|
||||
|
||||
public async SearchV1() {
|
||||
if (!this.request.query.q) throw new Error('Missing required property "q"');
|
||||
const query = parseTimelineArgs(this.request.query);
|
||||
const data = await this.client.search(this.request.query.q, { type: this.request.query.type, ...query });
|
||||
return data.data;
|
||||
}
|
||||
public register(fastify: FastifyInstance): void {
|
||||
fastify.get<ApiSearchMastodonRoute>('/v1/search', async (request, reply) => {
|
||||
if (!request.query.q) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required property "q"' });
|
||||
if (!request.query.type) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required property "type"' });
|
||||
|
||||
public async SearchV2() {
|
||||
if (!this.request.query.q) throw new Error('Missing required property "q"');
|
||||
const query = parseTimelineArgs(this.request.query);
|
||||
const type = this.request.query.type;
|
||||
const acct = !type || type === 'accounts' ? await this.client.search(this.request.query.q, { type: 'accounts', ...query }) : null;
|
||||
const stat = !type || type === 'statuses' ? await this.client.search(this.request.query.q, { type: 'statuses', ...query }) : null;
|
||||
const tags = !type || type === 'hashtags' ? await this.client.search(this.request.query.q, { type: 'hashtags', ...query }) : null;
|
||||
return {
|
||||
accounts: await Promise.all(acct?.data.accounts.map(async (account: Account) => await this.mastoConverters.convertAccount(account)) ?? []),
|
||||
statuses: await Promise.all(stat?.data.statuses.map(async (status: Status) => await this.mastoConverters.convertStatus(status, this.me)) ?? []),
|
||||
hashtags: tags?.data.hashtags ?? [],
|
||||
};
|
||||
}
|
||||
const type = request.query.type;
|
||||
if (type !== 'hashtags' && type !== 'statuses' && type !== 'accounts') {
|
||||
return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Invalid type' });
|
||||
}
|
||||
|
||||
public async getStatusTrends() {
|
||||
const data = await fetch(`${this.BASE_URL}/api/notes/featured`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
i: this.request.headers.authorization?.replace('Bearer ', ''),
|
||||
}),
|
||||
})
|
||||
.then(res => res.json() as Promise<Status[]>)
|
||||
.then(data => data.map(status => this.mastoConverters.convertStatus(status, this.me)));
|
||||
return Promise.all(data);
|
||||
}
|
||||
const { client, me } = await this.clientService.getAuthClient(request);
|
||||
|
||||
public async getSuggestions() {
|
||||
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: parseTimelineArgs(this.request.query).limit ?? 20,
|
||||
origin: 'local',
|
||||
sort: '+follower',
|
||||
state: 'alive',
|
||||
}),
|
||||
})
|
||||
.then(res => res.json() as Promise<Account[]>)
|
||||
.then(data => data.map((entry => ({
|
||||
source: 'global',
|
||||
account: entry,
|
||||
}))));
|
||||
return Promise.all(data.map(async suggestion => {
|
||||
suggestion.account = await this.mastoConverters.convertAccount(suggestion.account);
|
||||
return suggestion;
|
||||
}));
|
||||
if (toBoolean(request.query.resolve) && !me) {
|
||||
return reply.code(401).send({ error: 'The access token is invalid', error_description: 'Authentication is required to use the "resolve" property' });
|
||||
}
|
||||
if (toInt(request.query.offset) && !me) {
|
||||
return reply.code(401).send({ error: 'The access token is invalid', error_description: 'Authentication is required to use the "offset" property' });
|
||||
}
|
||||
|
||||
// TODO implement resolve
|
||||
|
||||
const query = parseTimelineArgs(request.query);
|
||||
const { data } = await client.search(request.query.q, { type, ...query });
|
||||
const response = {
|
||||
...data,
|
||||
accounts: await Promise.all(data.accounts.map((account: Account) => this.mastoConverters.convertAccount(account))),
|
||||
statuses: await Promise.all(data.statuses.map((status: Status) => this.mastoConverters.convertStatus(status, me))),
|
||||
};
|
||||
|
||||
if (type === 'hashtags') {
|
||||
attachOffsetPagination(request, reply, response.hashtags);
|
||||
} else {
|
||||
attachMinMaxPagination(request, reply, response[type]);
|
||||
}
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.get<ApiSearchMastodonRoute>('/v2/search', async (request, reply) => {
|
||||
if (!request.query.q) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required property "q"' });
|
||||
|
||||
const type = request.query.type;
|
||||
if (type !== undefined && type !== 'hashtags' && type !== 'statuses' && type !== 'accounts') {
|
||||
return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Invalid type' });
|
||||
}
|
||||
|
||||
const { client, me } = await this.clientService.getAuthClient(request);
|
||||
|
||||
if (toBoolean(request.query.resolve) && !me) {
|
||||
return reply.code(401).send({ error: 'The access token is invalid', error_description: 'Authentication is required to use the "resolve" property' });
|
||||
}
|
||||
if (toInt(request.query.offset) && !me) {
|
||||
return reply.code(401).send({ error: 'The access token is invalid', error_description: 'Authentication is required to use the "offset" property' });
|
||||
}
|
||||
|
||||
// TODO implement resolve
|
||||
|
||||
const query = parseTimelineArgs(request.query);
|
||||
const acct = !type || type === 'accounts' ? await client.search(request.query.q, { type: 'accounts', ...query }) : null;
|
||||
const stat = !type || type === 'statuses' ? await client.search(request.query.q, { type: 'statuses', ...query }) : null;
|
||||
const tags = !type || type === 'hashtags' ? await client.search(request.query.q, { type: 'hashtags', ...query }) : null;
|
||||
const response = {
|
||||
accounts: await Promise.all(acct?.data.accounts.map((account: Account) => this.mastoConverters.convertAccount(account)) ?? []),
|
||||
statuses: await Promise.all(stat?.data.statuses.map((status: Status) => this.mastoConverters.convertStatus(status, me)) ?? []),
|
||||
hashtags: tags?.data.hashtags ?? [],
|
||||
};
|
||||
|
||||
// Pagination hack, based on "best guess" expected behavior.
|
||||
// Mastodon doesn't document this part at all!
|
||||
const longestResult = [response.statuses, response.hashtags]
|
||||
.reduce((longest: unknown[], current: unknown[]) => current.length > longest.length ? current : longest, response.accounts);
|
||||
|
||||
// Ignore min/max pagination because how TF would that work with multiple result sets??
|
||||
// Offset pagination is the only possible option
|
||||
attachOffsetPagination(request, reply, longestResult);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.get<ApiSearchMastodonRoute>('/v1/trends/statuses', async (request, reply) => {
|
||||
const baseUrl = this.clientService.getBaseUrl(request);
|
||||
const res = await fetch(`${baseUrl}/api/notes/featured`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
...request.headers as HeadersInit,
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: '{}',
|
||||
});
|
||||
|
||||
await verifyResponse(res);
|
||||
|
||||
const data = await res.json() as Status[];
|
||||
const me = await this.clientService.getAuth(request);
|
||||
const response = await Promise.all(data.map(status => this.mastoConverters.convertStatus(status, me)));
|
||||
|
||||
attachMinMaxPagination(request, reply, response);
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.get<ApiSearchMastodonRoute>('/v2/suggestions', async (request, reply) => {
|
||||
const baseUrl = this.clientService.getBaseUrl(request);
|
||||
const res = await fetch(`${baseUrl}/api/users`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
...request.headers as HeadersInit,
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
limit: parseTimelineArgs(request.query).limit ?? 20,
|
||||
origin: 'local',
|
||||
sort: '+follower',
|
||||
state: 'alive',
|
||||
}),
|
||||
});
|
||||
|
||||
await verifyResponse(res);
|
||||
|
||||
const data = await res.json() as Account[];
|
||||
const response = await Promise.all(data.map(async entry => {
|
||||
return {
|
||||
source: 'global',
|
||||
account: await this.mastoConverters.convertAccount(entry),
|
||||
};
|
||||
}));
|
||||
|
||||
attachOffsetPagination(request, reply, response);
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function verifyResponse(res: Response): Promise<void> {
|
||||
if (res.ok) return;
|
||||
|
||||
const text = await res.text();
|
||||
|
||||
if (res.headers.get('content-type') === 'application/json') {
|
||||
try {
|
||||
const json = JSON.parse(text);
|
||||
|
||||
if (json && typeof(json) === 'object') {
|
||||
json.httpStatusCode = res.status;
|
||||
return json;
|
||||
}
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
|
||||
// Response is not a JSON object; treat as string
|
||||
throw new ApiError({
|
||||
code: 'INTERNAL_ERROR',
|
||||
message: text || 'Internal error occurred. Please contact us if the error persists.',
|
||||
id: '5d37dbcb-891e-41ca-a3d6-e690c97775ac',
|
||||
kind: 'server',
|
||||
httpStatusCode: res.status,
|
||||
});
|
||||
}
|
||||
|
|
|
@ -4,12 +4,11 @@
|
|||
*/
|
||||
|
||||
import querystring, { ParsedUrlQueryInput } from 'querystring';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { emojiRegexAtStartToEnd } from '@/misc/emoji-regex.js';
|
||||
import { getErrorData, MastodonLogger } from '@/server/api/mastodon/MastodonLogger.js';
|
||||
import { parseTimelineArgs, TimelineArgs, toBoolean, toInt } from '@/server/api/mastodon/timelineArgs.js';
|
||||
import { AuthenticateService } from '@/server/api/AuthenticateService.js';
|
||||
import { convertAttachment, convertPoll, MastoConverters } from '../converters.js';
|
||||
import { getAccessToken, getClient, MastodonApiServerService } from '../MastodonApiServerService.js';
|
||||
import { parseTimelineArgs, TimelineArgs, toBoolean, toInt } from '@/server/api/mastodon/argsUtils.js';
|
||||
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
|
||||
import { convertAttachment, convertPoll, MastodonConverters } from '../MastodonConverters.js';
|
||||
import type { Entity } from 'megalodon';
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
|
||||
|
@ -18,167 +17,112 @@ function normalizeQuery(data: Record<string, unknown>) {
|
|||
return querystring.parse(str);
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class ApiStatusMastodon {
|
||||
constructor(
|
||||
private readonly fastify: FastifyInstance,
|
||||
private readonly mastoConverters: MastoConverters,
|
||||
private readonly logger: MastodonLogger,
|
||||
private readonly authenticateService: AuthenticateService,
|
||||
private readonly mastodon: MastodonApiServerService,
|
||||
private readonly mastoConverters: MastodonConverters,
|
||||
private readonly clientService: MastodonClientService,
|
||||
) {}
|
||||
|
||||
public getStatus() {
|
||||
this.fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id', async (_request, reply) => {
|
||||
try {
|
||||
const { client, me } = await this.mastodon.getAuthClient(_request);
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const data = await client.getStatus(_request.params.id);
|
||||
reply.send(await this.mastoConverters.convertStatus(data.data, me));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`GET /v1/statuses/${_request.params.id}`, data);
|
||||
reply.code(_request.is404 ? 404 : 401).send(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
public register(fastify: FastifyInstance): void {
|
||||
fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
public getStatusSource() {
|
||||
this.fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id/source', async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const data = await client.getStatusSource(_request.params.id);
|
||||
reply.send(data.data);
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`GET /v1/statuses/${_request.params.id}/source`, data);
|
||||
reply.code(_request.is404 ? 404 : 401).send(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
const { client, me } = await this.clientService.getAuthClient(_request);
|
||||
const data = await client.getStatus(_request.params.id);
|
||||
const response = await this.mastoConverters.convertStatus(data.data, me);
|
||||
|
||||
public getContext() {
|
||||
this.fastify.get<{ Params: { id?: string }, Querystring: TimelineArgs }>('/v1/statuses/:id/context', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const { client, me } = await this.mastodon.getAuthClient(_request);
|
||||
const { data } = await client.getStatusContext(_request.params.id, parseTimelineArgs(_request.query));
|
||||
const ancestors = await Promise.all(data.ancestors.map(async status => await this.mastoConverters.convertStatus(status, me)));
|
||||
const descendants = await Promise.all(data.descendants.map(async status => await this.mastoConverters.convertStatus(status, me)));
|
||||
reply.send({ ancestors, descendants });
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`GET /v1/statuses/${_request.params.id}/context`, data);
|
||||
reply.code(_request.is404 ? 404 : 401).send(data);
|
||||
// Fixup - Discord ignores CWs and renders the entire post.
|
||||
if (response.sensitive && _request.headers['user-agent']?.match(/\bDiscordbot\//)) {
|
||||
response.content = '(preview disabled for sensitive content)';
|
||||
response.media_attachments = [];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public getHistory() {
|
||||
this.fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id/history', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const [user] = await this.authenticateService.authenticate(getAccessToken(_request.headers.authorization));
|
||||
const edits = await this.mastoConverters.getEdits(_request.params.id, user);
|
||||
reply.send(edits);
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`GET /v1/statuses/${_request.params.id}/history`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public getReblogged() {
|
||||
this.fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id/reblogged_by', async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const data = await client.getStatusRebloggedBy(_request.params.id);
|
||||
reply.send(await Promise.all(data.data.map(async (account: Entity.Account) => await this.mastoConverters.convertAccount(account))));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`GET /v1/statuses/${_request.params.id}/reblogged_by`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id/source', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.getStatusSource(_request.params.id);
|
||||
|
||||
reply.send(data.data);
|
||||
});
|
||||
}
|
||||
|
||||
public getFavourites() {
|
||||
this.fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id/favourited_by', async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const data = await client.getStatusFavouritedBy(_request.params.id);
|
||||
reply.send(await Promise.all(data.data.map(async (account: Entity.Account) => await this.mastoConverters.convertAccount(account))));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`GET /v1/statuses/${_request.params.id}/favourited_by`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.get<{ Params: { id?: string }, Querystring: TimelineArgs }>('/v1/statuses/:id/context', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const { client, me } = await this.clientService.getAuthClient(_request);
|
||||
const { data } = await client.getStatusContext(_request.params.id, parseTimelineArgs(_request.query));
|
||||
const ancestors = await Promise.all(data.ancestors.map(async status => await this.mastoConverters.convertStatus(status, me)));
|
||||
const descendants = await Promise.all(data.descendants.map(async status => await this.mastoConverters.convertStatus(status, me)));
|
||||
const response = { ancestors, descendants };
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public getMedia() {
|
||||
this.fastify.get<{ Params: { id?: string } }>('/v1/media/:id', async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const data = await client.getMedia(_request.params.id);
|
||||
reply.send(convertAttachment(data.data));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`GET /v1/media/${_request.params.id}`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id/history', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const user = await this.clientService.getAuth(_request);
|
||||
const edits = await this.mastoConverters.getEdits(_request.params.id, user);
|
||||
|
||||
reply.send(edits);
|
||||
});
|
||||
}
|
||||
|
||||
public getPoll() {
|
||||
this.fastify.get<{ Params: { id?: string } }>('/v1/polls/:id', async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const data = await client.getPoll(_request.params.id);
|
||||
reply.send(convertPoll(data.data));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`GET /v1/polls/${_request.params.id}`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id/reblogged_by', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.getStatusRebloggedBy(_request.params.id);
|
||||
const response = await Promise.all(data.data.map((account: Entity.Account) => this.mastoConverters.convertAccount(account)));
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public votePoll() {
|
||||
this.fastify.post<{ Params: { id?: string }, Body: { choices?: number[] } }>('/v1/polls/:id/votes', async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
if (!_request.body.choices) return reply.code(400).send({ error: 'Missing required payload "choices"' });
|
||||
const data = await client.votePoll(_request.params.id, _request.body.choices);
|
||||
reply.send(convertPoll(data.data));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`GET /v1/polls/${_request.params.id}/votes`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id/favourited_by', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.getStatusFavouritedBy(_request.params.id);
|
||||
const response = await Promise.all(data.data.map((account: Entity.Account) => this.mastoConverters.convertAccount(account)));
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public postStatus() {
|
||||
this.fastify.post<{
|
||||
fastify.get<{ Params: { id?: string } }>('/v1/media/:id', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.getMedia(_request.params.id);
|
||||
const response = convertAttachment(data.data);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.get<{ Params: { id?: string } }>('/v1/polls/:id', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.getPoll(_request.params.id);
|
||||
const response = convertPoll(data.data);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.post<{ Params: { id?: string }, Body: { choices?: number[] } }>('/v1/polls/:id/votes', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
if (!_request.body.choices) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required payload "choices"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.votePoll(_request.params.id, _request.body.choices);
|
||||
const response = convertPoll(data.data);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.post<{
|
||||
Body: {
|
||||
media_ids?: string[],
|
||||
poll?: {
|
||||
|
@ -202,63 +146,58 @@ export class ApiStatusMastodon {
|
|||
}
|
||||
}>('/v1/statuses', async (_request, reply) => {
|
||||
let body = _request.body;
|
||||
try {
|
||||
const { client, me } = await this.mastodon.getAuthClient(_request);
|
||||
if ((!body.poll && body['poll[options][]']) || (!body.media_ids && body['media_ids[]'])
|
||||
) {
|
||||
body = normalizeQuery(body);
|
||||
}
|
||||
const text = 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') {
|
||||
const id = body.in_reply_to_id;
|
||||
const post = await client.getStatus(id);
|
||||
const react = post.data.emoji_reactions.filter(e => e.me)[0].name;
|
||||
const data = await client.deleteEmojiReaction(id, react);
|
||||
reply.send(data.data);
|
||||
}
|
||||
if (!body.media_ids) body.media_ids = undefined;
|
||||
if (body.media_ids && !body.media_ids.length) body.media_ids = undefined;
|
||||
|
||||
if (body.poll && !body.poll.options) {
|
||||
return reply.code(400).send({ error: 'Missing required payload "poll.options"' });
|
||||
}
|
||||
if (body.poll && !body.poll.expires_in) {
|
||||
return reply.code(400).send({ error: 'Missing required payload "poll.expires_in"' });
|
||||
}
|
||||
|
||||
const options = {
|
||||
...body,
|
||||
sensitive: toBoolean(body.sensitive),
|
||||
poll: body.poll ? {
|
||||
options: body.poll.options!, // eslint-disable-line @typescript-eslint/no-non-null-assertion
|
||||
expires_in: toInt(body.poll.expires_in)!, // eslint-disable-line @typescript-eslint/no-non-null-assertion
|
||||
multiple: toBoolean(body.poll.multiple),
|
||||
hide_totals: toBoolean(body.poll.hide_totals),
|
||||
} : undefined,
|
||||
};
|
||||
|
||||
const data = await client.postStatus(text, options);
|
||||
reply.send(await this.mastoConverters.convertStatus(data.data as Entity.Status, me));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error('POST /v1/statuses', data);
|
||||
reply.code(401).send(data);
|
||||
if ((!body.poll && body['poll[options][]']) || (!body.media_ids && body['media_ids[]'])
|
||||
) {
|
||||
body = normalizeQuery(body);
|
||||
}
|
||||
});
|
||||
}
|
||||
const text = body.status ??= ' ';
|
||||
const removed = text.replace(/@\S+/g, '').replace(/\s|/g, '');
|
||||
const isDefaultEmoji = emojiRegexAtStartToEnd.test(removed);
|
||||
const isCustomEmoji = /^:[a-zA-Z0-9@_]+:$/.test(removed);
|
||||
|
||||
public updateStatus() {
|
||||
this.fastify.put<{
|
||||
const { client, me } = await this.clientService.getAuthClient(_request);
|
||||
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') {
|
||||
const id = body.in_reply_to_id;
|
||||
const post = await client.getStatus(id);
|
||||
const react = post.data.emoji_reactions.filter(e => e.me)[0].name;
|
||||
const data = await client.deleteEmojiReaction(id, react);
|
||||
reply.send(data.data);
|
||||
}
|
||||
if (!body.media_ids) body.media_ids = undefined;
|
||||
if (body.media_ids && !body.media_ids.length) body.media_ids = undefined;
|
||||
|
||||
if (body.poll && !body.poll.options) {
|
||||
return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required payload "poll.options"' });
|
||||
}
|
||||
if (body.poll && !body.poll.expires_in) {
|
||||
return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required payload "poll.expires_in"' });
|
||||
}
|
||||
|
||||
const options = {
|
||||
...body,
|
||||
sensitive: toBoolean(body.sensitive),
|
||||
poll: body.poll ? {
|
||||
options: body.poll.options!, // eslint-disable-line @typescript-eslint/no-non-null-assertion
|
||||
expires_in: toInt(body.poll.expires_in)!, // eslint-disable-line @typescript-eslint/no-non-null-assertion
|
||||
multiple: toBoolean(body.poll.multiple),
|
||||
hide_totals: toBoolean(body.poll.hide_totals),
|
||||
} : undefined,
|
||||
};
|
||||
|
||||
const data = await client.postStatus(text, options);
|
||||
const response = await this.mastoConverters.convertStatus(data.data as Entity.Status, me);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.put<{
|
||||
Params: { id: string },
|
||||
Body: {
|
||||
status?: string,
|
||||
|
@ -273,201 +212,138 @@ export class ApiStatusMastodon {
|
|||
},
|
||||
}
|
||||
}>('/v1/statuses/:id', async (_request, reply) => {
|
||||
try {
|
||||
const { client, me } = await this.mastodon.getAuthClient(_request);
|
||||
const body = _request.body;
|
||||
const { client, me } = await this.clientService.getAuthClient(_request);
|
||||
const body = _request.body;
|
||||
|
||||
if (!body.media_ids || !body.media_ids.length) {
|
||||
body.media_ids = undefined;
|
||||
}
|
||||
|
||||
const options = {
|
||||
...body,
|
||||
sensitive: toBoolean(body.sensitive),
|
||||
poll: body.poll ? {
|
||||
options: body.poll.options,
|
||||
expires_in: toInt(body.poll.expires_in),
|
||||
multiple: toBoolean(body.poll.multiple),
|
||||
hide_totals: toBoolean(body.poll.hide_totals),
|
||||
} : undefined,
|
||||
};
|
||||
|
||||
const data = await client.editStatus(_request.params.id, options);
|
||||
reply.send(await this.mastoConverters.convertStatus(data.data, me));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`POST /v1/statuses/${_request.params.id}`, data);
|
||||
reply.code(401).send(data);
|
||||
if (!body.media_ids || !body.media_ids.length) {
|
||||
body.media_ids = undefined;
|
||||
}
|
||||
|
||||
const options = {
|
||||
...body,
|
||||
sensitive: toBoolean(body.sensitive),
|
||||
poll: body.poll ? {
|
||||
options: body.poll.options,
|
||||
expires_in: toInt(body.poll.expires_in),
|
||||
multiple: toBoolean(body.poll.multiple),
|
||||
hide_totals: toBoolean(body.poll.hide_totals),
|
||||
} : undefined,
|
||||
};
|
||||
|
||||
const data = await client.editStatus(_request.params.id, options);
|
||||
const response = await this.mastoConverters.convertStatus(data.data, me);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public addFavourite() {
|
||||
this.fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/favourite', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const { client, me } = await this.mastodon.getAuthClient(_request);
|
||||
const data = await client.createEmojiReaction(_request.params.id, '❤');
|
||||
reply.send(await this.mastoConverters.convertStatus(data.data, me));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`POST /v1/statuses/${_request.params.id}/favorite`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/favourite', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const { client, me } = await this.clientService.getAuthClient(_request);
|
||||
const data = await client.createEmojiReaction(_request.params.id, '❤');
|
||||
const response = await this.mastoConverters.convertStatus(data.data, me);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public rmFavourite() {
|
||||
this.fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/unfavourite', async (_request, reply) => {
|
||||
try {
|
||||
const { client, me } = await this.mastodon.getAuthClient(_request);
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const data = await client.deleteEmojiReaction(_request.params.id, '❤');
|
||||
reply.send(await this.mastoConverters.convertStatus(data.data, me));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`GET /v1/statuses/${_request.params.id}/unfavorite`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/unfavourite', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const { client, me } = await this.clientService.getAuthClient(_request);
|
||||
const data = await client.deleteEmojiReaction(_request.params.id, '❤');
|
||||
const response = await this.mastoConverters.convertStatus(data.data, me);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public reblogStatus() {
|
||||
this.fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/reblog', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const { client, me } = await this.mastodon.getAuthClient(_request);
|
||||
const data = await client.reblogStatus(_request.params.id);
|
||||
reply.send(await this.mastoConverters.convertStatus(data.data, me));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`POST /v1/statuses/${_request.params.id}/reblog`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/reblog', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const { client, me } = await this.clientService.getAuthClient(_request);
|
||||
const data = await client.reblogStatus(_request.params.id);
|
||||
const response = await this.mastoConverters.convertStatus(data.data, me);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public unreblogStatus() {
|
||||
this.fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/unreblog', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const { client, me } = await this.mastodon.getAuthClient(_request);
|
||||
const data = await client.unreblogStatus(_request.params.id);
|
||||
reply.send(await this.mastoConverters.convertStatus(data.data, me));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`POST /v1/statuses/${_request.params.id}/unreblog`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/unreblog', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const { client, me } = await this.clientService.getAuthClient(_request);
|
||||
const data = await client.unreblogStatus(_request.params.id);
|
||||
const response = await this.mastoConverters.convertStatus(data.data, me);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public bookmarkStatus() {
|
||||
this.fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/bookmark', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const { client, me } = await this.mastodon.getAuthClient(_request);
|
||||
const data = await client.bookmarkStatus(_request.params.id);
|
||||
reply.send(await this.mastoConverters.convertStatus(data.data, me));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`POST /v1/statuses/${_request.params.id}/bookmark`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/bookmark', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const { client, me } = await this.clientService.getAuthClient(_request);
|
||||
const data = await client.bookmarkStatus(_request.params.id);
|
||||
const response = await this.mastoConverters.convertStatus(data.data, me);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public unbookmarkStatus() {
|
||||
this.fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/unbookmark', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const { client, me } = await this.mastodon.getAuthClient(_request);
|
||||
const data = await client.unbookmarkStatus(_request.params.id);
|
||||
reply.send(await this.mastoConverters.convertStatus(data.data, me));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`POST /v1/statuses/${_request.params.id}/unbookmark`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/unbookmark', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const { client, me } = await this.clientService.getAuthClient(_request);
|
||||
const data = await client.unbookmarkStatus(_request.params.id);
|
||||
const response = await this.mastoConverters.convertStatus(data.data, me);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/pin', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
public pinStatus() {
|
||||
this.fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/pin', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const { client, me } = await this.mastodon.getAuthClient(_request);
|
||||
const data = await client.pinStatus(_request.params.id);
|
||||
reply.send(await this.mastoConverters.convertStatus(data.data, me));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`POST /v1/statuses/${_request.params.id}/pin`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
const { client, me } = await this.clientService.getAuthClient(_request);
|
||||
const data = await client.pinStatus(_request.params.id);
|
||||
const response = await this.mastoConverters.convertStatus(data.data, me);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public unpinStatus() {
|
||||
this.fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/unpin', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const { client, me } = await this.mastodon.getAuthClient(_request);
|
||||
const data = await client.unpinStatus(_request.params.id);
|
||||
reply.send(await this.mastoConverters.convertStatus(data.data, me));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`POST /v1/statuses/${_request.params.id}/unpin`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/unpin', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const { client, me } = await this.clientService.getAuthClient(_request);
|
||||
const data = await client.unpinStatus(_request.params.id);
|
||||
const response = await this.mastoConverters.convertStatus(data.data, me);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public reactStatus() {
|
||||
this.fastify.post<{ Params: { id?: string, name?: string } }>('/v1/statuses/:id/react/:name', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
if (!_request.params.name) return reply.code(400).send({ error: 'Missing required parameter "name"' });
|
||||
const { client, me } = await this.mastodon.getAuthClient(_request);
|
||||
const data = await client.createEmojiReaction(_request.params.id, _request.params.name);
|
||||
reply.send(await this.mastoConverters.convertStatus(data.data, me));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`POST /v1/statuses/${_request.params.id}/react/${_request.params.name}`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.post<{ Params: { id?: string, name?: string } }>('/v1/statuses/:id/react/:name', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
if (!_request.params.name) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "name"' });
|
||||
|
||||
const { client, me } = await this.clientService.getAuthClient(_request);
|
||||
const data = await client.createEmojiReaction(_request.params.id, _request.params.name);
|
||||
const response = await this.mastoConverters.convertStatus(data.data, me);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public unreactStatus() {
|
||||
this.fastify.post<{ Params: { id?: string, name?: string } }>('/v1/statuses/:id/unreact/:name', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
if (!_request.params.name) return reply.code(400).send({ error: 'Missing required parameter "name"' });
|
||||
const { client, me } = await this.mastodon.getAuthClient(_request);
|
||||
const data = await client.deleteEmojiReaction(_request.params.id, _request.params.name);
|
||||
reply.send(await this.mastoConverters.convertStatus(data.data, me));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`POST /v1/statuses/${_request.params.id}/unreact/${_request.params.name}`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.post<{ Params: { id?: string, name?: string } }>('/v1/statuses/:id/unreact/:name', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
if (!_request.params.name) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "name"' });
|
||||
|
||||
const { client, me } = await this.clientService.getAuthClient(_request);
|
||||
const data = await client.deleteEmojiReaction(_request.params.id, _request.params.name);
|
||||
const response = await this.mastoConverters.convertStatus(data.data, me);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public deleteStatus() {
|
||||
this.fastify.delete<{ Params: { id?: string } }>('/v1/statuses/:id', async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const data = await client.deleteStatus(_request.params.id);
|
||||
reply.send(data.data);
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`DELETE /v1/statuses/${_request.params.id}`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.delete<{ Params: { id?: string } }>('/v1/statuses/:id', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.deleteStatus(_request.params.id);
|
||||
|
||||
reply.send(data.data);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,232 +3,156 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { getErrorData, MastodonLogger } from '@/server/api/mastodon/MastodonLogger.js';
|
||||
import { convertList, MastoConverters } from '../converters.js';
|
||||
import { getClient, MastodonApiServerService } from '../MastodonApiServerService.js';
|
||||
import { parseTimelineArgs, TimelineArgs, toBoolean } from '../timelineArgs.js';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
|
||||
import { attachMinMaxPagination } from '@/server/api/mastodon/pagination.js';
|
||||
import { convertList, MastodonConverters } from '../MastodonConverters.js';
|
||||
import { parseTimelineArgs, TimelineArgs, toBoolean } from '../argsUtils.js';
|
||||
import type { Entity } from 'megalodon';
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
|
||||
@Injectable()
|
||||
export class ApiTimelineMastodon {
|
||||
constructor(
|
||||
private readonly fastify: FastifyInstance,
|
||||
private readonly mastoConverters: MastoConverters,
|
||||
private readonly logger: MastodonLogger,
|
||||
private readonly mastodon: MastodonApiServerService,
|
||||
private readonly clientService: MastodonClientService,
|
||||
private readonly mastoConverters: MastodonConverters,
|
||||
) {}
|
||||
|
||||
public getTL() {
|
||||
this.fastify.get<{ Querystring: TimelineArgs }>('/v1/timelines/public', async (_request, reply) => {
|
||||
try {
|
||||
const { client, me } = await this.mastodon.getAuthClient(_request);
|
||||
const data = toBoolean(_request.query.local)
|
||||
? await client.getLocalTimeline(parseTimelineArgs(_request.query))
|
||||
: await client.getPublicTimeline(parseTimelineArgs(_request.query));
|
||||
reply.send(await Promise.all(data.data.map(async (status: Entity.Status) => await this.mastoConverters.convertStatus(status, me))));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error('GET /v1/timelines/public', data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
public register(fastify: FastifyInstance): void {
|
||||
fastify.get<{ Querystring: TimelineArgs }>('/v1/timelines/public', async (request, reply) => {
|
||||
const { client, me } = await this.clientService.getAuthClient(request);
|
||||
const query = parseTimelineArgs(request.query);
|
||||
const data = toBoolean(request.query.local)
|
||||
? await client.getLocalTimeline(query)
|
||||
: await client.getPublicTimeline(query);
|
||||
const response = await Promise.all(data.data.map((status: Entity.Status) => this.mastoConverters.convertStatus(status, me)));
|
||||
|
||||
public getHomeTl() {
|
||||
this.fastify.get<{ Querystring: TimelineArgs }>('/v1/timelines/home', async (_request, reply) => {
|
||||
try {
|
||||
const { client, me } = await this.mastodon.getAuthClient(_request);
|
||||
const data = await client.getHomeTimeline(parseTimelineArgs(_request.query));
|
||||
reply.send(await Promise.all(data.data.map(async (status: Entity.Status) => await this.mastoConverters.convertStatus(status, me))));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error('GET /v1/timelines/home', data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
attachMinMaxPagination(request, reply, response);
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public getTagTl() {
|
||||
this.fastify.get<{ Params: { hashtag?: string }, Querystring: TimelineArgs }>('/v1/timelines/tag/:hashtag', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.params.hashtag) return reply.code(400).send({ error: 'Missing required parameter "hashtag"' });
|
||||
const { client, me } = await this.mastodon.getAuthClient(_request);
|
||||
const data = await client.getTagTimeline(_request.params.hashtag, parseTimelineArgs(_request.query));
|
||||
reply.send(await Promise.all(data.data.map(async (status: Entity.Status) => await this.mastoConverters.convertStatus(status, me))));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`GET /v1/timelines/tag/${_request.params.hashtag}`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.get<{ Querystring: TimelineArgs }>('/v1/timelines/home', async (request, reply) => {
|
||||
const { client, me } = await this.clientService.getAuthClient(request);
|
||||
const query = parseTimelineArgs(request.query);
|
||||
const data = await client.getHomeTimeline(query);
|
||||
const response = await Promise.all(data.data.map((status: Entity.Status) => this.mastoConverters.convertStatus(status, me)));
|
||||
|
||||
attachMinMaxPagination(request, reply, response);
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public getListTL() {
|
||||
this.fastify.get<{ Params: { id?: string }, Querystring: TimelineArgs }>('/v1/timelines/list/:id', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const { client, me } = await this.mastodon.getAuthClient(_request);
|
||||
const data = await client.getListTimeline(_request.params.id, parseTimelineArgs(_request.query));
|
||||
reply.send(await Promise.all(data.data.map(async (status: Entity.Status) => await this.mastoConverters.convertStatus(status, me))));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`GET /v1/timelines/list/${_request.params.id}`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.get<{ Params: { hashtag?: string }, Querystring: TimelineArgs }>('/v1/timelines/tag/:hashtag', async (request, reply) => {
|
||||
if (!request.params.hashtag) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "hashtag"' });
|
||||
|
||||
const { client, me } = await this.clientService.getAuthClient(request);
|
||||
const query = parseTimelineArgs(request.query);
|
||||
const data = await client.getTagTimeline(request.params.hashtag, query);
|
||||
const response = await Promise.all(data.data.map((status: Entity.Status) => this.mastoConverters.convertStatus(status, me)));
|
||||
|
||||
attachMinMaxPagination(request, reply, response);
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public getConversations() {
|
||||
this.fastify.get<{ Querystring: TimelineArgs }>('/v1/conversations', async (_request, reply) => {
|
||||
try {
|
||||
const { client, me } = await this.mastodon.getAuthClient(_request);
|
||||
const data = await client.getConversationTimeline(parseTimelineArgs(_request.query));
|
||||
const conversations = await Promise.all(data.data.map(async (conversation: Entity.Conversation) => await this.mastoConverters.convertConversation(conversation, me)));
|
||||
reply.send(conversations);
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error('GET /v1/conversations', data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.get<{ Params: { id?: string }, Querystring: TimelineArgs }>('/v1/timelines/list/:id', async (request, reply) => {
|
||||
if (!request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const { client, me } = await this.clientService.getAuthClient(request);
|
||||
const query = parseTimelineArgs(request.query);
|
||||
const data = await client.getListTimeline(request.params.id, query);
|
||||
const response = await Promise.all(data.data.map(async (status: Entity.Status) => await this.mastoConverters.convertStatus(status, me)));
|
||||
|
||||
attachMinMaxPagination(request, reply, response);
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public getList() {
|
||||
this.fastify.get<{ Params: { id?: string } }>('/v1/lists/:id', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
const data = await client.getList(_request.params.id);
|
||||
reply.send(convertList(data.data));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`GET /v1/lists/${_request.params.id}`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.get<{ Querystring: TimelineArgs }>('/v1/conversations', async (request, reply) => {
|
||||
const { client, me } = await this.clientService.getAuthClient(request);
|
||||
const query = parseTimelineArgs(request.query);
|
||||
const data = await client.getConversationTimeline(query);
|
||||
const response = await Promise.all(data.data.map((conversation: Entity.Conversation) => this.mastoConverters.convertConversation(conversation, me)));
|
||||
|
||||
attachMinMaxPagination(request, reply, response);
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public getLists() {
|
||||
this.fastify.get('/v1/lists', async (_request, reply) => {
|
||||
try {
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
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) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error('GET /v1/lists', data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.get<{ Params: { id?: string } }>('/v1/lists/:id', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.getList(_request.params.id);
|
||||
const response = convertList(data.data);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public getListAccounts() {
|
||||
this.fastify.get<{ Params: { id?: string }, Querystring: { limit?: number, max_id?: string, since_id?: string } }>('/v1/lists/:id/accounts', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
const data = await client.getAccountsInList(_request.params.id, _request.query);
|
||||
const accounts = await Promise.all(data.data.map((account: Entity.Account) => this.mastoConverters.convertAccount(account)));
|
||||
reply.send(accounts);
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`GET /v1/lists/${_request.params.id}/accounts`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.get('/v1/lists', async (request, reply) => {
|
||||
const client = this.clientService.getClient(request);
|
||||
const data = await client.getLists();
|
||||
const response = data.data.map((list: Entity.List) => convertList(list));
|
||||
|
||||
attachMinMaxPagination(request, reply, response);
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public addListAccount() {
|
||||
this.fastify.post<{ Params: { id?: string }, Querystring: { accounts_id?: string[] } }>('/v1/lists/:id/accounts', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
if (!_request.query.accounts_id) return reply.code(400).send({ error: 'Missing required property "accounts_id"' });
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
const data = await client.addAccountsToList(_request.params.id, _request.query.accounts_id);
|
||||
reply.send(data.data);
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`POST /v1/lists/${_request.params.id}/accounts`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.get<{ Params: { id?: string }, Querystring: TimelineArgs }>('/v1/lists/:id/accounts', async (request, reply) => {
|
||||
if (!request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(request);
|
||||
const data = await client.getAccountsInList(request.params.id, parseTimelineArgs(request.query));
|
||||
const response = await Promise.all(data.data.map((account: Entity.Account) => this.mastoConverters.convertAccount(account)));
|
||||
|
||||
attachMinMaxPagination(request, reply, response);
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public rmListAccount() {
|
||||
this.fastify.delete<{ Params: { id?: string }, Querystring: { accounts_id?: string[] } }>('/v1/lists/:id/accounts', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
if (!_request.query.accounts_id) return reply.code(400).send({ error: 'Missing required property "accounts_id"' });
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
const data = await client.deleteAccountsFromList(_request.params.id, _request.query.accounts_id);
|
||||
reply.send(data.data);
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`DELETE /v1/lists/${_request.params.id}/accounts`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.post<{ Params: { id?: string }, Querystring: { accounts_id?: string[] } }>('/v1/lists/:id/accounts', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
if (!_request.query.accounts_id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required property "accounts_id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.addAccountsToList(_request.params.id, _request.query.accounts_id);
|
||||
|
||||
reply.send(data.data);
|
||||
});
|
||||
}
|
||||
|
||||
public createList() {
|
||||
this.fastify.post<{ Body: { title?: string } }>('/v1/lists', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.body.title) return reply.code(400).send({ error: 'Missing required payload "title"' });
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
const data = await client.createList(_request.body.title);
|
||||
reply.send(convertList(data.data));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error('POST /v1/lists', data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.delete<{ Params: { id?: string }, Querystring: { accounts_id?: string[] } }>('/v1/lists/:id/accounts', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
if (!_request.query.accounts_id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required property "accounts_id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.deleteAccountsFromList(_request.params.id, _request.query.accounts_id);
|
||||
|
||||
reply.send(data.data);
|
||||
});
|
||||
}
|
||||
|
||||
public updateList() {
|
||||
this.fastify.put<{ Params: { id?: string }, Body: { title?: string } }>('/v1/lists/:id', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
if (!_request.body.title) return reply.code(400).send({ error: 'Missing required payload "title"' });
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
const data = await client.updateList(_request.params.id, _request.body.title);
|
||||
reply.send(convertList(data.data));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`PUT /v1/lists/${_request.params.id}`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.post<{ Body: { title?: string } }>('/v1/lists', async (_request, reply) => {
|
||||
if (!_request.body.title) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required payload "title"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.createList(_request.body.title);
|
||||
const response = convertList(data.data);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
|
||||
public deleteList() {
|
||||
this.fastify.delete<{ Params: { id?: string } }>('/v1/lists/:id', async (_request, reply) => {
|
||||
try {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
await client.deleteList(_request.params.id);
|
||||
reply.send({});
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`DELETE /v1/lists/${_request.params.id}`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
fastify.put<{ Params: { id?: string }, Body: { title?: string } }>('/v1/lists/:id', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
if (!_request.body.title) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required payload "title"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
const data = await client.updateList(_request.params.id, _request.body.title);
|
||||
const response = convertList(data.data);
|
||||
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.delete<{ Params: { id?: string } }>('/v1/lists/:id', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
await client.deleteList(_request.params.id);
|
||||
|
||||
reply.send({});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
170
packages/backend/src/server/api/mastodon/pagination.ts
Normal file
170
packages/backend/src/server/api/mastodon/pagination.ts
Normal file
|
@ -0,0 +1,170 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { getBaseUrl } from '@/server/api/mastodon/MastodonClientService.js';
|
||||
|
||||
interface AnyEntity {
|
||||
readonly id: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches Mastodon's pagination headers to a response that is paginated by min_id / max_id parameters.
|
||||
* Results must be sorted, but can be in ascending or descending order.
|
||||
* Attached headers will always be in descending order.
|
||||
*
|
||||
* @param request Fastify request object
|
||||
* @param reply Fastify reply object
|
||||
* @param results Results array, ordered in ascending or descending order
|
||||
*/
|
||||
export function attachMinMaxPagination(request: FastifyRequest, reply: FastifyReply, results: AnyEntity[]): void {
|
||||
// No results, nothing to do
|
||||
if (!hasItems(results)) return;
|
||||
|
||||
// "next" link - older results
|
||||
const oldest = findOldest(results);
|
||||
const nextUrl = createPaginationUrl(request, { max_id: oldest }); // Next page (older) has IDs less than the oldest of this page
|
||||
const next = `<${nextUrl}>; rel="next"`;
|
||||
|
||||
// "prev" link - newer results
|
||||
const newest = findNewest(results);
|
||||
const prevUrl = createPaginationUrl(request, { min_id: newest }); // Previous page (newer) has IDs greater than the newest of this page
|
||||
const prev = `<${prevUrl}>; rel="prev"`;
|
||||
|
||||
// https://docs.joinmastodon.org/api/guidelines/#pagination
|
||||
const link = `${next}, ${prev}`;
|
||||
reply.header('link', link);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches Mastodon's pagination headers to a response that is paginated by limit / offset parameters.
|
||||
* Results must be sorted, but can be in ascending or descending order.
|
||||
* Attached headers will always be in descending order.
|
||||
*
|
||||
* @param request Fastify request object
|
||||
* @param reply Fastify reply object
|
||||
* @param results Results array, ordered in ascending or descending order
|
||||
*/
|
||||
export function attachOffsetPagination(request: FastifyRequest, reply: FastifyReply, results: unknown[]): void {
|
||||
const links: string[] = [];
|
||||
|
||||
// Find initial offset
|
||||
const offset = findOffset(request);
|
||||
const limit = findLimit(request);
|
||||
|
||||
// "next" link - older results
|
||||
if (hasItems(results)) {
|
||||
const oldest = offset + results.length;
|
||||
const nextUrl = createPaginationUrl(request, { offset: oldest }); // Next page (older) has entries less than the oldest of this page
|
||||
links.push(`<${nextUrl}>; rel="next"`);
|
||||
}
|
||||
|
||||
// "prev" link - newer results
|
||||
// We can only paginate backwards if a limit is specified
|
||||
if (limit) {
|
||||
// Make sure we don't cross below 0, as that will produce an API error
|
||||
if (limit <= offset) {
|
||||
const newest = offset - limit;
|
||||
const prevUrl = createPaginationUrl(request, { offset: newest }); // Previous page (newer) has entries greater than the newest of this page
|
||||
links.push(`<${prevUrl}>; rel="prev"`);
|
||||
} else {
|
||||
const prevUrl = createPaginationUrl(request, { offset: 0, limit: offset }); // Previous page (newer) has entries greater than the newest of this page
|
||||
links.push(`<${prevUrl}>; rel="prev"`);
|
||||
}
|
||||
}
|
||||
|
||||
// https://docs.joinmastodon.org/api/guidelines/#pagination
|
||||
if (links.length > 0) {
|
||||
const link = links.join(', ');
|
||||
reply.header('link', link);
|
||||
}
|
||||
}
|
||||
|
||||
function hasItems<T>(items: T[]): items is [T, ...T[]] {
|
||||
return items.length > 0;
|
||||
}
|
||||
|
||||
function findOffset(request: FastifyRequest): number {
|
||||
if (typeof(request.query) !== 'object') return 0;
|
||||
|
||||
const query = request.query as Record<string, string | string[] | undefined>;
|
||||
if (!query.offset) return 0;
|
||||
|
||||
if (Array.isArray(query.offset)) {
|
||||
const offsets = query.offset
|
||||
.map(o => parseInt(o))
|
||||
.filter(o => !isNaN(o));
|
||||
const offset = Math.max(...offsets);
|
||||
return isNaN(offset) ? 0 : offset;
|
||||
}
|
||||
|
||||
const offset = parseInt(query.offset);
|
||||
return isNaN(offset) ? 0 : offset;
|
||||
}
|
||||
|
||||
function findLimit(request: FastifyRequest): number | null {
|
||||
if (typeof(request.query) !== 'object') return null;
|
||||
|
||||
const query = request.query as Record<string, string | string[] | undefined>;
|
||||
if (!query.limit) return null;
|
||||
|
||||
if (Array.isArray(query.limit)) {
|
||||
const limits = query.limit
|
||||
.map(l => parseInt(l))
|
||||
.filter(l => !isNaN(l));
|
||||
const limit = Math.max(...limits);
|
||||
return isNaN(limit) ? null : limit;
|
||||
}
|
||||
|
||||
const limit = parseInt(query.limit);
|
||||
return isNaN(limit) ? null : limit;
|
||||
}
|
||||
|
||||
function findOldest(items: [AnyEntity, ...AnyEntity[]]): string {
|
||||
const first = items[0].id;
|
||||
const last = items[items.length - 1].id;
|
||||
|
||||
return isOlder(first, last) ? first : last;
|
||||
}
|
||||
|
||||
function findNewest(items: [AnyEntity, ...AnyEntity[]]): string {
|
||||
const first = items[0].id;
|
||||
const last = items[items.length - 1].id;
|
||||
|
||||
return isOlder(first, last) ? last : first;
|
||||
}
|
||||
|
||||
function isOlder(a: string, b: string): boolean {
|
||||
if (a === b) return false;
|
||||
|
||||
if (a.length !== b.length) {
|
||||
return a.length < b.length;
|
||||
}
|
||||
|
||||
return a < b;
|
||||
}
|
||||
|
||||
function createPaginationUrl(request: FastifyRequest, data: {
|
||||
min_id?: string;
|
||||
max_id?: string;
|
||||
offset?: number;
|
||||
limit?: number;
|
||||
}): string {
|
||||
const baseUrl = getBaseUrl(request);
|
||||
const requestUrl = new URL(request.url, baseUrl);
|
||||
|
||||
// Remove any existing pagination
|
||||
requestUrl.searchParams.delete('min_id');
|
||||
requestUrl.searchParams.delete('max_id');
|
||||
requestUrl.searchParams.delete('since_id');
|
||||
requestUrl.searchParams.delete('offset');
|
||||
|
||||
if (data.min_id) requestUrl.searchParams.set('min_id', data.min_id);
|
||||
if (data.max_id) requestUrl.searchParams.set('max_id', data.max_id);
|
||||
if (data.offset) requestUrl.searchParams.set('offset', String(data.offset));
|
||||
if (data.limit) requestUrl.searchParams.set('limit', String(data.limit));
|
||||
|
||||
return requestUrl.href;
|
||||
}
|
|
@ -5,7 +5,6 @@
|
|||
|
||||
import querystring from 'querystring';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import megalodon, { MegalodonInterface } from 'megalodon';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
/* import { kinds } from '@/misc/api-permissions.js';
|
||||
import type { Config } from '@/config.js';
|
||||
|
@ -14,6 +13,8 @@ import multer from 'fastify-multer';
|
|||
import { bindThis } from '@/decorators.js';
|
||||
import type { Config } from '@/config.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
|
||||
import { getErrorData } from '@/server/api/mastodon/MastodonLogger.js';
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
|
||||
const kinds = [
|
||||
|
@ -51,19 +52,13 @@ const kinds = [
|
|||
'write:gallery-likes',
|
||||
];
|
||||
|
||||
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 OAuth2ProviderService {
|
||||
constructor(
|
||||
@Inject(DI.config)
|
||||
private config: Config,
|
||||
|
||||
private readonly mastodonClientService: MastodonClientService,
|
||||
) { }
|
||||
|
||||
// https://datatracker.ietf.org/doc/html/rfc8414.html
|
||||
|
@ -122,8 +117,8 @@ export class OAuth2ProviderService {
|
|||
try {
|
||||
const parsed = querystring.parse(body);
|
||||
done(null, parsed);
|
||||
} catch (e: any) {
|
||||
done(e);
|
||||
} catch (e: unknown) {
|
||||
done(e instanceof Error ? e : new Error(String(e)));
|
||||
}
|
||||
});
|
||||
payload.on('error', done);
|
||||
|
@ -131,74 +126,53 @@ export class OAuth2ProviderService {
|
|||
|
||||
fastify.register(multer.contentParser);
|
||||
|
||||
fastify.get('/authorize', async (request, reply) => {
|
||||
const query: any = request.query;
|
||||
let param = "mastodon=true";
|
||||
if (query.state) param += `&state=${query.state}`;
|
||||
if (query.redirect_uri) param += `&redirect_uri=${query.redirect_uri}`;
|
||||
const client = query.client_id ? query.client_id : "";
|
||||
reply.redirect(
|
||||
`${Buffer.from(client.toString(), 'base64').toString()}?${param}`,
|
||||
);
|
||||
});
|
||||
for (const url of ['/authorize', '/authorize/']) {
|
||||
fastify.get<{ Querystring: Record<string, string | string[] | undefined> }>(url, async (request, reply) => {
|
||||
if (typeof(request.query.client_id) !== 'string') return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required query "client_id"' });
|
||||
|
||||
fastify.get('/authorize/', async (request, reply) => {
|
||||
const query: any = request.query;
|
||||
let param = "mastodon=true";
|
||||
if (query.state) param += `&state=${query.state}`;
|
||||
if (query.redirect_uri) param += `&redirect_uri=${query.redirect_uri}`;
|
||||
const client = query.client_id ? query.client_id : "";
|
||||
reply.redirect(
|
||||
`${Buffer.from(client.toString(), 'base64').toString()}?${param}`,
|
||||
);
|
||||
});
|
||||
const redirectUri = new URL(Buffer.from(request.query.client_id, 'base64').toString());
|
||||
redirectUri.searchParams.set('mastodon', 'true');
|
||||
if (request.query.state) redirectUri.searchParams.set('state', String(request.query.state));
|
||||
if (request.query.redirect_uri) redirectUri.searchParams.set('redirect_uri', String(request.query.redirect_uri));
|
||||
|
||||
fastify.post('/token', { preHandler: upload.none() }, async (request, reply) => {
|
||||
const body: any = request.body || request.query;
|
||||
if (body.grant_type === "client_credentials") {
|
||||
reply.redirect(redirectUri.toString());
|
||||
});
|
||||
}
|
||||
|
||||
fastify.post<{ Body?: Record<string, string | string[] | undefined>, Querystring: Record<string, string | string[] | undefined> }>('/token', { preHandler: upload.none() }, async (request, reply) => {
|
||||
const body = request.body ?? request.query;
|
||||
|
||||
if (body.grant_type === 'client_credentials') {
|
||||
const ret = {
|
||||
access_token: uuid(),
|
||||
token_type: "Bearer",
|
||||
scope: "read",
|
||||
token_type: 'Bearer',
|
||||
scope: 'read',
|
||||
created_at: Math.floor(new Date().getTime() / 1000),
|
||||
};
|
||||
reply.send(ret);
|
||||
}
|
||||
let client_id: any = body.client_id;
|
||||
const BASE_URL = `${request.protocol}://${request.hostname}`;
|
||||
const client = getClient(BASE_URL, '');
|
||||
let token = null;
|
||||
if (body.code) {
|
||||
//m = body.code.match(/^([a-zA-Z0-9]{8})([a-zA-Z0-9]{4})([a-zA-Z0-9]{4})([a-zA-Z0-9]{4})([a-zA-Z0-9]{12})/);
|
||||
//if (!m.length) {
|
||||
// ctx.body = { error: "Invalid code" };
|
||||
// return;
|
||||
//}
|
||||
//token = `${m[1]}-${m[2]}-${m[3]}-${m[4]}-${m[5]}`
|
||||
//console.log(body.code, token);
|
||||
token = body.code;
|
||||
}
|
||||
if (client_id instanceof Array) {
|
||||
client_id = client_id.toString();
|
||||
} else if (!client_id) {
|
||||
client_id = null;
|
||||
}
|
||||
|
||||
try {
|
||||
const atData = await client.fetchAccessToken(
|
||||
client_id,
|
||||
body.client_secret,
|
||||
token ? token : "",
|
||||
);
|
||||
if (!body.client_secret) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required query "client_secret"' });
|
||||
|
||||
const clientId = body.client_id ? String(body.clientId) : null;
|
||||
const secret = String(body.client_secret);
|
||||
const code = body.code ? String(body.code) : '';
|
||||
|
||||
// TODO fetch the access token directly
|
||||
const client = this.mastodonClientService.getClient(request);
|
||||
const atData = await client.fetchAccessToken(clientId, secret, code);
|
||||
|
||||
const ret = {
|
||||
access_token: atData.accessToken,
|
||||
token_type: "Bearer",
|
||||
scope: body.scope || "read write follow push",
|
||||
token_type: 'Bearer',
|
||||
scope: body.scope || 'read write follow push',
|
||||
created_at: Math.floor(new Date().getTime() / 1000),
|
||||
};
|
||||
reply.send(ret);
|
||||
} catch (err: any) {
|
||||
/* console.error(err); */
|
||||
reply.code(401).send(err.response.data);
|
||||
} catch (e: unknown) {
|
||||
const data = getErrorData(e);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ namespace Entity {
|
|||
email: string
|
||||
version: string
|
||||
thumbnail: string | null
|
||||
urls: URLs | null
|
||||
urls: URLs
|
||||
stats: Stats
|
||||
languages: Array<string>
|
||||
registrations: boolean
|
||||
|
|
|
@ -6,5 +6,7 @@ namespace Entity {
|
|||
me: boolean
|
||||
name: string
|
||||
accounts?: Array<Account>
|
||||
url?: string
|
||||
static_url?: string
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,769 +0,0 @@
|
|||
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'
|
||||
import objectAssignDeep from 'object-assign-deep'
|
||||
|
||||
import WebSocket from './web_socket'
|
||||
import Response from '../response'
|
||||
import { RequestCanceledError } from '../cancel'
|
||||
import proxyAgent, { ProxyConfig } from '../proxy_config'
|
||||
import { NO_REDIRECT, DEFAULT_SCOPE, DEFAULT_UA } from '../default'
|
||||
import FriendicaEntity from './entity'
|
||||
import MegalodonEntity from '../entity'
|
||||
import NotificationType, { UnknownNotificationTypeError } from '../notification'
|
||||
import FriendicaNotificationType from './notification'
|
||||
|
||||
namespace FriendicaAPI {
|
||||
/**
|
||||
* Interface
|
||||
*/
|
||||
export interface Interface {
|
||||
get<T = any>(path: string, params?: any, headers?: { [key: string]: string }, pathIsFullyQualified?: boolean): Promise<Response<T>>
|
||||
put<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
putForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
patch<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
patchForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
post<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
postForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
del<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
cancel(): void
|
||||
socket(path: string, stream: string, params?: string): WebSocket
|
||||
}
|
||||
|
||||
/**
|
||||
* Friendica API client.
|
||||
*
|
||||
* Using axios for request, you will handle promises.
|
||||
*/
|
||||
export class Client implements Interface {
|
||||
static DEFAULT_SCOPE = DEFAULT_SCOPE
|
||||
static DEFAULT_URL = 'https://mastodon.social'
|
||||
static NO_REDIRECT = NO_REDIRECT
|
||||
|
||||
private accessToken: string | null
|
||||
private baseUrl: string
|
||||
private userAgent: string
|
||||
private abortController: AbortController
|
||||
private proxyConfig: ProxyConfig | false = false
|
||||
|
||||
/**
|
||||
* @param baseUrl hostname or base URL
|
||||
* @param accessToken access token from OAuth2 authorization
|
||||
* @param userAgent UserAgent is specified in header on request.
|
||||
* @param proxyConfig Proxy setting, or set false if don't use proxy.
|
||||
*/
|
||||
constructor(
|
||||
baseUrl: string,
|
||||
accessToken: string | null = null,
|
||||
userAgent: string = DEFAULT_UA,
|
||||
proxyConfig: ProxyConfig | false = false
|
||||
) {
|
||||
this.accessToken = accessToken
|
||||
this.baseUrl = baseUrl
|
||||
this.userAgent = userAgent
|
||||
this.proxyConfig = proxyConfig
|
||||
this.abortController = new AbortController()
|
||||
axios.defaults.signal = this.abortController.signal
|
||||
}
|
||||
|
||||
/**
|
||||
* GET request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Query parameters
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async get<T>(
|
||||
path: string,
|
||||
params = {},
|
||||
headers: { [key: string]: string } = {},
|
||||
pathIsFullyQualified = false
|
||||
): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
params: params,
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.get<T>((pathIsFullyQualified ? '' : this.baseUrl) + path, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async put<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.put<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT request to mastodon REST API for multipart.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async putForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.putForm<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async patch<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.patch<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH request to mastodon REST API for multipart.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async patchForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.patchForm<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* POST request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async post<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios.post<T>(this.baseUrl + path, params, options).then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* POST request to mastodon REST API for multipart.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async postForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios.postForm<T>(this.baseUrl + path, params, options).then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async del<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
data: params,
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.delete(this.baseUrl + path, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel all requests in this instance.
|
||||
* @returns void
|
||||
*/
|
||||
public cancel(): void {
|
||||
return this.abortController.abort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get connection and receive websocket connection for Pleroma API.
|
||||
*
|
||||
* @param path relative path from baseUrl: normally it is `/streaming`.
|
||||
* @param stream Stream name, please refer: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/mastodon_api/mastodon_socket.ex#L19-28
|
||||
* @returns WebSocket, which inherits from EventEmitter
|
||||
*/
|
||||
public socket(path: string, stream: string, params?: string): WebSocket {
|
||||
if (!this.accessToken) {
|
||||
throw new Error('accessToken is required')
|
||||
}
|
||||
const url = this.baseUrl + path
|
||||
const streaming = new WebSocket(url, stream, params, this.accessToken, this.userAgent, this.proxyConfig)
|
||||
process.nextTick(() => {
|
||||
streaming.start()
|
||||
})
|
||||
return streaming
|
||||
}
|
||||
}
|
||||
|
||||
export namespace Entity {
|
||||
export type Account = FriendicaEntity.Account
|
||||
export type Activity = FriendicaEntity.Activity
|
||||
export type Application = FriendicaEntity.Application
|
||||
export type AsyncAttachment = FriendicaEntity.AsyncAttachment
|
||||
export type Attachment = FriendicaEntity.Attachment
|
||||
export type Card = FriendicaEntity.Card
|
||||
export type Context = FriendicaEntity.Context
|
||||
export type Conversation = FriendicaEntity.Conversation
|
||||
export type Emoji = FriendicaEntity.Emoji
|
||||
export type FeaturedTag = FriendicaEntity.FeaturedTag
|
||||
export type Field = FriendicaEntity.Field
|
||||
export type Filter = FriendicaEntity.Filter
|
||||
export type FollowRequest = FriendicaEntity.FollowRequest
|
||||
export type History = FriendicaEntity.History
|
||||
export type IdentityProof = FriendicaEntity.IdentityProof
|
||||
export type Instance = FriendicaEntity.Instance
|
||||
export type List = FriendicaEntity.List
|
||||
export type Marker = FriendicaEntity.Marker
|
||||
export type Mention = FriendicaEntity.Mention
|
||||
export type Notification = FriendicaEntity.Notification
|
||||
export type Poll = FriendicaEntity.Poll
|
||||
export type PollOption = FriendicaEntity.PollOption
|
||||
export type Preferences = FriendicaEntity.Preferences
|
||||
export type PushSubscription = FriendicaEntity.PushSubscription
|
||||
export type Relationship = FriendicaEntity.Relationship
|
||||
export type Report = FriendicaEntity.Report
|
||||
export type Results = FriendicaEntity.Results
|
||||
export type ScheduledStatus = FriendicaEntity.ScheduledStatus
|
||||
export type Source = FriendicaEntity.Source
|
||||
export type Stats = FriendicaEntity.Stats
|
||||
export type Status = FriendicaEntity.Status
|
||||
export type StatusParams = FriendicaEntity.StatusParams
|
||||
export type StatusSource = FriendicaEntity.StatusSource
|
||||
export type Tag = FriendicaEntity.Tag
|
||||
export type Token = FriendicaEntity.Token
|
||||
export type URLs = FriendicaEntity.URLs
|
||||
}
|
||||
|
||||
export namespace Converter {
|
||||
export const encodeNotificationType = (
|
||||
t: MegalodonEntity.NotificationType
|
||||
): FriendicaEntity.NotificationType | UnknownNotificationTypeError => {
|
||||
switch (t) {
|
||||
case NotificationType.Follow:
|
||||
return FriendicaNotificationType.Follow
|
||||
case NotificationType.Favourite:
|
||||
return FriendicaNotificationType.Favourite
|
||||
case NotificationType.Reblog:
|
||||
return FriendicaNotificationType.Reblog
|
||||
case NotificationType.Mention:
|
||||
return FriendicaNotificationType.Mention
|
||||
case NotificationType.FollowRequest:
|
||||
return FriendicaNotificationType.FollowRequest
|
||||
case NotificationType.Status:
|
||||
return FriendicaNotificationType.Status
|
||||
case NotificationType.PollExpired:
|
||||
return FriendicaNotificationType.Poll
|
||||
case NotificationType.Update:
|
||||
return FriendicaNotificationType.Update
|
||||
default:
|
||||
return new UnknownNotificationTypeError()
|
||||
}
|
||||
}
|
||||
|
||||
export const decodeNotificationType = (
|
||||
t: FriendicaEntity.NotificationType
|
||||
): MegalodonEntity.NotificationType | UnknownNotificationTypeError => {
|
||||
switch (t) {
|
||||
case FriendicaNotificationType.Follow:
|
||||
return NotificationType.Follow
|
||||
case FriendicaNotificationType.Favourite:
|
||||
return NotificationType.Favourite
|
||||
case FriendicaNotificationType.Mention:
|
||||
return NotificationType.Mention
|
||||
case FriendicaNotificationType.Reblog:
|
||||
return NotificationType.Reblog
|
||||
case FriendicaNotificationType.FollowRequest:
|
||||
return NotificationType.FollowRequest
|
||||
case FriendicaNotificationType.Status:
|
||||
return NotificationType.Status
|
||||
case FriendicaNotificationType.Poll:
|
||||
return NotificationType.PollExpired
|
||||
case FriendicaNotificationType.Update:
|
||||
return NotificationType.Update
|
||||
default:
|
||||
return new UnknownNotificationTypeError()
|
||||
}
|
||||
}
|
||||
|
||||
export const account = (a: Entity.Account): MegalodonEntity.Account => ({
|
||||
id: a.id,
|
||||
username: a.username,
|
||||
acct: a.acct,
|
||||
display_name: a.display_name,
|
||||
locked: a.locked,
|
||||
discoverable: a.discoverable,
|
||||
group: a.group,
|
||||
noindex: null,
|
||||
suspended: null,
|
||||
limited: null,
|
||||
created_at: a.created_at,
|
||||
followers_count: a.followers_count,
|
||||
following_count: a.following_count,
|
||||
statuses_count: a.statuses_count,
|
||||
note: a.note,
|
||||
url: a.url,
|
||||
avatar: a.avatar,
|
||||
avatar_static: a.avatar_static,
|
||||
header: a.header,
|
||||
header_static: a.header_static,
|
||||
emojis: a.emojis.map(e => emoji(e)),
|
||||
moved: a.moved ? account(a.moved) : null,
|
||||
fields: a.fields.map(f => field(f)),
|
||||
bot: a.bot,
|
||||
source: a.source ? source(a.source) : undefined
|
||||
})
|
||||
export const activity = (a: Entity.Activity): MegalodonEntity.Activity => a
|
||||
export const application = (a: Entity.Application): MegalodonEntity.Application => a
|
||||
export const attachment = (a: Entity.Attachment): MegalodonEntity.Attachment => a
|
||||
export const async_attachment = (a: Entity.AsyncAttachment) => {
|
||||
if (a.url) {
|
||||
return {
|
||||
id: a.id,
|
||||
type: a.type,
|
||||
url: a.url,
|
||||
remote_url: a.remote_url,
|
||||
preview_url: a.preview_url,
|
||||
text_url: a.text_url,
|
||||
meta: a.meta,
|
||||
description: a.description,
|
||||
blurhash: a.blurhash
|
||||
} as MegalodonEntity.Attachment
|
||||
} else {
|
||||
return a as MegalodonEntity.AsyncAttachment
|
||||
}
|
||||
}
|
||||
export const card = (c: Entity.Card): MegalodonEntity.Card => ({
|
||||
url: c.url,
|
||||
title: c.title,
|
||||
description: c.description,
|
||||
type: c.type,
|
||||
image: c.image,
|
||||
author_name: c.author_name,
|
||||
author_url: c.author_url,
|
||||
provider_name: c.provider_name,
|
||||
provider_url: c.provider_url,
|
||||
html: c.html,
|
||||
width: c.width,
|
||||
height: c.height,
|
||||
embed_url: null,
|
||||
blurhash: c.blurhash
|
||||
})
|
||||
export const context = (c: Entity.Context): MegalodonEntity.Context => ({
|
||||
ancestors: Array.isArray(c.ancestors) ? c.ancestors.map(a => status(a)) : [],
|
||||
descendants: Array.isArray(c.descendants) ? c.descendants.map(d => status(d)) : []
|
||||
})
|
||||
export const conversation = (c: Entity.Conversation): MegalodonEntity.Conversation => ({
|
||||
id: c.id,
|
||||
accounts: Array.isArray(c.accounts) ? c.accounts.map(a => account(a)) : [],
|
||||
last_status: c.last_status ? status(c.last_status) : null,
|
||||
unread: c.unread
|
||||
})
|
||||
export const emoji = (e: Entity.Emoji): MegalodonEntity.Emoji => ({
|
||||
shortcode: e.shortcode,
|
||||
static_url: e.static_url,
|
||||
url: e.url,
|
||||
visible_in_picker: e.visible_in_picker
|
||||
})
|
||||
export const featured_tag = (e: Entity.FeaturedTag): MegalodonEntity.FeaturedTag => e
|
||||
export const field = (f: Entity.Field): MegalodonEntity.Field => f
|
||||
export const filter = (f: Entity.Filter): MegalodonEntity.Filter => f
|
||||
export const follow_request = (f: Entity.FollowRequest): MegalodonEntity.FollowRequest => ({
|
||||
id: f.id,
|
||||
username: f.username,
|
||||
acct: f.acct,
|
||||
display_name: f.display_name,
|
||||
locked: f.locked,
|
||||
bot: f.bot,
|
||||
discoverable: f.discoverable,
|
||||
group: f.group,
|
||||
created_at: f.created_at,
|
||||
note: f.note,
|
||||
url: f.url,
|
||||
avatar: f.avatar,
|
||||
avatar_static: f.avatar_static,
|
||||
header: f.header,
|
||||
header_static: f.header_static,
|
||||
followers_count: f.followers_count,
|
||||
following_count: f.following_count,
|
||||
statuses_count: f.statuses_count,
|
||||
emojis: f.emojis.map(e => emoji(e)),
|
||||
fields: f.fields.map(f => field(f))
|
||||
})
|
||||
export const history = (h: Entity.History): MegalodonEntity.History => h
|
||||
export const identity_proof = (i: Entity.IdentityProof): MegalodonEntity.IdentityProof => i
|
||||
export const instance = (i: Entity.Instance): MegalodonEntity.Instance => {
|
||||
return {
|
||||
uri: i.uri,
|
||||
title: i.title,
|
||||
description: i.description,
|
||||
email: i.email,
|
||||
version: i.version,
|
||||
thumbnail: i.thumbnail,
|
||||
urls: i.urls ? urls(i.urls) : null,
|
||||
stats: stats(i.stats),
|
||||
languages: i.languages,
|
||||
registrations: i.registrations,
|
||||
approval_required: i.approval_required,
|
||||
invites_enabled: i.invites_enabled,
|
||||
configuration: {
|
||||
statuses: {
|
||||
max_characters: i.max_toot_chars
|
||||
}
|
||||
},
|
||||
contact_account: account(i.contact_account),
|
||||
rules: i.rules
|
||||
}
|
||||
}
|
||||
export const list = (l: Entity.List): MegalodonEntity.List => l
|
||||
export const marker = (m: Entity.Marker): MegalodonEntity.Marker => m
|
||||
export const mention = (m: Entity.Mention): MegalodonEntity.Mention => m
|
||||
export const notification = (n: Entity.Notification): MegalodonEntity.Notification | UnknownNotificationTypeError => {
|
||||
const notificationType = decodeNotificationType(n.type)
|
||||
if (notificationType instanceof UnknownNotificationTypeError) return notificationType
|
||||
if (n.status) {
|
||||
return {
|
||||
account: account(n.account),
|
||||
created_at: n.created_at,
|
||||
id: n.id,
|
||||
status: status(n.status),
|
||||
type: notificationType
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
account: account(n.account),
|
||||
created_at: n.created_at,
|
||||
id: n.id,
|
||||
type: notificationType
|
||||
}
|
||||
}
|
||||
}
|
||||
export const poll = (p: Entity.Poll): MegalodonEntity.Poll => p
|
||||
export const poll_option = (p: Entity.PollOption): MegalodonEntity.PollOption => p
|
||||
export const preferences = (p: Entity.Preferences): MegalodonEntity.Preferences => p
|
||||
export const push_subscription = (p: Entity.PushSubscription): MegalodonEntity.PushSubscription => p
|
||||
export const relationship = (r: Entity.Relationship): MegalodonEntity.Relationship => r
|
||||
export const report = (r: Entity.Report): MegalodonEntity.Report => ({
|
||||
id: r.id,
|
||||
action_taken: r.action_taken,
|
||||
action_taken_at: null,
|
||||
category: r.category,
|
||||
comment: r.comment,
|
||||
forwarded: r.forwarded,
|
||||
status_ids: r.status_ids,
|
||||
rule_ids: r.rule_ids,
|
||||
target_account: account(r.target_account)
|
||||
})
|
||||
export const results = (r: Entity.Results): MegalodonEntity.Results => ({
|
||||
accounts: Array.isArray(r.accounts) ? r.accounts.map(a => account(a)) : [],
|
||||
statuses: Array.isArray(r.statuses) ? r.statuses.map(s => status(s)) : [],
|
||||
hashtags: Array.isArray(r.hashtags) ? r.hashtags.map(h => tag(h)) : []
|
||||
})
|
||||
export const scheduled_status = (s: Entity.ScheduledStatus): MegalodonEntity.ScheduledStatus => {
|
||||
return {
|
||||
id: s.id,
|
||||
scheduled_at: s.scheduled_at,
|
||||
params: status_params(s.params),
|
||||
media_attachments: s.media_attachments ? s.media_attachments.map(a => attachment(a)) : null
|
||||
}
|
||||
}
|
||||
export const source = (s: Entity.Source): MegalodonEntity.Source => s
|
||||
export const stats = (s: Entity.Stats): MegalodonEntity.Stats => s
|
||||
export const status = (s: Entity.Status): MegalodonEntity.Status => ({
|
||||
id: s.id,
|
||||
uri: s.uri,
|
||||
url: s.url,
|
||||
account: account(s.account),
|
||||
in_reply_to_id: s.in_reply_to_id,
|
||||
in_reply_to_account_id: s.in_reply_to_account_id,
|
||||
reblog: s.reblog ? status(s.reblog) : s.quote ? status(s.quote) : null,
|
||||
content: s.content,
|
||||
plain_content: null,
|
||||
created_at: s.created_at,
|
||||
edited_at: s.edited_at || null,
|
||||
emojis: Array.isArray(s.emojis) ? s.emojis.map(e => emoji(e)) : [],
|
||||
replies_count: s.replies_count,
|
||||
reblogs_count: s.reblogs_count,
|
||||
favourites_count: s.favourites_count,
|
||||
reblogged: s.reblogged,
|
||||
favourited: s.favourited,
|
||||
muted: s.muted,
|
||||
sensitive: s.sensitive,
|
||||
spoiler_text: s.spoiler_text,
|
||||
visibility: s.visibility,
|
||||
media_attachments: Array.isArray(s.media_attachments) ? s.media_attachments.map(m => attachment(m)) : [],
|
||||
mentions: Array.isArray(s.mentions) ? s.mentions.map(m => mention(m)) : [],
|
||||
tags: s.tags,
|
||||
card: s.card ? card(s.card) : null,
|
||||
poll: s.poll ? poll(s.poll) : null,
|
||||
application: s.application ? application(s.application) : null,
|
||||
language: s.language,
|
||||
pinned: s.pinned,
|
||||
emoji_reactions: [],
|
||||
bookmarked: s.bookmarked ? s.bookmarked : false,
|
||||
quote: false
|
||||
})
|
||||
export const status_params = (s: Entity.StatusParams): MegalodonEntity.StatusParams => {
|
||||
return {
|
||||
text: s.text,
|
||||
in_reply_to_id: s.in_reply_to_id,
|
||||
media_ids: s.media_ids,
|
||||
sensitive: s.sensitive,
|
||||
spoiler_text: s.spoiler_text,
|
||||
visibility: s.visibility,
|
||||
scheduled_at: s.scheduled_at,
|
||||
application_id: parseInt(s.application_id)
|
||||
}
|
||||
}
|
||||
export const status_source = (s: Entity.StatusSource): MegalodonEntity.StatusSource => s
|
||||
export const tag = (t: Entity.Tag): MegalodonEntity.Tag => t
|
||||
export const token = (t: Entity.Token): MegalodonEntity.Token => t
|
||||
export const urls = (u: Entity.URLs): MegalodonEntity.URLs => u
|
||||
}
|
||||
}
|
||||
export default FriendicaAPI
|
|
@ -1,29 +0,0 @@
|
|||
/// <reference path="emoji.ts" />
|
||||
/// <reference path="source.ts" />
|
||||
/// <reference path="field.ts" />
|
||||
namespace FriendicaEntity {
|
||||
export type Account = {
|
||||
id: string
|
||||
username: string
|
||||
acct: string
|
||||
display_name: string
|
||||
locked: boolean
|
||||
discoverable?: boolean
|
||||
group: boolean | null
|
||||
created_at: string
|
||||
followers_count: number
|
||||
following_count: number
|
||||
statuses_count: number
|
||||
note: string
|
||||
url: string
|
||||
avatar: string
|
||||
avatar_static: string
|
||||
header: string
|
||||
header_static: string
|
||||
emojis: Array<Emoji>
|
||||
moved: Account | null
|
||||
fields: Array<Field>
|
||||
bot: boolean
|
||||
source?: Source
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Activity = {
|
||||
week: string
|
||||
statuses: string
|
||||
logins: string
|
||||
registrations: string
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Application = {
|
||||
name: string
|
||||
website?: string | null
|
||||
vapid_key?: string | null
|
||||
}
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
/// <reference path="attachment.ts" />
|
||||
namespace FriendicaEntity {
|
||||
export type AsyncAttachment = {
|
||||
id: string
|
||||
type: 'unknown' | 'image' | 'gifv' | 'video' | 'audio'
|
||||
url: string | null
|
||||
remote_url: string | null
|
||||
preview_url: string
|
||||
text_url: string | null
|
||||
meta: Meta | null
|
||||
description: string | null
|
||||
blurhash: string | null
|
||||
}
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Sub = {
|
||||
// For Image, Gifv, and Video
|
||||
width?: number
|
||||
height?: number
|
||||
size?: string
|
||||
aspect?: number
|
||||
|
||||
// For Gifv and Video
|
||||
frame_rate?: string
|
||||
|
||||
// For Audio, Gifv, and Video
|
||||
duration?: number
|
||||
bitrate?: number
|
||||
}
|
||||
|
||||
export type Focus = {
|
||||
x: number
|
||||
y: number
|
||||
}
|
||||
|
||||
export type Meta = {
|
||||
original?: Sub
|
||||
small?: Sub
|
||||
focus?: Focus
|
||||
length?: string
|
||||
duration?: number
|
||||
fps?: number
|
||||
size?: string
|
||||
width?: number
|
||||
height?: number
|
||||
aspect?: number
|
||||
audio_encode?: string
|
||||
audio_bitrate?: string
|
||||
audio_channel?: string
|
||||
}
|
||||
|
||||
export type Attachment = {
|
||||
id: string
|
||||
type: 'unknown' | 'image' | 'gifv' | 'video' | 'audio'
|
||||
url: string
|
||||
remote_url: string | null
|
||||
preview_url: string | null
|
||||
text_url: string | null
|
||||
meta: Meta | null
|
||||
description: string | null
|
||||
blurhash: string | null
|
||||
}
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Card = {
|
||||
url: string
|
||||
title: string
|
||||
description: string
|
||||
type: 'link' | 'photo' | 'video' | 'rich'
|
||||
image: string | null
|
||||
author_name: string
|
||||
author_url: string
|
||||
provider_name: string
|
||||
provider_url: string
|
||||
html: string
|
||||
width: number
|
||||
height: number
|
||||
blurhash: string | null
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
/// <reference path="status.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type Context = {
|
||||
ancestors: Array<Status>
|
||||
descendants: Array<Status>
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
/// <reference path="account.ts" />
|
||||
/// <reference path="status.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type Conversation = {
|
||||
id: string
|
||||
accounts: Array<Account>
|
||||
last_status: Status | null
|
||||
unread: boolean
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Emoji = {
|
||||
shortcode: string
|
||||
static_url: string
|
||||
url: string
|
||||
visible_in_picker: boolean
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type FeaturedTag = {
|
||||
id: string
|
||||
name: string
|
||||
statuses_count: number
|
||||
last_status_at: string
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Field = {
|
||||
name: string
|
||||
value: string
|
||||
verified_at: string | null
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Filter = {
|
||||
id: string
|
||||
phrase: string
|
||||
context: Array<FilterContext>
|
||||
expires_at: string | null
|
||||
irreversible: boolean
|
||||
whole_word: boolean
|
||||
}
|
||||
|
||||
export type FilterContext = string
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
/// <reference path="emoji.ts" />
|
||||
/// <reference path="field.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type FollowRequest = {
|
||||
id: number
|
||||
username: string
|
||||
acct: string
|
||||
display_name: string
|
||||
locked: boolean
|
||||
bot: boolean
|
||||
discoverable?: boolean
|
||||
group: boolean
|
||||
created_at: string
|
||||
note: string
|
||||
url: string
|
||||
avatar: string
|
||||
avatar_static: string
|
||||
header: string
|
||||
header_static: string
|
||||
followers_count: number
|
||||
following_count: number
|
||||
statuses_count: number
|
||||
emojis: Array<Emoji>
|
||||
fields: Array<Field>
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type History = {
|
||||
day: string
|
||||
uses: number
|
||||
accounts: number
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type IdentityProof = {
|
||||
provider: string
|
||||
provider_username: string
|
||||
updated_at: string
|
||||
proof_url: string
|
||||
profile_url: string
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
/// <reference path="account.ts" />
|
||||
/// <reference path="urls.ts" />
|
||||
/// <reference path="stats.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type Instance = {
|
||||
uri: string
|
||||
title: string
|
||||
description: string
|
||||
email: string
|
||||
version: string
|
||||
thumbnail: string | null
|
||||
urls: URLs | null
|
||||
stats: Stats
|
||||
languages: Array<string>
|
||||
registrations: boolean
|
||||
approval_required: boolean
|
||||
invites_enabled: boolean
|
||||
max_toot_chars: number
|
||||
contact_account: Account
|
||||
rules: Array<InstanceRule>
|
||||
}
|
||||
|
||||
export type InstanceRule = {
|
||||
id: string
|
||||
text: string
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type List = {
|
||||
id: string
|
||||
title: string
|
||||
replies_policy: RepliesPolicy
|
||||
}
|
||||
|
||||
export type RepliesPolicy = 'followed' | 'list' | 'none'
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Marker = {
|
||||
home: {
|
||||
last_read_id: string
|
||||
version: number
|
||||
updated_at: string
|
||||
}
|
||||
notifications: {
|
||||
last_read_id: string
|
||||
version: number
|
||||
updated_at: string
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Mention = {
|
||||
id: string
|
||||
username: string
|
||||
url: string
|
||||
acct: string
|
||||
}
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
/// <reference path="account.ts" />
|
||||
/// <reference path="status.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type Notification = {
|
||||
account: Account
|
||||
created_at: string
|
||||
id: string
|
||||
status?: Status
|
||||
type: NotificationType
|
||||
}
|
||||
|
||||
export type NotificationType = string
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
/// <reference path="poll_option.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type Poll = {
|
||||
id: string
|
||||
expires_at: string | null
|
||||
expired: boolean
|
||||
multiple: boolean
|
||||
votes_count: number
|
||||
options: Array<PollOption>
|
||||
voted: boolean
|
||||
}
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type PollOption = {
|
||||
title: string
|
||||
votes_count: number | null
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Preferences = {
|
||||
'posting:default:visibility': 'public' | 'unlisted' | 'private' | 'direct'
|
||||
'posting:default:sensitive': boolean
|
||||
'posting:default:language': string | null
|
||||
'reading:expand:media': 'default' | 'show_all' | 'hide_all'
|
||||
'reading:expand:spoilers': boolean
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Alerts = {
|
||||
follow: boolean
|
||||
favourite: boolean
|
||||
mention: boolean
|
||||
reblog: boolean
|
||||
poll: boolean
|
||||
}
|
||||
|
||||
export type PushSubscription = {
|
||||
id: string
|
||||
endpoint: string
|
||||
server_key: string
|
||||
alerts: Alerts
|
||||
}
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Relationship = {
|
||||
id: string
|
||||
following: boolean
|
||||
followed_by: boolean
|
||||
blocking: boolean
|
||||
blocked_by: boolean
|
||||
muting: boolean
|
||||
muting_notifications: boolean
|
||||
requested: boolean
|
||||
domain_blocking: boolean
|
||||
showing_reblogs: boolean
|
||||
endorsed: boolean
|
||||
notifying: boolean
|
||||
note: string | null
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
/// <reference path="account.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type Report = {
|
||||
id: string
|
||||
action_taken: boolean
|
||||
category: Category
|
||||
comment: string
|
||||
forwarded: boolean
|
||||
status_ids: Array<string> | null
|
||||
rule_ids: Array<string> | null
|
||||
target_account: Account
|
||||
}
|
||||
|
||||
export type Category = 'spam' | 'violation' | 'other'
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
/// <reference path="account.ts" />
|
||||
/// <reference path="status.ts" />
|
||||
/// <reference path="tag.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type Results = {
|
||||
accounts: Array<Account>
|
||||
statuses: Array<Status>
|
||||
hashtags: Array<Tag>
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
/// <reference path="attachment.ts" />
|
||||
/// <reference path="status_params.ts" />
|
||||
namespace FriendicaEntity {
|
||||
export type ScheduledStatus = {
|
||||
id: string
|
||||
scheduled_at: string
|
||||
params: StatusParams
|
||||
media_attachments: Array<Attachment>
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
/// <reference path="field.ts" />
|
||||
namespace FriendicaEntity {
|
||||
export type Source = {
|
||||
privacy: string | null
|
||||
sensitive: boolean | null
|
||||
language: string | null
|
||||
note: string
|
||||
fields: Array<Field>
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Stats = {
|
||||
user_count: number
|
||||
status_count: number
|
||||
domain_count: number
|
||||
}
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
/// <reference path="account.ts" />
|
||||
/// <reference path="application.ts" />
|
||||
/// <reference path="mention.ts" />
|
||||
/// <reference path="attachment.ts" />
|
||||
/// <reference path="emoji.ts" />
|
||||
/// <reference path="card.ts" />
|
||||
/// <reference path="poll.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type Status = {
|
||||
id: string
|
||||
uri: string
|
||||
url: string
|
||||
account: Account
|
||||
in_reply_to_id: string | null
|
||||
in_reply_to_account_id: string | null
|
||||
reblog: Status | null
|
||||
content: string
|
||||
created_at: string
|
||||
edited_at?: string | null
|
||||
emojis: Emoji[]
|
||||
replies_count: number
|
||||
reblogs_count: number
|
||||
favourites_count: number
|
||||
reblogged: boolean | null
|
||||
favourited: boolean | null
|
||||
muted: boolean | null
|
||||
sensitive: boolean
|
||||
spoiler_text: string
|
||||
visibility: 'public' | 'unlisted' | 'private' | 'direct'
|
||||
media_attachments: Array<Attachment>
|
||||
mentions: Array<Mention>
|
||||
tags: Array<StatusTag>
|
||||
card: Card | null
|
||||
poll: Poll | null
|
||||
application: Application | null
|
||||
language: string | null
|
||||
pinned: boolean | null
|
||||
bookmarked?: boolean
|
||||
// These parameters are unique parameters in fedibird.com for quote.
|
||||
quote_id?: string
|
||||
quote?: Status | null
|
||||
}
|
||||
|
||||
export type StatusTag = {
|
||||
name: string
|
||||
url: string
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type StatusParams = {
|
||||
text: string
|
||||
in_reply_to_id: string | null
|
||||
media_ids: Array<string> | null
|
||||
sensitive: boolean | null
|
||||
spoiler_text: string | null
|
||||
visibility: 'public' | 'unlisted' | 'private' | null
|
||||
scheduled_at: string | null
|
||||
application_id: string
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type StatusSource = {
|
||||
id: string
|
||||
text: string
|
||||
spoiler_text: string
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
/// <reference path="history.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type Tag = {
|
||||
name: string
|
||||
url: string
|
||||
history: Array<History>
|
||||
following?: boolean
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Token = {
|
||||
access_token: string
|
||||
token_type: string
|
||||
scope: string
|
||||
created_at: number
|
||||
}
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type URLs = {
|
||||
streaming_api: string
|
||||
}
|
||||
}
|
|
@ -1,38 +0,0 @@
|
|||
/// <reference path="./entities/account.ts" />
|
||||
/// <reference path="./entities/activity.ts" />
|
||||
/// <reference path="./entities/application.ts" />
|
||||
/// <reference path="./entities/async_attachment.ts" />
|
||||
/// <reference path="./entities/attachment.ts" />
|
||||
/// <reference path="./entities/card.ts" />
|
||||
/// <reference path="./entities/context.ts" />
|
||||
/// <reference path="./entities/conversation.ts" />
|
||||
/// <reference path="./entities/emoji.ts" />
|
||||
/// <reference path="./entities/featured_tag.ts" />
|
||||
/// <reference path="./entities/field.ts" />
|
||||
/// <reference path="./entities/filter.ts" />
|
||||
/// <reference path="./entities/follow_request.ts" />
|
||||
/// <reference path="./entities/history.ts" />
|
||||
/// <reference path="./entities/identity_proof.ts" />
|
||||
/// <reference path="./entities/instance.ts" />
|
||||
/// <reference path="./entities/list.ts" />
|
||||
/// <reference path="./entities/marker.ts" />
|
||||
/// <reference path="./entities/mention.ts" />
|
||||
/// <reference path="./entities/notification.ts" />
|
||||
/// <reference path="./entities/poll.ts" />
|
||||
/// <reference path="./entities/poll_option.ts" />
|
||||
/// <reference path="./entities/preferences.ts" />
|
||||
/// <reference path="./entities/push_subscription.ts" />
|
||||
/// <reference path="./entities/relationship.ts" />
|
||||
/// <reference path="./entities/report.ts" />
|
||||
/// <reference path="./entities/results.ts" />
|
||||
/// <reference path="./entities/scheduled_status.ts" />
|
||||
/// <reference path="./entities/source.ts" />
|
||||
/// <reference path="./entities/stats.ts" />
|
||||
/// <reference path="./entities/status.ts" />
|
||||
/// <reference path="./entities/status_params.ts" />
|
||||
/// <reference path="./entities/status_source.ts" />
|
||||
/// <reference path="./entities/tag.ts" />
|
||||
/// <reference path="./entities/token.ts" />
|
||||
/// <reference path="./entities/urls.ts" />
|
||||
|
||||
export default FriendicaEntity
|
|
@ -1,14 +0,0 @@
|
|||
import FriendicaEntity from './entity'
|
||||
|
||||
namespace FriendicaNotificationType {
|
||||
export const Mention: FriendicaEntity.NotificationType = 'mention'
|
||||
export const Reblog: FriendicaEntity.NotificationType = 'reblog'
|
||||
export const Favourite: FriendicaEntity.NotificationType = 'favourite'
|
||||
export const Follow: FriendicaEntity.NotificationType = 'follow'
|
||||
export const Poll: FriendicaEntity.NotificationType = 'poll'
|
||||
export const FollowRequest: FriendicaEntity.NotificationType = 'follow_request'
|
||||
export const Status: FriendicaEntity.NotificationType = 'status'
|
||||
export const Update: FriendicaEntity.NotificationType = 'update'
|
||||
}
|
||||
|
||||
export default FriendicaNotificationType
|
|
@ -1,18 +0,0 @@
|
|||
import { WebSocketInterface } from '../megalodon'
|
||||
import { EventEmitter } from 'events'
|
||||
import { ProxyConfig } from '../proxy_config'
|
||||
|
||||
export default class WebSocket extends EventEmitter implements WebSocketInterface {
|
||||
constructor(
|
||||
_url: string,
|
||||
_stream: string,
|
||||
_params: string | undefined,
|
||||
_accessToken: string,
|
||||
_userAgent: string,
|
||||
_proxyConfig: ProxyConfig | false = false
|
||||
) {
|
||||
super()
|
||||
}
|
||||
public start() {}
|
||||
public stop() {}
|
||||
}
|
|
@ -2,15 +2,14 @@ import Response from './response'
|
|||
import OAuth from './oauth'
|
||||
import { isCancel, RequestCanceledError } from './cancel'
|
||||
import { ProxyConfig } from './proxy_config'
|
||||
import generator, { MegalodonInterface, WebSocketInterface } from './megalodon'
|
||||
import { MegalodonInterface, WebSocketInterface } from './megalodon'
|
||||
import { detector } from './detector'
|
||||
import Mastodon from './mastodon'
|
||||
import Pleroma from './pleroma'
|
||||
import Misskey from './misskey'
|
||||
import Entity from './entity'
|
||||
import NotificationType from './notification'
|
||||
import * as NotificationType from './notification'
|
||||
import FilterContext from './filter_context'
|
||||
import Converter from './converter'
|
||||
import MastodonEntity from './mastodon/entity';
|
||||
|
||||
export {
|
||||
Response,
|
||||
|
@ -23,14 +22,8 @@ export {
|
|||
WebSocketInterface,
|
||||
NotificationType,
|
||||
FilterContext,
|
||||
Mastodon,
|
||||
Pleroma,
|
||||
Misskey,
|
||||
Entity,
|
||||
Converter,
|
||||
generator,
|
||||
MastodonEntity,
|
||||
}
|
||||
|
||||
export const megalodon = generator;
|
||||
|
||||
export default generator
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,662 +0,0 @@
|
|||
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'
|
||||
import objectAssignDeep from 'object-assign-deep'
|
||||
|
||||
import WebSocket from './web_socket'
|
||||
import Response from '../response'
|
||||
import { RequestCanceledError } from '../cancel'
|
||||
import proxyAgent, { ProxyConfig } from '../proxy_config'
|
||||
import { NO_REDIRECT, DEFAULT_SCOPE, DEFAULT_UA } from '../default'
|
||||
import MastodonEntity from './entity'
|
||||
import MegalodonEntity from '../entity'
|
||||
import NotificationType, { UnknownNotificationTypeError } from '../notification'
|
||||
import MastodonNotificationType from './notification'
|
||||
|
||||
namespace MastodonAPI {
|
||||
/**
|
||||
* Interface
|
||||
*/
|
||||
export interface Interface {
|
||||
get<T = any>(path: string, params?: any, headers?: { [key: string]: string }, pathIsFullyQualified?: boolean): Promise<Response<T>>
|
||||
put<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
putForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
patch<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
patchForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
post<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
postForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
del<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
cancel(): void
|
||||
socket(path: string, stream: string, params?: string): WebSocket
|
||||
}
|
||||
|
||||
/**
|
||||
* Mastodon API client.
|
||||
*
|
||||
* Using axios for request, you will handle promises.
|
||||
*/
|
||||
export class Client implements Interface {
|
||||
static DEFAULT_SCOPE = DEFAULT_SCOPE
|
||||
static DEFAULT_URL = 'https://mastodon.social'
|
||||
static NO_REDIRECT = NO_REDIRECT
|
||||
|
||||
private accessToken: string | null
|
||||
private baseUrl: string
|
||||
private userAgent: string
|
||||
private abortController: AbortController
|
||||
private proxyConfig: ProxyConfig | false = false
|
||||
|
||||
/**
|
||||
* @param baseUrl hostname or base URL
|
||||
* @param accessToken access token from OAuth2 authorization
|
||||
* @param userAgent UserAgent is specified in header on request.
|
||||
* @param proxyConfig Proxy setting, or set false if don't use proxy.
|
||||
*/
|
||||
constructor(
|
||||
baseUrl: string,
|
||||
accessToken: string | null = null,
|
||||
userAgent: string = DEFAULT_UA,
|
||||
proxyConfig: ProxyConfig | false = false
|
||||
) {
|
||||
this.accessToken = accessToken
|
||||
this.baseUrl = baseUrl
|
||||
this.userAgent = userAgent
|
||||
this.proxyConfig = proxyConfig
|
||||
this.abortController = new AbortController()
|
||||
axios.defaults.signal = this.abortController.signal
|
||||
}
|
||||
|
||||
/**
|
||||
* GET request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Query parameters
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async get<T>(
|
||||
path: string,
|
||||
params = {},
|
||||
headers: { [key: string]: string } = {},
|
||||
pathIsFullyQualified = false
|
||||
): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
params: params,
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.get<T>((pathIsFullyQualified ? '' : this.baseUrl) + path, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async put<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.put<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT request to mastodon REST API for multipart.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async putForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.putForm<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async patch<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.patch<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH request to mastodon REST API for multipart.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async patchForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.patchForm<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* POST request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async post<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios.post<T>(this.baseUrl + path, params, options).then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* POST request to mastodon REST API for multipart.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async postForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios.postForm<T>(this.baseUrl + path, params, options).then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async del<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
data: params,
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.delete(this.baseUrl + path, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel all requests in this instance.
|
||||
* @returns void
|
||||
*/
|
||||
public cancel(): void {
|
||||
return this.abortController.abort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get connection and receive websocket connection for Pleroma API.
|
||||
*
|
||||
* @param path relative path from baseUrl: normally it is `/streaming`.
|
||||
* @param stream Stream name, please refer: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/mastodon_api/mastodon_socket.ex#L19-28
|
||||
* @returns WebSocket, which inherits from EventEmitter
|
||||
*/
|
||||
public socket(path: string, stream: string, params?: string): WebSocket {
|
||||
if (!this.accessToken) {
|
||||
throw new Error('accessToken is required')
|
||||
}
|
||||
const url = this.baseUrl + path
|
||||
const streaming = new WebSocket(url, stream, params, this.accessToken, this.userAgent, this.proxyConfig)
|
||||
process.nextTick(() => {
|
||||
streaming.start()
|
||||
})
|
||||
return streaming
|
||||
}
|
||||
}
|
||||
|
||||
export namespace Entity {
|
||||
export type Account = MastodonEntity.Account
|
||||
export type Activity = MastodonEntity.Activity
|
||||
export type Announcement = MastodonEntity.Announcement
|
||||
export type Application = MastodonEntity.Application
|
||||
export type AsyncAttachment = MegalodonEntity.AsyncAttachment
|
||||
export type Attachment = MastodonEntity.Attachment
|
||||
export type Card = MastodonEntity.Card
|
||||
export type Context = MastodonEntity.Context
|
||||
export type Conversation = MastodonEntity.Conversation
|
||||
export type Emoji = MastodonEntity.Emoji
|
||||
export type FeaturedTag = MastodonEntity.FeaturedTag
|
||||
export type Field = MastodonEntity.Field
|
||||
export type Filter = MastodonEntity.Filter
|
||||
export type History = MastodonEntity.History
|
||||
export type IdentityProof = MastodonEntity.IdentityProof
|
||||
export type Instance = MastodonEntity.Instance
|
||||
export type List = MastodonEntity.List
|
||||
export type Marker = MastodonEntity.Marker
|
||||
export type Mention = MastodonEntity.Mention
|
||||
export type Notification = MastodonEntity.Notification
|
||||
export type Poll = MastodonEntity.Poll
|
||||
export type PollOption = MastodonEntity.PollOption
|
||||
export type Preferences = MastodonEntity.Preferences
|
||||
export type PushSubscription = MastodonEntity.PushSubscription
|
||||
export type Relationship = MastodonEntity.Relationship
|
||||
export type Report = MastodonEntity.Report
|
||||
export type Results = MastodonEntity.Results
|
||||
export type Role = MastodonEntity.Role
|
||||
export type ScheduledStatus = MastodonEntity.ScheduledStatus
|
||||
export type Source = MastodonEntity.Source
|
||||
export type Stats = MastodonEntity.Stats
|
||||
export type Status = MastodonEntity.Status
|
||||
export type StatusParams = MastodonEntity.StatusParams
|
||||
export type StatusSource = MastodonEntity.StatusSource
|
||||
export type Tag = MastodonEntity.Tag
|
||||
export type Token = MastodonEntity.Token
|
||||
export type URLs = MastodonEntity.URLs
|
||||
}
|
||||
|
||||
export namespace Converter {
|
||||
export const encodeNotificationType = (
|
||||
t: MegalodonEntity.NotificationType
|
||||
): MastodonEntity.NotificationType | UnknownNotificationTypeError => {
|
||||
switch (t) {
|
||||
case NotificationType.Follow:
|
||||
return MastodonNotificationType.Follow
|
||||
case NotificationType.Favourite:
|
||||
return MastodonNotificationType.Favourite
|
||||
case NotificationType.Reblog:
|
||||
return MastodonNotificationType.Reblog
|
||||
case NotificationType.Mention:
|
||||
return MastodonNotificationType.Mention
|
||||
case NotificationType.FollowRequest:
|
||||
return MastodonNotificationType.FollowRequest
|
||||
case NotificationType.Status:
|
||||
return MastodonNotificationType.Status
|
||||
case NotificationType.PollExpired:
|
||||
return MastodonNotificationType.Poll
|
||||
case NotificationType.Update:
|
||||
return MastodonNotificationType.Update
|
||||
case NotificationType.AdminSignup:
|
||||
return MastodonNotificationType.AdminSignup
|
||||
case NotificationType.AdminReport:
|
||||
return MastodonNotificationType.AdminReport
|
||||
default:
|
||||
return new UnknownNotificationTypeError()
|
||||
}
|
||||
}
|
||||
|
||||
export const decodeNotificationType = (
|
||||
t: MastodonEntity.NotificationType
|
||||
): MegalodonEntity.NotificationType | UnknownNotificationTypeError => {
|
||||
switch (t) {
|
||||
case MastodonNotificationType.Follow:
|
||||
return NotificationType.Follow
|
||||
case MastodonNotificationType.Favourite:
|
||||
return NotificationType.Favourite
|
||||
case MastodonNotificationType.Mention:
|
||||
return NotificationType.Mention
|
||||
case MastodonNotificationType.Reblog:
|
||||
return NotificationType.Reblog
|
||||
case MastodonNotificationType.FollowRequest:
|
||||
return NotificationType.FollowRequest
|
||||
case MastodonNotificationType.Status:
|
||||
return NotificationType.Status
|
||||
case MastodonNotificationType.Poll:
|
||||
return NotificationType.PollExpired
|
||||
case MastodonNotificationType.Update:
|
||||
return NotificationType.Update
|
||||
case MastodonNotificationType.AdminSignup:
|
||||
return NotificationType.AdminSignup
|
||||
case MastodonNotificationType.AdminReport:
|
||||
return NotificationType.AdminReport
|
||||
default:
|
||||
return new UnknownNotificationTypeError()
|
||||
}
|
||||
}
|
||||
|
||||
export const account = (a: Entity.Account): MegalodonEntity.Account => a
|
||||
export const activity = (a: Entity.Activity): MegalodonEntity.Activity => a
|
||||
export const announcement = (a: Entity.Announcement): MegalodonEntity.Announcement => a
|
||||
export const application = (a: Entity.Application): MegalodonEntity.Application => a
|
||||
export const attachment = (a: Entity.Attachment): MegalodonEntity.Attachment => a
|
||||
export const async_attachment = (a: Entity.AsyncAttachment) => {
|
||||
if (a.url) {
|
||||
return {
|
||||
id: a.id,
|
||||
type: a.type,
|
||||
url: a.url!,
|
||||
remote_url: a.remote_url,
|
||||
preview_url: a.preview_url,
|
||||
text_url: a.text_url,
|
||||
meta: a.meta,
|
||||
description: a.description,
|
||||
blurhash: a.blurhash
|
||||
} as MegalodonEntity.Attachment
|
||||
} else {
|
||||
return a as MegalodonEntity.AsyncAttachment
|
||||
}
|
||||
}
|
||||
export const card = (c: Entity.Card): MegalodonEntity.Card => c
|
||||
export const context = (c: Entity.Context): MegalodonEntity.Context => ({
|
||||
ancestors: Array.isArray(c.ancestors) ? c.ancestors.map(a => status(a)) : [],
|
||||
descendants: Array.isArray(c.descendants) ? c.descendants.map(d => status(d)) : []
|
||||
})
|
||||
export const conversation = (c: Entity.Conversation): MegalodonEntity.Conversation => ({
|
||||
id: c.id,
|
||||
accounts: Array.isArray(c.accounts) ? c.accounts.map(a => account(a)) : [],
|
||||
last_status: c.last_status ? status(c.last_status) : null,
|
||||
unread: c.unread
|
||||
})
|
||||
export const emoji = (e: Entity.Emoji): MegalodonEntity.Emoji => e
|
||||
export const featured_tag = (e: Entity.FeaturedTag): MegalodonEntity.FeaturedTag => e
|
||||
export const field = (f: Entity.Field): MegalodonEntity.Field => f
|
||||
export const filter = (f: Entity.Filter): MegalodonEntity.Filter => f
|
||||
export const history = (h: Entity.History): MegalodonEntity.History => h
|
||||
export const identity_proof = (i: Entity.IdentityProof): MegalodonEntity.IdentityProof => i
|
||||
export const instance = (i: Entity.Instance): MegalodonEntity.Instance => i
|
||||
export const list = (l: Entity.List): MegalodonEntity.List => l
|
||||
export const marker = (m: Entity.Marker | Record<never, never>): MegalodonEntity.Marker | Record<never, never> => m
|
||||
export const mention = (m: Entity.Mention): MegalodonEntity.Mention => m
|
||||
export const notification = (n: Entity.Notification): MegalodonEntity.Notification | UnknownNotificationTypeError => {
|
||||
const notificationType = decodeNotificationType(n.type)
|
||||
if (notificationType instanceof UnknownNotificationTypeError) return notificationType
|
||||
if (n.status) {
|
||||
return {
|
||||
account: account(n.account),
|
||||
created_at: n.created_at,
|
||||
id: n.id,
|
||||
status: status(n.status),
|
||||
type: notificationType
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
account: account(n.account),
|
||||
created_at: n.created_at,
|
||||
id: n.id,
|
||||
type: notificationType
|
||||
}
|
||||
}
|
||||
}
|
||||
export const poll = (p: Entity.Poll): MegalodonEntity.Poll => p
|
||||
export const poll_option = (p: Entity.PollOption): MegalodonEntity.PollOption => p
|
||||
export const preferences = (p: Entity.Preferences): MegalodonEntity.Preferences => p
|
||||
export const push_subscription = (p: Entity.PushSubscription): MegalodonEntity.PushSubscription => p
|
||||
export const relationship = (r: Entity.Relationship): MegalodonEntity.Relationship => r
|
||||
export const report = (r: Entity.Report): MegalodonEntity.Report => r
|
||||
export const results = (r: Entity.Results): MegalodonEntity.Results => ({
|
||||
accounts: Array.isArray(r.accounts) ? r.accounts.map(a => account(a)) : [],
|
||||
statuses: Array.isArray(r.statuses) ? r.statuses.map(s => status(s)) : [],
|
||||
hashtags: Array.isArray(r.hashtags) ? r.hashtags.map(h => tag(h)) : []
|
||||
})
|
||||
export const scheduled_status = (s: Entity.ScheduledStatus): MegalodonEntity.ScheduledStatus => s
|
||||
export const source = (s: Entity.Source): MegalodonEntity.Source => s
|
||||
export const stats = (s: Entity.Stats): MegalodonEntity.Stats => s
|
||||
export const status = (s: Entity.Status): MegalodonEntity.Status => ({
|
||||
id: s.id,
|
||||
uri: s.uri,
|
||||
url: s.url,
|
||||
account: account(s.account),
|
||||
in_reply_to_id: s.in_reply_to_id,
|
||||
in_reply_to_account_id: s.in_reply_to_account_id,
|
||||
reblog: s.reblog ? status(s.reblog) : s.quote ? status(s.quote) : null,
|
||||
content: s.content,
|
||||
plain_content: null,
|
||||
created_at: s.created_at,
|
||||
edited_at: s.edited_at || null,
|
||||
emojis: Array.isArray(s.emojis) ? s.emojis.map(e => emoji(e)) : [],
|
||||
replies_count: s.replies_count,
|
||||
reblogs_count: s.reblogs_count,
|
||||
favourites_count: s.favourites_count,
|
||||
reblogged: s.reblogged,
|
||||
favourited: s.favourited,
|
||||
muted: s.muted,
|
||||
sensitive: s.sensitive,
|
||||
spoiler_text: s.spoiler_text,
|
||||
visibility: s.visibility,
|
||||
media_attachments: Array.isArray(s.media_attachments) ? s.media_attachments.map(m => attachment(m)) : [],
|
||||
mentions: Array.isArray(s.mentions) ? s.mentions.map(m => mention(m)) : [],
|
||||
tags: s.tags,
|
||||
card: s.card ? card(s.card) : null,
|
||||
poll: s.poll ? poll(s.poll) : null,
|
||||
application: s.application ? application(s.application) : null,
|
||||
language: s.language,
|
||||
pinned: s.pinned,
|
||||
emoji_reactions: [],
|
||||
bookmarked: s.bookmarked ? s.bookmarked : false,
|
||||
// Now quote is supported only fedibird.com.
|
||||
quote: s.quote !== undefined && s.quote !== null
|
||||
})
|
||||
export const status_params = (s: Entity.StatusParams): MegalodonEntity.StatusParams => s
|
||||
export const status_source = (s: Entity.StatusSource): MegalodonEntity.StatusSource => s
|
||||
export const tag = (t: Entity.Tag): MegalodonEntity.Tag => t
|
||||
export const token = (t: Entity.Token): MegalodonEntity.Token => t
|
||||
export const urls = (u: Entity.URLs): MegalodonEntity.URLs => u
|
||||
}
|
||||
}
|
||||
export default MastodonAPI
|
|
@ -37,8 +37,15 @@ namespace MastodonEntity {
|
|||
min_expiration: number
|
||||
max_expiration: number
|
||||
}
|
||||
accounts: {
|
||||
max_featured_tags: number;
|
||||
max_pinned_statuses: number;
|
||||
}
|
||||
reactions: {
|
||||
max_reactions: number,
|
||||
}
|
||||
}
|
||||
contact_account: Account
|
||||
contact_account: Account | null
|
||||
rules: Array<InstanceRule>
|
||||
}
|
||||
|
||||
|
|
16
packages/megalodon/src/mastodon/entities/reaction.ts
Normal file
16
packages/megalodon/src/mastodon/entities/reaction.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
/// <reference path="account.ts" />
|
||||
|
||||
namespace MastodonEntity {
|
||||
export type Reaction = {
|
||||
name: string
|
||||
count: number
|
||||
me?: boolean
|
||||
url?: string
|
||||
static_url?: string
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@
|
|||
/// <reference path="emoji.ts" />
|
||||
/// <reference path="card.ts" />
|
||||
/// <reference path="poll.ts" />
|
||||
/// <reference path="reaction.ts" />
|
||||
|
||||
namespace MastodonEntity {
|
||||
export type Status = {
|
||||
|
@ -41,6 +42,8 @@ namespace MastodonEntity {
|
|||
// These parameters are unique parameters in fedibird.com for quote.
|
||||
quote_id?: string
|
||||
quote?: Status | null
|
||||
// These parameters are unique to glitch-soc for emoji reactions.
|
||||
reactions?: Reaction[]
|
||||
}
|
||||
|
||||
export type StatusTag = {
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
/// <reference path="./entities/poll_option.ts" />
|
||||
/// <reference path="./entities/preferences.ts" />
|
||||
/// <reference path="./entities/push_subscription.ts" />
|
||||
/// <reference path="./entities/reaction.ts" />
|
||||
/// <reference path="./entities/relationship.ts" />
|
||||
/// <reference path="./entities/report.ts" />
|
||||
/// <reference path="./entities/results.ts" />
|
||||
|
|
|
@ -1,16 +1,33 @@
|
|||
import MastodonEntity from './entity'
|
||||
export const Mention = 'mention' as const;
|
||||
export const Reblog = 'reblog' as const;
|
||||
export const Favourite = 'favourite' as const;
|
||||
export const Follow = 'follow' as const;
|
||||
export const Poll = 'poll' as const;
|
||||
export const FollowRequest = 'follow_request' as const;
|
||||
export const Status = 'status' as const;
|
||||
export const Update = 'update' as const;
|
||||
export const AdminSignup = 'admin.sign_up' as const;
|
||||
export const AdminReport = 'admin.report' as const;
|
||||
export const Reaction = 'reaction' as const;
|
||||
export const ModerationWarning = 'moderation_warning' as const;
|
||||
export const SeveredRelationships = 'severed_relationships' as const;
|
||||
export const AnnualReport = 'annual_report' as const;
|
||||
|
||||
namespace MastodonNotificationType {
|
||||
export const Mention: MastodonEntity.NotificationType = 'mention'
|
||||
export const Reblog: MastodonEntity.NotificationType = 'reblog'
|
||||
export const Favourite: MastodonEntity.NotificationType = 'favourite'
|
||||
export const Follow: MastodonEntity.NotificationType = 'follow'
|
||||
export const Poll: MastodonEntity.NotificationType = 'poll'
|
||||
export const FollowRequest: MastodonEntity.NotificationType = 'follow_request'
|
||||
export const Status: MastodonEntity.NotificationType = 'status'
|
||||
export const Update: MastodonEntity.NotificationType = 'update'
|
||||
export const AdminSignup: MastodonEntity.NotificationType = 'admin.sign_up'
|
||||
export const AdminReport: MastodonEntity.NotificationType = 'admin.report'
|
||||
}
|
||||
export const mastodonNotificationTypes = [
|
||||
Mention,
|
||||
Reblog,
|
||||
Favourite,
|
||||
Follow,
|
||||
Poll,
|
||||
FollowRequest,
|
||||
Status,
|
||||
Update,
|
||||
AdminSignup,
|
||||
AdminReport,
|
||||
Reaction,
|
||||
ModerationWarning,
|
||||
SeveredRelationships,
|
||||
AnnualReport,
|
||||
];
|
||||
|
||||
export default MastodonNotificationType
|
||||
export type MastodonNotificationType = typeof mastodonNotificationTypes[number];
|
||||
|
|
|
@ -1,348 +0,0 @@
|
|||
import WS from 'ws'
|
||||
import dayjs, { Dayjs } from 'dayjs'
|
||||
import { EventEmitter } from 'events'
|
||||
import proxyAgent, { ProxyConfig } from '../proxy_config'
|
||||
import { WebSocketInterface } from '../megalodon'
|
||||
import MastodonAPI from './api_client'
|
||||
import { UnknownNotificationTypeError } from '../notification'
|
||||
|
||||
/**
|
||||
* WebSocket
|
||||
* Pleroma is not support streaming. It is support websocket instead of streaming.
|
||||
* So this class connect to Phoenix websocket for Pleroma.
|
||||
*/
|
||||
export default class WebSocket extends EventEmitter implements WebSocketInterface {
|
||||
public url: string
|
||||
public stream: string
|
||||
public params: string | null
|
||||
public parser: Parser
|
||||
public headers: { [key: string]: string }
|
||||
public proxyConfig: ProxyConfig | false = false
|
||||
private _accessToken: string
|
||||
private _reconnectInterval: number
|
||||
private _reconnectMaxAttempts: number
|
||||
private _reconnectCurrentAttempts: number
|
||||
private _connectionClosed: boolean
|
||||
private _client: WS | null
|
||||
private _pongReceivedTimestamp: Dayjs
|
||||
private _heartbeatInterval: number = 60000
|
||||
private _pongWaiting: boolean = false
|
||||
|
||||
/**
|
||||
* @param url Full url of websocket: e.g. https://pleroma.io/api/v1/streaming
|
||||
* @param stream Stream name, please refer: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/mastodon_api/mastodon_socket.ex#L19-28
|
||||
* @param accessToken The access token.
|
||||
* @param userAgent The specified User Agent.
|
||||
* @param proxyConfig Proxy setting, or set false if don't use proxy.
|
||||
*/
|
||||
constructor(
|
||||
url: string,
|
||||
stream: string,
|
||||
params: string | undefined,
|
||||
accessToken: string,
|
||||
userAgent: string,
|
||||
proxyConfig: ProxyConfig | false = false
|
||||
) {
|
||||
super()
|
||||
this.url = url
|
||||
this.stream = stream
|
||||
if (params === undefined) {
|
||||
this.params = null
|
||||
} else {
|
||||
this.params = params
|
||||
}
|
||||
this.parser = new Parser()
|
||||
this.headers = {
|
||||
'User-Agent': userAgent
|
||||
}
|
||||
this.proxyConfig = proxyConfig
|
||||
this._accessToken = accessToken
|
||||
this._reconnectInterval = 10000
|
||||
this._reconnectMaxAttempts = Infinity
|
||||
this._reconnectCurrentAttempts = 0
|
||||
this._connectionClosed = false
|
||||
this._client = null
|
||||
this._pongReceivedTimestamp = dayjs()
|
||||
}
|
||||
|
||||
/**
|
||||
* Start websocket connection.
|
||||
*/
|
||||
public start() {
|
||||
this._connectionClosed = false
|
||||
this._resetRetryParams()
|
||||
this._startWebSocketConnection()
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset connection and start new websocket connection.
|
||||
*/
|
||||
private _startWebSocketConnection() {
|
||||
this._resetConnection()
|
||||
this._setupParser()
|
||||
this._client = this._connect(this.url, this.stream, this.params, this._accessToken, this.headers, this.proxyConfig)
|
||||
this._bindSocket(this._client)
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop current connection.
|
||||
*/
|
||||
public stop() {
|
||||
this._connectionClosed = true
|
||||
this._resetConnection()
|
||||
this._resetRetryParams()
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up current connection, and listeners.
|
||||
*/
|
||||
private _resetConnection() {
|
||||
if (this._client) {
|
||||
this._client.close(1000)
|
||||
this._client.removeAllListeners()
|
||||
this._client = null
|
||||
}
|
||||
|
||||
if (this.parser) {
|
||||
this.parser.removeAllListeners()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the parameters used in reconnect.
|
||||
*/
|
||||
private _resetRetryParams() {
|
||||
this._reconnectCurrentAttempts = 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconnects to the same endpoint.
|
||||
*/
|
||||
private _reconnect() {
|
||||
setTimeout(() => {
|
||||
// Skip reconnect when client is connecting.
|
||||
// https://github.com/websockets/ws/blob/7.2.1/lib/websocket.js#L365
|
||||
if (this._client && this._client.readyState === WS.CONNECTING) {
|
||||
return
|
||||
}
|
||||
|
||||
if (this._reconnectCurrentAttempts < this._reconnectMaxAttempts) {
|
||||
this._reconnectCurrentAttempts++
|
||||
this._clearBinding()
|
||||
if (this._client) {
|
||||
// In reconnect, we want to close the connection immediately,
|
||||
// because recoonect is necessary when some problems occur.
|
||||
this._client.terminate()
|
||||
}
|
||||
// Call connect methods
|
||||
console.log('Reconnecting')
|
||||
this._client = this._connect(this.url, this.stream, this.params, this._accessToken, this.headers, this.proxyConfig)
|
||||
this._bindSocket(this._client)
|
||||
}
|
||||
}, this._reconnectInterval)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param url Base url of streaming endpoint.
|
||||
* @param stream The specified stream name.
|
||||
* @param accessToken Access token.
|
||||
* @param headers The specified headers.
|
||||
* @param proxyConfig Proxy setting, or set false if don't use proxy.
|
||||
* @return A WebSocket instance.
|
||||
*/
|
||||
private _connect(
|
||||
url: string,
|
||||
stream: string,
|
||||
params: string | null,
|
||||
accessToken: string,
|
||||
headers: { [key: string]: string },
|
||||
proxyConfig: ProxyConfig | false
|
||||
): WS {
|
||||
const parameter: Array<string> = [`stream=${stream}`]
|
||||
|
||||
if (params) {
|
||||
parameter.push(params)
|
||||
}
|
||||
|
||||
if (accessToken !== null) {
|
||||
parameter.push(`access_token=${accessToken}`)
|
||||
}
|
||||
const requestURL: string = `${url}/?${parameter.join('&')}`
|
||||
let options: WS.ClientOptions = {
|
||||
headers: headers
|
||||
}
|
||||
if (proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
agent: proxyAgent(proxyConfig)
|
||||
})
|
||||
}
|
||||
|
||||
const cli: WS = new WS(requestURL, options)
|
||||
return cli
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear binding event for web socket client.
|
||||
*/
|
||||
private _clearBinding() {
|
||||
if (this._client) {
|
||||
this._client.removeAllListeners('close')
|
||||
this._client.removeAllListeners('pong')
|
||||
this._client.removeAllListeners('open')
|
||||
this._client.removeAllListeners('message')
|
||||
this._client.removeAllListeners('error')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind event for web socket client.
|
||||
* @param client A WebSocket instance.
|
||||
*/
|
||||
private _bindSocket(client: WS) {
|
||||
client.on('close', (code: number, _reason: Buffer) => {
|
||||
// Refer the code: https://tools.ietf.org/html/rfc6455#section-7.4
|
||||
if (code === 1000) {
|
||||
this.emit('close', {})
|
||||
} else {
|
||||
console.log(`Closed connection with ${code}`)
|
||||
// If already called close method, it does not retry.
|
||||
if (!this._connectionClosed) {
|
||||
this._reconnect()
|
||||
}
|
||||
}
|
||||
})
|
||||
client.on('pong', () => {
|
||||
this._pongWaiting = false
|
||||
this.emit('pong', {})
|
||||
this._pongReceivedTimestamp = dayjs()
|
||||
// It is required to anonymous function since get this scope in checkAlive.
|
||||
setTimeout(() => this._checkAlive(this._pongReceivedTimestamp), this._heartbeatInterval)
|
||||
})
|
||||
client.on('open', () => {
|
||||
this.emit('connect', {})
|
||||
// Call first ping event.
|
||||
setTimeout(() => {
|
||||
client.ping('')
|
||||
}, 10000)
|
||||
})
|
||||
client.on('message', (data: WS.Data, isBinary: boolean) => {
|
||||
this.parser.parse(data, isBinary)
|
||||
})
|
||||
client.on('error', (err: Error) => {
|
||||
this.emit('error', err)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up parser when receive message.
|
||||
*/
|
||||
private _setupParser() {
|
||||
this.parser.on('update', (status: MastodonAPI.Entity.Status) => {
|
||||
this.emit('update', MastodonAPI.Converter.status(status))
|
||||
})
|
||||
this.parser.on('notification', (notification: MastodonAPI.Entity.Notification) => {
|
||||
const n = MastodonAPI.Converter.notification(notification)
|
||||
if (n instanceof UnknownNotificationTypeError) {
|
||||
console.warn(`Unknown notification event has received: ${notification}`)
|
||||
} else {
|
||||
this.emit('notification', n)
|
||||
}
|
||||
})
|
||||
this.parser.on('delete', (id: string) => {
|
||||
this.emit('delete', id)
|
||||
})
|
||||
this.parser.on('conversation', (conversation: MastodonAPI.Entity.Conversation) => {
|
||||
this.emit('conversation', MastodonAPI.Converter.conversation(conversation))
|
||||
})
|
||||
this.parser.on('status_update', (status: MastodonAPI.Entity.Status) => {
|
||||
this.emit('status_update', MastodonAPI.Converter.status(status))
|
||||
})
|
||||
this.parser.on('error', (err: Error) => {
|
||||
this.emit('parser-error', err)
|
||||
})
|
||||
this.parser.on('heartbeat', _ => {
|
||||
this.emit('heartbeat', 'heartbeat')
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Call ping and wait to pong.
|
||||
*/
|
||||
private _checkAlive(timestamp: Dayjs) {
|
||||
const now: Dayjs = dayjs()
|
||||
// Block multiple calling, if multiple pong event occur.
|
||||
// It the duration is less than interval, through ping.
|
||||
if (now.diff(timestamp) > this._heartbeatInterval - 1000 && !this._connectionClosed) {
|
||||
// Skip ping when client is connecting.
|
||||
// https://github.com/websockets/ws/blob/7.2.1/lib/websocket.js#L289
|
||||
if (this._client && this._client.readyState !== WS.CONNECTING) {
|
||||
this._pongWaiting = true
|
||||
this._client.ping('')
|
||||
setTimeout(() => {
|
||||
if (this._pongWaiting) {
|
||||
this._pongWaiting = false
|
||||
this._reconnect()
|
||||
}
|
||||
}, 10000)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parser
|
||||
* This class provides parser for websocket message.
|
||||
*/
|
||||
export class Parser extends EventEmitter {
|
||||
/**
|
||||
* @param message Message body of websocket.
|
||||
*/
|
||||
public parse(data: WS.Data, isBinary: boolean) {
|
||||
const message = isBinary ? data : data.toString()
|
||||
if (typeof message !== 'string') {
|
||||
this.emit('heartbeat', {})
|
||||
return
|
||||
}
|
||||
|
||||
if (message === '') {
|
||||
this.emit('heartbeat', {})
|
||||
return
|
||||
}
|
||||
|
||||
let event = ''
|
||||
let payload = ''
|
||||
let mes = {}
|
||||
try {
|
||||
const obj = JSON.parse(message)
|
||||
event = obj.event
|
||||
payload = obj.payload
|
||||
mes = JSON.parse(payload)
|
||||
} catch (err) {
|
||||
// delete event does not have json object
|
||||
if (event !== 'delete') {
|
||||
this.emit('error', new Error(`Error parsing websocket reply: ${message}, error message: ${err}`))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
switch (event) {
|
||||
case 'update':
|
||||
this.emit('update', mes as MastodonAPI.Entity.Status)
|
||||
break
|
||||
case 'notification':
|
||||
this.emit('notification', mes as MastodonAPI.Entity.Notification)
|
||||
break
|
||||
case 'conversation':
|
||||
this.emit('conversation', mes as MastodonAPI.Entity.Conversation)
|
||||
break
|
||||
case 'delete':
|
||||
this.emit('delete', payload)
|
||||
break
|
||||
case 'status.update':
|
||||
this.emit('status_update', mes as MastodonAPI.Entity.Status)
|
||||
break
|
||||
default:
|
||||
this.emit('error', new Error(`Unknown event has received: ${message}`))
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,11 +1,6 @@
|
|||
import Response from './response'
|
||||
import OAuth from './oauth'
|
||||
import Pleroma from './pleroma'
|
||||
import { ProxyConfig } from './proxy_config'
|
||||
import Mastodon from './mastodon'
|
||||
import Entity from './entity'
|
||||
import Misskey from './misskey'
|
||||
import Friendica from './friendica'
|
||||
|
||||
export interface WebSocketInterface {
|
||||
start(): void
|
||||
|
@ -347,7 +342,7 @@ export interface MegalodonInterface {
|
|||
* @param ids Array of account IDs.
|
||||
* @return Array of Relationship.
|
||||
*/
|
||||
getRelationships(ids: Array<string>): Promise<Response<Array<Entity.Relationship>>>
|
||||
getRelationships(ids: string | Array<string>): Promise<Response<Array<Entity.Relationship>>>
|
||||
/**
|
||||
* Search for matching accounts by username or display name.
|
||||
*
|
||||
|
@ -1413,42 +1408,3 @@ export class NodeinfoError extends Error {
|
|||
Object.setPrototypeOf(this, new.target.prototype)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get client for each SNS according to megalodon interface.
|
||||
*
|
||||
* @param sns Name of your SNS, `mastodon` or `pleroma`.
|
||||
* @param baseUrl hostname or base URL.
|
||||
* @param accessToken access token from OAuth2 authorization
|
||||
* @param userAgent UserAgent is specified in header on request.
|
||||
* @param proxyConfig Proxy setting, or set false if don't use proxy.
|
||||
* @return Client instance for each SNS you specified.
|
||||
*/
|
||||
const generator = (
|
||||
sns: 'mastodon' | 'pleroma' | 'misskey' | 'friendica',
|
||||
baseUrl: string,
|
||||
accessToken: string | null = null,
|
||||
userAgent: string | null = null,
|
||||
proxyConfig: ProxyConfig | false = false
|
||||
): MegalodonInterface => {
|
||||
switch (sns) {
|
||||
case 'pleroma': {
|
||||
const pleroma = new Pleroma(baseUrl, accessToken, userAgent, proxyConfig)
|
||||
return pleroma
|
||||
}
|
||||
case 'misskey': {
|
||||
const misskey = new Misskey(baseUrl, accessToken, userAgent, proxyConfig)
|
||||
return misskey
|
||||
}
|
||||
case 'friendica': {
|
||||
const friendica = new Friendica(baseUrl, accessToken, userAgent, proxyConfig)
|
||||
return friendica
|
||||
}
|
||||
case 'mastodon': {
|
||||
const mastodon = new Mastodon(baseUrl, accessToken, userAgent, proxyConfig)
|
||||
return mastodon
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default generator
|
||||
|
|
|
@ -303,8 +303,8 @@ export default class Misskey implements MegalodonInterface {
|
|||
max_id?: string
|
||||
since_id?: string
|
||||
pinned?: boolean
|
||||
exclude_replies: boolean
|
||||
exclude_reblogs: boolean
|
||||
exclude_replies?: boolean
|
||||
exclude_reblogs?: boolean
|
||||
only_media?: boolean
|
||||
}
|
||||
): Promise<Response<Array<Entity.Status>>> {
|
||||
|
@ -591,12 +591,12 @@ export default class Misskey implements MegalodonInterface {
|
|||
*/
|
||||
public async getRelationship(id: string): Promise<Response<Entity.Relationship>> {
|
||||
return this.client
|
||||
.post<MisskeyAPI.Entity.Relation>('/api/users/relation', {
|
||||
.post<MisskeyAPI.Entity.Relation[]>('/api/users/relation', {
|
||||
userId: id
|
||||
})
|
||||
.then(res => {
|
||||
return Object.assign(res, {
|
||||
data: MisskeyAPI.Converter.relation(res.data)
|
||||
data: MisskeyAPI.Converter.relation(res.data[0])
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -606,11 +606,16 @@ export default class Misskey implements MegalodonInterface {
|
|||
*
|
||||
* @param ids Array of account ID, for example `['1sdfag', 'ds12aa']`.
|
||||
*/
|
||||
public async getRelationships(ids: Array<string>): Promise<Response<Array<Entity.Relationship>>> {
|
||||
return Promise.all(ids.map(id => this.getRelationship(id))).then(results => ({
|
||||
...results[0],
|
||||
data: results.map(r => r.data)
|
||||
}))
|
||||
public async getRelationships(ids: string | Array<string>): Promise<Response<Array<Entity.Relationship>>> {
|
||||
return this.client
|
||||
.post<MisskeyAPI.Entity.Relation[]>('/api/users/relation', {
|
||||
userId: ids
|
||||
})
|
||||
.then(res => {
|
||||
return Object.assign(res, {
|
||||
data: res.data.map(r => MisskeyAPI.Converter.relation(r))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -652,48 +657,82 @@ export default class Misskey implements MegalodonInterface {
|
|||
// ======================================
|
||||
// accounts/bookmarks
|
||||
// ======================================
|
||||
public async getBookmarks(_options?: {
|
||||
/**
|
||||
* POST /api/i/favorites
|
||||
*/
|
||||
public async getBookmarks(options?: {
|
||||
limit?: number
|
||||
max_id?: string
|
||||
since_id?: string
|
||||
min_id?: string
|
||||
}): Promise<Response<Array<Entity.Status>>> {
|
||||
return new Promise((_, reject) => {
|
||||
const err = new NoImplementedError('misskey does not support')
|
||||
reject(err)
|
||||
})
|
||||
let params = {}
|
||||
if (options) {
|
||||
if (options.limit) {
|
||||
params = Object.assign(params, {
|
||||
limit: options.limit
|
||||
})
|
||||
}
|
||||
if (options.max_id) {
|
||||
params = Object.assign(params, {
|
||||
untilId: options.max_id
|
||||
})
|
||||
}
|
||||
if (options.min_id) {
|
||||
params = Object.assign(params, {
|
||||
sinceId: options.min_id
|
||||
})
|
||||
}
|
||||
}
|
||||
return this.client.post<Array<MisskeyAPI.Entity.Favorite>>('/api/i/favorites', params).then(res => {
|
||||
return Object.assign(res, {
|
||||
data: res.data.map(fav => MisskeyAPI.Converter.note(fav.note, this.baseUrl))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/users/reactions
|
||||
*/
|
||||
public async getReactions(userId: string, options?: { limit?: number; max_id?: string; min_id?: string }): Promise<Response<MisskeyEntity.NoteReaction[]>> {
|
||||
let params = {
|
||||
userId,
|
||||
};
|
||||
if (options) {
|
||||
if (options.limit) {
|
||||
params = Object.assign(params, {
|
||||
limit: options.limit
|
||||
})
|
||||
}
|
||||
if (options.max_id) {
|
||||
params = Object.assign(params, {
|
||||
untilId: options.max_id
|
||||
})
|
||||
}
|
||||
if (options.min_id) {
|
||||
params = Object.assign(params, {
|
||||
sinceId: options.min_id
|
||||
})
|
||||
}
|
||||
}
|
||||
return this.client.post<MisskeyAPI.Entity.NoteReaction[]>('/api/users/reactions', params);
|
||||
}
|
||||
|
||||
// ======================================
|
||||
// accounts/favourites
|
||||
// ======================================
|
||||
/**
|
||||
* POST /api/i/favorites
|
||||
* POST /api/users/reactions
|
||||
*/
|
||||
public async getFavourites(options?: { limit?: number; max_id?: string; min_id?: string }): Promise<Response<Array<Entity.Status>>> {
|
||||
let params = {}
|
||||
if (options) {
|
||||
if (options.limit) {
|
||||
params = Object.assign(params, {
|
||||
limit: options.limit
|
||||
})
|
||||
}
|
||||
if (options.max_id) {
|
||||
params = Object.assign(params, {
|
||||
untilId: options.max_id
|
||||
})
|
||||
}
|
||||
if (options.min_id) {
|
||||
params = Object.assign(params, {
|
||||
sinceId: options.min_id
|
||||
})
|
||||
}
|
||||
}
|
||||
return this.client.post<Array<MisskeyAPI.Entity.Favorite>>('/api/i/favorites', params).then(res => {
|
||||
return Object.assign(res, {
|
||||
data: res.data.map(fav => MisskeyAPI.Converter.note(fav.note, this.baseUrl))
|
||||
})
|
||||
})
|
||||
public async getFavourites(options?: { limit?: number; max_id?: string; min_id?: string; userId?: string }): Promise<Response<Array<Entity.Status>>> {
|
||||
const userId = options?.userId ?? (await this.verifyAccountCredentials()).data.id;
|
||||
|
||||
const response = await this.getReactions(userId, options);
|
||||
|
||||
return {
|
||||
...response,
|
||||
data: response.data.map(r => MisskeyAPI.Converter.note(r.note, this.baseUrl)),
|
||||
};
|
||||
}
|
||||
|
||||
// ======================================
|
||||
|
@ -2352,6 +2391,18 @@ export default class Misskey implements MegalodonInterface {
|
|||
}
|
||||
}))
|
||||
}
|
||||
default: {
|
||||
return {
|
||||
status: 400,
|
||||
statusText: 'bad request',
|
||||
headers: {},
|
||||
data: {
|
||||
accounts: [],
|
||||
statuses: [],
|
||||
hashtags: [],
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2504,6 +2555,7 @@ export default class Misskey implements MegalodonInterface {
|
|||
}))
|
||||
}
|
||||
|
||||
// TODO implement
|
||||
public async getEmojiReaction(_id: string, _emoji: string): Promise<Response<Entity.Reaction>> {
|
||||
return new Promise((_, reject) => {
|
||||
const err = new NoImplementedError('misskey does not support')
|
||||
|
|
|
@ -9,7 +9,8 @@ import MisskeyEntity from './entity'
|
|||
import MegalodonEntity from '../entity'
|
||||
import WebSocket from './web_socket'
|
||||
import MisskeyNotificationType from './notification'
|
||||
import NotificationType, { UnknownNotificationTypeError } from '../notification'
|
||||
import * as NotificationType from '../notification'
|
||||
import { UnknownNotificationTypeError } from '../notification';
|
||||
|
||||
namespace MisskeyAPI {
|
||||
export namespace Entity {
|
||||
|
@ -32,6 +33,7 @@ namespace MisskeyAPI {
|
|||
export type Notification = MisskeyEntity.Notification
|
||||
export type Poll = MisskeyEntity.Poll
|
||||
export type Reaction = MisskeyEntity.Reaction
|
||||
export type NoteReaction = MisskeyEntity.NoteReaction
|
||||
export type Relation = MisskeyEntity.Relation
|
||||
export type User = MisskeyEntity.User
|
||||
export type UserDetail = MisskeyEntity.UserDetail
|
||||
|
@ -285,6 +287,7 @@ namespace MisskeyAPI {
|
|||
plain_content: n.text ? n.text : null,
|
||||
created_at: n.createdAt,
|
||||
edited_at: n.updatedAt || null,
|
||||
// TODO this is probably wrong
|
||||
emojis: mapEmojis(n.emojis).concat(mapReactionEmojis(n.reactionEmojis)),
|
||||
replies_count: n.repliesCount,
|
||||
reblogs_count: n.renoteCount,
|
||||
|
@ -303,7 +306,7 @@ namespace MisskeyAPI {
|
|||
application: null,
|
||||
language: null,
|
||||
pinned: null,
|
||||
emoji_reactions: typeof n.reactions === 'object' ? mapReactions(n.reactions, n.myReaction) : [],
|
||||
emoji_reactions: typeof n.reactions === 'object' ? mapReactions(n.reactions, n.reactionEmojis, n.myReaction) : [],
|
||||
bookmarked: false,
|
||||
quote: n.renote && n.text ? note(n.renote, n.user.host ? n.user.host : host ? host : null) : null
|
||||
}
|
||||
|
@ -333,23 +336,37 @@ namespace MisskeyAPI {
|
|||
) : 0;
|
||||
};
|
||||
|
||||
export const mapReactions = (r: { [key: string]: number }, myReaction?: string): Array<MegalodonEntity.Reaction> => {
|
||||
return Object.keys(r).map(key => {
|
||||
if (myReaction && key === myReaction) {
|
||||
return {
|
||||
count: r[key],
|
||||
me: true,
|
||||
name: key
|
||||
}
|
||||
}
|
||||
return {
|
||||
count: r[key],
|
||||
me: false,
|
||||
name: key
|
||||
}
|
||||
export const mapReactions = (r: { [key: string]: number }, e: Record<string, string | undefined>, myReaction?: string): Array<MegalodonEntity.Reaction> => {
|
||||
return Object.entries(r).map(([key, count]) => {
|
||||
const me = myReaction != null && key === myReaction;
|
||||
|
||||
// Name is equal to the key for native emoji reactions, and as a fallback.
|
||||
let name = key;
|
||||
|
||||
// Custom emoji have a leading / trailing ":", which we need to remove.
|
||||
const match = key.match(/^:([^@:]+)(@[^:]+)?:$/);
|
||||
if (match) {
|
||||
const [, prefix, host] = match;
|
||||
|
||||
// Local custom emoji end in "@.", which we need to remove.
|
||||
if (host && host !== '@.') {
|
||||
name = prefix + host;
|
||||
} else {
|
||||
name = prefix;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
count,
|
||||
me,
|
||||
name,
|
||||
url: e[name],
|
||||
static_url: e[name],
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TODO implement other properties
|
||||
const mapReactionEmojis = (r: { [key: string]: string }): Array<MegalodonEntity.Emoji> => {
|
||||
return Object.keys(r).map(key => ({
|
||||
shortcode: key,
|
||||
|
@ -370,7 +387,7 @@ namespace MisskeyAPI {
|
|||
result.push({
|
||||
count: 1,
|
||||
me: false,
|
||||
name: e.type
|
||||
name: e.type,
|
||||
})
|
||||
}
|
||||
})
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
/// <reference path="user.ts" />
|
||||
/// <reference path="note.ts" />
|
||||
|
||||
namespace MisskeyEntity {
|
||||
export type Reaction = {
|
||||
|
@ -7,4 +8,8 @@ namespace MisskeyEntity {
|
|||
user: User
|
||||
type: string
|
||||
}
|
||||
|
||||
export type NoteReaction = Reaction & {
|
||||
note: Note
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,20 +1,16 @@
|
|||
import Entity from './entity'
|
||||
|
||||
namespace NotificationType {
|
||||
export const Follow: Entity.NotificationType = 'follow'
|
||||
export const Favourite: Entity.NotificationType = 'favourite'
|
||||
export const Reblog: Entity.NotificationType = 'reblog'
|
||||
export const Mention: Entity.NotificationType = 'mention'
|
||||
export const EmojiReaction: Entity.NotificationType = 'emoji_reaction'
|
||||
export const FollowRequest: Entity.NotificationType = 'follow_request'
|
||||
export const Status: Entity.NotificationType = 'status'
|
||||
export const PollVote: Entity.NotificationType = 'poll_vote'
|
||||
export const PollExpired: Entity.NotificationType = 'poll_expired'
|
||||
export const Update: Entity.NotificationType = 'update'
|
||||
export const Move: Entity.NotificationType = 'move'
|
||||
export const AdminSignup: Entity.NotificationType = 'admin.sign_up'
|
||||
export const AdminReport: Entity.NotificationType = 'admin.report'
|
||||
}
|
||||
export const Follow = 'follow' as const;
|
||||
export const Favourite = 'favourite' as const;
|
||||
export const Reblog = 'reblog' as const;
|
||||
export const Mention = 'mention' as const;
|
||||
export const EmojiReaction = 'emoji_reaction' as const;
|
||||
export const FollowRequest = 'follow_request' as const;
|
||||
export const Status = 'status' as const;
|
||||
export const PollVote = 'poll_vote' as const;
|
||||
export const PollExpired = 'poll_expired' as const;
|
||||
export const Update = 'update' as const;
|
||||
export const Move = 'move' as const;
|
||||
export const AdminSignup = 'admin.sign_up' as const;
|
||||
export const AdminReport = 'admin.report' as const;
|
||||
|
||||
export class UnknownNotificationTypeError extends Error {
|
||||
constructor() {
|
||||
|
@ -23,4 +19,20 @@ export class UnknownNotificationTypeError extends Error {
|
|||
}
|
||||
}
|
||||
|
||||
export default NotificationType
|
||||
export const notificationTypes = [
|
||||
Follow,
|
||||
Favourite,
|
||||
Reblog,
|
||||
Mention,
|
||||
EmojiReaction,
|
||||
FollowRequest,
|
||||
Status,
|
||||
PollVote,
|
||||
PollExpired,
|
||||
Update,
|
||||
Move,
|
||||
AdminSignup,
|
||||
AdminReport,
|
||||
];
|
||||
|
||||
export type NotificationType = typeof notificationTypes[number];
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,824 +0,0 @@
|
|||
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'
|
||||
import objectAssignDeep from 'object-assign-deep'
|
||||
|
||||
import MegalodonEntity from '../entity'
|
||||
import PleromaEntity from './entity'
|
||||
import Response from '../response'
|
||||
import { RequestCanceledError } from '../cancel'
|
||||
import proxyAgent, { ProxyConfig } from '../proxy_config'
|
||||
import { NO_REDIRECT, DEFAULT_SCOPE, DEFAULT_UA } from '../default'
|
||||
import WebSocket from './web_socket'
|
||||
import NotificationType, { UnknownNotificationTypeError } from '../notification'
|
||||
import PleromaNotificationType from './notification'
|
||||
|
||||
namespace PleromaAPI {
|
||||
export namespace Entity {
|
||||
export type Account = PleromaEntity.Account
|
||||
export type Activity = PleromaEntity.Activity
|
||||
export type Announcement = PleromaEntity.Announcement
|
||||
export type Application = PleromaEntity.Application
|
||||
export type AsyncAttachment = PleromaEntity.AsyncAttachment
|
||||
export type Attachment = PleromaEntity.Attachment
|
||||
export type Card = PleromaEntity.Card
|
||||
export type Context = PleromaEntity.Context
|
||||
export type Conversation = PleromaEntity.Conversation
|
||||
export type Emoji = PleromaEntity.Emoji
|
||||
export type FeaturedTag = PleromaEntity.FeaturedTag
|
||||
export type Field = PleromaEntity.Field
|
||||
export type Filter = PleromaEntity.Filter
|
||||
export type History = PleromaEntity.History
|
||||
export type IdentityProof = PleromaEntity.IdentityProof
|
||||
export type Instance = PleromaEntity.Instance
|
||||
export type List = PleromaEntity.List
|
||||
export type Marker = PleromaEntity.Marker
|
||||
export type Mention = PleromaEntity.Mention
|
||||
export type Notification = PleromaEntity.Notification
|
||||
export type Poll = PleromaEntity.Poll
|
||||
export type PollOption = PleromaEntity.PollOption
|
||||
export type Preferences = PleromaEntity.Preferences
|
||||
export type PushSubscription = PleromaEntity.PushSubscription
|
||||
export type Reaction = PleromaEntity.Reaction
|
||||
export type Relationship = PleromaEntity.Relationship
|
||||
export type Report = PleromaEntity.Report
|
||||
export type Results = PleromaEntity.Results
|
||||
export type ScheduledStatus = PleromaEntity.ScheduledStatus
|
||||
export type Source = PleromaEntity.Source
|
||||
export type Stats = PleromaEntity.Stats
|
||||
export type Status = PleromaEntity.Status
|
||||
export type StatusParams = PleromaEntity.StatusParams
|
||||
export type StatusSource = PleromaEntity.StatusSource
|
||||
export type Tag = PleromaEntity.Tag
|
||||
export type Token = PleromaEntity.Token
|
||||
export type URLs = PleromaEntity.URLs
|
||||
}
|
||||
|
||||
export namespace Converter {
|
||||
export const decodeNotificationType = (
|
||||
t: PleromaEntity.NotificationType
|
||||
): MegalodonEntity.NotificationType | UnknownNotificationTypeError => {
|
||||
switch (t) {
|
||||
case PleromaNotificationType.Mention:
|
||||
return NotificationType.Mention
|
||||
case PleromaNotificationType.Reblog:
|
||||
return NotificationType.Reblog
|
||||
case PleromaNotificationType.Favourite:
|
||||
return NotificationType.Favourite
|
||||
case PleromaNotificationType.Follow:
|
||||
return NotificationType.Follow
|
||||
case PleromaNotificationType.Poll:
|
||||
return NotificationType.PollExpired
|
||||
case PleromaNotificationType.PleromaEmojiReaction:
|
||||
return NotificationType.EmojiReaction
|
||||
case PleromaNotificationType.FollowRequest:
|
||||
return NotificationType.FollowRequest
|
||||
case PleromaNotificationType.Update:
|
||||
return NotificationType.Update
|
||||
case PleromaNotificationType.Move:
|
||||
return NotificationType.Move
|
||||
default:
|
||||
return new UnknownNotificationTypeError()
|
||||
}
|
||||
}
|
||||
export const encodeNotificationType = (
|
||||
t: MegalodonEntity.NotificationType
|
||||
): PleromaEntity.NotificationType | UnknownNotificationTypeError => {
|
||||
switch (t) {
|
||||
case NotificationType.Follow:
|
||||
return PleromaNotificationType.Follow
|
||||
case NotificationType.Favourite:
|
||||
return PleromaNotificationType.Favourite
|
||||
case NotificationType.Reblog:
|
||||
return PleromaNotificationType.Reblog
|
||||
case NotificationType.Mention:
|
||||
return PleromaNotificationType.Mention
|
||||
case NotificationType.PollExpired:
|
||||
return PleromaNotificationType.Poll
|
||||
case NotificationType.EmojiReaction:
|
||||
return PleromaNotificationType.PleromaEmojiReaction
|
||||
case NotificationType.FollowRequest:
|
||||
return PleromaNotificationType.FollowRequest
|
||||
case NotificationType.Update:
|
||||
return PleromaNotificationType.Update
|
||||
case NotificationType.Move:
|
||||
return PleromaNotificationType.Move
|
||||
default:
|
||||
return new UnknownNotificationTypeError()
|
||||
}
|
||||
}
|
||||
|
||||
export const account = (a: Entity.Account): MegalodonEntity.Account => {
|
||||
return {
|
||||
id: a.id,
|
||||
username: a.username,
|
||||
acct: a.acct,
|
||||
display_name: a.display_name,
|
||||
locked: a.locked,
|
||||
discoverable: a.discoverable,
|
||||
group: null,
|
||||
noindex: a.noindex,
|
||||
suspended: a.suspended,
|
||||
limited: a.limited,
|
||||
created_at: a.created_at,
|
||||
followers_count: a.followers_count,
|
||||
following_count: a.following_count,
|
||||
statuses_count: a.statuses_count,
|
||||
note: a.note,
|
||||
url: a.url,
|
||||
avatar: a.avatar,
|
||||
avatar_static: a.avatar_static,
|
||||
header: a.header,
|
||||
header_static: a.header_static,
|
||||
emojis: a.emojis.map(e => emoji(e)),
|
||||
moved: a.moved ? account(a.moved) : null,
|
||||
fields: a.fields,
|
||||
bot: a.bot,
|
||||
source: a.source
|
||||
}
|
||||
}
|
||||
export const activity = (a: Entity.Activity): MegalodonEntity.Activity => a
|
||||
export const announcement = (a: Entity.Announcement): MegalodonEntity.Announcement => ({
|
||||
id: a.id,
|
||||
content: a.content,
|
||||
starts_at: a.starts_at,
|
||||
ends_at: a.ends_at,
|
||||
published: a.published,
|
||||
all_day: a.all_day,
|
||||
published_at: a.published_at,
|
||||
updated_at: a.updated_at,
|
||||
read: null,
|
||||
mentions: a.mentions,
|
||||
statuses: a.statuses,
|
||||
tags: a.tags,
|
||||
emojis: a.emojis,
|
||||
reactions: a.reactions
|
||||
})
|
||||
export const application = (a: Entity.Application): MegalodonEntity.Application => a
|
||||
export const attachment = (a: Entity.Attachment): MegalodonEntity.Attachment => a
|
||||
export const async_attachment = (a: Entity.AsyncAttachment) => {
|
||||
if (a.url) {
|
||||
return {
|
||||
id: a.id,
|
||||
type: a.type,
|
||||
url: a.url!,
|
||||
remote_url: a.remote_url,
|
||||
preview_url: a.preview_url,
|
||||
text_url: a.text_url,
|
||||
meta: a.meta,
|
||||
description: a.description,
|
||||
blurhash: a.blurhash
|
||||
} as MegalodonEntity.Attachment
|
||||
} else {
|
||||
return a as MegalodonEntity.AsyncAttachment
|
||||
}
|
||||
}
|
||||
export const card = (c: Entity.Card): MegalodonEntity.Card => ({
|
||||
url: c.url,
|
||||
title: c.title,
|
||||
description: c.description,
|
||||
type: c.type,
|
||||
image: c.image,
|
||||
author_name: null,
|
||||
author_url: null,
|
||||
provider_name: c.provider_name,
|
||||
provider_url: c.provider_url,
|
||||
html: null,
|
||||
width: null,
|
||||
height: null,
|
||||
embed_url: null,
|
||||
blurhash: null
|
||||
})
|
||||
export const context = (c: Entity.Context): MegalodonEntity.Context => ({
|
||||
ancestors: Array.isArray(c.ancestors) ? c.ancestors.map(a => status(a)) : [],
|
||||
descendants: Array.isArray(c.descendants) ? c.descendants.map(d => status(d)) : []
|
||||
})
|
||||
export const conversation = (c: Entity.Conversation): MegalodonEntity.Conversation => ({
|
||||
id: c.id,
|
||||
accounts: Array.isArray(c.accounts) ? c.accounts.map(a => account(a)) : [],
|
||||
last_status: c.last_status ? status(c.last_status) : null,
|
||||
unread: c.unread
|
||||
})
|
||||
export const emoji = (e: Entity.Emoji): MegalodonEntity.Emoji => ({
|
||||
shortcode: e.shortcode,
|
||||
static_url: e.static_url,
|
||||
url: e.url,
|
||||
visible_in_picker: e.visible_in_picker
|
||||
})
|
||||
export const featured_tag = (f: Entity.FeaturedTag): MegalodonEntity.FeaturedTag => f
|
||||
export const field = (f: Entity.Field): MegalodonEntity.Field => f
|
||||
export const filter = (f: Entity.Filter): MegalodonEntity.Filter => f
|
||||
export const history = (h: Entity.History): MegalodonEntity.History => h
|
||||
export const identity_proof = (i: Entity.IdentityProof): MegalodonEntity.IdentityProof => i
|
||||
export const instance = (i: Entity.Instance): MegalodonEntity.Instance => ({
|
||||
uri: i.uri,
|
||||
title: i.title,
|
||||
description: i.description,
|
||||
email: i.email,
|
||||
version: i.version,
|
||||
thumbnail: i.thumbnail,
|
||||
urls: urls(i.urls),
|
||||
stats: stats(i.stats),
|
||||
languages: i.languages,
|
||||
registrations: i.registrations,
|
||||
approval_required: i.approval_required,
|
||||
configuration: {
|
||||
statuses: {
|
||||
max_characters: i.max_toot_chars,
|
||||
max_media_attachments: i.max_media_attachments
|
||||
},
|
||||
polls: {
|
||||
max_options: i.poll_limits.max_options,
|
||||
max_characters_per_option: i.poll_limits.max_option_chars,
|
||||
min_expiration: i.poll_limits.min_expiration,
|
||||
max_expiration: i.poll_limits.max_expiration
|
||||
}
|
||||
}
|
||||
})
|
||||
export const list = (l: Entity.List): MegalodonEntity.List => ({
|
||||
id: l.id,
|
||||
title: l.title,
|
||||
replies_policy: null
|
||||
})
|
||||
export const marker = (m: Entity.Marker | Record<never, never>): MegalodonEntity.Marker | Record<never, never> => {
|
||||
if ((m as any).notifications) {
|
||||
const mm = m as Entity.Marker
|
||||
return {
|
||||
notifications: {
|
||||
last_read_id: mm.notifications.last_read_id,
|
||||
version: mm.notifications.version,
|
||||
updated_at: mm.notifications.updated_at,
|
||||
unread_count: mm.notifications.pleroma.unread_count
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
export const mention = (m: Entity.Mention): MegalodonEntity.Mention => m
|
||||
export const notification = (n: Entity.Notification): MegalodonEntity.Notification | UnknownNotificationTypeError => {
|
||||
const notificationType = decodeNotificationType(n.type)
|
||||
if (notificationType instanceof UnknownNotificationTypeError) return notificationType
|
||||
if (n.status && n.emoji) {
|
||||
return {
|
||||
id: n.id,
|
||||
account: account(n.account),
|
||||
created_at: n.created_at,
|
||||
status: status(n.status),
|
||||
emoji: n.emoji,
|
||||
type: notificationType
|
||||
}
|
||||
} else if (n.status) {
|
||||
return {
|
||||
id: n.id,
|
||||
account: account(n.account),
|
||||
created_at: n.created_at,
|
||||
status: status(n.status),
|
||||
type: notificationType
|
||||
}
|
||||
} else if (n.target) {
|
||||
return {
|
||||
id: n.id,
|
||||
account: account(n.account),
|
||||
created_at: n.created_at,
|
||||
target: account(n.target),
|
||||
type: notificationType
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
id: n.id,
|
||||
account: account(n.account),
|
||||
created_at: n.created_at,
|
||||
type: notificationType
|
||||
}
|
||||
}
|
||||
}
|
||||
export const poll = (p: Entity.Poll): MegalodonEntity.Poll => p
|
||||
export const pollOption = (p: Entity.PollOption): MegalodonEntity.PollOption => p
|
||||
export const preferences = (p: Entity.Preferences): MegalodonEntity.Preferences => p
|
||||
export const push_subscription = (p: Entity.PushSubscription): MegalodonEntity.PushSubscription => p
|
||||
export const reaction = (r: Entity.Reaction): MegalodonEntity.Reaction => {
|
||||
const p = {
|
||||
count: r.count,
|
||||
me: r.me,
|
||||
name: r.name
|
||||
}
|
||||
if (r.accounts) {
|
||||
return Object.assign({}, p, {
|
||||
accounts: r.accounts.map(a => account(a))
|
||||
})
|
||||
}
|
||||
return p
|
||||
}
|
||||
export const relationship = (r: Entity.Relationship): MegalodonEntity.Relationship => ({
|
||||
id: r.id,
|
||||
following: r.following,
|
||||
followed_by: r.followed_by,
|
||||
blocking: r.blocking,
|
||||
blocked_by: r.blocked_by,
|
||||
muting: r.muting,
|
||||
muting_notifications: r.muting_notifications,
|
||||
requested: r.requested,
|
||||
domain_blocking: r.domain_blocking,
|
||||
showing_reblogs: r.showing_reblogs,
|
||||
endorsed: r.endorsed,
|
||||
notifying: r.notifying,
|
||||
note: r.note
|
||||
})
|
||||
export const report = (r: Entity.Report): MegalodonEntity.Report => ({
|
||||
id: r.id,
|
||||
action_taken: r.action_taken,
|
||||
action_taken_at: null,
|
||||
category: null,
|
||||
comment: null,
|
||||
forwarded: null,
|
||||
status_ids: null,
|
||||
rule_ids: null
|
||||
})
|
||||
export const results = (r: Entity.Results): MegalodonEntity.Results => ({
|
||||
accounts: Array.isArray(r.accounts) ? r.accounts.map(a => account(a)) : [],
|
||||
statuses: Array.isArray(r.statuses) ? r.statuses.map(s => status(s)) : [],
|
||||
hashtags: Array.isArray(r.hashtags) ? r.hashtags.map(h => tag(h)) : []
|
||||
})
|
||||
export const scheduled_status = (s: Entity.ScheduledStatus): MegalodonEntity.ScheduledStatus => ({
|
||||
id: s.id,
|
||||
scheduled_at: s.scheduled_at,
|
||||
params: status_params(s.params),
|
||||
media_attachments: Array.isArray(s.media_attachments) ? s.media_attachments.map(m => attachment(m)) : null
|
||||
})
|
||||
export const source = (s: Entity.Source): MegalodonEntity.Source => s
|
||||
export const stats = (s: Entity.Stats): MegalodonEntity.Stats => s
|
||||
export const status = (s: Entity.Status): MegalodonEntity.Status => ({
|
||||
id: s.id,
|
||||
uri: s.uri,
|
||||
url: s.url,
|
||||
account: account(s.account),
|
||||
in_reply_to_id: s.in_reply_to_id,
|
||||
in_reply_to_account_id: s.in_reply_to_account_id,
|
||||
reblog: s.reblog ? status(s.reblog) : null,
|
||||
content: s.content,
|
||||
plain_content: s.pleroma.content?.['text/plain'] ? s.pleroma.content['text/plain'] : null,
|
||||
created_at: s.created_at,
|
||||
edited_at: s.edited_at || null,
|
||||
emojis: Array.isArray(s.emojis) ? s.emojis.map(e => emoji(e)) : [],
|
||||
replies_count: s.replies_count,
|
||||
reblogs_count: s.reblogs_count,
|
||||
favourites_count: s.favourites_count,
|
||||
reblogged: s.reblogged,
|
||||
favourited: s.favourited,
|
||||
muted: s.muted,
|
||||
sensitive: s.sensitive,
|
||||
spoiler_text: s.spoiler_text,
|
||||
visibility: s.visibility,
|
||||
media_attachments: Array.isArray(s.media_attachments) ? s.media_attachments.map(m => attachment(m)) : [],
|
||||
mentions: Array.isArray(s.mentions) ? s.mentions.map(m => mention(m)) : [],
|
||||
tags: s.tags,
|
||||
card: s.card ? card(s.card) : null,
|
||||
poll: s.poll ? poll(s.poll) : null,
|
||||
application: s.application ? application(s.application) : null,
|
||||
language: s.language,
|
||||
pinned: s.pinned,
|
||||
emoji_reactions: Array.isArray(s.pleroma.emoji_reactions) ? s.pleroma.emoji_reactions.map(r => reaction(r)) : [],
|
||||
bookmarked: s.bookmarked ? s.bookmarked : false,
|
||||
quote: s.reblog !== null && s.reblog.content !== s.content
|
||||
})
|
||||
export const status_params = (s: Entity.StatusParams): MegalodonEntity.StatusParams => {
|
||||
return {
|
||||
text: s.text,
|
||||
in_reply_to_id: s.in_reply_to_id,
|
||||
media_ids: Array.isArray(s.media_ids) ? s.media_ids : null,
|
||||
sensitive: s.sensitive,
|
||||
spoiler_text: s.spoiler_text,
|
||||
visibility: s.visibility,
|
||||
scheduled_at: s.scheduled_at,
|
||||
application_id: null
|
||||
}
|
||||
}
|
||||
export const status_source = (s: Entity.StatusSource): MegalodonEntity.StatusSource => s
|
||||
export const tag = (t: Entity.Tag): MegalodonEntity.Tag => t
|
||||
export const token = (t: Entity.Token): MegalodonEntity.Token => t
|
||||
export const urls = (u: Entity.URLs): MegalodonEntity.URLs => u
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface
|
||||
*/
|
||||
export interface Interface {
|
||||
get<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
put<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
putForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
patch<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
patchForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
post<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
postForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
del<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
cancel(): void
|
||||
socket(path: string, stream: string, params?: string): WebSocket
|
||||
}
|
||||
|
||||
/**
|
||||
* Mastodon API client.
|
||||
*
|
||||
* Using axios for request, you will handle promises.
|
||||
*/
|
||||
export class Client implements Interface {
|
||||
static DEFAULT_SCOPE = DEFAULT_SCOPE
|
||||
static DEFAULT_URL = 'https://pleroma.io'
|
||||
static NO_REDIRECT = NO_REDIRECT
|
||||
|
||||
private accessToken: string | null
|
||||
private baseUrl: string
|
||||
private userAgent: string
|
||||
private abortController: AbortController
|
||||
private proxyConfig: ProxyConfig | false = false
|
||||
|
||||
/**
|
||||
* @param baseUrl hostname or base URL
|
||||
* @param accessToken access token from OAuth2 authorization
|
||||
* @param userAgent UserAgent is specified in header on request.
|
||||
* @param proxyConfig Proxy setting, or set false if don't use proxy.
|
||||
*/
|
||||
constructor(
|
||||
baseUrl: string,
|
||||
accessToken: string | null = null,
|
||||
userAgent: string = DEFAULT_UA,
|
||||
proxyConfig: ProxyConfig | false = false
|
||||
) {
|
||||
this.accessToken = accessToken
|
||||
this.baseUrl = baseUrl
|
||||
this.userAgent = userAgent
|
||||
this.proxyConfig = proxyConfig
|
||||
this.abortController = new AbortController()
|
||||
axios.defaults.signal = this.abortController.signal
|
||||
}
|
||||
|
||||
/**
|
||||
* GET request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Query parameters
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async get<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
params: params,
|
||||
headers: headers
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.get<T>(this.baseUrl + path, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async put<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.put<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT request to mastodon REST API for multipart.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async putForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.putForm<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async patch<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.patch<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH request to mastodon REST API for multipart.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async patchForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.patchForm<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* POST request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async post<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios.post<T>(this.baseUrl + path, params, options).then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* POST request to mastodon REST API for multipart.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async postForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios.postForm<T>(this.baseUrl + path, params, options).then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async del<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
data: params,
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.delete(this.baseUrl + path, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel all requests in this instance.
|
||||
* @returns void
|
||||
*/
|
||||
public cancel(): void {
|
||||
return this.abortController.abort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get connection and receive websocket connection for Pleroma API.
|
||||
*
|
||||
* @param path relative path from baseUrl: normally it is `/streaming`.
|
||||
* @param stream Stream name, please refer: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/mastodon_api/mastodon_socket.ex#L19-28
|
||||
* @returns WebSocket, which inherits from EventEmitter
|
||||
*/
|
||||
public socket(path: string, stream: string, params?: string): WebSocket {
|
||||
if (!this.accessToken) {
|
||||
throw new Error('accessToken is required')
|
||||
}
|
||||
const url = this.baseUrl + path
|
||||
const streaming = new WebSocket(url, stream, params, this.accessToken, this.userAgent, this.proxyConfig)
|
||||
process.nextTick(() => {
|
||||
streaming.start()
|
||||
})
|
||||
return streaming
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default PleromaAPI
|
|
@ -1,31 +0,0 @@
|
|||
/// <reference path="emoji.ts" />
|
||||
/// <reference path="source.ts" />
|
||||
/// <reference path="field.ts" />
|
||||
namespace PleromaEntity {
|
||||
export type Account = {
|
||||
id: string
|
||||
username: string
|
||||
acct: string
|
||||
display_name: string
|
||||
locked: boolean
|
||||
discoverable?: boolean
|
||||
noindex: boolean | null
|
||||
suspended: boolean | null
|
||||
limited: boolean | null
|
||||
created_at: string
|
||||
followers_count: number
|
||||
following_count: number
|
||||
statuses_count: number
|
||||
note: string
|
||||
url: string
|
||||
avatar: string
|
||||
avatar_static: string
|
||||
header: string
|
||||
header_static: string
|
||||
emojis: Array<Emoji>
|
||||
moved: Account | null
|
||||
fields: Array<Field>
|
||||
bot: boolean
|
||||
source?: Source
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Activity = {
|
||||
week: string
|
||||
statuses: string
|
||||
logins: string
|
||||
registrations: string
|
||||
}
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
/// <reference path="emoji.ts" />
|
||||
|
||||
namespace PleromaEntity {
|
||||
export type Announcement = {
|
||||
id: string
|
||||
content: string
|
||||
starts_at: string | null
|
||||
ends_at: string | null
|
||||
published: boolean
|
||||
all_day: boolean
|
||||
published_at: string
|
||||
updated_at: string
|
||||
mentions: Array<AnnouncementAccount>
|
||||
statuses: Array<AnnouncementStatus>
|
||||
tags: Array<StatusTag>
|
||||
emojis: Array<Emoji>
|
||||
reactions: Array<AnnouncementReaction>
|
||||
}
|
||||
|
||||
export type AnnouncementAccount = {
|
||||
id: string
|
||||
username: string
|
||||
url: string
|
||||
acct: string
|
||||
}
|
||||
|
||||
export type AnnouncementStatus = {
|
||||
id: string
|
||||
url: string
|
||||
}
|
||||
|
||||
export type AnnouncementReaction = {
|
||||
name: string
|
||||
count: number
|
||||
me: boolean | null
|
||||
url: string | null
|
||||
static_url: string | null
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Application = {
|
||||
name: string
|
||||
website?: string | null
|
||||
vapid_key?: string | null
|
||||
}
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
/// <reference path="attachment.ts" />
|
||||
namespace PleromaEntity {
|
||||
export type AsyncAttachment = {
|
||||
id: string
|
||||
type: 'unknown' | 'image' | 'gifv' | 'video' | 'audio'
|
||||
url: string | null
|
||||
remote_url: string | null
|
||||
preview_url: string
|
||||
text_url: string | null
|
||||
meta: Meta | null
|
||||
description: string | null
|
||||
blurhash: string | null
|
||||
}
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Sub = {
|
||||
// For Image, Gifv, and Video
|
||||
width?: number
|
||||
height?: number
|
||||
size?: string
|
||||
aspect?: number
|
||||
|
||||
// For Gifv and Video
|
||||
frame_rate?: string
|
||||
|
||||
// For Audio, Gifv, and Video
|
||||
duration?: number
|
||||
bitrate?: number
|
||||
}
|
||||
|
||||
export type Focus = {
|
||||
x: number
|
||||
y: number
|
||||
}
|
||||
|
||||
export type Meta = {
|
||||
original?: Sub
|
||||
small?: Sub
|
||||
focus?: Focus
|
||||
length?: string
|
||||
duration?: number
|
||||
fps?: number
|
||||
size?: string
|
||||
width?: number
|
||||
height?: number
|
||||
aspect?: number
|
||||
audio_encode?: string
|
||||
audio_bitrate?: string
|
||||
audio_channel?: string
|
||||
}
|
||||
|
||||
export type Attachment = {
|
||||
id: string
|
||||
type: 'unknown' | 'image' | 'gifv' | 'video' | 'audio'
|
||||
url: string
|
||||
remote_url: string | null
|
||||
preview_url: string | null
|
||||
text_url: string | null
|
||||
meta: Meta | null
|
||||
description: string | null
|
||||
blurhash: string | null
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Card = {
|
||||
url: string
|
||||
title: string
|
||||
description: string
|
||||
type: 'link' | 'photo' | 'video' | 'rich'
|
||||
image: string | null
|
||||
provider_name: string
|
||||
provider_url: string
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
/// <reference path="status.ts" />
|
||||
|
||||
namespace PleromaEntity {
|
||||
export type Context = {
|
||||
ancestors: Array<Status>
|
||||
descendants: Array<Status>
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
/// <reference path="account.ts" />
|
||||
/// <reference path="status.ts" />
|
||||
|
||||
namespace PleromaEntity {
|
||||
export type Conversation = {
|
||||
id: string
|
||||
accounts: Array<Account>
|
||||
last_status: Status | null
|
||||
unread: boolean
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Emoji = {
|
||||
shortcode: string
|
||||
static_url: string
|
||||
url: string
|
||||
visible_in_picker: boolean
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type FeaturedTag = {
|
||||
id: string
|
||||
name: string
|
||||
statuses_count: number
|
||||
last_status_at: string
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Field = {
|
||||
name: string
|
||||
value: string
|
||||
verified_at: string | null
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Filter = {
|
||||
id: string
|
||||
phrase: string
|
||||
context: Array<FilterContext>
|
||||
expires_at: string | null
|
||||
irreversible: boolean
|
||||
whole_word: boolean
|
||||
}
|
||||
|
||||
export type FilterContext = string
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type History = {
|
||||
day: string
|
||||
uses: number
|
||||
accounts: number
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type IdentityProof = {
|
||||
provider: string
|
||||
provider_username: string
|
||||
updated_at: string
|
||||
proof_url: string
|
||||
profile_url: string
|
||||
}
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
/// <reference path="account.ts" />
|
||||
/// <reference path="urls.ts" />
|
||||
/// <reference path="stats.ts" />
|
||||
|
||||
namespace PleromaEntity {
|
||||
export type Instance = {
|
||||
uri: string
|
||||
title: string
|
||||
description: string
|
||||
email: string
|
||||
version: string
|
||||
thumbnail: string | null
|
||||
urls: URLs
|
||||
stats: Stats
|
||||
languages: Array<string>
|
||||
registrations: boolean
|
||||
approval_required: boolean
|
||||
max_toot_chars: number
|
||||
max_media_attachments?: number
|
||||
pleroma: {
|
||||
metadata: {
|
||||
account_activation_required: boolean
|
||||
birthday_min_age: number
|
||||
birthday_required: boolean
|
||||
features: Array<string>
|
||||
federation: {
|
||||
enabled: boolean
|
||||
exclusions: boolean
|
||||
}
|
||||
fields_limits: {
|
||||
max_fields: number
|
||||
max_remote_fields: number
|
||||
name_length: number
|
||||
value_length: number
|
||||
}
|
||||
post_formats: Array<string>
|
||||
}
|
||||
}
|
||||
poll_limits: {
|
||||
max_expiration: number
|
||||
min_expiration: number
|
||||
max_option_chars: number
|
||||
max_options: number
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type List = {
|
||||
id: string
|
||||
title: string
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Marker = {
|
||||
notifications: {
|
||||
last_read_id: string
|
||||
version: number
|
||||
updated_at: string
|
||||
pleroma: {
|
||||
unread_count: number
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Reference in a new issue