Skip to content

Commit

Permalink
feat(backend): suspend type detection
Browse files Browse the repository at this point in the history
  • Loading branch information
anatawa12 committed May 22, 2024
1 parent 79a05a9 commit 1381222
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/

export class SuspendedStateInsteadOfIsSspended1716345771510 {
name = 'SuspendedStateInsteadOfIsSspended1716345771510'

async up(queryRunner) {
await queryRunner.query(`CREATE TYPE "public"."instance_suspendedstate_enum" AS ENUM('none', 'manuallySuspended', 'goneSuspended', 'autoSuspendedForNotResponding')`);

await queryRunner.query(`DROP INDEX "public"."IDX_34500da2e38ac393f7bb6b299c"`);

await queryRunner.query(`ALTER TABLE "instance" RENAME COLUMN "isSuspended" TO "suspendedState"`);

await queryRunner.query(`ALTER TABLE "instance" ALTER COLUMN "suspendedState" DROP DEFAULT`);

await queryRunner.query(`ALTER TABLE "instance" ALTER COLUMN "suspendedState" TYPE "public"."instance_suspendedstate_enum" USING (
CASE "suspendedState"
WHEN TRUE THEN 'manuallySuspended'::instance_suspendedstate_enum
ELSE 'none'::instance_suspendedstate_enum
END
)`);
//await queryRunner.query(`ALTER TABLE "instance" ALTER COLUMN "suspendedState" TYPE "public"."instance_suspendedstate_enum" USING "suspendedState"::instance_suspendedstate_enum`);

await queryRunner.query(`ALTER TABLE "instance" ALTER COLUMN "suspendedState" SET DEFAULT 'none'`);

await queryRunner.query(`CREATE INDEX "IDX_f6d861bb95acd4639c5015d00c" ON "instance" ("suspendedState") `);
}

async down(queryRunner) {
await queryRunner.query(`DROP INDEX "public"."IDX_f6d861bb95acd4639c5015d00c"`);

await queryRunner.query(`ALTER TABLE "instance" ALTER COLUMN "suspendedState" DROP DEFAULT`);

// await queryRunner.query(`ALTER TABLE "instance" ALTER COLUMN "suspendedState" TYPE boolean`);
await queryRunner.query(`ALTER TABLE "instance" ALTER COLUMN "suspendedState" TYPE boolean USING (
CASE "suspendedState"
WHEN 'none'::instance_suspendedstate_enum THEN FALSE
ELSE TRUE
END
)`);

await queryRunner.query(`ALTER TABLE "instance" ALTER COLUMN "suspendedState" SET DEFAULT false`);

await queryRunner.query(`ALTER TABLE "instance" RENAME COLUMN "suspendedState" TO "isSuspended"`);

await queryRunner.query(`CREATE INDEX "IDX_34500da2e38ac393f7bb6b299c" ON "instance" ("isSuspended") `);

await queryRunner.query(`DROP TYPE "public"."instance_suspendedstate_enum"`);
}
}
3 changes: 2 additions & 1 deletion packages/backend/src/core/entities/InstanceEntityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export class InstanceEntityService {
followingCount: instance.followingCount,
followersCount: instance.followersCount,
isNotResponding: instance.isNotResponding,
isSuspended: instance.isSuspended,
isSuspended: instance.suspendedState != 'none',
suspendedState: instance.suspendedState,
isBlocked: this.utilityService.isBlockedHost(meta.blockedHosts, instance.host),
softwareName: instance.softwareName,
softwareVersion: instance.softwareVersion,
Expand Down
9 changes: 5 additions & 4 deletions packages/backend/src/models/Instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ export class MiInstance {
public notRespondingSince: Date | null;

/**
* このインスタンスへの配信を停止するか
* このインスタンスへの配信状態
*/
@Index()
@Column('boolean', {
default: false,
@Column('enum', {
default: 'none',
enum: ['none', 'manuallySuspended', 'goneSuspended', 'autoSuspendedForNotResponding'],
})
public isSuspended: boolean;
public suspendedState: 'none' | 'manuallySuspended' | 'goneSuspended' | 'autoSuspendedForNotResponding';

@Column('varchar', {
length: 64, nullable: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export const packedFederationInstanceSchema = {
type: 'boolean',
optional: false, nullable: false,
},
suspendedState: {
type: 'string',
nullable: false, optional: false,
enum: ['none', 'manuallySuspended', 'goneSuspended', 'autoSuspendedForNotResponding'],
},
isBlocked: {
type: 'boolean',
optional: false, nullable: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { UtilityService } from '@/core/UtilityService.js';
import { bindThis } from '@/decorators.js';
import { QueueLoggerService } from '../QueueLoggerService.js';
import type { DeliverJobData } from '../types.js';
import {Not} from "typeorm";

@Injectable()
export class DeliverProcessorService {
Expand Down Expand Up @@ -62,7 +63,7 @@ export class DeliverProcessorService {
if (suspendedHosts == null) {
suspendedHosts = await this.instancesRepository.find({
where: {
isSuspended: true,
suspendedState: Not("none"),
},
});
this.suspendedHostsCache.set(suspendedHosts);
Expand Down Expand Up @@ -103,9 +104,9 @@ export class DeliverProcessorService {
});
} else if (i.notRespondingSince) {
// 1週間以上不通ならサスペンド
if (i.notRespondingSince.getTime() <= Date.now() - 1000 * 60 * 60 * 24 * 7) {
if (i.suspendedState == 'none' && i.notRespondingSince.getTime() <= Date.now() - 1000 * 60 * 60 * 24 * 7) {
this.federatedInstanceService.update(i.id, {
isSuspended: true,
suspendedState: 'autoSuspendedForNotResponding',
});
}
}
Expand All @@ -125,7 +126,7 @@ export class DeliverProcessorService {
if (job.data.isSharedInbox && res.statusCode === 410) {
this.federatedInstanceService.fetch(host).then(i => {
this.federatedInstanceService.update(i.id, {
isSuspended: true,
suspendedState: 'goneSuspended',
});
});
throw new Bull.UnrecoverableError(`${host} is gone`);
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/server/api/ApiServerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export class ApiServerService {
const instances = await this.instancesRepository.find({
select: ['host'],
where: {
isSuspended: false,
suspendedState: 'none',
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
throw new Error('instance not found');
}

let isSuspendedBefore = instance.suspendedState !== 'none';
let suspendedState: undefined | 'manuallySuspended' | 'none';

if (ps.isSuspended != null && isSuspendedBefore !== ps.isSuspended) {
suspendedState = ps.isSuspended ? 'manuallySuspended' : 'none';
}

await this.federatedInstanceService.update(instance.id, {
isSuspended: ps.isSuspended,
suspendedState,
moderationNote: ps.moderationNote,
});

if (ps.isSuspended != null && instance.isSuspended !== ps.isSuspended) {
if (ps.isSuspended != null && isSuspendedBefore !== ps.isSuspended) {
if (ps.isSuspended) {
this.moderationLogService.log(me, 'suspendRemoteInstance', {
id: instance.id,
Expand Down
2 changes: 2 additions & 0 deletions packages/misskey-js/src/autogen/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4475,6 +4475,8 @@ export type components = {
followersCount: number;
isNotResponding: boolean;
isSuspended: boolean;
/** @enum {string} */
suspendedState: 'none' | 'manuallySuspended' | 'goneSuspended' | 'autoSuspendedForNotResponding';
isBlocked: boolean;
/** @example misskey */
softwareName: string | null;
Expand Down

0 comments on commit 1381222

Please sign in to comment.