Fix for undefined url param in some pages
This commit is contained in:
parent
9bc3f885bc
commit
dba4d2240e
|
@ -2,48 +2,51 @@
|
|||
* Web Client Server
|
||||
*/
|
||||
|
||||
import { dirname } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import Koa from 'koa';
|
||||
import Router from '@koa/router';
|
||||
import send from 'koa-send';
|
||||
import favicon from 'koa-favicon';
|
||||
import views from 'koa-views';
|
||||
import sharp from 'sharp';
|
||||
import { createBullBoard } from '@bull-board/api';
|
||||
import { BullAdapter } from '@bull-board/api/bullAdapter.js';
|
||||
import { KoaAdapter } from '@bull-board/koa';
|
||||
import { dirname } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import Koa from 'koa';
|
||||
import Router from '@koa/router';
|
||||
import send from 'koa-send';
|
||||
import favicon from 'koa-favicon';
|
||||
import views from 'koa-views';
|
||||
import sharp from 'sharp';
|
||||
import { createBullBoard } from '@bull-board/api';
|
||||
import { BullAdapter } from '@bull-board/api/bullAdapter.js';
|
||||
import { KoaAdapter } from '@bull-board/koa';
|
||||
|
||||
import { In, IsNull } from 'typeorm';
|
||||
import { fetchMeta } from '@/misc/fetch-meta.js';
|
||||
import config from '@/config/index.js';
|
||||
import { Users, Notes, UserProfiles, Pages, Channels, Clips, GalleryPosts } from '@/models/index.js';
|
||||
import * as Acct from '@/misc/acct.js';
|
||||
import { getNoteSummary } from '@/misc/get-note-summary.js';
|
||||
import { queues } from '@/queue/queues.js';
|
||||
import { genOpenapiSpec } from '../api/openapi/gen-spec.js';
|
||||
import { urlPreviewHandler } from './url-preview.js';
|
||||
import { manifestHandler } from './manifest.js';
|
||||
import packFeed from './feed.js';
|
||||
import { MINUTE, DAY } from '@/const.js';
|
||||
import { In, IsNull } from 'typeorm';
|
||||
import { fetchMeta } from '@/misc/fetch-meta.js';
|
||||
import config from '@/config/index.js';
|
||||
import { Users, Notes, UserProfiles, Pages, Channels, Clips, GalleryPosts } from '@/models/index.js';
|
||||
import * as Acct from '@/misc/acct.js';
|
||||
import { getNoteSummary } from '@/misc/get-note-summary.js';
|
||||
import { queues } from '@/queue/queues.js';
|
||||
import { genOpenapiSpec } from '../api/openapi/gen-spec.js';
|
||||
import { urlPreviewHandler } from './url-preview.js';
|
||||
import { manifestHandler } from './manifest.js';
|
||||
import packFeed from './feed.js';
|
||||
import { MINUTE, DAY } from '@/const.js';
|
||||
|
||||
const _filename = fileURLToPath(import.meta.url);
|
||||
const _dirname = dirname(_filename);
|
||||
const _filename = fileURLToPath(import.meta.url);
|
||||
const _dirname = dirname(_filename);
|
||||
|
||||
const staticAssets = `${_dirname}/../../../assets/`;
|
||||
const clientAssets = `${_dirname}/../../../../client/assets/`;
|
||||
const assets = `${_dirname}/../../../../../built/_client_dist_/`;
|
||||
const swAssets = `${_dirname}/../../../../../built/_sw_dist_/`;
|
||||
const staticAssets = `${_dirname}/../../../assets/`;
|
||||
const clientAssets = `${_dirname}/../../../../client/assets/`;
|
||||
const assets = `${_dirname}/../../../../../built/_client_dist_/`;
|
||||
const swAssets = `${_dirname}/../../../../../built/_sw_dist_/`;
|
||||
|
||||
// Init app
|
||||
const app = new Koa();
|
||||
// Init app
|
||||
const app = new Koa();
|
||||
|
||||
//#region Bull Dashboard
|
||||
const bullBoardPath = '/queue';
|
||||
//#region Bull Dashboard
|
||||
const bullBoardPath = '/queue';
|
||||
|
||||
// Authenticate
|
||||
app.use(async (ctx, next) => {
|
||||
// used as a url param to prevent caching css and images
|
||||
const nowDateMs = Date.now();
|
||||
|
||||
// Authenticate
|
||||
app.use(async (ctx, next) => {
|
||||
if (ctx.path === bullBoardPath || ctx.path.startsWith(bullBoardPath + '/')) {
|
||||
const token = ctx.cookies.get('token');
|
||||
if (token == null) {
|
||||
|
@ -57,21 +60,21 @@ app.use(async (ctx, next) => {
|
|||
}
|
||||
}
|
||||
await next();
|
||||
});
|
||||
});
|
||||
|
||||
const serverAdapter = new KoaAdapter();
|
||||
const serverAdapter = new KoaAdapter();
|
||||
|
||||
createBullBoard({
|
||||
createBullBoard({
|
||||
queues: queues.map(q => new BullAdapter(q)),
|
||||
serverAdapter,
|
||||
});
|
||||
});
|
||||
|
||||
serverAdapter.setBasePath(bullBoardPath);
|
||||
app.use(serverAdapter.registerPlugin());
|
||||
//#endregion
|
||||
serverAdapter.setBasePath(bullBoardPath);
|
||||
app.use(serverAdapter.registerPlugin());
|
||||
//#endregion
|
||||
|
||||
// Init renderer
|
||||
app.use(views(_dirname + '/views', {
|
||||
// Init renderer
|
||||
app.use(views(_dirname + '/views', {
|
||||
extension: 'pug',
|
||||
options: {
|
||||
version: config.version,
|
||||
|
@ -80,52 +83,52 @@ app.use(views(_dirname + '/views', {
|
|||
JSON.parse(readFileSync(`${_dirname}/../../../../../built/_client_dist_/manifest.json`, 'utf-8'))['src/init.ts'],
|
||||
config,
|
||||
},
|
||||
}));
|
||||
}));
|
||||
|
||||
// Serve favicon
|
||||
app.use(favicon(`${_dirname}/../../../assets/favicon.ico`));
|
||||
// Serve favicon
|
||||
app.use(favicon(`${_dirname}/../../../assets/favicon.ico`));
|
||||
|
||||
// Common request handler
|
||||
app.use(async (ctx, next) => {
|
||||
// Common request handler
|
||||
app.use(async (ctx, next) => {
|
||||
// IFrameの中に入れられないようにする
|
||||
ctx.set('X-Frame-Options', 'DENY');
|
||||
await next();
|
||||
});
|
||||
});
|
||||
|
||||
// Init router
|
||||
const router = new Router();
|
||||
// Init router
|
||||
const router = new Router();
|
||||
|
||||
//#region static assets
|
||||
//#region static assets
|
||||
|
||||
router.get('/static-assets/(.*)', async ctx => {
|
||||
router.get('/static-assets/(.*)', async ctx => {
|
||||
await send(ctx as any, ctx.path.replace('/static-assets/', ''), {
|
||||
root: staticAssets,
|
||||
maxage: 7 * DAY,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/client-assets/(.*)', async ctx => {
|
||||
router.get('/client-assets/(.*)', async ctx => {
|
||||
await send(ctx as any, ctx.path.replace('/client-assets/', ''), {
|
||||
root: clientAssets,
|
||||
maxage: 7 * DAY,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/assets/(.*)', async ctx => {
|
||||
router.get('/assets/(.*)', async ctx => {
|
||||
await send(ctx as any, ctx.path.replace('/assets/', ''), {
|
||||
root: assets,
|
||||
maxage: 7 * DAY,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Apple touch icon
|
||||
router.get('/apple-touch-icon.png', async ctx => {
|
||||
// Apple touch icon
|
||||
router.get('/apple-touch-icon.png', async ctx => {
|
||||
await send(ctx as any, '/apple-touch-icon.png', {
|
||||
root: staticAssets,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/twemoji/(.*)', async ctx => {
|
||||
router.get('/twemoji/(.*)', async ctx => {
|
||||
const path = ctx.path.replace('/twemoji/', '');
|
||||
|
||||
if (!path.match(/^[0-9a-f-]+\.svg$/)) {
|
||||
|
@ -139,9 +142,9 @@ router.get('/twemoji/(.*)', async ctx => {
|
|||
root: `${_dirname}/../../../node_modules/@discordapp/twemoji/dist/svg/`,
|
||||
maxage: 30 * DAY,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/twemoji-badge/(.*)', async ctx => {
|
||||
router.get('/twemoji-badge/(.*)', async ctx => {
|
||||
const path = ctx.path.replace('/twemoji-badge/', '');
|
||||
|
||||
if (!path.match(/^[0-9a-f-]+\.png$/)) {
|
||||
|
@ -182,42 +185,42 @@ router.get('/twemoji-badge/(.*)', async ctx => {
|
|||
ctx.set('Cache-Control', 'max-age=2592000');
|
||||
ctx.set('Content-Type', 'image/png');
|
||||
ctx.body = buffer;
|
||||
});
|
||||
});
|
||||
|
||||
// ServiceWorker
|
||||
router.get(`/sw.js`, async ctx => {
|
||||
// ServiceWorker
|
||||
router.get(`/sw.js`, async ctx => {
|
||||
await send(ctx as any, `/sw.js`, {
|
||||
root: swAssets,
|
||||
maxage: 10 * MINUTE,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Manifest
|
||||
router.get('/manifest.json', manifestHandler);
|
||||
// Manifest
|
||||
router.get('/manifest.json', manifestHandler);
|
||||
|
||||
router.get('/robots.txt', async ctx => {
|
||||
router.get('/robots.txt', async ctx => {
|
||||
await send(ctx as any, '/robots.txt', {
|
||||
root: staticAssets,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
//#endregion
|
||||
//#endregion
|
||||
|
||||
// Docs
|
||||
router.get('/api-doc', async ctx => {
|
||||
// Docs
|
||||
router.get('/api-doc', async ctx => {
|
||||
await send(ctx as any, '/redoc.html', {
|
||||
root: staticAssets,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// URL preview endpoint
|
||||
router.get('/url', urlPreviewHandler);
|
||||
// URL preview endpoint
|
||||
router.get('/url', urlPreviewHandler);
|
||||
|
||||
router.get('/api.json', async ctx => {
|
||||
router.get('/api.json', async ctx => {
|
||||
ctx.body = genOpenapiSpec();
|
||||
});
|
||||
});
|
||||
|
||||
const getFeed = async (acct: string) => {
|
||||
const getFeed = async (acct: string) => {
|
||||
const meta = await fetchMeta();
|
||||
if (meta.privateMode) {
|
||||
return;
|
||||
|
@ -230,10 +233,10 @@ const getFeed = async (acct: string) => {
|
|||
});
|
||||
|
||||
return user && await packFeed(user);
|
||||
};
|
||||
};
|
||||
|
||||
// Atom
|
||||
router.get('/@:user.atom', async ctx => {
|
||||
// Atom
|
||||
router.get('/@:user.atom', async ctx => {
|
||||
const feed = await getFeed(ctx.params.user);
|
||||
|
||||
if (feed) {
|
||||
|
@ -242,10 +245,10 @@ router.get('/@:user.atom', async ctx => {
|
|||
} else {
|
||||
ctx.status = 404;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// RSS
|
||||
router.get('/@:user.rss', async ctx => {
|
||||
// RSS
|
||||
router.get('/@:user.rss', async ctx => {
|
||||
const feed = await getFeed(ctx.params.user);
|
||||
|
||||
if (feed) {
|
||||
|
@ -254,10 +257,10 @@ router.get('/@:user.rss', async ctx => {
|
|||
} else {
|
||||
ctx.status = 404;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// JSON
|
||||
router.get('/@:user.json', async ctx => {
|
||||
// JSON
|
||||
router.get('/@:user.json', async ctx => {
|
||||
const feed = await getFeed(ctx.params.user);
|
||||
|
||||
if (feed) {
|
||||
|
@ -266,11 +269,11 @@ router.get('/@:user.json', async ctx => {
|
|||
} else {
|
||||
ctx.status = 404;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
//#region SSR (for crawlers)
|
||||
// User
|
||||
router.get(['/@:user', '/@:user/:sub'], async (ctx, next) => {
|
||||
//#region SSR (for crawlers)
|
||||
// User
|
||||
router.get(['/@:user', '/@:user/:sub'], async (ctx, next) => {
|
||||
const { username, host } = Acct.parse(ctx.params.user);
|
||||
const user = await Users.findOneBy({
|
||||
usernameLower: username.toLowerCase(),
|
||||
|
@ -295,6 +298,7 @@ router.get(['/@:user', '/@:user/:sub'], async (ctx, next) => {
|
|||
icon: meta.iconUrl,
|
||||
themeColor: meta.themeColor,
|
||||
privateMode: meta.privateMode,
|
||||
nowDateMs: nowDateMs,
|
||||
});
|
||||
ctx.set('Cache-Control', 'public, max-age=15');
|
||||
} else {
|
||||
|
@ -302,9 +306,9 @@ router.get(['/@:user', '/@:user/:sub'], async (ctx, next) => {
|
|||
// モデレータがAPI経由で参照可能にするために404にはしない
|
||||
await next();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/users/:user', async ctx => {
|
||||
router.get('/users/:user', async ctx => {
|
||||
const user = await Users.findOneBy({
|
||||
id: ctx.params.user,
|
||||
host: IsNull(),
|
||||
|
@ -317,10 +321,10 @@ router.get('/users/:user', async ctx => {
|
|||
}
|
||||
|
||||
ctx.redirect(`/@${user.username}${ user.host == null ? '' : '@' + user.host}`);
|
||||
});
|
||||
});
|
||||
|
||||
// Note
|
||||
router.get('/notes/:note', async (ctx, next) => {
|
||||
// Note
|
||||
router.get('/notes/:note', async (ctx, next) => {
|
||||
const note = await Notes.findOneBy({
|
||||
id: ctx.params.note,
|
||||
visibility: In(['public', 'home']),
|
||||
|
@ -340,6 +344,7 @@ router.get('/notes/:note', async (ctx, next) => {
|
|||
icon: meta.iconUrl,
|
||||
privateMode: meta.privateMode,
|
||||
themeColor: meta.themeColor,
|
||||
nowDateMs: nowDateMs,
|
||||
});
|
||||
|
||||
ctx.set('Cache-Control', 'public, max-age=15');
|
||||
|
@ -348,10 +353,10 @@ router.get('/notes/:note', async (ctx, next) => {
|
|||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
});
|
||||
|
||||
// Page
|
||||
router.get('/@:user/pages/:page', async (ctx, next) => {
|
||||
// Page
|
||||
router.get('/@:user/pages/:page', async (ctx, next) => {
|
||||
const { username, host } = Acct.parse(ctx.params.user);
|
||||
const user = await Users.findOneBy({
|
||||
usernameLower: username.toLowerCase(),
|
||||
|
@ -377,6 +382,7 @@ router.get('/@:user/pages/:page', async (ctx, next) => {
|
|||
icon: meta.iconUrl,
|
||||
themeColor: meta.themeColor,
|
||||
privateMode: meta.privateMode,
|
||||
nowDateMs: nowDateMs,
|
||||
});
|
||||
|
||||
if (['public'].includes(page.visibility)) {
|
||||
|
@ -389,11 +395,11 @@ router.get('/@:user/pages/:page', async (ctx, next) => {
|
|||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
});
|
||||
|
||||
// Clip
|
||||
// TODO: 非publicなclipのハンドリング
|
||||
router.get('/clips/:clip', async (ctx, next) => {
|
||||
// Clip
|
||||
// TODO: 非publicなclipのハンドリング
|
||||
router.get('/clips/:clip', async (ctx, next) => {
|
||||
const clip = await Clips.findOneBy({
|
||||
id: ctx.params.clip,
|
||||
});
|
||||
|
@ -410,6 +416,7 @@ router.get('/clips/:clip', async (ctx, next) => {
|
|||
privateMode: meta.privateMode,
|
||||
icon: meta.iconUrl,
|
||||
themeColor: meta.themeColor,
|
||||
nowDateMs: nowDateMs,
|
||||
});
|
||||
|
||||
ctx.set('Cache-Control', 'public, max-age=15');
|
||||
|
@ -418,10 +425,10 @@ router.get('/clips/:clip', async (ctx, next) => {
|
|||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
});
|
||||
|
||||
// Gallery post
|
||||
router.get('/gallery/:post', async (ctx, next) => {
|
||||
// Gallery post
|
||||
router.get('/gallery/:post', async (ctx, next) => {
|
||||
const post = await GalleryPosts.findOneBy({ id: ctx.params.post });
|
||||
|
||||
if (post) {
|
||||
|
@ -436,6 +443,7 @@ router.get('/gallery/:post', async (ctx, next) => {
|
|||
icon: meta.iconUrl,
|
||||
themeColor: meta.themeColor,
|
||||
privateMode: meta.privateMode,
|
||||
nowDateMs: nowDateMs,
|
||||
});
|
||||
|
||||
ctx.set('Cache-Control', 'public, max-age=15');
|
||||
|
@ -444,10 +452,10 @@ router.get('/gallery/:post', async (ctx, next) => {
|
|||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
});
|
||||
|
||||
// Channel
|
||||
router.get('/channels/:channel', async (ctx, next) => {
|
||||
// Channel
|
||||
router.get('/channels/:channel', async (ctx, next) => {
|
||||
const channel = await Channels.findOneBy({
|
||||
id: ctx.params.channel,
|
||||
});
|
||||
|
@ -461,6 +469,7 @@ router.get('/channels/:channel', async (ctx, next) => {
|
|||
icon: meta.iconUrl,
|
||||
themeColor: meta.themeColor,
|
||||
privateMode: meta.privateMode,
|
||||
nowDateMs: nowDateMs,
|
||||
});
|
||||
|
||||
ctx.set('Cache-Control', 'public, max-age=15');
|
||||
|
@ -469,10 +478,10 @@ router.get('/channels/:channel', async (ctx, next) => {
|
|||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
//#endregion
|
||||
});
|
||||
//#endregion
|
||||
|
||||
router.get('/_info_card_', async ctx => {
|
||||
router.get('/_info_card_', async ctx => {
|
||||
const meta = await fetchMeta(true);
|
||||
if (meta.privateMode) {
|
||||
ctx.status = 403;
|
||||
|
@ -488,35 +497,35 @@ router.get('/_info_card_', async ctx => {
|
|||
originalUsersCount: await Users.countBy({ host: IsNull() }),
|
||||
originalNotesCount: await Notes.countBy({ userHost: IsNull() }),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/bios', async ctx => {
|
||||
router.get('/bios', async ctx => {
|
||||
await ctx.render('bios', {
|
||||
version: config.version,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/cli', async ctx => {
|
||||
router.get('/cli', async ctx => {
|
||||
await ctx.render('cli', {
|
||||
version: config.version,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
const override = (source: string, target: string, depth = 0) =>
|
||||
const override = (source: string, target: string, depth = 0) =>
|
||||
[, ...target.split('/').filter(x => x), ...source.split('/').filter(x => x).splice(depth)].join('/');
|
||||
|
||||
router.get('/flush', async ctx => {
|
||||
router.get('/flush', async ctx => {
|
||||
await ctx.render('flush');
|
||||
});
|
||||
});
|
||||
|
||||
// streamingに非WebSocketリクエストが来た場合にbase htmlをキャシュ付きで返すと、Proxy等でそのパスがキャッシュされておかしくなる
|
||||
router.get('/streaming', async ctx => {
|
||||
// streamingに非WebSocketリクエストが来た場合にbase htmlをキャシュ付きで返すと、Proxy等でそのパスがキャッシュされておかしくなる
|
||||
router.get('/streaming', async ctx => {
|
||||
ctx.status = 503;
|
||||
ctx.set('Cache-Control', 'private, max-age=0');
|
||||
});
|
||||
});
|
||||
|
||||
// Render base html for all requests
|
||||
router.get('(.*)', async ctx => {
|
||||
// Render base html for all requests
|
||||
router.get('(.*)', async ctx => {
|
||||
const meta = await fetchMeta();
|
||||
let motd = ['Loading...'];
|
||||
if (meta.customMOTD.length > 0) {
|
||||
|
@ -526,7 +535,6 @@ router.get('(.*)', async ctx => {
|
|||
if (meta.customSplashIcons.length > 0) {
|
||||
splashIconUrl = meta.customSplashIcons[Math.floor(Math.random() * meta.customSplashIcons.length)];
|
||||
}
|
||||
const nowDateMs = Date.now();
|
||||
await ctx.render('base', {
|
||||
img: meta.bannerUrl,
|
||||
title: meta.name || 'Calckey',
|
||||
|
@ -540,9 +548,10 @@ router.get('(.*)', async ctx => {
|
|||
nowDateMs: nowDateMs,
|
||||
});
|
||||
ctx.set('Cache-Control', 'public, max-age=3');
|
||||
});
|
||||
});
|
||||
|
||||
// Register router
|
||||
app.use(router.routes());
|
||||
// Register router
|
||||
app.use(router.routes());
|
||||
|
||||
export default app;
|
||||
|
||||
export default app;
|
||||
|
|
Loading…
Reference in New Issue