2023-01-13 04:40:33 +00:00
|
|
|
import type { ApObject } from "./type.js";
|
|
|
|
import { getApIds } from "./type.js";
|
|
|
|
import type Resolver from "./resolver.js";
|
|
|
|
import { resolvePerson } from "./models/person.js";
|
|
|
|
import { unique, concat } from "@/prelude/array.js";
|
|
|
|
import promiseLimit from "promise-limit";
|
|
|
|
import type {
|
|
|
|
CacheableRemoteUser,
|
|
|
|
CacheableUser,
|
|
|
|
} from "@/models/entities/user.js";
|
|
|
|
import { User } from "@/models/entities/user.js";
|
2020-01-30 09:58:13 +00:00
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
type Visibility = "public" | "home" | "followers" | "specified";
|
2020-01-30 09:58:13 +00:00
|
|
|
|
|
|
|
type AudienceInfo = {
|
2023-01-13 04:40:33 +00:00
|
|
|
visibility: Visibility;
|
|
|
|
mentionedUsers: CacheableUser[];
|
|
|
|
visibleUsers: CacheableUser[];
|
2020-01-30 09:58:13 +00:00
|
|
|
};
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
export async function parseAudience(
|
|
|
|
actor: CacheableRemoteUser,
|
|
|
|
to?: ApObject,
|
|
|
|
cc?: ApObject,
|
|
|
|
resolver?: Resolver,
|
|
|
|
): Promise<AudienceInfo> {
|
2020-01-30 09:58:13 +00:00
|
|
|
const toGroups = groupingAudience(getApIds(to), actor);
|
|
|
|
const ccGroups = groupingAudience(getApIds(cc), actor);
|
|
|
|
|
|
|
|
const others = unique(concat([toGroups.other, ccGroups.other]));
|
|
|
|
|
2022-03-25 07:27:41 +00:00
|
|
|
const limit = promiseLimit<CacheableUser | null>(2);
|
2023-01-13 04:40:33 +00:00
|
|
|
const mentionedUsers = (
|
|
|
|
await Promise.all(
|
|
|
|
others.map((id) =>
|
|
|
|
limit(() => resolvePerson(id, resolver).catch(() => null)),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
).filter((x): x is CacheableUser => x != null);
|
2020-01-30 09:58:13 +00:00
|
|
|
|
|
|
|
if (toGroups.public.length > 0) {
|
|
|
|
return {
|
2023-01-13 04:40:33 +00:00
|
|
|
visibility: "public",
|
2020-01-30 09:58:13 +00:00
|
|
|
mentionedUsers,
|
2021-12-09 14:58:30 +00:00
|
|
|
visibleUsers: [],
|
2020-01-30 09:58:13 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ccGroups.public.length > 0) {
|
|
|
|
return {
|
2023-01-13 04:40:33 +00:00
|
|
|
visibility: "home",
|
2020-01-30 09:58:13 +00:00
|
|
|
mentionedUsers,
|
2021-12-09 14:58:30 +00:00
|
|
|
visibleUsers: [],
|
2020-01-30 09:58:13 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (toGroups.followers.length > 0) {
|
|
|
|
return {
|
2023-01-13 04:40:33 +00:00
|
|
|
visibility: "followers",
|
2020-01-30 09:58:13 +00:00
|
|
|
mentionedUsers,
|
2021-12-09 14:58:30 +00:00
|
|
|
visibleUsers: [],
|
2020-01-30 09:58:13 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2023-01-13 04:40:33 +00:00
|
|
|
visibility: "specified",
|
2020-01-30 09:58:13 +00:00
|
|
|
mentionedUsers,
|
2021-12-09 14:58:30 +00:00
|
|
|
visibleUsers: mentionedUsers,
|
2020-01-30 09:58:13 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-25 07:27:41 +00:00
|
|
|
function groupingAudience(ids: string[], actor: CacheableRemoteUser) {
|
2020-01-30 09:58:13 +00:00
|
|
|
const groups = {
|
|
|
|
public: [] as string[],
|
|
|
|
followers: [] as string[],
|
|
|
|
other: [] as string[],
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const id of ids) {
|
|
|
|
if (isPublic(id)) {
|
|
|
|
groups.public.push(id);
|
|
|
|
} else if (isFollowers(id, actor)) {
|
|
|
|
groups.followers.push(id);
|
|
|
|
} else {
|
|
|
|
groups.other.push(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
groups.other = unique(groups.other);
|
|
|
|
|
|
|
|
return groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isPublic(id: string) {
|
|
|
|
return [
|
2023-01-13 04:40:33 +00:00
|
|
|
"https://www.w3.org/ns/activitystreams#Public",
|
|
|
|
"as#Public",
|
|
|
|
"Public",
|
2020-01-30 09:58:13 +00:00
|
|
|
].includes(id);
|
|
|
|
}
|
|
|
|
|
2022-03-25 07:27:41 +00:00
|
|
|
function isFollowers(id: string, actor: CacheableRemoteUser) {
|
2023-01-13 04:40:33 +00:00
|
|
|
return id === (actor.followersUri || `${actor.uri}/followers`);
|
2020-01-30 09:58:13 +00:00
|
|
|
}
|