2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import promiseLimit from 'promise-limit';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-02-16 14:09:41 +00:00
|
|
|
import type { User } from '@/models/index.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { toArray, unique } from '@/misc/prelude/array.js';
|
2023-02-13 06:28:07 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { isMention } from '../type.js';
|
2022-12-03 16:59:28 +00:00
|
|
|
import { ApResolverService, Resolver } from '../ApResolverService.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { ApPersonService } from './ApPersonService.js';
|
|
|
|
import type { IObject, IApMention } from '../type.js';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ApMentionService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
private apResolverService: ApResolverService,
|
|
|
|
private apPersonService: ApPersonService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-12-03 16:59:28 +00:00
|
|
|
public async extractApMentions(tags: IObject | IObject[] | null | undefined, resolver: Resolver) {
|
2022-09-17 18:27:08 +00:00
|
|
|
const hrefs = unique(this.extractApMentionObjects(tags).map(x => x.href as string));
|
2022-12-03 16:59:28 +00:00
|
|
|
|
2023-02-13 06:28:07 +00:00
|
|
|
const limit = promiseLimit<User | null>(2);
|
2022-09-17 18:27:08 +00:00
|
|
|
const mentionedUsers = (await Promise.all(
|
|
|
|
hrefs.map(x => limit(() => this.apPersonService.resolvePerson(x, resolver).catch(() => null))),
|
2023-02-13 06:28:07 +00:00
|
|
|
)).filter((x): x is User => x != null);
|
2023-07-07 22:08:16 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
return mentionedUsers;
|
|
|
|
}
|
2023-07-07 22:08:16 +00:00
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public extractApMentionObjects(tags: IObject | IObject[] | null | undefined): IApMention[] {
|
|
|
|
if (tags == null) return [];
|
|
|
|
return toArray(tags).filter(isMention);
|
|
|
|
}
|
|
|
|
}
|