This commit is contained in:
syuilo 2022-03-25 13:12:12 +09:00
parent 714d3f6977
commit 4e10e94efd
15 changed files with 17 additions and 72 deletions

View File

@ -41,65 +41,6 @@ export class UserRepository extends Repository<User> {
public validateBirthday = ajv.compile(this.birthdaySchema); public validateBirthday = ajv.compile(this.birthdaySchema);
//#endregion //#endregion
private suspendedUsersCache: Set<User['id']> = new Set();
private silencedUsersCache: Set<User['id']> = new Set();
private moderatorsCache: Set<User['id']> = 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']) { public async getRelation(me: User['id'], target: User['id']) {
const [following1, following2, followReq1, followReq2, toBlocking, fromBlocked, mute] = await Promise.all([ const [following1, following2, followReq1, followReq2, toBlocking, fromBlocked, mute] = await Promise.all([
Followings.findOne({ Followings.findOne({

View File

@ -18,6 +18,10 @@ const logger = apLogger;
export default async function(resolver: Resolver, actor: CacheableRemoteUser, activity: IAnnounce, targetUri: string): Promise<void> { export default async function(resolver: Resolver, actor: CacheableRemoteUser, activity: IAnnounce, targetUri: string): Promise<void> {
const uri = getApId(activity); const uri = getApId(activity);
if (actor.isSuspended) {
return;
}
// アナウンス先をブロックしてたら中断 // アナウンス先をブロックしてたら中断
const meta = await fetchMeta(); const meta = await fetchMeta();
if (meta.blockedHosts.includes(extractDbHost(uri))) return; if (meta.blockedHosts.includes(extractDbHost(uri))) return;

View File

@ -38,7 +38,7 @@ export async function performActivity(actor: CacheableRemoteUser, activity: IObj
} }
async function performOneActivity(actor: CacheableRemoteUser, activity: IObject): Promise<void> { async function performOneActivity(actor: CacheableRemoteUser, activity: IObject): Promise<void> {
if (Users.checkSuspended(actor.id)) return; if (actor.isSuspended) return;
if (isCreate(activity)) { if (isCreate(activity)) {
await create(actor, activity); await create(actor, activity);

View File

@ -15,7 +15,7 @@ const logger = apLogger;
*/ */
export async function createImage(actor: CacheableRemoteUser, value: any): Promise<DriveFile> { export async function createImage(actor: CacheableRemoteUser, value: any): Promise<DriveFile> {
// 投稿者が凍結されていたらスキップ // 投稿者が凍結されていたらスキップ
if (Users.checkSuspended(actor.id)) { if (actor.isSuspended) {
throw new Error('actor has been suspended'); throw new Error('actor has been suspended');
} }

View File

@ -93,7 +93,7 @@ export async function createNote(value: string | IObject, resolver?: Resolver, s
const actor = await resolvePerson(getOneApId(note.attributedTo), resolver) as CacheableRemoteUser; 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'); throw new Error('actor has been suspended');
} }

View File

@ -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({ throw new ApiError({
message: 'Your account has been suspended.', message: 'Your account has been suspended.',
code: 'YOUR_ACCOUNT_SUSPENDED', code: 'YOUR_ACCOUNT_SUSPENDED',

View File

@ -42,7 +42,7 @@ export default define(meta, paramDef, async (ps, user) => {
throw new ApiError(meta.errors.noSuchFile); 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); throw new ApiError(meta.errors.accessDenied);
} }

View File

@ -70,7 +70,7 @@ export default define(meta, paramDef, async (ps, user) => {
throw new ApiError(meta.errors.noSuchFile); 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); throw new ApiError(meta.errors.accessDenied);
} }

View File

@ -64,7 +64,7 @@ export default define(meta, paramDef, async (ps, user) => {
throw new ApiError(meta.errors.noSuchFile); 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); throw new ApiError(meta.errors.accessDenied);
} }

View File

@ -48,7 +48,7 @@ export default define(meta, paramDef, async (ps, user) => {
throw e; 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); throw new ApiError(meta.errors.accessDenied);
} }

View File

@ -49,7 +49,7 @@ export const paramDef = {
export default define(meta, paramDef, async (ps, user) => { export default define(meta, paramDef, async (ps, user) => {
const m = await fetchMeta(); const m = await fetchMeta();
if (m.disableGlobalTimeline) { if (m.disableGlobalTimeline) {
if (user == null || !Users.checkModerator(user.id)) { if (user == null || (!user.isAdmin && !user.isModerator)) {
throw new ApiError(meta.errors.gtlDisabled); throw new ApiError(meta.errors.gtlDisabled);
} }
} }

View File

@ -56,7 +56,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export // eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => { export default define(meta, paramDef, async (ps, user) => {
const m = await fetchMeta(); const m = await fetchMeta();
if (m.disableLocalTimeline && !Users.checkModerator(user.id)) { if (m.disableLocalTimeline && (!user.isAdmin && !user.isModerator)) {
throw new ApiError(meta.errors.stlDisabled); throw new ApiError(meta.errors.stlDisabled);
} }

View File

@ -55,7 +55,7 @@ export const paramDef = {
export default define(meta, paramDef, async (ps, user) => { export default define(meta, paramDef, async (ps, user) => {
const m = await fetchMeta(); const m = await fetchMeta();
if (m.disableLocalTimeline) { if (m.disableLocalTimeline) {
if (user == null || !Users.checkModerator(user.id)) { if (user == null || (!user.isAdmin && !user.isModerator)) {
throw new ApiError(meta.errors.ltlDisabled); throw new ApiError(meta.errors.ltlDisabled);
} }
} }

View File

@ -61,7 +61,7 @@ export const paramDef = {
export default define(meta, paramDef, async (ps, me) => { export default define(meta, paramDef, async (ps, me) => {
let user; let user;
const isAdminOrModerator = me && Users.checkModerator(me.id); const isAdminOrModerator = me && (me.isAdmin || me.isModerator);
if (ps.userIds) { if (ps.userIds) {
if (ps.userIds.length === 0) { if (ps.userIds.length === 0) {

View File

@ -150,7 +150,7 @@ export default async (user: { id: User['id']; username: User['username']; host:
if (data.channel != null) data.localOnly = true; 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'; data.visibility = 'home';
} }