2021-08-19 07:55:45 -05:00
|
|
|
import config from '@/config/index';
|
|
|
|
import { Note } from '@/models/entities/note';
|
|
|
|
import { User, IRemoteUser } from '@/models/entities/user';
|
|
|
|
import { UserPublickey } from '@/models/entities/user-publickey';
|
|
|
|
import { MessagingMessage } from '@/models/entities/messaging-message';
|
|
|
|
import { Notes, Users, UserPublickeys, MessagingMessages } from '@/models/index';
|
|
|
|
import { IObject, getApId } from './type';
|
|
|
|
import { resolvePerson } from './models/person';
|
2020-05-08 18:21:42 -05:00
|
|
|
import escapeRegexp = require('escape-regexp');
|
|
|
|
|
|
|
|
export default class DbResolver {
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AP Note => Misskey Note in DB
|
|
|
|
*/
|
|
|
|
public async getNoteFromApId(value: string | IObject): Promise<Note | null> {
|
|
|
|
const parsed = this.parseUri(value);
|
|
|
|
|
|
|
|
if (parsed.id) {
|
|
|
|
return (await Notes.findOne({
|
2021-12-09 08:58:30 -06:00
|
|
|
id: parsed.id,
|
2020-05-08 18:21:42 -05:00
|
|
|
})) || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parsed.uri) {
|
|
|
|
return (await Notes.findOne({
|
2021-12-09 08:58:30 -06:00
|
|
|
uri: parsed.uri,
|
2020-05-08 18:21:42 -05:00
|
|
|
})) || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-10-31 22:14:42 -05:00
|
|
|
public async getMessageFromApId(value: string | IObject): Promise<MessagingMessage | null> {
|
|
|
|
const parsed = this.parseUri(value);
|
|
|
|
|
|
|
|
if (parsed.id) {
|
|
|
|
return (await MessagingMessages.findOne({
|
2021-12-09 08:58:30 -06:00
|
|
|
id: parsed.id,
|
2020-10-31 22:14:42 -05:00
|
|
|
})) || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parsed.uri) {
|
|
|
|
return (await MessagingMessages.findOne({
|
2021-12-09 08:58:30 -06:00
|
|
|
uri: parsed.uri,
|
2020-10-31 22:14:42 -05:00
|
|
|
})) || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-08 18:21:42 -05:00
|
|
|
/**
|
|
|
|
* AP Person => Misskey User in DB
|
|
|
|
*/
|
|
|
|
public async getUserFromApId(value: string | IObject): Promise<User | null> {
|
|
|
|
const parsed = this.parseUri(value);
|
|
|
|
|
|
|
|
if (parsed.id) {
|
|
|
|
return (await Users.findOne({
|
2021-12-09 08:58:30 -06:00
|
|
|
id: parsed.id,
|
2020-05-08 18:21:42 -05:00
|
|
|
})) || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parsed.uri) {
|
|
|
|
return (await Users.findOne({
|
2021-12-09 08:58:30 -06:00
|
|
|
uri: parsed.uri,
|
2020-05-08 18:21:42 -05:00
|
|
|
})) || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AP KeyId => Misskey User and Key
|
|
|
|
*/
|
|
|
|
public async getAuthUserFromKeyId(keyId: string): Promise<AuthUser | null> {
|
|
|
|
const key = await UserPublickeys.findOne({
|
2021-12-09 08:58:30 -06:00
|
|
|
keyId,
|
2020-05-08 18:21:42 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
if (key == null) return null;
|
|
|
|
|
|
|
|
const user = await Users.findOne(key.userId) as IRemoteUser;
|
|
|
|
|
|
|
|
return {
|
|
|
|
user,
|
2021-12-09 08:58:30 -06:00
|
|
|
key,
|
2020-05-08 18:21:42 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AP Actor id => Misskey User and Key
|
|
|
|
*/
|
|
|
|
public async getAuthUserFromApId(uri: string): Promise<AuthUser | null> {
|
|
|
|
const user = await resolvePerson(uri) as IRemoteUser;
|
|
|
|
|
|
|
|
if (user == null) return null;
|
|
|
|
|
2021-07-10 09:14:57 -05:00
|
|
|
const key = await UserPublickeys.findOne(user.id);
|
2020-05-08 18:21:42 -05:00
|
|
|
|
|
|
|
return {
|
|
|
|
user,
|
2021-12-09 08:58:30 -06:00
|
|
|
key,
|
2020-05-08 18:21:42 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public parseUri(value: string | IObject): UriParseResult {
|
|
|
|
const uri = getApId(value);
|
|
|
|
|
|
|
|
const localRegex = new RegExp('^' + escapeRegexp(config.url) + '/' + '(\\w+)' + '/' + '(\\w+)');
|
|
|
|
const matchLocal = uri.match(localRegex);
|
|
|
|
|
|
|
|
if (matchLocal) {
|
|
|
|
return {
|
|
|
|
type: matchLocal[1],
|
2021-12-09 08:58:30 -06:00
|
|
|
id: matchLocal[2],
|
2020-05-08 18:21:42 -05:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
2021-12-09 08:58:30 -06:00
|
|
|
uri,
|
2020-05-08 18:21:42 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type AuthUser = {
|
|
|
|
user: IRemoteUser;
|
2021-07-10 09:14:57 -05:00
|
|
|
key?: UserPublickey;
|
2020-05-08 18:21:42 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
type UriParseResult = {
|
|
|
|
/** id in DB (local object only) */
|
|
|
|
id?: string;
|
|
|
|
/** uri in DB (remote object only) */
|
|
|
|
uri?: string;
|
|
|
|
/** hint of type (local object only, ex: notes, users) */
|
|
|
|
type?: string
|
|
|
|
};
|