feat: improve follow export
This commit is contained in:
@ -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,
|
||||
|
@ -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<DbUserJobData>, done: any): Promise<void> {
|
||||
export async function exportFollowing(job: Bull.Job<DbUserJobData>, done: () => void): Promise<void> {
|
||||
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<DbUserJobData>, 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<DbUserJobData>, 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<DbUserJobData>, done: any):
|
||||
});
|
||||
|
||||
if (followings.length === 0) {
|
||||
job.progress(100);
|
||||
break;
|
||||
}
|
||||
|
||||
@ -58,7 +62,11 @@ export async function exportFollowing(job: Bull.Job<DbUserJobData>, 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<DbUserJobData>, done: any):
|
||||
}
|
||||
});
|
||||
});
|
||||
exportedCount++;
|
||||
}
|
||||
|
||||
const total = await Followings.count({
|
||||
followerId: user.id,
|
||||
});
|
||||
|
||||
job.progress(exportedCount / total);
|
||||
}
|
||||
|
||||
stream.end();
|
||||
|
@ -21,6 +21,8 @@ export type DbJobData = DbUserJobData | DbUserImportJobData | DbUserDeleteJobDat
|
||||
|
||||
export type DbUserJobData = {
|
||||
user: ThinUser;
|
||||
excludeMuting: boolean;
|
||||
excludeInactive: boolean;
|
||||
};
|
||||
|
||||
export type DbUserDeleteJobData = {
|
||||
|
@ -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);
|
||||
});
|
||||
|
Reference in New Issue
Block a user