From 20134a53678fa0654aa4f1a56aebbef6347f9320 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 10 Dec 2021 01:22:35 +0900 Subject: feat: improve follow export --- .../src/queue/processors/db/export-following.ts | 31 +++++++++++----------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'packages/backend/src/queue/processors/db') diff --git a/packages/backend/src/queue/processors/db/export-following.ts b/packages/backend/src/queue/processors/db/export-following.ts index 54e3b11372..fbb9e25247 100644 --- a/packages/backend/src/queue/processors/db/export-following.ts +++ b/packages/backend/src/queue/processors/db/export-following.ts @@ -6,13 +6,14 @@ import { queueLogger } from '../../logger'; import addFile from '@/services/drive/add-file'; import * as dateFormat from 'dateformat'; import { getFullApAccount } from '@/misc/convert-host'; -import { Users, Followings } from '@/models/index'; -import { MoreThan } from 'typeorm'; +import { Users, Followings, Mutings } from '@/models/index'; +import { In, MoreThan, Not } from 'typeorm'; import { DbUserJobData } from '@/queue/types'; +import { Following } from '@/models/entities/following'; const logger = queueLogger.createSubLogger('export-following'); -export async function exportFollowing(job: Bull.Job, done: any): Promise { +export async function exportFollowing(job: Bull.Job, done: () => void): Promise { logger.info(`Exporting following of ${job.data.user.id} ...`); const user = await Users.findOne(job.data.user.id); @@ -22,7 +23,7 @@ export async function exportFollowing(job: Bull.Job, done: any): } // Create temp file - const [path, cleanup] = await new Promise<[string, any]>((res, rej) => { + const [path, cleanup] = await new Promise<[string, () => void]>((res, rej) => { tmp.file((e, path, fd, cleanup) => { if (e) return rej(e); res([path, cleanup]); @@ -33,13 +34,17 @@ export async function exportFollowing(job: Bull.Job, done: any): const stream = fs.createWriteStream(path, { flags: 'a' }); - let exportedCount = 0; - let cursor: any = null; + let cursor: Following['id'] | null = null; + + const mutings = job.data.excludeMuting ? await Mutings.find({ + muterId: user.id, + }) : []; while (true) { const followings = await Followings.find({ where: { followerId: user.id, + ...(mutings.length > 0 ? { followeeId: Not(In(mutings.map(x => x.muteeId))) } : {}), ...(cursor ? { id: MoreThan(cursor) } : {}), }, take: 100, @@ -49,7 +54,6 @@ export async function exportFollowing(job: Bull.Job, done: any): }); if (followings.length === 0) { - job.progress(100); break; } @@ -58,7 +62,11 @@ export async function exportFollowing(job: Bull.Job, done: any): for (const following of followings) { const u = await Users.findOne({ id: following.followeeId }); if (u == null) { - exportedCount++; continue; + continue; + } + + if (job.data.excludeInactive && u.updatedAt && (Date.now() - u.updatedAt.getTime() > 1000 * 60 * 60 * 24 * 90)) { + continue; } const content = getFullApAccount(u.username, u.host); @@ -72,14 +80,7 @@ export async function exportFollowing(job: Bull.Job, done: any): } }); }); - exportedCount++; } - - const total = await Followings.count({ - followerId: user.id, - }); - - job.progress(exportedCount / total); } stream.end(); -- cgit v1.2.3-freya