2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import escapeRegexp from 'escape-regexp';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-02-15 04:06:06 +00:00
|
|
|
import type { NotesRepository, UserPublickeysRepository, UsersRepository } from '@/models/index.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { Config } from '@/config.js';
|
2023-04-04 06:56:47 +00:00
|
|
|
import { MemoryKVCache } from '@/misc/cache.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import type { UserPublickey } from '@/models/entities/UserPublickey.js';
|
2023-04-04 08:32:09 +00:00
|
|
|
import { CacheService } from '@/core/CacheService.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import type { Note } from '@/models/entities/Note.js';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-02-13 06:50:22 +00:00
|
|
|
import { RemoteUser, User } from '@/models/entities/User.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { getApId } from './type.js';
|
|
|
|
import { ApPersonService } from './models/ApPersonService.js';
|
|
|
|
import type { IObject } from './type.js';
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ApDbResolverService {
|
2023-04-04 06:56:47 +00:00
|
|
|
private publicKeyCache: MemoryKVCache<UserPublickey | null>;
|
|
|
|
private publicKeyByUserIdCache: MemoryKVCache<UserPublickey | null>;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userPublickeysRepository)
|
|
|
|
private userPublickeysRepository: UserPublickeysRepository,
|
|
|
|
|
2023-04-04 08:32:09 +00:00
|
|
|
private cacheService: CacheService,
|
2022-09-17 18:27:08 +00:00
|
|
|
private apPersonService: ApPersonService,
|
|
|
|
) {
|
2023-04-04 06:56:47 +00:00
|
|
|
this.publicKeyCache = new MemoryKVCache<UserPublickey | null>(Infinity);
|
|
|
|
this.publicKeyByUserIdCache = new MemoryKVCache<UserPublickey | null>(Infinity);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public parseUri(value: string | IObject): UriParseResult {
|
|
|
|
const uri = getApId(value);
|
|
|
|
|
|
|
|
// the host part of a URL is case insensitive, so use the 'i' flag.
|
|
|
|
const localRegex = new RegExp('^' + escapeRegexp(this.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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AP Note => Misskey Note in DB
|
|
|
|
*/
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async getNoteFromApId(value: string | IObject): Promise<Note | null> {
|
|
|
|
const parsed = this.parseUri(value);
|
|
|
|
|
|
|
|
if (parsed.local) {
|
|
|
|
if (parsed.type !== 'notes') return null;
|
|
|
|
|
|
|
|
return await this.notesRepository.findOneBy({
|
|
|
|
id: parsed.id,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return await this.notesRepository.findOneBy({
|
|
|
|
uri: parsed.uri,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AP Person => Misskey User in DB
|
|
|
|
*/
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2023-02-13 06:28:07 +00:00
|
|
|
public async getUserFromApId(value: string | IObject): Promise<User | null> {
|
2022-09-17 18:27:08 +00:00
|
|
|
const parsed = this.parseUri(value);
|
|
|
|
|
|
|
|
if (parsed.local) {
|
|
|
|
if (parsed.type !== 'users') return null;
|
|
|
|
|
2023-04-04 08:32:09 +00:00
|
|
|
return await this.cacheService.userByIdCache.fetchMaybe(parsed.id, () => this.usersRepository.findOneBy({
|
2022-09-17 18:27:08 +00:00
|
|
|
id: parsed.id,
|
2023-02-13 06:28:07 +00:00
|
|
|
}).then(x => x ?? undefined)) ?? null;
|
2022-09-17 18:27:08 +00:00
|
|
|
} else {
|
2023-04-04 08:32:09 +00:00
|
|
|
return await this.cacheService.uriPersonCache.fetch(parsed.uri, () => this.usersRepository.findOneBy({
|
2022-09-17 18:27:08 +00:00
|
|
|
uri: parsed.uri,
|
2023-02-13 06:28:07 +00:00
|
|
|
}));
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AP KeyId => Misskey User and Key
|
|
|
|
*/
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async getAuthUserFromKeyId(keyId: string): Promise<{
|
2023-02-13 06:50:22 +00:00
|
|
|
user: RemoteUser;
|
2022-09-17 18:27:08 +00:00
|
|
|
key: UserPublickey;
|
|
|
|
} | null> {
|
2022-09-18 18:11:50 +00:00
|
|
|
const key = await this.publicKeyCache.fetch(keyId, async () => {
|
2022-09-17 18:27:08 +00:00
|
|
|
const key = await this.userPublickeysRepository.findOneBy({
|
|
|
|
keyId,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (key == null) return null;
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}, key => key != null);
|
|
|
|
|
|
|
|
if (key == null) return null;
|
|
|
|
|
|
|
|
return {
|
2023-04-04 08:32:09 +00:00
|
|
|
user: await this.cacheService.findUserById(key.userId) as RemoteUser,
|
2022-09-17 18:27:08 +00:00
|
|
|
key,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AP Actor id => Misskey User and Key
|
|
|
|
*/
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async getAuthUserFromApId(uri: string): Promise<{
|
2023-02-13 06:50:22 +00:00
|
|
|
user: RemoteUser;
|
2022-09-17 18:27:08 +00:00
|
|
|
key: UserPublickey | null;
|
|
|
|
} | null> {
|
2023-02-13 06:50:22 +00:00
|
|
|
const user = await this.apPersonService.resolvePerson(uri) as RemoteUser;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
if (user == null) return null;
|
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
const key = await this.publicKeyByUserIdCache.fetch(user.id, () => this.userPublickeysRepository.findOneBy({ userId: user.id }), v => v != null);
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
user,
|
|
|
|
key,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|