diff --git a/src/config/load.ts b/src/config/load.ts index 50ae47d9e2..4d174d0e27 100644 --- a/src/config/load.ts +++ b/src/config/load.ts @@ -7,6 +7,7 @@ import { URL } from 'url'; import * as yaml from 'js-yaml'; import { Source, Mixin } from './types'; import * as pkg from '../../package.json'; +import { toPuny } from '../misc/convert-host'; /** * Path of configuration directory @@ -27,12 +28,12 @@ export default function load() { const url = validateUrl(config.url); - config.url = normalizeUrl(config.url); + config.url = toPuny(normalizeUrl(config.url)); config.port = config.port || parseInt(process.env.PORT, 10); - mixin.host = url.host; - mixin.hostname = url.hostname; + mixin.host = toPuny(url.host); + mixin.hostname = toPuny(url.hostname); mixin.scheme = url.protocol.replace(/:$/, ''); mixin.wsScheme = mixin.scheme.replace('http', 'ws'); mixin.wsUrl = `${mixin.wsScheme}://${mixin.host}`; diff --git a/src/migrate.ts b/src/migrate.ts index 46df8429db..b9d6eb3396 100644 --- a/src/migrate.ts +++ b/src/migrate.ts @@ -25,6 +25,7 @@ import { UserPublickey } from './models/entities/user-publickey'; import { UserKeypair } from './models/entities/user-keypair'; import { extractPublic } from './crypto_key'; import { Emoji } from './models/entities/emoji'; +import { toPuny } from './misc/convert-host'; const u = (config as any).mongodb.user ? encodeURIComponent((config as any).mongodb.user) : null; const p = (config as any).mongodb.pass ? encodeURIComponent((config as any).mongodb.pass) : null; @@ -87,7 +88,7 @@ async function main() { createdAt: user.createdAt || new Date(), username: user.username, usernameLower: user.username.toLowerCase(), - host: user.host, + host: toPuny(user.host), token: generateUserToken(), password: user.password, isAdmin: user.isAdmin, @@ -133,10 +134,10 @@ async function main() { followeeId: following.followeeId.toHexString(), // 非正規化 - followerHost: following._follower ? following._follower.host : null, + followerHost: following._follower ? toPuny(following._follower.host) : null, followerInbox: following._follower ? following._follower.inbox : null, followerSharedInbox: following._follower ? following._follower.sharedInbox : null, - followeeHost: following._followee ? following._followee.host : null, + followeeHost: following._followee ? toPuny(following._followee.host) : null, followeeInbox: following._followee ? following._followee.inbox : null, followeeSharedInbox: following._followee ? following._followee.sharedInbo : null }); @@ -159,7 +160,7 @@ async function main() { await DriveFiles.save({ id: file._id.toHexString(), userId: user._id.toHexString(), - userHost: user.host, + userHost: toPuny(user.host), createdAt: file.uploadDate || new Date(), md5: file.md5, name: file.filename, @@ -191,7 +192,7 @@ async function main() { await DriveFiles.save({ id: file._id.toHexString(), userId: user._id.toHexString(), - userHost: user.host, + userHost: toPuny(user.host), createdAt: file.uploadDate || new Date(), md5: file.md5, name: file.filename, @@ -210,7 +211,7 @@ async function main() { await DriveFiles.save({ id: file._id.toHexString(), userId: user._id.toHexString(), - userHost: user.host, + userHost: toPuny(user.host), createdAt: file.uploadDate || new Date(), md5: file.md5, name: file.filename, @@ -313,7 +314,7 @@ async function main() { aliases: emoji.aliases, url: emoji.url, uri: emoji.uri, - host: emoji.host, + host: toPuny(emoji.host), name: emoji.name }); } diff --git a/src/misc/acct/render.ts b/src/misc/acct/render.ts index 67e063fcb3..094eceffe9 100644 --- a/src/misc/acct/render.ts +++ b/src/misc/acct/render.ts @@ -1,5 +1,5 @@ import Acct from './type'; export default (user: Acct) => { - return user.host === null ? user.username : `${user.username}@${user.host}`; + return user.host == null ? user.username : `${user.username}@${user.host}`; }; diff --git a/src/misc/convert-host.ts b/src/misc/convert-host.ts index 8f2f1c7aba..f7feebd55c 100644 --- a/src/misc/convert-host.ts +++ b/src/misc/convert-host.ts @@ -1,27 +1,22 @@ import config from '../config'; -import { toUnicode, toASCII } from 'punycode'; +import { toASCII } from 'punycode'; import { URL } from 'url'; export function getFullApAccount(username: string, host: string) { - return host ? `${username}@${toApHost(host)}` : `${username}@${toApHost(config.host)}`; + return host ? `${username}@${toPuny(host)}` : `${username}@${toPuny(config.host)}`; } export function isSelfHost(host: string) { if (host == null) return true; - return toApHost(config.host) === toApHost(host); + return toPuny(config.host) === toPuny(host); } export function extractDbHost(uri: string) { const url = new URL(uri); - return toDbHost(url.hostname); + return toPuny(url.hostname); } -export function toDbHost(host: string) { - if (host == null) return null; - return toUnicode(host.toLowerCase()); -} - -export function toApHost(host: string) { +export function toPuny(host: string) { if (host == null) return null; return toASCII(host.toLowerCase()); } diff --git a/src/models/repositories/user.ts b/src/models/repositories/user.ts index f1a51c1c5f..3939e3142a 100644 --- a/src/models/repositories/user.ts +++ b/src/models/repositories/user.ts @@ -166,7 +166,7 @@ export class UserRepository extends Repository { } public isLocalUser(user: User): user is ILocalUser { - return user.host === null; + return user.host == null; } public isRemoteUser(user: User): user is IRemoteUser { diff --git a/src/queue/processors/db/import-following.ts b/src/queue/processors/db/import-following.ts index 07a1639494..aae24b22d6 100644 --- a/src/queue/processors/db/import-following.ts +++ b/src/queue/processors/db/import-following.ts @@ -5,7 +5,7 @@ import follow from '../../../services/following/create'; 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'; +import { isSelfHost, toPuny } from '../../../misc/convert-host'; import { Users, DriveFiles } from '../../../models'; const logger = queueLogger.createSubLogger('import-following'); @@ -35,7 +35,7 @@ export async function importFollowing(job: Bull.Job, done: any): Promise { host: null, usernameLower: username.toLowerCase() }) : await Users.findOne({ - host: toDbHost(host), + host: toPuny(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 ff896c1bef..c7273ea6b4 100644 --- a/src/queue/processors/db/import-user-lists.ts +++ b/src/queue/processors/db/import-user-lists.ts @@ -5,7 +5,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'; +import { isSelfHost, toPuny } from '../../../misc/convert-host'; import { DriveFiles, Users, UserLists, UserListJoinings } from '../../../models'; import { genId } from '../../../misc/gen-id'; @@ -47,7 +47,7 @@ export async function importUserLists(job: Bull.Job, done: any): Promise { host: null, usernameLower: username.toLowerCase() }) : await Users.findOne({ - host: toDbHost(host), + host: toPuny(host), usernameLower: username.toLowerCase() }); diff --git a/src/remote/activitypub/models/person.ts b/src/remote/activitypub/models/person.ts index fbc329e860..2362455e02 100644 --- a/src/remote/activitypub/models/person.ts +++ b/src/remote/activitypub/models/person.ts @@ -24,6 +24,7 @@ import { UserServiceLinking } from '../../../models/entities/user-service-linkin import { instanceChart, usersChart } from '../../../services/chart'; import { UserPublickey } from '../../../models/entities/user-publickey'; import { isDuplicateKeyValueError } from '../../../misc/is-duplicate-key-value-error'; +import { toPuny } from '../../../misc/convert-host'; const logger = apLogger; /** @@ -124,7 +125,7 @@ export async function createPerson(uri: string, resolver?: Resolver): Promise { const body = ctx.request.body as any; @@ -103,7 +104,7 @@ export default async (ctx: Koa.BaseContext) => { createdAt: new Date(), username: username, usernameLower: username.toLowerCase(), - host: host, + host: toPuny(host), token: secret, password: hash, isAdmin: config.autoAdmin && usersCount === 0, diff --git a/src/server/api/stream/channels/hybrid-timeline.ts b/src/server/api/stream/channels/hybrid-timeline.ts index 3f09dd8398..30643aeda8 100644 --- a/src/server/api/stream/channels/hybrid-timeline.ts +++ b/src/server/api/stream/channels/hybrid-timeline.ts @@ -24,7 +24,7 @@ export default class extends Channel { if (!( this.user.id === note.userId || this.following.includes(note.userId) || - note.user.host === null + note.user.host == null )) return; if (['followers', 'specified'].includes(note.visibility)) { diff --git a/src/services/register-or-fetch-instance-doc.ts b/src/services/register-or-fetch-instance-doc.ts index c96c8a1e32..459f538e96 100644 --- a/src/services/register-or-fetch-instance-doc.ts +++ b/src/services/register-or-fetch-instance-doc.ts @@ -2,10 +2,13 @@ import { Instance } from '../models/entities/instance'; import { Instances } from '../models'; import { federationChart } from './chart'; import { genId } from '../misc/gen-id'; +import { toPuny } from '../misc/convert-host'; export async function registerOrFetchInstanceDoc(host: string): Promise { if (host == null) return null; + host = toPuny(host); + const index = await Instances.findOne({ host }); if (index == null) {