summaryrefslogtreecommitdiff
path: root/src/queue/processors
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2021-08-21 12:41:56 +0900
committerGitHub <noreply@github.com>2021-08-21 12:41:56 +0900
commitfd1ef4a62d670aab5f0c0089ab3806639c779813 (patch)
tree0a13e5827e0a45dcc9aa592ea09a968369ff0058 /src/queue/processors
parentfix bug (diff)
downloadsharkey-fd1ef4a62d670aab5f0c0089ab3806639c779813.tar.gz
sharkey-fd1ef4a62d670aab5f0c0089ab3806639c779813.tar.bz2
sharkey-fd1ef4a62d670aab5f0c0089ab3806639c779813.zip
enhance(server): Use job queue for account delete (#7668)
* enhance(server): Use job queue for account delete Fix #5336 * ジョブをひとつに * remove done call * clean up * add User.isDeleted * コミット忘れ * Update 1629512953000-user-is-deleted.ts * show dialog * lint * Update 1629512953000-user-is-deleted.ts
Diffstat (limited to 'src/queue/processors')
-rw-r--r--src/queue/processors/db/delete-account.ts79
-rw-r--r--src/queue/processors/db/index.ts4
2 files changed, 82 insertions, 1 deletions
diff --git a/src/queue/processors/db/delete-account.ts b/src/queue/processors/db/delete-account.ts
new file mode 100644
index 0000000000..95614b61aa
--- /dev/null
+++ b/src/queue/processors/db/delete-account.ts
@@ -0,0 +1,79 @@
+import * as Bull from 'bull';
+import { queueLogger } from '../../logger';
+import { DriveFiles, Notes, Users } from '@/models/index';
+import { DbUserJobData } from '@/queue/types';
+import { Note } from '@/models/entities/note';
+import { DriveFile } from '@/models/entities/drive-file';
+import { MoreThan } from 'typeorm';
+import { deleteFileSync } from '@/services/drive/delete-file';
+
+const logger = queueLogger.createSubLogger('delete-account');
+
+export async function deleteAccount(job: Bull.Job<DbUserJobData>): Promise<string | void> {
+ logger.info(`Deleting account of ${job.data.user.id} ...`);
+
+ const user = await Users.findOne(job.data.user.id);
+ if (user == null) {
+ return;
+ }
+
+ { // Delete notes
+ let cursor: Note['id'] | null = null;
+
+ while (true) {
+ const notes = await Notes.find({
+ where: {
+ userId: user.id,
+ ...(cursor ? { id: MoreThan(cursor) } : {})
+ },
+ take: 100,
+ order: {
+ id: 1
+ }
+ });
+
+ if (notes.length === 0) {
+ break;
+ }
+
+ cursor = notes[notes.length - 1].id;
+
+ await Notes.delete(notes.map(note => note.id));
+ }
+
+ logger.succ(`All of notes deleted`);
+ }
+
+ { // Delete files
+ let cursor: DriveFile['id'] | null = null;
+
+ while (true) {
+ const files = await DriveFiles.find({
+ where: {
+ userId: user.id,
+ ...(cursor ? { id: MoreThan(cursor) } : {})
+ },
+ take: 10,
+ order: {
+ id: 1
+ }
+ });
+
+ if (files.length === 0) {
+ break;
+ }
+
+ cursor = files[files.length - 1].id;
+
+ for (const file of files) {
+ await deleteFileSync(file);
+ }
+ }
+
+ logger.succ(`All of files deleted`);
+ }
+
+ await Users.delete(job.data.user.id);
+
+ return 'Account deleted';
+}
diff --git a/src/queue/processors/db/index.ts b/src/queue/processors/db/index.ts
index b56b7bfa2c..b051a28e0b 100644
--- a/src/queue/processors/db/index.ts
+++ b/src/queue/processors/db/index.ts
@@ -8,6 +8,7 @@ import { exportBlocking } from './export-blocking';
import { exportUserLists } from './export-user-lists';
import { importFollowing } from './import-following';
import { importUserLists } from './import-user-lists';
+import { deleteAccount } from './delete-account';
const jobs = {
deleteDriveFiles,
@@ -17,7 +18,8 @@ const jobs = {
exportBlocking,
exportUserLists,
importFollowing,
- importUserLists
+ importUserLists,
+ deleteAccount,
} as Record<string, Bull.ProcessCallbackFunction<DbJobData> | Bull.ProcessPromiseFunction<DbJobData>>;
export default function(dbQueue: Bull.Queue<DbJobData>) {