From 0a0aa0e2dbaf345a840b57216906ac52292ae3da Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Tue, 12 Mar 2019 23:38:11 +0900 Subject: [PATCH] Fix #4484 (#4485) * Fix #4484 * import order --- src/misc/convert-host.ts | 21 ++++++++++++++++++++ src/queue/processors/db/export-blocking.ts | 4 ++-- src/queue/processors/db/export-following.ts | 4 ++-- src/queue/processors/db/export-mute.ts | 4 ++-- src/queue/processors/db/export-user-lists.ts | 4 ++-- src/queue/processors/db/import-following.ts | 6 +++--- src/queue/processors/db/import-user-lists.ts | 6 +++--- 7 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 src/misc/convert-host.ts diff --git a/src/misc/convert-host.ts b/src/misc/convert-host.ts new file mode 100644 index 0000000000..d4478bd85b --- /dev/null +++ b/src/misc/convert-host.ts @@ -0,0 +1,21 @@ +import config from '../config'; +import { toUnicode, toASCII } from 'punycode'; + +export function getFullApAccount(username: string, host: string) { + return host ? `${username}@${toApHost(host)}` : `${username}@${toApHost(config.host)}`; +} + +export function isSelfHost(host: string) { + if (host == null) return true; + return toApHost(config.host) === toApHost(host); +} + +export function toDbHost(host: string) { + if (host == null) return null; + return toUnicode(host.toLowerCase()); +} + +export function toApHost(host: string) { + if (host == null) return null; + return toASCII(host.toLowerCase()); +} diff --git a/src/queue/processors/db/export-blocking.ts b/src/queue/processors/db/export-blocking.ts index e56aec94ac..7f32c06472 100644 --- a/src/queue/processors/db/export-blocking.ts +++ b/src/queue/processors/db/export-blocking.ts @@ -8,7 +8,7 @@ import addFile from '../../../services/drive/add-file'; import User from '../../../models/user'; import dateFormat = require('dateformat'); import Blocking from '../../../models/blocking'; -import config from '../../../config'; +import { getFullApAccount } from '../../../misc/convert-host'; const logger = queueLogger.createSubLogger('export-blocking'); @@ -56,7 +56,7 @@ export async function exportBlocking(job: Bull.Job, done: any): Promise { for (const block of blockings) { const u = await User.findOne({ _id: block.blockeeId }, { fields: { username: true, host: true } }); - const content = u.host ? `${u.username}@${u.host}` : `${u.username}@${config.host}`; + const content = getFullApAccount(u.username, u.host); await new Promise((res, rej) => { stream.write(content + '\n', err => { if (err) { diff --git a/src/queue/processors/db/export-following.ts b/src/queue/processors/db/export-following.ts index 1d8a501b78..019414072a 100644 --- a/src/queue/processors/db/export-following.ts +++ b/src/queue/processors/db/export-following.ts @@ -8,7 +8,7 @@ import addFile from '../../../services/drive/add-file'; import User from '../../../models/user'; import dateFormat = require('dateformat'); import Following from '../../../models/following'; -import config from '../../../config'; +import { getFullApAccount } from '../../../misc/convert-host'; const logger = queueLogger.createSubLogger('export-following'); @@ -56,7 +56,7 @@ export async function exportFollowing(job: Bull.Job, done: any): Promise { for (const following of followings) { const u = await User.findOne({ _id: following.followeeId }, { fields: { username: true, host: true } }); - const content = u.host ? `${u.username}@${u.host}` : `${u.username}@${config.host}`; + const content = getFullApAccount(u.username, u.host); await new Promise((res, rej) => { stream.write(content + '\n', err => { if (err) { diff --git a/src/queue/processors/db/export-mute.ts b/src/queue/processors/db/export-mute.ts index 6f2dd6df13..5ded7cf651 100644 --- a/src/queue/processors/db/export-mute.ts +++ b/src/queue/processors/db/export-mute.ts @@ -8,7 +8,7 @@ import addFile from '../../../services/drive/add-file'; import User from '../../../models/user'; import dateFormat = require('dateformat'); import Mute from '../../../models/mute'; -import config from '../../../config'; +import { getFullApAccount } from '../../../misc/convert-host'; const logger = queueLogger.createSubLogger('export-mute'); @@ -56,7 +56,7 @@ export async function exportMute(job: Bull.Job, done: any): Promise { for (const mute of mutes) { const u = await User.findOne({ _id: mute.muteeId }, { fields: { username: true, host: true } }); - const content = u.host ? `${u.username}@${u.host}` : `${u.username}@${config.host}`; + const content = getFullApAccount(u.username, u.host); await new Promise((res, rej) => { stream.write(content + '\n', err => { if (err) { diff --git a/src/queue/processors/db/export-user-lists.ts b/src/queue/processors/db/export-user-lists.ts index 6b5f72c15e..dfbf152ec0 100644 --- a/src/queue/processors/db/export-user-lists.ts +++ b/src/queue/processors/db/export-user-lists.ts @@ -7,8 +7,8 @@ import { queueLogger } from '../../logger'; import addFile from '../../../services/drive/add-file'; import User from '../../../models/user'; import dateFormat = require('dateformat'); -import config from '../../../config'; import UserList from '../../../models/user-list'; +import { getFullApAccount } from '../../../misc/convert-host'; const logger = queueLogger.createSubLogger('export-user-lists'); @@ -46,7 +46,7 @@ export async function exportUserLists(job: Bull.Job, done: any): Promise { }); for (const u of users) { - const acct = u.host ? `${u.username}@${u.host}` : `${u.username}@${config.host}`; + const acct = getFullApAccount(u.username, u.host); const content = `${list.title},${acct}`; await new Promise((res, rej) => { stream.write(content + '\n', err => { diff --git a/src/queue/processors/db/import-following.ts b/src/queue/processors/db/import-following.ts index 9b8b9f9614..ad9129c1f5 100644 --- a/src/queue/processors/db/import-following.ts +++ b/src/queue/processors/db/import-following.ts @@ -3,13 +3,13 @@ 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 { isSelfHost, toDbHost } from '../../../misc/convert-host'; const logger = queueLogger.createSubLogger('import-following'); @@ -31,11 +31,11 @@ export async function importFollowing(job: Bull.Job, done: any): Promise { for (const line of csv.trim().split('\n')) { const { username, host } = parseAcct(line.trim()); - let target = host === config.host ? await User.findOne({ + let target = isSelfHost(host) ? await User.findOne({ host: null, usernameLower: username.toLowerCase() }) : await User.findOne({ - host: host, + host: toDbHost(host), usernameLower: username.toLowerCase() }); diff --git a/src/queue/processors/db/import-user-lists.ts b/src/queue/processors/db/import-user-lists.ts index 7250920090..50d3c649d4 100644 --- a/src/queue/processors/db/import-user-lists.ts +++ b/src/queue/processors/db/import-user-lists.ts @@ -3,7 +3,6 @@ import * as mongo from 'mongodb'; import { queueLogger } from '../../logger'; import User from '../../../models/user'; -import config from '../../../config'; import UserList from '../../../models/user-list'; import DriveFile from '../../../models/drive-file'; import { getOriginalUrl } from '../../../misc/get-drive-file-url'; @@ -11,6 +10,7 @@ import parseAcct from '../../../misc/acct/parse'; import resolveUser from '../../../remote/resolve-user'; import { pushUserToUserList } from '../../../services/user-list/push'; import { downloadTextFile } from '../../../misc/download-text-file'; +import { isSelfHost, toDbHost } from '../../../misc/convert-host'; const logger = queueLogger.createSubLogger('import-user-lists'); @@ -47,11 +47,11 @@ export async function importUserLists(job: Bull.Job, done: any): Promise { }); } - let target = host === config.host ? await User.findOne({ + let target = isSelfHost(host) ? await User.findOne({ host: null, usernameLower: username.toLowerCase() }) : await User.findOne({ - host: host, + host: toDbHost(host), usernameLower: username.toLowerCase() });