Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Scheduled Note Delete #70

Merged
merged 5 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG_engawa.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
### Release Date

### General
-
- ノートの自動削除機能を追加

### Client
-
Expand Down
4 changes: 4 additions & 0 deletions locales/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ export interface Locale extends ILocale {
* リアクションを削除
*/
"removeReaction": string;
/**
* ノートの削除を予約
*/
"scheduledNoteDelete": string;
/**
* ニャ化を表示しない
*/
Expand Down
1 change: 1 addition & 0 deletions locales/ja-JP.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ opacity: "不透明度"
noteUpdatedAt: "編集済み: {date} {time}"
editReaction: "リアクションを編集"
removeReaction: "リアクションを削除"
scheduledNoteDelete: "ノートの削除を予約"
noNyaization: "ニャ化を表示しない"
revertNoNyaization: "ニャ化を含めて表示する"
viewTextSource: "テキストのソースを表示する"
Expand Down
11 changes: 11 additions & 0 deletions packages/backend/migration/1720161864577-AddDeleteAt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class AddDeleteAt1720161864577 {
name = 'AddDeleteAt1720161864577'

async up(queryRunner) {
await queryRunner.query(`ALTER TABLE "note" ADD "deleteAt" TIMESTAMP WITH TIME ZONE`);
}

async down(queryRunner) {
await queryRunner.query(`ALTER TABLE "note" DROP COLUMN "deleteAt"`)
}
}
13 changes: 13 additions & 0 deletions packages/backend/src/core/NoteCreateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import { isReply } from '@/misc/is-reply.js';
import { trackPromise } from '@/misc/promise-tracker.js';
import { isNotNull } from '@/misc/is-not-null.js';
import { IdentifiableError } from '@/misc/identifiable-error.js';
import { Data } from 'ws';

type NotificationType = 'reply' | 'renote' | 'quote' | 'mention';

Expand Down Expand Up @@ -153,6 +154,7 @@ type Option = {
uri?: string | null;
url?: string | null;
app?: MiApp | null;
deleteAt?: Date | null;
};

@Injectable()
Expand Down Expand Up @@ -427,6 +429,7 @@ export class NoteCreateService implements OnApplicationShutdown {
hasPoll: data.poll != null,
hasEvent: data.event != null,
cw: data.cw ?? null,
deleteAt: data.deleteAt,
tags: tags.map(tag => normalizeForSearch(tag)),
emojis,
userId: user.id,
Expand Down Expand Up @@ -597,6 +600,16 @@ export class NoteCreateService implements OnApplicationShutdown {
});
}

if (data.deleteAt) {
const delay = data.deleteAt.getTime() - Date.now();
this.queueService.scheduledNoteDeleteQueue.add(note.id, {
noteId: note.id,
}, {
delay,
removeOnComplete: true,
});
}

