upd: prettify layout

This commit is contained in:
Mar0xy 2023-09-24 12:05:59 +02:00
parent 34e6717dab
commit 1501ab261d
No known key found for this signature in database
GPG key ID: 56569BBE47D2C828
10 changed files with 168 additions and 140 deletions

View file

@ -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];

View file

@ -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,
};

View file

@ -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 {

View file

@ -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,

View file

@ -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);

View file

@ -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(

View file

@ -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;
}