2023-07-27 05:31:52 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-05-05 23:17:55 +00:00
|
|
|
import fs from 'node:fs';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { format as DateFormat } from 'date-fns';
|
|
|
|
import { In } from 'typeorm';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-10-03 11:26:11 +00:00
|
|
|
import type { AntennasRepository, UsersRepository, UserListMembershipsRepository, MiUser } from '@/models/_.js';
|
2023-05-05 23:17:55 +00:00
|
|
|
import Logger from '@/logger.js';
|
|
|
|
import { DriveService } from '@/core/DriveService.js';
|
|
|
|
import { bindThis } from '@/decorators.js';
|
|
|
|
import { createTemp } from '@/misc/create-temp.js';
|
2023-05-06 00:34:21 +00:00
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
2023-05-05 23:17:55 +00:00
|
|
|
import { QueueLoggerService } from '../QueueLoggerService.js';
|
|
|
|
import type { DBExportAntennasData } from '../types.js';
|
2023-05-29 02:54:49 +00:00
|
|
|
import type * as Bull from 'bullmq';
|
2023-05-05 23:17:55 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ExportAntennasProcessorService {
|
|
|
|
private logger: Logger;
|
|
|
|
|
|
|
|
constructor (
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
2023-05-06 00:34:21 +00:00
|
|
|
|
2023-05-05 23:17:55 +00:00
|
|
|
@Inject(DI.antennasRepository)
|
|
|
|
private antennsRepository: AntennasRepository,
|
2023-05-06 00:34:21 +00:00
|
|
|
|
2023-10-03 11:26:11 +00:00
|
|
|
@Inject(DI.userListMembershipsRepository)
|
|
|
|
private userListMembershipsRepository: UserListMembershipsRepository,
|
2023-07-07 22:08:16 +00:00
|
|
|
|
2023-05-05 23:17:55 +00:00
|
|
|
private driveService: DriveService,
|
|
|
|
private utilityService: UtilityService,
|
|
|
|
private queueLoggerService: QueueLoggerService,
|
|
|
|
) {
|
|
|
|
this.logger = this.queueLoggerService.logger.createSubLogger('export-antennas');
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
2023-05-29 02:54:49 +00:00
|
|
|
public async process(job: Bull.Job<DBExportAntennasData>): Promise<void> {
|
2023-05-05 23:17:55 +00:00
|
|
|
const user = await this.usersRepository.findOneBy({ id: job.data.user.id });
|
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const [path, cleanup] = await createTemp();
|
|
|
|
const stream = fs.createWriteStream(path, { flags: 'a' });
|
|
|
|
const write = (input: string): Promise<void> => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
stream.write(input, err => {
|
|
|
|
if (err) {
|
|
|
|
this.logger.error(err);
|
|
|
|
reject();
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
const antennas = await this.antennsRepository.findBy({ userId: job.data.user.id });
|
|
|
|
write('[');
|
|
|
|
for (const [index, antenna] of antennas.entries()) {
|
2023-08-16 08:51:28 +00:00
|
|
|
let users: MiUser[] | undefined;
|
2023-05-05 23:17:55 +00:00
|
|
|
if (antenna.userListId !== null) {
|
2023-10-03 11:26:11 +00:00
|
|
|
const memberships = await this.userListMembershipsRepository.findBy({ userListId: antenna.userListId });
|
2023-05-05 23:17:55 +00:00
|
|
|
users = await this.usersRepository.findBy({
|
2023-10-03 11:26:11 +00:00
|
|
|
id: In(memberships.map(j => j.userId)),
|
2023-05-05 23:17:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
write(JSON.stringify({
|
|
|
|
name: antenna.name,
|
|
|
|
src: antenna.src,
|
|
|
|
keywords: antenna.keywords,
|
|
|
|
excludeKeywords: antenna.excludeKeywords,
|
|
|
|
users: antenna.users,
|
2023-05-06 00:34:21 +00:00
|
|
|
userListAccts: typeof users !== 'undefined' ? users.map((u) => {
|
2023-05-05 23:17:55 +00:00
|
|
|
return this.utilityService.getFullApAccount(u.username, u.host); // acct
|
|
|
|
}) : null,
|
|
|
|
caseSensitive: antenna.caseSensitive,
|
|
|
|
withReplies: antenna.withReplies,
|
|
|
|
withFile: antenna.withFile,
|
|
|
|
notify: antenna.notify,
|
|
|
|
}));
|
|
|
|
if (antennas.length - 1 !== index) {
|
|
|
|
write(', ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
write(']');
|
|
|
|
stream.end();
|
|
|
|
|
|
|
|
const fileName = 'antennas-' + DateFormat(new Date(), 'yyyy-MM-dd-HH-mm-ss') + '.json';
|
|
|
|
const driveFile = await this.driveService.addFile({ user, path, name: fileName, force: true, ext: 'json' });
|
|
|
|
this.logger.succ('Exported to: ' + driveFile.id);
|
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|