if (!silent) {
if (this.userEntityService.isLocalUser(user)) this.activeUsersChart.write(user);

Expand Down
12 changes: 12 additions & 0 deletions packages/backend/src/core/QueueModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import {
RelationshipJobData,
UserWebhookDeliverJobData,
SystemWebhookDeliverJobData,
ScheduledNoteDeleteJobData
} from '../queue/types.js';
import type { Provider } from '@nestjs/common';

export type SystemQueue = Bull.Queue<Record<string, unknown>>;
export type EndedPollNotificationQueue = Bull.Queue<EndedPollNotificationJobData>;
export type ScheduledNoteDeleteQueue = Bull.Queue<ScheduledNoteDeleteJobData>;
export type DeliverQueue = Bull.Queue<DeliverJobData>;
export type InboxQueue = Bull.Queue<InboxJobData>;
export type DbQueue = Bull.Queue;
Expand All @@ -42,6 +44,12 @@ const $endedPollNotification: Provider = {
inject: [DI.config, DI.redisForJobQueue],
};

const $scheduledNoteDeleted: Provider = {
provide: 'queue:scheduledNoteDelete',
useFactory: (config: Config, redisForJobQueue: Redis.Redis) => new Bull.Queue(QUEUE.SCHEDULED_NOTE_DELETE, baseQueueOptions(config, QUEUE.SCHEDULED_NOTE_DELETE, redisForJobQueue)),
inject: [DI.config, DI.redisForJobQueue],
}

const $deliver: Provider = {
provide: 'queue:deliver',
useFactory: (config: Config, redisForJobQueue: Redis.Redis) => new Bull.Queue(QUEUE.DELIVER, baseQueueOptions(config, QUEUE.DELIVER, redisForJobQueue)),
Expand Down Expand Up @@ -90,6 +98,7 @@ const $systemWebhookDeliver: Provider = {
providers: [
$system,
$endedPollNotification,
$scheduledNoteDeleted,
$deliver,
$inbox,
$db,
Expand All @@ -101,6 +110,7 @@ const $systemWebhookDeliver: Provider = {
exports: [
$system,
$endedPollNotification,
$scheduledNoteDeleted,
$deliver,
$inbox,
$db,
Expand All @@ -114,6 +124,7 @@ export class QueueModule implements OnApplicationShutdown {
constructor(
@Inject('queue:system') public systemQueue: SystemQueue,
@Inject('queue:endedPollNotification') public endedPollNotificationQueue: EndedPollNotificationQueue,
@Inject('queue:scheduledNoteDelete') public scheduledNoteDeleteQueue: ScheduledNoteDeleteQueue,
@Inject('queue:deliver') public deliverQueue: DeliverQueue,
@Inject('queue:inbox') public inboxQueue: InboxQueue,
@Inject('queue:db') public dbQueue: DbQueue,
Expand All @@ -130,6 +141,7 @@ export class QueueModule implements OnApplicationShutdown {
await Promise.all([
this.systemQueue.close(),
this.endedPollNotificationQueue.close(),
this.scheduledNoteDeleteQueue.close(),
this.deliverQueue.close(),
this.inboxQueue.close(),
this.dbQueue.close(),
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/core/QueueService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import type {
SystemQueue,
UserWebhookDeliverQueue,
SystemWebhookDeliverQueue,
ScheduledNoteDeleteQueue,
} from './QueueModule.js';
import type httpSignature from '@peertube/http-signature';
import type * as Bull from 'bullmq';
Expand All @@ -45,6 +46,7 @@ export class QueueService {

@Inject('queue:system') public systemQueue: SystemQueue,
@Inject('queue:endedPollNotification') public endedPollNotificationQueue: EndedPollNotificationQueue,
@Inject('queue:scheduledNoteDelete') public scheduledNoteDeleteQueue: ScheduledNoteDeleteQueue,
@Inject('queue:deliver') public deliverQueue: DeliverQueue,
@Inject('queue:inbox') public inboxQueue: InboxQueue,
@Inject('queue:db') public dbQueue: DbQueue,
Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/core/entities/NoteEntityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ export class NoteEntityService implements OnModuleInit {

poll: note.hasPoll ? this.populatePoll(note, meId) : undefined,
event: note.hasEvent ? this.populateEvent(note) : undefined,
deleteAt: note.deleteAt?.toISOString() ?? undefined,

...(meId && Object.keys(note.reactions).length > 0 ? {
myReaction: this.populateMyReaction(note, meId, options?._hint_),
Expand Down
5 changes: 5 additions & 0 deletions packages/backend/src/models/Note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ export class MiNote {
})
public hasPoll: boolean;

@Column('timestamp with time zone', {
nullable: true,
})
public deleteAt: Date | null;

@Index()
@Column({
...id(),
Expand Down
5 changes: 5 additions & 0 deletions packages/backend/src/models/json-schema/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ export const packedNoteSchema = {
},
},
},
deleteAt: {
type: 'string',
optional: true, nullable: true,
format: 'date-time',
},
emojis: {
type: 'object',
optional: true, nullable: false,
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/queue/QueueProcessorModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { TickChartsProcessorService } from './processors/TickChartsProcessorServ
import { AggregateRetentionProcessorService } from './processors/AggregateRetentionProcessorService.js';
import { ExportFavoritesProcessorService } from './processors/ExportFavoritesProcessorService.js';
import { RelationshipProcessorService } from './processors/RelationshipProcessorService.js';
import { ScheduledNoteDeleteProcessorService } from './processors/ScheduledNoteDeleteProcessorService.js';

@Module({
imports: [
Expand Down Expand Up @@ -77,6 +78,7 @@ import { RelationshipProcessorService } from './processors/RelationshipProcessor
UserWebhookDeliverProcessorService,
SystemWebhookDeliverProcessorService,
EndedPollNotificationProcessorService,
ScheduledNoteDeleteProcessorService,
DeliverProcessorService,
InboxProcessorService,
AggregateRetentionProcessorService,
Expand Down
20 changes: 20 additions & 0 deletions packages/backend/src/queue/QueueProcessorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { CleanProcessorService } from './processors/CleanProcessorService.js';
import { AggregateRetentionProcessorService } from './processors/AggregateRetentionProcessorService.js';
import { QueueLoggerService } from './QueueLoggerService.js';
import { QUEUE, baseQueueOptions } from './const.js';
import { ScheduledNoteDeleteProcessorService } from './processors/ScheduledNoteDeleteProcessorService.js';

// ref. https://github.com/misskey-dev/misskey/pull/7635#issue-971097019
function httpRelatedBackoff(attemptsMade: number) {
Expand Down Expand Up @@ -84,6 +85,7 @@ export class QueueProcessorService implements OnApplicationShutdown {
private relationshipQueueWorker: Bull.Worker;
private objectStorageQueueWorker: Bull.Worker;
private endedPollNotificationQueueWorker: Bull.Worker;
private scheduledNoteDeleteQueueWorker: Bull.Worker;

constructor(
@Inject(DI.config)
Expand All @@ -96,6 +98,7 @@ export class QueueProcessorService implements OnApplicationShutdown {
private userWebhookDeliverProcessorService: UserWebhookDeliverProcessorService,
private systemWebhookDeliverProcessorService: SystemWebhookDeliverProcessorService,
private endedPollNotificationProcessorService: EndedPollNotificationProcessorService,
private scheduledNoteDeleteProcessorservice: ScheduledNoteDeleteProcessorService,
private deliverProcessorService: DeliverProcessorService,
private inboxProcessorService: InboxProcessorService,
private deleteDriveFilesProcessorService: DeleteDriveFilesProcessorService,
Expand Down Expand Up @@ -508,6 +511,21 @@ export class QueueProcessorService implements OnApplicationShutdown {
});
}
//#endregion

//#region scheduled note delete
{
this.scheduledNoteDeleteQueueWorker = new Bull.Worker(QUEUE.SCHEDULED_NOTE_DELETE, (job) => {
if (this.config.sentryForBackend) {
return Sentry.startSpan({ name: 'Queue: ScheduledNoteDelete' }, () => this.scheduledNoteDeleteProcessorservice.process(job));
} else {
return this.scheduledNoteDeleteProcessorservice.process(job);
}
}, {
...baseQueueOptions(this.config, QUEUE.SCHEDULED_NOTE_DELETE, this.redisForJobQueue),
autorun: false,
});
}
//#endregion
}

@bindThis
Expand All @@ -522,6 +540,7 @@ export class QueueProcessorService implements OnApplicationShutdown {
this.relationshipQueueWorker.run(),
this.objectStorageQueueWorker.run(),
this.endedPollNotificationQueueWorker.run(),
this.scheduledNoteDeleteQueueWorker.run(),
]);
}

Expand All @@ -537,6 +556,7 @@ export class QueueProcessorService implements OnApplicationShutdown {
this.relationshipQueueWorker.close(),
this.objectStorageQueueWorker.close(),
this.endedPollNotificationQueueWorker.close(),
this.scheduledNoteDeleteQueueWorker.close(),
]);
}

Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/queue/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const QUEUE = {
INBOX: 'inbox',
SYSTEM: 'system',
ENDED_POLL_NOTIFICATION: 'endedPollNotification',
SCHEDULED_NOTE_DELETE: 'scheduledNoteDelete',
DB: 'db',
RELATIONSHIP: 'relationship',
OBJECT_STORAGE: 'objectStorage',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Inject, Injectable } from '@nestjs/common';
import { DI } from '@/di-symbols.js';
import type { NotesRepository, UsersRepository } from '@/models/_.js';
import type Logger from '@/logger.js';
import { bindThis } from '@/decorators.js';
import { NoteDeleteService } from '@/core/NoteDeleteService.js';
import { QueueLoggerService } from '../QueueLoggerService.js';
import type * as Bull from 'bullmq';
import type { ScheduledNoteDeleteJobData } from '../types.js';

@Injectable()
export class ScheduledNoteDeleteProcessorService {
private logger: Logger;

constructor(
@Inject(DI.notesRepository)
private notesRepository: NotesRepository,

@Inject(DI.usersRepository)
private usersRepository: UsersRepository,

private noteDeleteService: NoteDeleteService,
private queueLoggerService: QueueLoggerService,
) {
this.logger = this.queueLoggerService.logger.createSubLogger('scheduled-note-delete');
}

@bindThis
public async process(job: Bull.Job<ScheduledNoteDeleteJobData>): Promise<void> {
const note = await this.notesRepository.findOneBy({ id: job.data.noteId});

if (note == null) {
return;
}

const user = await this.usersRepository.findOneBy({ id: note.userId });

if (user == null) {
return;
}

await this.noteDeleteService.delete(user, note);
this.logger.info(`Delete note ${note.id}`);
}
}
4 changes: 4 additions & 0 deletions packages/backend/src/queue/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ export type EndedPollNotificationJobData = {
noteId: MiNote['id'];
};

export type ScheduledNoteDeleteJobData = {
noteId: MiNote['id'];
};

export type SystemWebhookDeliverJobData = {
type: string;
content: unknown;
Expand Down
25 changes: 25 additions & 0 deletions packages/backend/src/server/api/endpoints/notes/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ export const meta = {
code: 'CONTAINS_TOO_MANY_MENTIONS',
id: '4de0363a-3046-481b-9b0f-feff3e211025',
},

cannotScheduleDeleteEarlierThanNow: {
message: 'Cannot specify delete time earlier than now.',
code: 'CANNOT_SCHEDULE_DELETE_EARLIER_THAN_NOW',
id: '9f04994a-3aa2-11ef-a495-177eea74788f',
},
},
} as const;

Expand Down Expand Up @@ -202,6 +208,14 @@ export const paramDef = {
metadata: { type: 'object' },
},
},
scheduledDelete: {
type: 'object',
nullable: true,
properties: {
deleteAt: { type: 'integer', nullable: true },
deleteAfter: { type: 'integer', nullable: true, minimum: 1 },
},
},
},
// (re)note with text, files and poll are optional
if: {
Expand Down Expand Up @@ -364,6 +378,16 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
}
}

if (ps.scheduledDelete) {
if (typeof ps.scheduledDelete.deleteAt === 'number') {
if (ps.scheduledDelete.deleteAt < Date.now()) {
throw new ApiError(meta.errors.cannotScheduleDeleteEarlierThanNow);
} else if (typeof ps.scheduledDelete.deleteAfter === 'number') {
ps.scheduledDelete.deleteAt = Date.now() + ps.scheduledDelete.deleteAfter;
}
}
}

let channel: MiChannel | null = null;
if (ps.channelId != null) {
channel = await this.channelsRepository.findOneBy({ id: ps.channelId, isArchived: false });
Expand Down Expand Up @@ -402,6 +426,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
apMentions: ps.noExtractMentions ? [] : undefined,
apHashtags: ps.noExtractHashtags ? [] : undefined,
apEmojis: ps.noExtractEmojis ? [] : undefined,
deleteAt: ps.scheduledDelete?.deleteAt ? new Date(ps.scheduledDelete.deleteAt) : ps.scheduledDelete?.deleteAfter ? new Date(Date.now() + ps.scheduledDelete.deleteAfter) : null,
});

return {
Expand Down
Loading
Loading