2023-07-27 07:31:52 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-07-15 11:39:38 +02:00
|
|
|
import { randomUUID } from 'node:crypto';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { dirname } from 'node:path';
|
|
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2022-12-04 09:33:51 +01:00
|
|
|
import { createBullBoard } from '@bull-board/api';
|
2023-07-25 09:32:52 +02:00
|
|
|
import { BullMQAdapter } from '@bull-board/api/bullMQAdapter.js';
|
2022-12-04 09:33:51 +01:00
|
|
|
import { FastifyAdapter } from '@bull-board/fastify';
|
2022-09-17 20:27:08 +02:00
|
|
|
import ms from 'ms';
|
|
|
|
import sharp from 'sharp';
|
2022-12-03 11:42:05 +01:00
|
|
|
import pug from 'pug';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { In, IsNull } from 'typeorm';
|
2022-12-03 11:42:05 +01:00
|
|
|
import fastifyStatic from '@fastify/static';
|
|
|
|
import fastifyView from '@fastify/view';
|
2022-12-04 09:33:51 +01:00
|
|
|
import fastifyCookie from '@fastify/cookie';
|
2022-12-20 05:05:36 +01:00
|
|
|
import fastifyProxy from '@fastify/http-proxy';
|
2022-12-23 00:50:58 +01:00
|
|
|
import vary from 'vary';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { getNoteSummary } from '@/misc/get-note-summary.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import * as Acct from '@/misc/acct.js';
|
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
2022-12-04 02:16:03 +01:00
|
|
|
import type { DbQueue, DeliverQueue, EndedPollNotificationQueue, InboxQueue, ObjectStorageQueue, SystemQueue, WebhookDeliverQueue } from '@/core/QueueModule.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
|
|
|
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
|
|
|
import { PageEntityService } from '@/core/entities/PageEntityService.js';
|
|
|
|
import { GalleryPostEntityService } from '@/core/entities/GalleryPostEntityService.js';
|
|
|
|
import { ClipEntityService } from '@/core/entities/ClipEntityService.js';
|
|
|
|
import { ChannelEntityService } from '@/core/entities/ChannelEntityService.js';
|
2023-08-16 10:51:28 +02:00
|
|
|
import type { ChannelsRepository, ClipsRepository, FlashsRepository, GalleryPostsRepository, MiMeta, NotesRepository, PagesRepository, UserProfilesRepository, UsersRepository } from '@/models/index.js';
|
2023-04-12 05:52:14 +02:00
|
|
|
import type Logger from '@/logger.js';
|
2022-11-17 01:31:07 +01:00
|
|
|
import { deepClone } from '@/misc/clone.js';
|
2022-12-04 07:03:09 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-01-06 02:20:42 +01:00
|
|
|
import { FlashEntityService } from '@/core/entities/FlashEntityService.js';
|
2023-01-12 13:02:26 +01:00
|
|
|
import { RoleService } from '@/core/RoleService.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import manifest from './manifest.json' assert { type: 'json' };
|
|
|
|
import { FeedService } from './FeedService.js';
|
|
|
|
import { UrlPreviewService } from './UrlPreviewService.js';
|
2023-04-12 05:52:14 +02:00
|
|
|
import { ClientLoggerService } from './ClientLoggerService.js';
|
2023-05-10 03:57:27 +02:00
|
|
|
import type { FastifyInstance, FastifyPluginOptions, FastifyReply } from 'fastify';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
const _filename = fileURLToPath(import.meta.url);
|
|
|
|
const _dirname = dirname(_filename);
|
|
|
|
|
|
|
|
const staticAssets = `${_dirname}/../../../assets/`;
|
2022-12-27 06:36:33 +01:00
|
|
|
const clientAssets = `${_dirname}/../../../../frontend/assets/`;
|
|
|
|
const assets = `${_dirname}/../../../../../built/_frontend_dist_/`;
|
2022-09-17 20:27:08 +02:00
|
|
|
const swAssets = `${_dirname}/../../../../../built/_sw_dist_/`;
|
2022-12-20 05:05:36 +01:00
|
|
|
const viteOut = `${_dirname}/../../../../../built/_vite_/`;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ClientServerService {
|
2023-04-12 05:52:14 +02:00
|
|
|
private logger: Logger;
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.galleryPostsRepository)
|
|
|
|
private galleryPostsRepository: GalleryPostsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.channelsRepository)
|
|
|
|
private channelsRepository: ChannelsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.clipsRepository)
|
|
|
|
private clipsRepository: ClipsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.pagesRepository)
|
|
|
|
private pagesRepository: PagesRepository,
|
|
|
|
|
2023-01-06 02:20:42 +01:00
|
|
|
@Inject(DI.flashsRepository)
|
|
|
|
private flashsRepository: FlashsRepository,
|
|
|
|
|
|
|
|
private flashEntityService: FlashEntityService,
|
2022-09-17 20:27:08 +02:00
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private noteEntityService: NoteEntityService,
|
|
|
|
private pageEntityService: PageEntityService,
|
|
|
|
private galleryPostEntityService: GalleryPostEntityService,
|
|
|
|
private clipEntityService: ClipEntityService,
|
|
|
|
private channelEntityService: ChannelEntityService,
|
|
|
|
private metaService: MetaService,
|
|
|
|
private urlPreviewService: UrlPreviewService,
|
|
|
|
private feedService: FeedService,
|
2023-01-12 13:02:26 +01:00
|
|
|
private roleService: RoleService,
|
2023-04-12 05:52:14 +02:00
|
|
|
private clientLoggerService: ClientLoggerService,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
@Inject('queue:system') public systemQueue: SystemQueue,
|
|
|
|
@Inject('queue:endedPollNotification') public endedPollNotificationQueue: EndedPollNotificationQueue,
|
|
|
|
@Inject('queue:deliver') public deliverQueue: DeliverQueue,
|
|
|
|
@Inject('queue:inbox') public inboxQueue: InboxQueue,
|
|
|
|
@Inject('queue:db') public dbQueue: DbQueue,
|
|
|
|
@Inject('queue:objectStorage') public objectStorageQueue: ObjectStorageQueue,
|
|
|
|
@Inject('queue:webhookDeliver') public webhookDeliverQueue: WebhookDeliverQueue,
|
|
|
|
) {
|
2022-12-04 07:03:09 +01:00
|
|
|
//this.createServer = this.createServer.bind(this);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-12-03 11:42:05 +01:00
|
|
|
private async manifestHandler(reply: FastifyReply) {
|
2022-11-17 01:31:07 +01:00
|
|
|
const res = deepClone(manifest);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
const instance = await this.metaService.fetch(true);
|
|
|
|
|
|
|
|
res.short_name = instance.name ?? 'Misskey';
|
|
|
|
res.name = instance.name ?? 'Misskey';
|
|
|
|
if (instance.themeColor) res.theme_color = instance.themeColor;
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.header('Cache-Control', 'max-age=300');
|
|
|
|
return (res);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
2023-06-09 07:00:53 +02:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
private generateCommonPugData(meta: MiMeta) {
|
2023-06-09 07:00:53 +02:00
|
|
|
return {
|
|
|
|
instanceName: meta.name ?? 'Misskey',
|
|
|
|
icon: meta.iconUrl,
|
|
|
|
themeColor: meta.themeColor,
|
|
|
|
serverErrorImageUrl: meta.serverErrorImageUrl ?? 'https://xn--931a.moe/assets/error.jpg',
|
|
|
|
infoImageUrl: meta.infoImageUrl ?? 'https://xn--931a.moe/assets/info.jpg',
|
|
|
|
notFoundImageUrl: meta.notFoundImageUrl ?? 'https://xn--931a.moe/assets/not-found.jpg',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-12-03 11:42:05 +01:00
|
|
|
public createServer(fastify: FastifyInstance, options: FastifyPluginOptions, done: (err?: Error) => void) {
|
2022-12-04 09:33:51 +01:00
|
|
|
fastify.register(fastifyCookie, {});
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
//#region Bull Dashboard
|
|
|
|
const bullBoardPath = '/queue';
|
|
|
|
|
|
|
|
// Authenticate
|
2022-12-04 09:33:51 +01:00
|
|
|
fastify.addHook('onRequest', async (request, reply) => {
|
2023-08-20 03:00:10 +02:00
|
|
|
// %71ueueとかでリクエストされたら困るため
|
|
|
|
const url = decodeURI(request.url);
|
|
|
|
if (url === bullBoardPath || url.startsWith(bullBoardPath + '/')) {
|
2022-12-04 09:33:51 +01:00
|
|
|
const token = request.cookies.token;
|
2022-09-17 20:27:08 +02:00
|
|
|
if (token == null) {
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.code(401);
|
2022-12-04 09:33:51 +01:00
|
|
|
throw new Error('login required');
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
const user = await this.usersRepository.findOneBy({ token });
|
2023-01-12 13:02:26 +01:00
|
|
|
if (user == null) {
|
|
|
|
reply.code(403);
|
|
|
|
throw new Error('no such user');
|
|
|
|
}
|
|
|
|
const isAdministrator = await this.roleService.isAdministrator(user);
|
|
|
|
if (!isAdministrator) {
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.code(403);
|
2022-12-04 09:33:51 +01:00
|
|
|
throw new Error('access denied');
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-12-04 09:33:51 +01:00
|
|
|
const serverAdapter = new FastifyAdapter();
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
createBullBoard({
|
|
|
|
queues: [
|
|
|
|
this.systemQueue,
|
|
|
|
this.endedPollNotificationQueue,
|
|
|
|
this.deliverQueue,
|
|
|
|
this.inboxQueue,
|
|
|
|
this.dbQueue,
|
|
|
|
this.objectStorageQueue,
|
|
|
|
this.webhookDeliverQueue,
|
2023-07-25 09:32:52 +02:00
|
|
|
].map(q => new BullMQAdapter(q)),
|
2022-09-17 20:27:08 +02:00
|
|
|
serverAdapter,
|
|
|
|
});
|
|
|
|
|
|
|
|
serverAdapter.setBasePath(bullBoardPath);
|
2023-02-09 02:46:01 +01:00
|
|
|
(fastify.register as any)(serverAdapter.registerPlugin(), { prefix: bullBoardPath });
|
2022-09-17 20:27:08 +02:00
|
|
|
//#endregion
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.register(fastifyView, {
|
|
|
|
root: _dirname + '/views',
|
|
|
|
engine: {
|
|
|
|
pug: pug,
|
|
|
|
},
|
|
|
|
defaultContext: {
|
2022-09-17 20:27:08 +02:00
|
|
|
version: this.config.version,
|
|
|
|
config: this.config,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.addHook('onRequest', (request, reply, done) => {
|
|
|
|
// クリックジャッキング防止のためiFrameの中に入れられないようにする
|
|
|
|
reply.header('X-Frame-Options', 'DENY');
|
|
|
|
done();
|
|
|
|
});
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2022-12-20 05:05:36 +01:00
|
|
|
//#region vite assets
|
|
|
|
if (this.config.clientManifestExists) {
|
|
|
|
fastify.register(fastifyStatic, {
|
|
|
|
root: viteOut,
|
|
|
|
prefix: '/vite/',
|
|
|
|
maxAge: ms('30 days'),
|
|
|
|
decorateReply: false,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
fastify.register(fastifyProxy, {
|
|
|
|
upstream: 'http://localhost:5173', // TODO: port configuration
|
|
|
|
prefix: '/vite',
|
|
|
|
rewritePrefix: '/vite',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
//#region static assets
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.register(fastifyStatic, {
|
|
|
|
root: staticAssets,
|
|
|
|
prefix: '/static-assets/',
|
|
|
|
maxAge: ms('7 days'),
|
|
|
|
decorateReply: false,
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.register(fastifyStatic, {
|
|
|
|
root: clientAssets,
|
|
|
|
prefix: '/client-assets/',
|
|
|
|
maxAge: ms('7 days'),
|
|
|
|
decorateReply: false,
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.register(fastifyStatic, {
|
|
|
|
root: assets,
|
|
|
|
prefix: '/assets/',
|
|
|
|
maxAge: ms('7 days'),
|
|
|
|
decorateReply: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
fastify.get('/favicon.ico', async (request, reply) => {
|
|
|
|
return reply.sendFile('/favicon.ico', staticAssets);
|
|
|
|
});
|
|
|
|
|
|
|
|
fastify.get('/apple-touch-icon.png', async (request, reply) => {
|
|
|
|
return reply.sendFile('/apple-touch-icon.png', staticAssets);
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
|
2022-12-26 08:04:56 +01:00
|
|
|
fastify.get<{ Params: { path: string } }>('/fluent-emoji/:path(.*)', async (request, reply) => {
|
|
|
|
const path = request.params.path;
|
|
|
|
|
|
|
|
if (!path.match(/^[0-9a-f-]+\.png$/)) {
|
|
|
|
reply.code(404);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.header('Content-Security-Policy', 'default-src \'none\'; style-src \'unsafe-inline\'');
|
|
|
|
|
|
|
|
return await reply.sendFile(path, `${_dirname}/../../../../../fluent-emojis/dist/`, {
|
|
|
|
maxAge: ms('30 days'),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get<{ Params: { path: string } }>('/twemoji/:path(.*)', async (request, reply) => {
|
|
|
|
const path = request.params.path;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
if (!path.match(/^[0-9a-f-]+\.svg$/)) {
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.code(404);
|
2022-09-17 20:27:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.header('Content-Security-Policy', 'default-src \'none\'; style-src \'unsafe-inline\'');
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
return await reply.sendFile(path, `${_dirname}/../../../node_modules/@discordapp/twemoji/dist/svg/`, {
|
|
|
|
maxAge: ms('30 days'),
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get<{ Params: { path: string } }>('/twemoji-badge/:path(.*)', async (request, reply) => {
|
|
|
|
const path = request.params.path;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
if (!path.match(/^[0-9a-f-]+\.png$/)) {
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.code(404);
|
2022-09-17 20:27:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const mask = await sharp(
|
|
|
|
`${_dirname}/../../../node_modules/@discordapp/twemoji/dist/svg/${path.replace('.png', '')}.svg`,
|
|
|
|
{ density: 1000 },
|
|
|
|
)
|
|
|
|
.resize(488, 488)
|
|
|
|
.greyscale()
|
|
|
|
.normalise()
|
|
|
|
.linear(1.75, -(128 * 1.75) + 128) // 1.75x contrast
|
|
|
|
.flatten({ background: '#000' })
|
|
|
|
.extend({
|
|
|
|
top: 12,
|
|
|
|
bottom: 12,
|
|
|
|
left: 12,
|
|
|
|
right: 12,
|
|
|
|
background: '#000',
|
|
|
|
})
|
|
|
|
.toColorspace('b-w')
|
|
|
|
.png()
|
|
|
|
.toBuffer();
|
|
|
|
|
|
|
|
const buffer = await sharp({
|
|
|
|
create: { width: 512, height: 512, channels: 4, background: { r: 0, g: 0, b: 0, alpha: 0 } },
|
|
|
|
})
|
|
|
|
.pipelineColorspace('b-w')
|
|
|
|
.boolean(mask, 'eor')
|
|
|
|
.resize(96, 96)
|
|
|
|
.png()
|
|
|
|
.toBuffer();
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.header('Content-Security-Policy', 'default-src \'none\'; style-src \'unsafe-inline\'');
|
|
|
|
reply.header('Cache-Control', 'max-age=2592000');
|
|
|
|
reply.header('Content-Type', 'image/png');
|
|
|
|
return buffer;
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// ServiceWorker
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get('/sw.js', async (request, reply) => {
|
|
|
|
return await reply.sendFile('/sw.js', swAssets, {
|
|
|
|
maxAge: ms('10 minutes'),
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Manifest
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get('/manifest.json', async (request, reply) => await this.manifestHandler(reply));
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get('/robots.txt', async (request, reply) => {
|
|
|
|
return await reply.sendFile('/robots.txt', staticAssets);
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
|
2022-12-28 23:53:59 +01:00
|
|
|
// OpenSearch XML
|
|
|
|
fastify.get('/opensearch.xml', async (request, reply) => {
|
|
|
|
const meta = await this.metaService.fetch();
|
|
|
|
|
2023-01-09 05:26:42 +01:00
|
|
|
const name = meta.name ?? 'Misskey';
|
2022-12-31 01:12:42 +01:00
|
|
|
let content = '';
|
|
|
|
content += '<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">';
|
2023-01-06 15:00:54 +01:00
|
|
|
content += `<ShortName>${name}</ShortName>`;
|
2022-12-28 23:53:59 +01:00
|
|
|
content += `<Description>${name} Search</Description>`;
|
2022-12-31 01:12:42 +01:00
|
|
|
content += '<InputEncoding>UTF-8</InputEncoding>';
|
2022-12-28 23:53:59 +01:00
|
|
|
content += `<Image width="16" height="16" type="image/x-icon">${this.config.url}/favicon.ico</Image>`;
|
|
|
|
content += `<Url type="text/html" template="${this.config.url}/search?q={searchTerms}"/>`;
|
2022-12-31 01:12:42 +01:00
|
|
|
content += '</OpenSearchDescription>';
|
2022-12-28 23:53:59 +01:00
|
|
|
|
|
|
|
reply.header('Content-Type', 'application/opensearchdescription+xml');
|
|
|
|
return await reply.send(content);
|
|
|
|
});
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
//#endregion
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
const renderBase = async (reply: FastifyReply) => {
|
|
|
|
const meta = await this.metaService.fetch();
|
2023-02-09 09:19:12 +01:00
|
|
|
reply.header('Cache-Control', 'public, max-age=30');
|
2022-12-03 11:42:05 +01:00
|
|
|
return await reply.view('base', {
|
|
|
|
img: meta.bannerUrl,
|
2022-12-28 23:53:59 +01:00
|
|
|
url: this.config.url,
|
2023-06-09 07:00:53 +02:00
|
|
|
title: meta.name ?? 'Misskey',
|
2022-12-03 11:42:05 +01:00
|
|
|
desc: meta.description,
|
2023-06-09 07:00:53 +02:00
|
|
|
...this.generateCommonPugData(meta),
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
2022-12-03 11:42:05 +01:00
|
|
|
};
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
// URL preview endpoint
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get<{ Querystring: { url: string; lang: string; } }>('/url', (request, reply) => this.urlPreviewService.handle(request, reply));
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
const getFeed = async (acct: string) => {
|
|
|
|
const { username, host } = Acct.parse(acct);
|
|
|
|
const user = await this.usersRepository.findOneBy({
|
|
|
|
usernameLower: username.toLowerCase(),
|
|
|
|
host: host ?? IsNull(),
|
|
|
|
isSuspended: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
return user && await this.feedService.packFeed(user);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Atom
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get<{ Params: { user: string; } }>('/@:user.atom', async (request, reply) => {
|
|
|
|
const feed = await getFeed(request.params.user);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
if (feed) {
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.header('Content-Type', 'application/atom+xml; charset=utf-8');
|
|
|
|
return feed.atom1();
|
2022-09-17 20:27:08 +02:00
|
|
|
} else {
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.code(404);
|
2023-02-09 03:02:37 +01:00
|
|
|
return;
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// RSS
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get<{ Params: { user: string; } }>('/@:user.rss', async (request, reply) => {
|
|
|
|
const feed = await getFeed(request.params.user);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
if (feed) {
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.header('Content-Type', 'application/rss+xml; charset=utf-8');
|
|
|
|
return feed.rss2();
|
2022-09-17 20:27:08 +02:00
|
|
|
} else {
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.code(404);
|
2023-02-09 03:02:37 +01:00
|
|
|
return;
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// JSON
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get<{ Params: { user: string; } }>('/@:user.json', async (request, reply) => {
|
|
|
|
const feed = await getFeed(request.params.user);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
if (feed) {
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.header('Content-Type', 'application/json; charset=utf-8');
|
|
|
|
return feed.json1();
|
2022-09-17 20:27:08 +02:00
|
|
|
} else {
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.code(404);
|
2023-02-09 03:02:37 +01:00
|
|
|
return;
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//#region SSR (for crawlers)
|
|
|
|
// User
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get<{ Params: { user: string; sub?: string; } }>('/@:user/:sub?', async (request, reply) => {
|
|
|
|
const { username, host } = Acct.parse(request.params.user);
|
2022-09-17 20:27:08 +02:00
|
|
|
const user = await this.usersRepository.findOneBy({
|
|
|
|
usernameLower: username.toLowerCase(),
|
|
|
|
host: host ?? IsNull(),
|
|
|
|
isSuspended: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (user != null) {
|
|
|
|
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: user.id });
|
|
|
|
const meta = await this.metaService.fetch();
|
|
|
|
const me = profile.fields
|
|
|
|
? profile.fields
|
|
|
|
.filter(filed => filed.value != null && filed.value.match(/^https?:/))
|
|
|
|
.map(field => field.value)
|
|
|
|
: [];
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.header('Cache-Control', 'public, max-age=15');
|
2023-05-11 09:22:46 +02:00
|
|
|
if (profile.preventAiLearning) {
|
2023-05-10 03:57:27 +02:00
|
|
|
reply.header('X-Robots-Tag', 'noimageai');
|
|
|
|
reply.header('X-Robots-Tag', 'noai');
|
|
|
|
}
|
2022-12-03 11:42:05 +01:00
|
|
|
return await reply.view('user', {
|
2022-09-17 20:27:08 +02:00
|
|
|
user, profile, me,
|
2023-04-06 12:48:24 +02:00
|
|
|
avatarUrl: user.avatarUrl ?? this.userEntityService.getIdenticonUrl(user),
|
2022-12-03 11:42:05 +01:00
|
|
|
sub: request.params.sub,
|
2023-06-09 07:00:53 +02:00
|
|
|
...this.generateCommonPugData(meta),
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// リモートユーザーなので
|
|
|
|
// モデレータがAPI経由で参照可能にするために404にはしない
|
2022-12-03 11:42:05 +01:00
|
|
|
return await renderBase(reply);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get<{ Params: { user: string; } }>('/users/:user', async (request, reply) => {
|
2022-09-17 20:27:08 +02:00
|
|
|
const user = await this.usersRepository.findOneBy({
|
2022-12-03 11:42:05 +01:00
|
|
|
id: request.params.user,
|
2022-09-17 20:27:08 +02:00
|
|
|
host: IsNull(),
|
|
|
|
isSuspended: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (user == null) {
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.code(404);
|
2022-09-17 20:27:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.redirect(`/@${user.username}${ user.host == null ? '' : '@' + user.host}`);
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Note
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get<{ Params: { note: string; } }>('/notes/:note', async (request, reply) => {
|
2022-12-23 00:50:58 +01:00
|
|
|
vary(reply.raw, 'Accept');
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
const note = await this.notesRepository.findOneBy({
|
2022-12-03 11:42:05 +01:00
|
|
|
id: request.params.note,
|
2022-09-17 20:27:08 +02:00
|
|
|
visibility: In(['public', 'home']),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (note) {
|
|
|
|
const _note = await this.noteEntityService.pack(note);
|
|
|
|
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: note.userId });
|
|
|
|
const meta = await this.metaService.fetch();
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.header('Cache-Control', 'public, max-age=15');
|
2023-05-11 09:22:46 +02:00
|
|
|
if (profile.preventAiLearning) {
|
2023-05-10 03:57:27 +02:00
|
|
|
reply.header('X-Robots-Tag', 'noimageai');
|
|
|
|
reply.header('X-Robots-Tag', 'noai');
|
|
|
|
}
|
2022-12-03 11:42:05 +01:00
|
|
|
return await reply.view('note', {
|
2022-09-17 20:27:08 +02:00
|
|
|
note: _note,
|
|
|
|
profile,
|
2023-04-06 12:48:24 +02:00
|
|
|
avatarUrl: _note.user.avatarUrl,
|
2022-09-17 20:27:08 +02:00
|
|
|
// TODO: Let locale changeable by instance setting
|
|
|
|
summary: getNoteSummary(_note),
|
2023-06-09 07:00:53 +02:00
|
|
|
...this.generateCommonPugData(meta),
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
2022-12-03 11:42:05 +01:00
|
|
|
} else {
|
|
|
|
return await renderBase(reply);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Page
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get<{ Params: { user: string; page: string; } }>('/@:user/pages/:page', async (request, reply) => {
|
|
|
|
const { username, host } = Acct.parse(request.params.user);
|
2022-09-17 20:27:08 +02:00
|
|
|
const user = await this.usersRepository.findOneBy({
|
|
|
|
usernameLower: username.toLowerCase(),
|
|
|
|
host: host ?? IsNull(),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (user == null) return;
|
|
|
|
|
|
|
|
const page = await this.pagesRepository.findOneBy({
|
2022-12-03 11:42:05 +01:00
|
|
|
name: request.params.page,
|
2022-09-17 20:27:08 +02:00
|
|
|
userId: user.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (page) {
|
|
|
|
const _page = await this.pageEntityService.pack(page);
|
|
|
|
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: page.userId });
|
|
|
|
const meta = await this.metaService.fetch();
|
2022-12-03 11:42:05 +01:00
|
|
|
if (['public'].includes(page.visibility)) {
|
|
|
|
reply.header('Cache-Control', 'public, max-age=15');
|
|
|
|
} else {
|
|
|
|
reply.header('Cache-Control', 'private, max-age=0, must-revalidate');
|
|
|
|
}
|
2023-05-11 09:22:46 +02:00
|
|
|
if (profile.preventAiLearning) {
|
2023-05-10 03:57:27 +02:00
|
|
|
reply.header('X-Robots-Tag', 'noimageai');
|
|
|
|
reply.header('X-Robots-Tag', 'noai');
|
|
|
|
}
|
2022-12-03 11:42:05 +01:00
|
|
|
return await reply.view('page', {
|
2022-09-17 20:27:08 +02:00
|
|
|
page: _page,
|
|
|
|
profile,
|
2023-04-06 12:48:24 +02:00
|
|
|
avatarUrl: _page.user.avatarUrl,
|
2023-06-09 07:00:53 +02:00
|
|
|
...this.generateCommonPugData(meta),
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
2022-12-03 11:42:05 +01:00
|
|
|
} else {
|
|
|
|
return await renderBase(reply);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-06 02:20:42 +01:00
|
|
|
// Flash
|
|
|
|
fastify.get<{ Params: { id: string; } }>('/play/:id', async (request, reply) => {
|
|
|
|
const flash = await this.flashsRepository.findOneBy({
|
|
|
|
id: request.params.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (flash) {
|
|
|
|
const _flash = await this.flashEntityService.pack(flash);
|
|
|
|
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: flash.userId });
|
|
|
|
const meta = await this.metaService.fetch();
|
|
|
|
reply.header('Cache-Control', 'public, max-age=15');
|
2023-05-11 09:22:46 +02:00
|
|
|
if (profile.preventAiLearning) {
|
2023-05-10 03:57:27 +02:00
|
|
|
reply.header('X-Robots-Tag', 'noimageai');
|
|
|
|
reply.header('X-Robots-Tag', 'noai');
|
|
|
|
}
|
2023-01-06 02:20:42 +01:00
|
|
|
return await reply.view('flash', {
|
|
|
|
flash: _flash,
|
|
|
|
profile,
|
2023-04-06 12:48:24 +02:00
|
|
|
avatarUrl: _flash.user.avatarUrl,
|
2023-06-09 07:00:53 +02:00
|
|
|
...this.generateCommonPugData(meta),
|
2023-01-06 02:20:42 +01:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return await renderBase(reply);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
// Clip
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get<{ Params: { clip: string; } }>('/clips/:clip', async (request, reply) => {
|
2022-09-17 20:27:08 +02:00
|
|
|
const clip = await this.clipsRepository.findOneBy({
|
2022-12-03 11:42:05 +01:00
|
|
|
id: request.params.clip,
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
|
2023-01-09 05:26:42 +01:00
|
|
|
if (clip && clip.isPublic) {
|
2022-09-17 20:27:08 +02:00
|
|
|
const _clip = await this.clipEntityService.pack(clip);
|
|
|
|
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: clip.userId });
|
|
|
|
const meta = await this.metaService.fetch();
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.header('Cache-Control', 'public, max-age=15');
|
2023-05-11 09:22:46 +02:00
|
|
|
if (profile.preventAiLearning) {
|
2023-05-10 03:57:27 +02:00
|
|
|
reply.header('X-Robots-Tag', 'noimageai');
|
|
|
|
reply.header('X-Robots-Tag', 'noai');
|
|
|
|
}
|
2022-12-03 11:42:05 +01:00
|
|
|
return await reply.view('clip', {
|
2022-09-17 20:27:08 +02:00
|
|
|
clip: _clip,
|
|
|
|
profile,
|
2023-04-06 12:48:24 +02:00
|
|
|
avatarUrl: _clip.user.avatarUrl,
|
2023-06-09 07:00:53 +02:00
|
|
|
...this.generateCommonPugData(meta),
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
2022-12-03 11:42:05 +01:00
|
|
|
} else {
|
|
|
|
return await renderBase(reply);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Gallery post
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get<{ Params: { post: string; } }>('/gallery/:post', async (request, reply) => {
|
|
|
|
const post = await this.galleryPostsRepository.findOneBy({ id: request.params.post });
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
if (post) {
|
|
|
|
const _post = await this.galleryPostEntityService.pack(post);
|
|
|
|
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: post.userId });
|
|
|
|
const meta = await this.metaService.fetch();
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.header('Cache-Control', 'public, max-age=15');
|
2023-05-11 09:22:46 +02:00
|
|
|
if (profile.preventAiLearning) {
|
2023-05-10 03:57:27 +02:00
|
|
|
reply.header('X-Robots-Tag', 'noimageai');
|
|
|
|
reply.header('X-Robots-Tag', 'noai');
|
|
|
|
}
|
2022-12-03 11:42:05 +01:00
|
|
|
return await reply.view('gallery-post', {
|
2022-09-17 20:27:08 +02:00
|
|
|
post: _post,
|
|
|
|
profile,
|
2023-04-06 12:48:24 +02:00
|
|
|
avatarUrl: _post.user.avatarUrl,
|
2023-06-09 07:00:53 +02:00
|
|
|
...this.generateCommonPugData(meta),
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
2022-12-03 11:42:05 +01:00
|
|
|
} else {
|
|
|
|
return await renderBase(reply);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Channel
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get<{ Params: { channel: string; } }>('/channels/:channel', async (request, reply) => {
|
2022-09-17 20:27:08 +02:00
|
|
|
const channel = await this.channelsRepository.findOneBy({
|
2022-12-03 11:42:05 +01:00
|
|
|
id: request.params.channel,
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (channel) {
|
|
|
|
const _channel = await this.channelEntityService.pack(channel);
|
|
|
|
const meta = await this.metaService.fetch();
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.header('Cache-Control', 'public, max-age=15');
|
|
|
|
return await reply.view('channel', {
|
2022-09-17 20:27:08 +02:00
|
|
|
channel: _channel,
|
2023-06-09 07:00:53 +02:00
|
|
|
...this.generateCommonPugData(meta),
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
2022-12-03 11:42:05 +01:00
|
|
|
} else {
|
|
|
|
return await renderBase(reply);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
//#endregion
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get('/_info_card_', async (request, reply) => {
|
2022-09-17 20:27:08 +02:00
|
|
|
const meta = await this.metaService.fetch(true);
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.removeHeader('X-Frame-Options');
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
return await reply.view('info-card', {
|
2022-09-17 20:27:08 +02:00
|
|
|
version: this.config.version,
|
|
|
|
host: this.config.host,
|
|
|
|
meta: meta,
|
|
|
|
originalUsersCount: await this.usersRepository.countBy({ host: IsNull() }),
|
|
|
|
originalNotesCount: await this.notesRepository.countBy({ userHost: IsNull() }),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get('/bios', async (request, reply) => {
|
|
|
|
return await reply.view('bios', {
|
2022-09-17 20:27:08 +02:00
|
|
|
version: this.config.version,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get('/cli', async (request, reply) => {
|
|
|
|
return await reply.view('cli', {
|
2022-09-17 20:27:08 +02:00
|
|
|
version: this.config.version,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const override = (source: string, target: string, depth = 0) =>
|
|
|
|
[, ...target.split('/').filter(x => x), ...source.split('/').filter(x => x).splice(depth)].join('/');
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get('/flush', async (request, reply) => {
|
|
|
|
return await reply.view('flush');
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// streamingに非WebSocketリクエストが来た場合にbase htmlをキャシュ付きで返すと、Proxy等でそのパスがキャッシュされておかしくなる
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get('/streaming', async (request, reply) => {
|
|
|
|
reply.code(503);
|
|
|
|
reply.header('Cache-Control', 'private, max-age=0');
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Render base html for all requests
|
2022-12-03 11:42:05 +01:00
|
|
|
fastify.get('*', async (request, reply) => {
|
|
|
|
return await renderBase(reply);
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
|
2023-04-12 05:52:14 +02:00
|
|
|
fastify.setErrorHandler(async (error, request, reply) => {
|
2023-07-15 11:39:38 +02:00
|
|
|
const errId = randomUUID();
|
2023-08-05 06:56:33 +02:00
|
|
|
this.clientLoggerService.logger.error(`Internal error occurred in ${request.routerPath}: ${error.message}`, {
|
2023-04-12 05:52:14 +02:00
|
|
|
path: request.routerPath,
|
|
|
|
params: request.params,
|
|
|
|
query: request.query,
|
|
|
|
code: error.name,
|
|
|
|
stack: error.stack,
|
|
|
|
id: errId,
|
|
|
|
});
|
|
|
|
reply.code(500);
|
|
|
|
reply.header('Cache-Control', 'max-age=10, must-revalidate');
|
|
|
|
return await reply.view('error', {
|
|
|
|
code: error.code,
|
|
|
|
id: errId,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
done();
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
}
|