summaryrefslogtreecommitdiff
path: root/src/queue
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2019-03-12 00:34:19 +0900
committersyuilo <syuilotan@yahoo.co.jp>2019-03-12 00:34:19 +0900
commitc932f7a25bfd0944b21fc026a647fa1d04750418 (patch)
treeb10f9725ab30b094b1ac95ba980881e6d9a09888 /src/queue
parentRefactor: Extract downloadTextFile function (diff)
downloadsharkey-c932f7a25bfd0944b21fc026a647fa1d04750418.tar.gz
sharkey-c932f7a25bfd0944b21fc026a647fa1d04750418.tar.bz2
sharkey-c932f7a25bfd0944b21fc026a647fa1d04750418.zip
Resolve #1736
Diffstat (limited to 'src/queue')
-rw-r--r--src/queue/index.ts10
-rw-r--r--src/queue/processors/db/import-following.ts62
-rw-r--r--src/queue/processors/db/index.ts2
3 files changed, 74 insertions, 0 deletions
diff --git a/src/queue/index.ts b/src/queue/index.ts
index 09e0ad59c9..44b24b66cb 100644
--- a/src/queue/index.ts
+++ b/src/queue/index.ts
@@ -146,6 +146,16 @@ export function createExportUserListsJob(user: ILocalUser) {
});
}
+export function createImportFollowingJob(user: ILocalUser, fileId: IDriveFile['_id']) {
+ return dbQueue.add('importFollowing', {
+ user: user,
+ fileId: fileId
+ }, {
+ removeOnComplete: true,
+ removeOnFail: true
+ });
+}
+
export function createImportUserListsJob(user: ILocalUser, fileId: IDriveFile['_id']) {
return dbQueue.add('importUserLists', {
user: user,
diff --git a/src/queue/processors/db/import-following.ts b/src/queue/processors/db/import-following.ts
new file mode 100644
index 0000000000..8ab20aa8bb
--- /dev/null
+++ b/src/queue/processors/db/import-following.ts
@@ -0,0 +1,62 @@
+import * as Bull from 'bull';
+import * as mongo from 'mongodb';
+
+import { queueLogger } from '../../logger';
+import User from '../../../models/user';
+import config from '../../../config';
+import follow from '../../../services/following/create';
+import DriveFile from '../../../models/drive-file';
+import { getOriginalUrl } from '../../../misc/get-drive-file-url';
+import parseAcct from '../../../misc/acct/parse';
+import resolveUser from '../../../remote/resolve-user';
+import { downloadTextFile } from '../../../misc/download-text-file';
+import Following from '../../../models/following';
+
+const logger = queueLogger.createSubLogger('import-following');
+
+export async function importFollowing(job: Bull.Job, done: any): Promise<void> {
+ logger.info(`Importing following of ${job.data.user._id} ...`);
+
+ const user = await User.findOne({
+ _id: new mongo.ObjectID(job.data.user._id.toString())
+ });
+
+ const file = await DriveFile.findOne({
+ _id: new mongo.ObjectID(job.data.fileId.toString())
+ });
+
+ const url = getOriginalUrl(file);
+
+ const csv = await downloadTextFile(url);
+
+ for (const line of csv.trim().split('\n')) {
+ const { username, host } = parseAcct(line.trim());
+
+ let target = host === config.host ? await User.findOne({
+ host: null,
+ usernameLower: username.toLowerCase()
+ }) : await User.findOne({
+ host: host,
+ usernameLower: username.toLowerCase()
+ });
+
+ if (host == null && target == null) continue;
+
+ if (target == null) {
+ target = await resolveUser(username, host);
+ }
+
+ // Check if already following
+ const exist = await Following.findOne({
+ followerId: user._id,
+ followeeId: target._id
+ });
+
+ if (exist) continue;
+
+ follow(user, target);
+ }
+
+ logger.succ('Imported');
+ done();
+}
diff --git a/src/queue/processors/db/index.ts b/src/queue/processors/db/index.ts
index 4a97a1c884..1bc9a9af7c 100644
--- a/src/queue/processors/db/index.ts
+++ b/src/queue/processors/db/index.ts
@@ -6,6 +6,7 @@ import { exportFollowing } from './export-following';
import { exportMute } from './export-mute';
import { exportBlocking } from './export-blocking';
import { exportUserLists } from './export-user-lists';
+import { importFollowing } from './import-following';
import { importUserLists } from './import-user-lists';
const jobs = {
@@ -16,6 +17,7 @@ const jobs = {
exportMute,
exportBlocking,
exportUserLists,
+ importFollowing,
importUserLists
} as any;