perf(server): reduce db query

This commit is contained in:
syuilo
2022-03-21 05:21:37 +09:00
parent 78736c70f7
commit 6f2e93c6a1
6 changed files with 30 additions and 17 deletions

View File

@@ -18,6 +18,7 @@ import { ILocalUser, User } from '@/models/entities/user.js';
import { In } from 'typeorm';
import { renderLike } from '@/remote/activitypub/renderer/like.js';
import { getUserKeypair } from '@/misc/keypair-store.js';
import { noteCache, userCache } from './activitypub/cache.js';
// Init router
const router = new Router();
@@ -65,11 +66,12 @@ router.post('/users/:user/inbox', json(), inbox);
router.get('/notes/:note', async (ctx, next) => {
if (!isActivityPubReq(ctx)) return await next();
const note = await Notes.findOne({
// TODO: typeorm 3.0にしたら .then(x => x || null) は消せる
const note = await noteCache.fetch(ctx.params.note, () => Notes.findOne({
id: ctx.params.note,
visibility: In(['public' as const, 'home' as const]),
localOnly: false,
});
}).then(x => x || null));
if (note == null) {
ctx.status = 404;
@@ -148,7 +150,7 @@ router.get('/users/:user/publickey', async ctx => {
});
// user
async function userInfo(ctx: Router.RouterContext, user: User | undefined) {
async function userInfo(ctx: Router.RouterContext, user: User | undefined | null) {
if (user == null) {
ctx.status = 404;
return;
@@ -164,11 +166,12 @@ router.get('/users/:user', async (ctx, next) => {
const userId = ctx.params.user;
const user = await Users.findOne({
// TODO: typeorm 3.0にしたら .then(x => x || null) は消せる
const user = await userCache.fetch(userId, () => Users.findOne({
id: userId,
host: null,
isSuspended: false,
});
}).then(x => x || null));
await userInfo(ctx, user);
});