2022-03-25 07:27:41 +00:00
|
|
|
import escapeRegexp from 'escape-regexp';
|
2022-02-27 02:07:39 +00:00
|
|
|
import config from '@/config/index.js';
|
|
|
|
import { Note } from '@/models/entities/note.js';
|
2022-07-12 12:41:10 +00:00
|
|
|
import { CacheableRemoteUser, CacheableUser } from '@/models/entities/user.js';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { UserPublickey } from '@/models/entities/user-publickey.js';
|
|
|
|
import { MessagingMessage } from '@/models/entities/messaging-message.js';
|
|
|
|
import { Notes, Users, UserPublickeys, MessagingMessages } from '@/models/index.js';
|
2022-03-25 07:27:41 +00:00
|
|
|
import { Cache } from '@/misc/cache.js';
|
2022-03-26 10:09:57 +00:00
|
|
|
import { uriPersonCache, userByIdCache } from '@/services/user-cache.js';
|
2022-06-04 06:15:44 +00:00
|
|
|
import { IObject, getApId } from './type.js';
|
|
|
|
import { resolvePerson } from './models/person.js';
|
2022-03-25 07:27:41 +00:00
|
|
|
|
|
|
|
const publicKeyCache = new Cache<UserPublickey | null>(Infinity);
|
|
|
|
const publicKeyByUserIdCache = new Cache<UserPublickey | null>(Infinity);
|
2020-05-08 23:21:42 +00:00
|
|
|
|
2022-06-04 02:29:20 +00:00
|
|
|
export type UriParseResult = {
|
|
|
|
/** wether the URI was generated by us */
|
|
|
|
local: true;
|
|
|
|
/** id in DB */
|
|
|
|
id: string;
|
|
|
|
/** hint of type, e.g. "notes", "users" */
|
|
|
|
type: string;
|
|
|
|
/** any remaining text after type and id, not including the slash after id. undefined if empty */
|
|
|
|
rest?: string;
|
|
|
|
} | {
|
|
|
|
/** wether the URI was generated by us */
|
|
|
|
local: false;
|
|
|
|
/** uri in DB */
|
|
|
|
uri: string;
|
|
|
|
};
|
|
|
|
|
2022-06-04 06:15:44 +00:00
|
|
|
export function parseUri(value: string | IObject): UriParseResult {
|
2022-06-04 02:29:20 +00:00
|
|
|
const uri = getApId(value);
|
|
|
|
|
|
|
|
// the host part of a URL is case insensitive, so use the 'i' flag.
|
|
|
|
const localRegex = new RegExp('^' + escapeRegexp(config.url) + '/(\\w+)/(\\w+)(?:\/(.+))?', 'i');
|
|
|
|
const matchLocal = uri.match(localRegex);
|
|
|
|
|
|
|
|
if (matchLocal) {
|
|
|
|
return {
|
|
|
|
local: true,
|
|
|
|
type: matchLocal[1],
|
|
|
|
id: matchLocal[2],
|
|
|
|
rest: matchLocal[3],
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
local: false,
|
|
|
|
uri,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 23:21:42 +00:00
|
|
|
export default class DbResolver {
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AP Note => Misskey Note in DB
|
|
|
|
*/
|
|
|
|
public async getNoteFromApId(value: string | IObject): Promise<Note | null> {
|
2022-06-04 02:29:20 +00:00
|
|
|
const parsed = parseUri(value);
|
|
|
|
|
|
|
|
if (parsed.local) {
|
|
|
|
if (parsed.type !== 'notes') return null;
|
2020-05-08 23:21:42 +00:00
|
|
|
|
2022-03-26 09:42:37 +00:00
|
|
|
return await Notes.findOneBy({
|
2021-12-09 14:58:30 +00:00
|
|
|
id: parsed.id,
|
2022-03-26 09:42:37 +00:00
|
|
|
});
|
2022-06-04 02:29:20 +00:00
|
|
|
} else {
|
2022-03-26 09:42:37 +00:00
|
|
|
return await Notes.findOneBy({
|
2021-12-09 14:58:30 +00:00
|
|
|
uri: parsed.uri,
|
2022-03-26 09:42:37 +00:00
|
|
|
});
|
2020-05-08 23:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-01 03:14:42 +00:00
|
|
|
public async getMessageFromApId(value: string | IObject): Promise<MessagingMessage | null> {
|
2022-06-04 02:29:20 +00:00
|
|
|
const parsed = parseUri(value);
|
|
|
|
|
|
|
|
if (parsed.local) {
|
|
|
|
if (parsed.type !== 'notes') return null;
|
2020-11-01 03:14:42 +00:00
|
|
|
|
2022-03-26 09:42:37 +00:00
|
|
|
return await MessagingMessages.findOneBy({
|
2021-12-09 14:58:30 +00:00
|
|
|
id: parsed.id,
|
2022-03-26 09:42:37 +00:00
|
|
|
});
|
2022-06-04 02:29:20 +00:00
|
|
|
} else {
|
2022-03-26 09:42:37 +00:00
|
|
|
return await MessagingMessages.findOneBy({
|
2021-12-09 14:58:30 +00:00
|
|
|
uri: parsed.uri,
|
2022-03-26 09:42:37 +00:00
|
|
|
});
|
2020-11-01 03:14:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 23:21:42 +00:00
|
|
|
/**
|
|
|
|
* AP Person => Misskey User in DB
|
|
|
|
*/
|
2022-03-26 10:09:57 +00:00
|
|
|
public async getUserFromApId(value: string | IObject): Promise<CacheableUser | null> {
|
2022-06-04 02:29:20 +00:00
|
|
|
const parsed = parseUri(value);
|
|
|
|
|
|
|
|
if (parsed.local) {
|
|
|
|
if (parsed.type !== 'users') return null;
|
2020-05-08 23:21:42 +00:00
|
|
|
|
2022-03-26 10:09:57 +00:00
|
|
|
return await userByIdCache.fetchMaybe(parsed.id, () => Users.findOneBy({
|
2021-12-09 14:58:30 +00:00
|
|
|
id: parsed.id,
|
2022-03-26 10:09:57 +00:00
|
|
|
}).then(x => x ?? undefined)) ?? null;
|
2022-06-04 02:29:20 +00:00
|
|
|
} else {
|
2022-03-26 10:09:57 +00:00
|
|
|
return await uriPersonCache.fetch(parsed.uri, () => Users.findOneBy({
|
2021-12-09 14:58:30 +00:00
|
|
|
uri: parsed.uri,
|
2022-03-26 10:09:57 +00:00
|
|
|
}));
|
2020-05-08 23:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AP KeyId => Misskey User and Key
|
|
|
|
*/
|
2022-03-25 07:27:41 +00:00
|
|
|
public async getAuthUserFromKeyId(keyId: string): Promise<{
|
|
|
|
user: CacheableRemoteUser;
|
|
|
|
key: UserPublickey;
|
|
|
|
} | null> {
|
|
|
|
const key = await publicKeyCache.fetch(keyId, async () => {
|
2022-03-26 06:34:00 +00:00
|
|
|
const key = await UserPublickeys.findOneBy({
|
2022-03-25 07:27:41 +00:00
|
|
|
keyId,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (key == null) return null;
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}, key => key != null);
|
2020-05-08 23:21:42 +00:00
|
|
|
|
|
|
|
if (key == null) return null;
|
|
|
|
|
|
|
|
return {
|
2022-03-26 06:34:00 +00:00
|
|
|
user: await userByIdCache.fetch(key.userId, () => Users.findOneByOrFail({ id: key.userId })) as CacheableRemoteUser,
|
2021-12-09 14:58:30 +00:00
|
|
|
key,
|
2020-05-08 23:21:42 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AP Actor id => Misskey User and Key
|
|
|
|
*/
|
2022-03-25 07:27:41 +00:00
|
|
|
public async getAuthUserFromApId(uri: string): Promise<{
|
|
|
|
user: CacheableRemoteUser;
|
|
|
|
key: UserPublickey | null;
|
|
|
|
} | null> {
|
|
|
|
const user = await resolvePerson(uri) as CacheableRemoteUser;
|
2020-05-08 23:21:42 +00:00
|
|
|
|
|
|
|
if (user == null) return null;
|
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
const key = await publicKeyByUserIdCache.fetch(user.id, () => UserPublickeys.findOneBy({ userId: user.id }), v => v != null);
|
2020-05-08 23:21:42 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
user,
|
2021-12-09 14:58:30 +00:00
|
|
|
key,
|
2020-05-08 23:21:42 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|