From 4e10e94efd23192df770979e1a9c8605925da27d Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 25 Mar 2022 13:12:12 +0900 Subject: [PATCH] wip --- .../backend/src/models/repositories/user.ts | 59 ------------------- .../activitypub/kernel/announce/note.ts | 4 ++ .../src/remote/activitypub/kernel/index.ts | 2 +- .../src/remote/activitypub/models/image.ts | 2 +- .../src/remote/activitypub/models/note.ts | 2 +- packages/backend/src/server/api/call.ts | 2 +- .../api/endpoints/drive/files/delete.ts | 2 +- .../server/api/endpoints/drive/files/show.ts | 2 +- .../api/endpoints/drive/files/update.ts | 2 +- .../src/server/api/endpoints/notes/delete.ts | 2 +- .../api/endpoints/notes/global-timeline.ts | 2 +- .../api/endpoints/notes/hybrid-timeline.ts | 2 +- .../api/endpoints/notes/local-timeline.ts | 2 +- .../src/server/api/endpoints/users/show.ts | 2 +- packages/backend/src/services/note/create.ts | 2 +- 15 files changed, 17 insertions(+), 72 deletions(-) diff --git a/packages/backend/src/models/repositories/user.ts b/packages/backend/src/models/repositories/user.ts index 7f971f534..17b7987a4 100644 --- a/packages/backend/src/models/repositories/user.ts +++ b/packages/backend/src/models/repositories/user.ts @@ -41,65 +41,6 @@ export class UserRepository extends Repository { public validateBirthday = ajv.compile(this.birthdaySchema); //#endregion - private suspendedUsersCache: Set = new Set(); - private silencedUsersCache: Set = new Set(); - private moderatorsCache: Set = new Set(); - - constructor() { - super(); - - const fetchCache = () => { - this.find({ - where: { - isSuspended: true, - }, - select: ['id'], - }).then(users => { - this.suspendedUsersCache = new Set(users.map(user => user.id)); - }); - - this.find({ - where: { - isSilenced: true, - }, - select: ['id'], - }).then(users => { - this.silencedUsersCache = new Set(users.map(user => user.id)); - }); - - this.find({ - where: [{ - isAdmin: true, - }, { - isModerator: true, - }], - select: ['id'], - }).then(users => { - this.moderatorsCache = new Set(users.map(user => user.id)); - }); - }; - - setImmediate(() => { - fetchCache(); - }); - - setInterval(() => { - fetchCache(); - }, 1000 * 60 * 5); - } - - public checkSuspended(userId: User['id']): boolean { - return this.suspendedUsersCache.has(userId); - } - - public checkSilenced(userId: User['id']): boolean { - return this.silencedUsersCache.has(userId); - } - - public checkModerator(userId: User['id']): boolean { - return this.moderatorsCache.has(userId); - } - public async getRelation(me: User['id'], target: User['id']) { const [following1, following2, followReq1, followReq2, toBlocking, fromBlocked, mute] = await Promise.all([ Followings.findOne({ diff --git a/packages/backend/src/remote/activitypub/kernel/announce/note.ts b/packages/backend/src/remote/activitypub/kernel/announce/note.ts index 43585f36c..680749f4d 100644 --- a/packages/backend/src/remote/activitypub/kernel/announce/note.ts +++ b/packages/backend/src/remote/activitypub/kernel/announce/note.ts @@ -18,6 +18,10 @@ const logger = apLogger; export default async function(resolver: Resolver, actor: CacheableRemoteUser, activity: IAnnounce, targetUri: string): Promise { const uri = getApId(activity); + if (actor.isSuspended) { + return; + } + // アナウンス先をブロックしてたら中断 const meta = await fetchMeta(); if (meta.blockedHosts.includes(extractDbHost(uri))) return; diff --git a/packages/backend/src/remote/activitypub/kernel/index.ts b/packages/backend/src/remote/activitypub/kernel/index.ts index 423a1c773..254a12160 100644 --- a/packages/backend/src/remote/activitypub/kernel/index.ts +++ b/packages/backend/src/remote/activitypub/kernel/index.ts @@ -38,7 +38,7 @@ export async function performActivity(actor: CacheableRemoteUser, activity: IObj } async function performOneActivity(actor: CacheableRemoteUser, activity: IObject): Promise { - if (Users.checkSuspended(actor.id)) return; + if (actor.isSuspended) return; if (isCreate(activity)) { await create(actor, activity); diff --git a/packages/backend/src/remote/activitypub/models/image.ts b/packages/backend/src/remote/activitypub/models/image.ts index 1bdb769a6..33316dcd9 100644 --- a/packages/backend/src/remote/activitypub/models/image.ts +++ b/packages/backend/src/remote/activitypub/models/image.ts @@ -15,7 +15,7 @@ const logger = apLogger; */ export async function createImage(actor: CacheableRemoteUser, value: any): Promise { // 投稿者が凍結されていたらスキップ - if (Users.checkSuspended(actor.id)) { + if (actor.isSuspended) { throw new Error('actor has been suspended'); } diff --git a/packages/backend/src/remote/activitypub/models/note.ts b/packages/backend/src/remote/activitypub/models/note.ts index b978798ad..bdcec6b25 100644 --- a/packages/backend/src/remote/activitypub/models/note.ts +++ b/packages/backend/src/remote/activitypub/models/note.ts @@ -93,7 +93,7 @@ export async function createNote(value: string | IObject, resolver?: Resolver, s const actor = await resolvePerson(getOneApId(note.attributedTo), resolver) as CacheableRemoteUser; // 投稿者が凍結されていたらスキップ - if (Users.checkSuspended(actor.id)) { + if (actor.isSuspended) { throw new Error('actor has been suspended'); } diff --git a/packages/backend/src/server/api/call.ts b/packages/backend/src/server/api/call.ts index 28b58dd7e..fe9fb352c 100644 --- a/packages/backend/src/server/api/call.ts +++ b/packages/backend/src/server/api/call.ts @@ -41,7 +41,7 @@ export default async (endpoint: string, user: CacheableLocalUser | null | undefi }); } - if (ep.meta.requireCredential && Users.checkSuspended(user!.id)) { + if (ep.meta.requireCredential && user!.isSuspended) { throw new ApiError({ message: 'Your account has been suspended.', code: 'YOUR_ACCOUNT_SUSPENDED', diff --git a/packages/backend/src/server/api/endpoints/drive/files/delete.ts b/packages/backend/src/server/api/endpoints/drive/files/delete.ts index 660e757ed..eac8eabfc 100644 --- a/packages/backend/src/server/api/endpoints/drive/files/delete.ts +++ b/packages/backend/src/server/api/endpoints/drive/files/delete.ts @@ -42,7 +42,7 @@ export default define(meta, paramDef, async (ps, user) => { throw new ApiError(meta.errors.noSuchFile); } - if (!Users.checkModerator(user.id) && (file.userId !== user.id)) { + if ((!user.isAdmin && !user.isModerator) && (file.userId !== user.id)) { throw new ApiError(meta.errors.accessDenied); } diff --git a/packages/backend/src/server/api/endpoints/drive/files/show.ts b/packages/backend/src/server/api/endpoints/drive/files/show.ts index fe2d35ed2..16b313cab 100644 --- a/packages/backend/src/server/api/endpoints/drive/files/show.ts +++ b/packages/backend/src/server/api/endpoints/drive/files/show.ts @@ -70,7 +70,7 @@ export default define(meta, paramDef, async (ps, user) => { throw new ApiError(meta.errors.noSuchFile); } - if (!Users.checkModerator(user.id) && (file.userId !== user.id)) { + if ((!user.isAdmin && !user.isModerator) && (file.userId !== user.id)) { throw new ApiError(meta.errors.accessDenied); } diff --git a/packages/backend/src/server/api/endpoints/drive/files/update.ts b/packages/backend/src/server/api/endpoints/drive/files/update.ts index 56d2b385e..30d7847b5 100644 --- a/packages/backend/src/server/api/endpoints/drive/files/update.ts +++ b/packages/backend/src/server/api/endpoints/drive/files/update.ts @@ -64,7 +64,7 @@ export default define(meta, paramDef, async (ps, user) => { throw new ApiError(meta.errors.noSuchFile); } - if (!Users.checkModerator(user.id) && (file.userId !== user.id)) { + if ((!user.isAdmin && !user.isModerator) && (file.userId !== user.id)) { throw new ApiError(meta.errors.accessDenied); } diff --git a/packages/backend/src/server/api/endpoints/notes/delete.ts b/packages/backend/src/server/api/endpoints/notes/delete.ts index c4471f0ab..a1ab06d46 100644 --- a/packages/backend/src/server/api/endpoints/notes/delete.ts +++ b/packages/backend/src/server/api/endpoints/notes/delete.ts @@ -48,7 +48,7 @@ export default define(meta, paramDef, async (ps, user) => { throw e; }); - if (!Users.checkModerator(user.id) && (note.userId !== user.id)) { + if ((!user.isAdmin && !user.isModerator) && (note.userId !== user.id)) { throw new ApiError(meta.errors.accessDenied); } diff --git a/packages/backend/src/server/api/endpoints/notes/global-timeline.ts b/packages/backend/src/server/api/endpoints/notes/global-timeline.ts index e60e706fd..09a819466 100644 --- a/packages/backend/src/server/api/endpoints/notes/global-timeline.ts +++ b/packages/backend/src/server/api/endpoints/notes/global-timeline.ts @@ -49,7 +49,7 @@ export const paramDef = { export default define(meta, paramDef, async (ps, user) => { const m = await fetchMeta(); if (m.disableGlobalTimeline) { - if (user == null || !Users.checkModerator(user.id)) { + if (user == null || (!user.isAdmin && !user.isModerator)) { throw new ApiError(meta.errors.gtlDisabled); } } diff --git a/packages/backend/src/server/api/endpoints/notes/hybrid-timeline.ts b/packages/backend/src/server/api/endpoints/notes/hybrid-timeline.ts index 4f3e1d9cc..7c9c12296 100644 --- a/packages/backend/src/server/api/endpoints/notes/hybrid-timeline.ts +++ b/packages/backend/src/server/api/endpoints/notes/hybrid-timeline.ts @@ -56,7 +56,7 @@ export const paramDef = { // eslint-disable-next-line import/no-default-export export default define(meta, paramDef, async (ps, user) => { const m = await fetchMeta(); - if (m.disableLocalTimeline && !Users.checkModerator(user.id)) { + if (m.disableLocalTimeline && (!user.isAdmin && !user.isModerator)) { throw new ApiError(meta.errors.stlDisabled); } diff --git a/packages/backend/src/server/api/endpoints/notes/local-timeline.ts b/packages/backend/src/server/api/endpoints/notes/local-timeline.ts index be308cd09..bb0bbe2a2 100644 --- a/packages/backend/src/server/api/endpoints/notes/local-timeline.ts +++ b/packages/backend/src/server/api/endpoints/notes/local-timeline.ts @@ -55,7 +55,7 @@ export const paramDef = { export default define(meta, paramDef, async (ps, user) => { const m = await fetchMeta(); if (m.disableLocalTimeline) { - if (user == null || !Users.checkModerator(user.id)) { + if (user == null || (!user.isAdmin && !user.isModerator)) { throw new ApiError(meta.errors.ltlDisabled); } } diff --git a/packages/backend/src/server/api/endpoints/users/show.ts b/packages/backend/src/server/api/endpoints/users/show.ts index d2545ceee..263c102a7 100644 --- a/packages/backend/src/server/api/endpoints/users/show.ts +++ b/packages/backend/src/server/api/endpoints/users/show.ts @@ -61,7 +61,7 @@ export const paramDef = { export default define(meta, paramDef, async (ps, me) => { let user; - const isAdminOrModerator = me && Users.checkModerator(me.id); + const isAdminOrModerator = me && (me.isAdmin || me.isModerator); if (ps.userIds) { if (ps.userIds.length === 0) { diff --git a/packages/backend/src/services/note/create.ts b/packages/backend/src/services/note/create.ts index 605f9e54a..98bcc88ec 100644 --- a/packages/backend/src/services/note/create.ts +++ b/packages/backend/src/services/note/create.ts @@ -150,7 +150,7 @@ export default async (user: { id: User['id']; username: User['username']; host: if (data.channel != null) data.localOnly = true; // サイレンス - if (Users.checkSilenced(user.id) && data.visibility === 'public' && data.channel == null) { + if (user.isSilenced && data.visibility === 'public' && data.channel == null) { data.visibility = 'home'; }