2023-07-27 05:31:52 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-08-05 01:33:00 +00:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2022-09-17 18:27:08 +00:00
|
|
|
import promiseLimit from 'promise-limit';
|
2023-09-15 05:28:29 +00:00
|
|
|
import type { MiUser } from '@/models/_.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';
|
2023-08-05 01:33:00 +00:00
|
|
|
import { 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(
|
|
|
|
private apPersonService: ApPersonService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2023-08-16 08:51:28 +00:00
|
|
|
public async extractApMentions(tags: IObject | IObject[] | null | undefined, resolver: Resolver): Promise<MiUser[]> {
|
2023-07-07 23:57:13 +00:00
|
|
|
const hrefs = unique(this.extractApMentionObjects(tags).map(x => x.href));
|
2022-12-03 16:59:28 +00:00
|
|
|
|
2023-08-16 08:51:28 +00:00
|
|
|
const limit = promiseLimit<MiUser | 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-08-16 08:51:28 +00:00
|
|
|
)).filter((x): x is MiUser => 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);
|
|
|
|
}
|
|
|
|
}
|