summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/mastodon/endpoints
diff options
context:
space:
mode:
authorMar0xy <marie@kaifa.ch>2023-09-24 12:05:59 +0200
committerMar0xy <marie@kaifa.ch>2023-09-24 12:05:59 +0200
commit1501ab261d75a85c763a06a69fcf9a1b628cdcef (patch)
treeaa741ea300282b7f99cd876f0922e55dbdf4b355 /packages/backend/src/server/api/mastodon/endpoints
parentadd: filter endpoint to masto api (diff)
downloadsharkey-1501ab261d75a85c763a06a69fcf9a1b628cdcef.tar.gz
sharkey-1501ab261d75a85c763a06a69fcf9a1b628cdcef.tar.bz2
sharkey-1501ab261d75a85c763a06a69fcf9a1b628cdcef.zip
upd: prettify layout
Diffstat (limited to 'packages/backend/src/server/api/mastodon/endpoints')
-rw-r--r--packages/backend/src/server/api/mastodon/endpoints/account.ts25
-rw-r--r--packages/backend/src/server/api/mastodon/endpoints/auth.ts70
-rw-r--r--packages/backend/src/server/api/mastodon/endpoints/filter.ts3
-rw-r--r--packages/backend/src/server/api/mastodon/endpoints/meta.ts14
-rw-r--r--packages/backend/src/server/api/mastodon/endpoints/notifications.ts12
-rw-r--r--packages/backend/src/server/api/mastodon/endpoints/search.ts6
-rw-r--r--packages/backend/src/server/api/mastodon/endpoints/timeline.ts45
7 files changed, 97 insertions, 78 deletions
diff --git a/packages/backend/src/server/api/mastodon/endpoints/account.ts b/packages/backend/src/server/api/mastodon/endpoints/account.ts
index a9580aeedb..21e1fa9fc3 100644
--- a/packages/backend/src/server/api/mastodon/endpoints/account.ts
+++ b/packages/backend/src/server/api/mastodon/endpoints/account.ts
@@ -1,11 +1,10 @@
-import { FindOptionsWhere, IsNull } from "typeorm";
-import type { MegalodonInterface } from "megalodon";
+import type { MegalodonInterface } from 'megalodon';
import type { FastifyRequest } from 'fastify';
-import { argsToBools, convertTimelinesArgsId, limitToInt } from "./timeline.js";
-import { convertId, IdConvertType as IdType, convertAccount, convertFeaturedTag, convertList, convertRelationship, convertStatus } from '../converters.js';
+import { argsToBools, convertTimelinesArgsId, limitToInt } from './timeline.js';
+import { convertId, IdConvertType as IdType, convertAccount, convertRelationship, convertStatus } from '../converters.js';
const relationshipModel = {
- id: "",
+ id: '',
following: false,
followed_by: false,
delivery_following: false,
@@ -18,7 +17,7 @@ const relationshipModel = {
showing_reblogs: false,
endorsed: false,
notifying: false,
- note: "",
+ note: '',
};
export class apiAccountMastodon {
@@ -39,16 +38,16 @@ export class apiAccountMastodon {
acct.id = convertId(acct.id, IdType.MastodonId);
acct.display_name = acct.display_name || acct.username;
acct.url = `${this.BASE_URL}/@${acct.url}`;
- acct.note = acct.note || "";
+ acct.note = acct.note || '';
acct.avatar_static = acct.avatar;
- acct.header = acct.header || "/static-assets/transparent.png";
- acct.header_static = acct.header || "/static-assets/transparent.png";
+ acct.header = acct.header || '/static-assets/transparent.png';
+ acct.header_static = acct.header || '/static-assets/transparent.png';
acct.source = {
note: acct.note,
fields: acct.fields,
- privacy: "",
+ privacy: '',
sensitive: false,
- language: "",
+ language: '',
};
console.log(acct);
return acct;
@@ -72,7 +71,7 @@ export class apiAccountMastodon {
public async lookup() {
try {
- const data = await this.client.search((this.request.query as any).acct, { type: "accounts" });
+ const data = await this.client.search((this.request.query as any).acct, { type: 'accounts' });
return convertAccount(data.data.accounts[0]);
} catch (e: any) {
console.error(e);
@@ -83,7 +82,7 @@ export class apiAccountMastodon {
public async getRelationships(users: [string]) {
try {
- relationshipModel.id = users?.toString() || "1";
+ relationshipModel.id = users?.toString() || '1';
if (!users) {
return [relationshipModel];
diff --git a/packages/backend/src/server/api/mastodon/endpoints/auth.ts b/packages/backend/src/server/api/mastodon/endpoints/auth.ts
index 48b5ec55c9..27664ae1fa 100644
--- a/packages/backend/src/server/api/mastodon/endpoints/auth.ts
+++ b/packages/backend/src/server/api/mastodon/endpoints/auth.ts
@@ -1,49 +1,49 @@
-import type { MegalodonInterface } from "megalodon";
+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",
+ '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",
+ 'write:account',
+ 'write:drive',
+ 'write:blocks',
+ 'write:favorites',
+ 'write:following',
+ 'write:messaging',
+ 'write:mutes',
+ 'write:notes',
+ 'write:notifications',
+ 'write:reactions',
+ 'write:votes',
+ 'write:pages',
+ 'write:page-likes',
+ 'write:user-groups',
+ 'write:channels',
+ 'write:gallery',
+ 'write:gallery-likes',
];
export async function apiAuthMastodon(request: FastifyRequest, client: MegalodonInterface) {
const body: any = request.body || request.query;
try {
let scope = body.scopes;
- if (typeof scope === "string") scope = scope.split(" ");
+ if (typeof scope === 'string') scope = scope.split(' ');
const pushScope = new Set<string>();
for (const s of scope) {
if (s.match(/^read/)) for (const r of readScope) pushScope.add(r);
@@ -62,7 +62,7 @@ export async function apiAuthMastodon(request: FastifyRequest, client: Megalodon
name: appData.name,
website: body.website,
redirect_uri: red,
- client_id: Buffer.from(appData.url || "").toString("base64"),
+ client_id: Buffer.from(appData.url || '').toString('base64'),
client_secret: appData.clientSecret,
};
diff --git a/packages/backend/src/server/api/mastodon/endpoints/filter.ts b/packages/backend/src/server/api/mastodon/endpoints/filter.ts
index 221090f289..175f2d519e 100644
--- a/packages/backend/src/server/api/mastodon/endpoints/filter.ts
+++ b/packages/backend/src/server/api/mastodon/endpoints/filter.ts
@@ -1,6 +1,5 @@
-import type { MegalodonInterface } from "megalodon";
+import type { MegalodonInterface } from 'megalodon';
import type { FastifyRequest } from 'fastify';
-import { convertTimelinesArgsId } from "./timeline.js";
import { IdConvertType as IdType, convertId, convertFilter } from '../converters.js';
export class apiFilterMastodon {
diff --git a/packages/backend/src/server/api/mastodon/endpoints/meta.ts b/packages/backend/src/server/api/mastodon/endpoints/meta.ts
index a37742a068..28ea6c08fd 100644
--- a/packages/backend/src/server/api/mastodon/endpoints/meta.ts
+++ b/packages/backend/src/server/api/mastodon/endpoints/meta.ts
@@ -1,7 +1,7 @@
-import { Entity } from "megalodon";
-import { MAX_NOTE_TEXT_LENGTH, FILE_TYPE_BROWSERSAFE } from "@/const.js";
+import { Entity } from 'megalodon';
+import { MAX_NOTE_TEXT_LENGTH, FILE_TYPE_BROWSERSAFE } from '@/const.js';
import type { Config } from '@/config.js';
-import type { MiMeta } from "@/models/Meta.js";
+import type { MiMeta } from '@/models/Meta.js';
export async function getInstance(
response: Entity.Instance,
@@ -11,13 +11,13 @@ export async function getInstance(
) {
return {
uri: config.url,
- title: meta.name || "Sharkey",
+ title: meta.name || 'Sharkey',
short_description:
- meta.description?.substring(0, 50) || "See real server website",
+ meta.description?.substring(0, 50) || 'See real server website',
description:
meta.description ||
"This is a vanilla Sharkey Instance. It doesn't seem to have a description.",
- email: response.email || "",
+ email: response.email || '',
version: `3.0.0 (compatible; Sharkey ${config.version})`,
urls: response.urls,
stats: {
@@ -25,7 +25,7 @@ export async function getInstance(
status_count: response.stats.status_count,
domain_count: response.stats.domain_count,
},
- thumbnail: meta.backgroundImageUrl || "/static-assets/transparent.png",
+ thumbnail: meta.backgroundImageUrl || '/static-assets/transparent.png',
languages: meta.langs,
registrations: !meta.disableRegistration || response.registrations,
approval_required: !response.registrations,
diff --git a/packages/backend/src/server/api/mastodon/endpoints/notifications.ts b/packages/backend/src/server/api/mastodon/endpoints/notifications.ts
index 4e8c314a54..667379e8bc 100644
--- a/packages/backend/src/server/api/mastodon/endpoints/notifications.ts
+++ b/packages/backend/src/server/api/mastodon/endpoints/notifications.ts
@@ -1,10 +1,10 @@
-import type { MegalodonInterface } from "megalodon";
+import type { MegalodonInterface } from 'megalodon';
import type { FastifyRequest } from 'fastify';
-import { convertTimelinesArgsId } from "./timeline.js";
+import { convertTimelinesArgsId } from './timeline.js';
import { IdConvertType as IdType, convertId, convertNotification } from '../converters.js';
function toLimitToInt(q: any) {
- if (q.limit) if (typeof q.limit === "string") q.limit = parseInt(q.limit, 10);
+ if (q.limit) if (typeof q.limit === 'string') q.limit = parseInt(q.limit, 10);
return q;
}
@@ -23,8 +23,8 @@ export class apiNotifyMastodon {
const notifs = data.data;
const processed = notifs.map((n) => {
n = convertNotification(n);
- if (n.type !== "follow" && n.type !== "follow_request") {
- if (n.type === "reaction") n.type = "favourite";
+ if (n.type !== 'follow' && n.type !== 'follow_request') {
+ if (n.type === 'reaction') n.type = 'favourite';
return n;
} else {
return n;
@@ -41,7 +41,7 @@ export class apiNotifyMastodon {
try {
const data = await this.client.getNotification( convertId((this.request.params as any).id, IdType.SharkeyId) );
const notif = convertNotification(data.data);
- if (notif.type !== "follow" && notif.type !== "follow_request" && notif.type === "reaction") notif.type = "favourite";
+ if (notif.type !== 'follow' && notif.type !== 'follow_request' && notif.type === 'reaction') notif.type = 'favourite';
return notif;
} catch (e: any) {
console.error(e);
diff --git a/packages/backend/src/server/api/mastodon/endpoints/search.ts b/packages/backend/src/server/api/mastodon/endpoints/search.ts
index d55831640a..6b36582b91 100644
--- a/packages/backend/src/server/api/mastodon/endpoints/search.ts
+++ b/packages/backend/src/server/api/mastodon/endpoints/search.ts
@@ -1,7 +1,7 @@
-import type { MegalodonInterface } from "megalodon";
-import { Converter } from "megalodon";
+import type { MegalodonInterface } from 'megalodon';
+import { Converter } from 'megalodon';
import type { FastifyRequest } from 'fastify';
-import { convertTimelinesArgsId, limitToInt } from "./timeline.js";
+import { convertTimelinesArgsId, limitToInt } from './timeline.js';
import { convertAccount, convertStatus } from '../converters.js';
async function getHighlight(
diff --git a/packages/backend/src/server/api/mastodon/endpoints/timeline.ts b/packages/backend/src/server/api/mastodon/endpoints/timeline.ts
index 4d4cb2c419..1d2812c37c 100644
--- a/packages/backend/src/server/api/mastodon/endpoints/timeline.ts
+++ b/packages/backend/src/server/api/mastodon/endpoints/timeline.ts
@@ -1,19 +1,19 @@
import { convertId, IdConvertType as IdType, convertAccount, convertConversation, convertList, convertStatus } from '../converters.js';
-import { ParsedUrlQuery } from "querystring";
+import { ParsedUrlQuery } from 'querystring';
export function limitToInt(q: ParsedUrlQuery) {
let object: any = q;
if (q.limit)
- if (typeof q.limit === "string") object.limit = parseInt(q.limit, 10);
+ if (typeof q.limit === 'string') object.limit = parseInt(q.limit, 10);
if (q.offset)
- if (typeof q.offset === "string") object.offset = parseInt(q.offset, 10);
+ if (typeof q.offset === 'string') object.offset = parseInt(q.offset, 10);
return object;
}
export function argsToBools(q: ParsedUrlQuery) {
// Values taken from https://docs.joinmastodon.org/client/intro/#boolean
const toBoolean = (value: string) =>
- !["0", "f", "F", "false", "FALSE", "off", "OFF"].includes(value);
+ !['0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].includes(value);
// Keys taken from:
// - https://docs.joinmastodon.org/methods/accounts/#statuses
@@ -21,27 +21,48 @@ export function argsToBools(q: ParsedUrlQuery) {
// - https://docs.joinmastodon.org/methods/timelines/#tag
let object: any = q;
if (q.only_media)
- if (typeof q.only_media === "string")
+ if (typeof q.only_media === 'string')
object.only_media = toBoolean(q.only_media);
if (q.exclude_replies)
- if (typeof q.exclude_replies === "string")
+ if (typeof q.exclude_replies === 'string')
object.exclude_replies = toBoolean(q.exclude_replies);
if (q.exclude_reblogs)
- if (typeof q.exclude_reblogs === "string")
+ if (typeof q.exclude_reblogs === 'string')
object.exclude_reblogs = toBoolean(q.exclude_reblogs);
if (q.pinned)
- if (typeof q.pinned === "string") object.pinned = toBoolean(q.pinned);
+ if (typeof q.pinned === 'string') object.pinned = toBoolean(q.pinned);
if (q.local)
- if (typeof q.local === "string") object.local = toBoolean(q.local);
+ if (typeof q.local === 'string') object.local = toBoolean(q.local);
return q;
}
export function convertTimelinesArgsId(q: ParsedUrlQuery) {
- if (typeof q.min_id === "string")
+ if (typeof q.min_id === 'string')
q.min_id = convertId(q.min_id, IdType.SharkeyId);
- if (typeof q.max_id === "string")
+ if (typeof q.max_id === 'string')
q.max_id = convertId(q.max_id, IdType.SharkeyId);
- if (typeof q.since_id === "string")
+ if (typeof q.since_id === 'string')
q.since_id = convertId(q.since_id, IdType.SharkeyId);
return q;
+}
+
+function escapeHTML(str: string) {
+ if (!str) {
+ return '';
+ }
+ return str
+ .replace(/&/g, '&amp;')
+ .replace(/</g, '&lt;')
+ .replace(/>/g, '&gt;')
+ .replace(/'/g, '&quot;')
+ .replace(/'/g, '&#039;');
+}
+
+function nl2br(str: string) {
+ if (!str) {
+ return '';
+ }
+ str = str.replace(/\r\n/g, '<br />');
+ str = str.replace(/(\n|\r)/g, '<br />');
+ return str;
} \ No newline at end of file