From 20134a53678fa0654aa4f1a56aebbef6347f9320 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 10 Dec 2021 01:22:35 +0900 Subject: [PATCH] feat: improve follow export --- CHANGELOG.md | 2 + locales/ja-JP.yml | 2 + packages/backend/src/queue/index.ts | 4 +- .../queue/processors/db/export-following.ts | 31 +-- packages/backend/src/queue/types.ts | 2 + .../api/endpoints/i/export-following.ts | 13 +- .../src/pages/settings/import-export.vue | 186 +++++++++++------- 7 files changed, 156 insertions(+), 84 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5f505dc2c..777e83ba5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ ### Improvements - Added a user-level instance mute in user settings +- フォローエクスポートでミュートしているユーザーを含めないオプションを追加 +- フォローエクスポートで使われていないアカウントを含めないオプションを追加 ### Bugfixes - クライアント: タッチ機能付きディスプレイを使っていてマウス操作をしている場合に一部機能が動作しない問題を修正 diff --git a/locales/ja-JP.yml b/locales/ja-JP.yml index 2a8d0bd9e8..469c606930 100644 --- a/locales/ja-JP.yml +++ b/locales/ja-JP.yml @@ -1318,6 +1318,8 @@ _exportOrImport: muteList: "ミュート" blockingList: "ブロック" userLists: "リスト" + excludeMutingUsers: "ミュートしているユーザーを除外" + excludeInactiveUsers: "使われていないアカウントを除外" _charts: federationInstancesIncDec: "連合の増減" diff --git a/packages/backend/src/queue/index.ts b/packages/backend/src/queue/index.ts index 199c83cf65..c5a07673bd 100644 --- a/packages/backend/src/queue/index.ts +++ b/packages/backend/src/queue/index.ts @@ -126,9 +126,11 @@ export function createExportNotesJob(user: ThinUser) { }); } -export function createExportFollowingJob(user: ThinUser) { +export function createExportFollowingJob(user: ThinUser, excludeMuting = false, excludeInactive = false) { return dbQueue.add('exportFollowing', { user: user, + excludeMuting, + excludeInactive, }, { removeOnComplete: true, removeOnFail: true, 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(); diff --git a/packages/backend/src/queue/types.ts b/packages/backend/src/queue/types.ts index c8c7147152..91036177dc 100644 --- a/packages/backend/src/queue/types.ts +++ b/packages/backend/src/queue/types.ts @@ -21,6 +21,8 @@ export type DbJobData = DbUserJobData | DbUserImportJobData | DbUserDeleteJobDat export type DbUserJobData = { user: ThinUser; + excludeMuting: boolean; + excludeInactive: boolean; }; export type DbUserDeleteJobData = { diff --git a/packages/backend/src/server/api/endpoints/i/export-following.ts b/packages/backend/src/server/api/endpoints/i/export-following.ts index a351161111..15c09941e8 100644 --- a/packages/backend/src/server/api/endpoints/i/export-following.ts +++ b/packages/backend/src/server/api/endpoints/i/export-following.ts @@ -1,3 +1,4 @@ +import $ from 'cafy'; import define from '../../define'; import { createExportFollowingJob } from '@/queue/index'; import ms from 'ms'; @@ -9,8 +10,18 @@ export const meta = { duration: ms('1hour'), max: 1, }, + params: { + excludeMuting: { + validator: $.optional.bool, + default: false, + }, + excludeInactive: { + validator: $.optional.bool, + default: false, + }, + }, }; export default define(meta, async (ps, user) => { - createExportFollowingJob(user); + createExportFollowingJob(user, ps.excludeMuting, ps.excludeInactive); }); diff --git a/packages/client/src/pages/settings/import-export.vue b/packages/client/src/pages/settings/import-export.vue index 5b5b704695..a1dd6a1539 100644 --- a/packages/client/src/pages/settings/import-export.vue +++ b/packages/client/src/pages/settings/import-export.vue @@ -2,106 +2,158 @@
- {{ $ts.export }} + {{ $ts.export }} - {{ $ts.export }} - {{ $ts.import }} + + + {{ $ts._exportOrImport.excludeMutingUsers }} + + + {{ $ts._exportOrImport.excludeInactiveUsers }} + + {{ $ts.export }} + + + {{ $ts.import }} + - {{ $ts.export }} - {{ $ts.import }} + {{ $ts.export }} + {{ $ts.import }} - {{ $ts.export }} - {{ $ts.import }} + {{ $ts.export }} + {{ $ts.import }} - {{ $ts.export }} - {{ $ts.import }} + {{ $ts.export }} + {{ $ts.import }}