Fixed 267U.pre2

This commit is contained in:
2026-07-26 18:25:37 +09:00
parent 50bfaeafdf
commit 317d00a284
1286 changed files with 80222 additions and 1 deletions
@@ -0,0 +1,86 @@
import * as crypto from "node:crypto";
import { URL } from "node:url";
export function createSignedPost(args) {
const u = new URL(args.url);
const digestHeader = `SHA-256=${crypto.createHash("sha256").update(args.body).digest("base64")}`;
const request = {
url: u.href,
method: "POST",
headers: objectAssignWithLcKey({
Date: new Date().toUTCString(),
Host: u.hostname,
"Content-Type": "application/activity+json",
Digest: digestHeader
}, args.additionalHeaders)
};
const result = signToRequest(request, args.key, [
"(request-target)",
"date",
"host",
"digest"
]);
return {
request,
signingString: result.signingString,
signature: result.signature,
signatureHeader: result.signatureHeader
};
}
export function createSignedGet(args) {
const u = new URL(args.url);
const request = {
url: u.href,
method: "GET",
headers: objectAssignWithLcKey({
Accept: "application/activity+json, application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"",
Date: new Date().toUTCString(),
Host: new URL(args.url).hostname
}, args.additionalHeaders)
};
const result = signToRequest(request, args.key, [
"(request-target)",
"date",
"host",
"accept"
]);
return {
request,
signingString: result.signingString,
signature: result.signature,
signatureHeader: result.signatureHeader
};
}
function signToRequest(request, key, includeHeaders) {
const signingString = genSigningString(request, includeHeaders);
const signature = crypto.sign("sha256", Buffer.from(signingString), key.privateKeyPem).toString("base64");
const signatureHeader = `keyId="${key.keyId}",algorithm="rsa-sha256",headers="${includeHeaders.join(" ")}",signature="${signature}"`;
request.headers = objectAssignWithLcKey(request.headers, {
Signature: signatureHeader
});
return {
request,
signingString,
signature,
signatureHeader
};
}
function genSigningString(request, includeHeaders) {
request.headers = lcObjectKey(request.headers);
const results = [];
for (const key of includeHeaders.map((x)=>x.toLowerCase())){
if (key === "(request-target)") {
results.push(`(request-target): ${request.method.toLowerCase()} ${new URL(request.url).pathname}`);
} else {
results.push(`${key}: ${request.headers[key]}`);
}
}
return results.join("\n");
}
function lcObjectKey(src) {
const dst = {};
for (const key of Object.keys(src).filter((x)=>x !== "__proto__" && typeof src[x] === "string"))dst[key.toLowerCase()] = src[key];
return dst;
}
function objectAssignWithLcKey(a, b) {
return Object.assign(lcObjectKey(a), lcObjectKey(b));
}
@@ -0,0 +1,71 @@
import { getApIds } from "./type.js";
import Resolver from "./resolver.js";
import { resolvePerson } from "./models/person.js";
import { unique, concat } from "../../prelude/array.js";
import promiseLimit from "promise-limit";
import { RecursionLimiter } from "../../models/repositories/user-profile.js";
export async function parseAudience(actor, to, cc, resolver, limiter = new RecursionLimiter()) {
const toGroups = groupingAudience(getApIds(to), actor);
const ccGroups = groupingAudience(getApIds(cc), actor);
const others = unique(concat([
toGroups.other,
ccGroups.other
]));
resolver ??= new Resolver();
const limit = promiseLimit(2);
const mentionedUsers = (await Promise.all(others.map((id)=>limit(()=>resolvePerson(id, resolver, limiter).catch(()=>null))))).filter((x)=>x != null);
if (toGroups.public.length > 0) {
return {
visibility: "public",
mentionedUsers,
visibleUsers: []
};
}
if (ccGroups.public.length > 0) {
return {
visibility: "home",
mentionedUsers,
visibleUsers: []
};
}
if (toGroups.followers.length > 0) {
return {
visibility: "followers",
mentionedUsers,
visibleUsers: []
};
}
return {
visibility: "specified",
mentionedUsers,
visibleUsers: mentionedUsers
};
}
function groupingAudience(ids, actor) {
const groups = {
public: [],
followers: [],
other: []
};
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) {
return [
"https://www.w3.org/ns/activitystreams#Public",
"as:Public",
"Public"
].includes(id);
}
function isFollowers(id, actor) {
return id === (actor.followersUri || `${actor.uri}/followers`);
}
@@ -0,0 +1,135 @@
import { URL } from "url";
import httpSignature from "@peertube/http-signature";
import config from "../../config/index.js";
import { fetchMeta } from "../../misc/fetch-meta.js";
import { toPuny } from "../../misc/convert-host.js";
import DbResolver from "./db-resolver.js";
import { getApId } from "./type.js";
import { shouldBlockInstance } from "../../misc/should-block-instance.js";
import { verify } from "node:crypto";
import { toSingle } from "../../prelude/array.js";
import { createHash } from "node:crypto";
import { tickFetch } from "../../metrics.js";
export async function hasSignature(req) {
const meta = await fetchMeta();
const required = meta.secureMode || meta.privateMode;
try {
httpSignature.parseRequest(req, {
headers: []
});
} catch (e) {
if (e instanceof Error && e.name === "MissingHeaderError") {
return required ? "missing" : "optional";
}
return "invalid";
}
return required ? "supplied" : "unneeded";
}
export async function checkFetch(req) {
const meta = await fetchMeta();
if (meta.secureMode || meta.privateMode) {
if (req.headers.host !== config.host) return 400;
let signature;
try {
signature = httpSignature.parseRequest(req, {
headers: [
"(request-target)",
"host",
"date"
],
authorizationHeaderName: 'signature'
});
} catch (e) {
return 401;
}
const keyId = new URL(signature.keyId);
const host = toPuny(keyId.hostname);
if (await shouldBlockInstance(host, meta)) {
return 403;
}
if (meta.privateMode && host !== config.host && host !== config.domain && !meta.allowedHosts.includes(host)) {
return 403;
}
const keyIdLower = signature.keyId.toLowerCase();
if (keyIdLower.startsWith("acct:")) {
// Old keyId is no longer supported.
return 401;
}
const dbResolver = new DbResolver();
// HTTP-Signature keyIdを元にDBから取得
let authUser = await dbResolver.getAuthUserFromKeyId(signature.keyId);
// keyIdでわからなければ、resolveしてみる
if (authUser == null) {
try {
keyId.hash = "";
authUser = await dbResolver.getAuthUserFromApId(getApId(keyId.toString()));
} catch (e) {
// できなければ駄目
return 403;
}
}
// publicKey がなくても終了
if (authUser?.key == null) {
return 403;
}
// Cannot authenticate against local user
if (authUser.user.uri === null || authUser.user.host === null) {
return 400;
}
// Check if keyId hostname matches actor hostname
if (toPuny(new URL(authUser.user.uri).hostname) !== host) {
return 403;
}
// HTTP-Signatureの検証
let httpSignatureValidated = httpSignature.verifySignature(signature, authUser.key.keyPem);
// If signature validation failed, try refetching the actor
if (!httpSignatureValidated) {
authUser.key = await dbResolver.refetchPublicKeyForApId(authUser.user);
if (authUser.key == null) {
return 403;
}
httpSignatureValidated = httpSignature.verifySignature(signature, authUser.key.keyPem);
}
if (!httpSignatureValidated) {
return 403;
}
if (!verifySignature(signature, authUser.key)) {
return 401;
}
tickFetch();
return 200;
}
return 200;
}
export async function getSignatureUser(req) {
const signature = httpSignature.parseRequest(req, {
headers: []
});
const keyId = new URL(signature.keyId);
const dbResolver = new DbResolver();
// Retrieve from DB by HTTP-Signature keyId
const authUser = await dbResolver.getAuthUserFromKeyId(signature.keyId);
if (authUser) {
return authUser;
}
// Resolve if failed to retrieve by keyId
keyId.hash = "";
return await dbResolver.getAuthUserFromApId(getApId(keyId.toString()));
}
export function verifySignature(sig, key) {
if (![
'hs2019',
'rsa-sha256'
].includes(sig.algorithm.toLowerCase())) return false;
try {
return verify('rsa-sha256', Buffer.from(sig.signingString, 'utf8'), key.keyPem, Buffer.from(sig.params.signature, 'base64'));
} catch {
// Algo not supported
return false;
}
}
export function verifyDigest(body, digest) {
digest = toSingle(digest);
if (body == null || digest == null || !digest.toLowerCase().startsWith('sha-256=')) return false;
return createHash('sha256').update(body).digest('base64') === digest.substring(8);
}
@@ -0,0 +1,148 @@
import config from "../../config/index.js";
import { Notes, Users, UserPublickeys, MessagingMessages } from "../../models/index.js";
import { Cache } from "../../misc/cache.js";
import { uriPersonCache, userByIdCache } from "../../services/user-cache.js";
import { getApId } from "./type.js";
import { resolvePerson, updatePerson } from "./models/person.js";
import { subscriber } from "../../db/redis.js";
import { toPuny } from "../../misc/convert-host.js";
const publicKeyCache = new Cache("publicKey", 60 * 30);
const publicKeyByUserIdCache = new Cache("publicKeyByUserId", 60 * 30);
export function parseUri(value) {
const uri = getApId(value);
const parsed = new URL(uri);
if (toPuny(parsed.host) === toPuny(config.host)) {
const localRegex = new RegExp(`^.*?/(\\w+)/(\\w+)(?:/(.+))?`);
const matchLocal = uri.match(localRegex);
if (matchLocal == null) {
throw new Error(`Failed to parse local URI: ${uri}`);
}
return {
local: true,
type: matchLocal[1],
id: matchLocal[2],
rest: matchLocal[3]
};
} else {
return {
local: false,
uri
};
}
}
export default class DbResolver {
/**
* AP Note => Misskey Note in DB
*/ async getNoteFromApId(value) {
const parsed = parseUri(value);
if (parsed.local) {
if (parsed.type !== "notes") return null;
return await Notes.findOneBy({
id: parsed.id
});
} else {
return await Notes.findOne({
where: [
{
uri: parsed.uri
},
{
url: parsed.uri
}
]
});
}
}
async getMessageFromApId(value) {
const parsed = parseUri(value);
if (parsed.local) {
if (parsed.type !== "notes") return null;
return await MessagingMessages.findOneBy({
id: parsed.id
});
} else {
return await MessagingMessages.findOneBy({
uri: parsed.uri
});
}
}
/**
* AP Person => Misskey User in DB
*/ async getUserFromApId(value) {
const parsed = parseUri(value);
if (parsed.local) {
if (parsed.type !== "users") return null;
return await userByIdCache.fetchMaybe(parsed.id, ()=>Users.findOneBy({
id: parsed.id
}).then((x)=>x ?? undefined), true) ?? null;
} else {
return await uriPersonCache.fetch(parsed.uri, ()=>Users.findOneBy({
uri: parsed.uri
}), true);
}
}
/**
* AP KeyId => Misskey User and Key
*/ async getAuthUserFromKeyId(keyId) {
const key = await publicKeyCache.fetch(keyId, async ()=>{
const key = await UserPublickeys.findOneBy({
keyId
});
if (key == null) return null;
return key;
}, true, (key)=>key != null);
if (key == null) return null;
return {
user: await userByIdCache.fetch(key.userId, ()=>Users.findOneByOrFail({
id: key.userId
}), true),
key
};
}
/**
* AP Actor id => Misskey User and Key
*/ async getAuthUserFromApId(uri) {
const user = await resolvePerson(uri);
if (user == null) return null;
const key = await publicKeyByUserIdCache.fetch(user.id, ()=>UserPublickeys.findOneBy({
userId: user.id
}), true, (v)=>v != null);
return {
user,
key
};
}
async refetchPublicKeyForApId(user) {
try {
await updatePerson(user.uri, undefined, undefined, user);
let key = await UserPublickeys.findOneBy({
userId: user.id
});
if (key != null) {
await publicKeyByUserIdCache.set(user.id, key);
}
return key;
} catch {
return null;
}
}
}
subscriber.on("message", async (_, data)=>{
const obj = JSON.parse(data);
if (obj.channel === "internal") {
const { type, body } = obj.message;
switch(type){
case "remoteUserDeleted":
case "localUserDeleted":
{
const toDelete = Array.from(await publicKeyByUserIdCache.getAll()).filter((v)=>v[1]?.userId === body.id).map((v)=>v[0]);
const toDeleteKey = Array.from(await publicKeyCache.getAll()).filter((v)=>v[1]?.userId === body.id).map((v)=>v[0]);
await publicKeyByUserIdCache.delete(...toDelete);
await publicKeyCache.delete(...toDeleteKey);
break;
}
default:
break;
}
}
});
@@ -0,0 +1,120 @@
import { IsNull, Not } from "typeorm";
import { Followings, Users } from "../../models/index.js";
import { deliver } from "../../queue/index.js";
import { skippedInstances } from "../../misc/skipped-instances.js";
import { apLogger } from "./logger.js";
const isFollowers = (recipe)=>recipe.type === "Followers";
const isDirect = (recipe)=>recipe.type === "Direct";
//#endregion
export default class DeliverManager {
actor;
activity;
recipes = [];
/**
* Constructor
* @param actor Actor
* @param activity Activity to deliver
*/ constructor(actor, activity){
this.actor = actor;
this.activity = activity;
}
/**
* Add recipe for followers deliver
*/ addFollowersRecipe() {
const deliver = {
type: "Followers"
};
this.addRecipe(deliver);
}
/**
* Add recipe for direct deliver
* @param to To
*/ addDirectRecipe(to) {
const recipe = {
type: "Direct",
to
};
this.addRecipe(recipe);
}
/**
* Add recipe
* @param recipe Recipe
*/ addRecipe(recipe) {
this.recipes.push(recipe);
}
/**
* Execute delivers
*/ async execute() {
if (!Users.isLocalUser(this.actor)) return;
const inboxes = new Set();
/*
build inbox list
Process follower recipes first to avoid duplication when processing
direct recipes later.
*/ if (this.recipes.some((r)=>isFollowers(r))) {
// followers deliver
// TODO: SELECT DISTINCT ON ("followerSharedInbox") "followerSharedInbox" みたいな問い合わせにすればよりパフォーマンス向上できそう
// ただ、sharedInboxがnullなリモートユーザーも稀におり、その対応ができなさそう?
const followers = await Followings.find({
where: {
followeeId: this.actor.id,
followerHost: Not(IsNull())
},
select: {
followerSharedInbox: true,
followerInbox: true
}
});
for (const following of followers){
const inbox = following.followerSharedInbox || following.followerInbox;
inboxes.add(inbox);
}
}
this.recipes.filter((recipe)=>// followers recipes have already been processed
isDirect(recipe) && // check that shared inbox has not been added yet
!(recipe.to.sharedInbox && inboxes.has(recipe.to.sharedInbox)) && // check that they actually have an inbox
recipe.to.inbox != null).forEach((recipe)=>inboxes.add(recipe.to.inbox));
const instancesToSkip = await skippedInstances(// get (unique) list of hosts
Array.from(new Set(Array.from(inboxes).map((inbox)=>{
try {
return new URL(inbox).host;
} catch (e) {
apLogger.error(`Invalid inbox URL: ${inbox}`);
return null;
}
}).filter((host)=>host != null))));
// deliver
for (const inbox of inboxes){
// skip instances as indicated
try {
const host = new URL(inbox).host;
if (instancesToSkip.includes(host)) continue;
} catch (e) {
// skip invalid URLs
apLogger.error(`Invalid inbox URL: ${inbox}`);
continue;
}
deliver(this.actor, this.activity, inbox);
}
}
}
//#region Utilities
/**
* Deliver activity to followers
* @param activity Activity
* @param from Followee
*/ export async function deliverToFollowers(actor, activity) {
const manager = new DeliverManager(actor, activity);
manager.addFollowersRecipe();
await manager.execute();
}
/**
* Deliver activity to user
* @param activity Activity
* @param to Target user
*/ export async function deliverToUser(actor, activity, to) {
const manager = new DeliverManager(actor, activity);
manager.addDirectRecipe(to);
await manager.execute();
} //#endregion
@@ -0,0 +1,21 @@
import accept from "../../../../services/following/requests/accept.js";
import DbResolver from "../../db-resolver.js";
import { relayAccepted } from "../../../../services/relay.js";
export default (async (actor, activity)=>{
// ※ activityはこっちから投げたフォローリクエストなので、activity.actorは存在するローカルユーザーである必要がある
const dbResolver = new DbResolver();
const follower = await dbResolver.getUserFromApId(activity.actor);
if (follower == null) {
return "skip: follower not found";
}
if (follower.host != null) {
return "skip: follower is not a local user";
}
// relay
const match = activity.id?.match(/follow-relay\/(\w+)/);
if (match) {
return await relayAccepted(match[1]);
}
await accept(actor, follower);
return "ok";
});
@@ -0,0 +1,18 @@
import Resolver from "../../resolver.js";
import acceptFollow from "./follow.js";
import { isFollow, getApType, isQuoteRequest } from "../../type.js";
import { apLogger } from "../../logger.js";
import { acceptQuoteRequest } from "./quote-request.js";
const logger = apLogger;
export default (async (actor, activity)=>{
const uri = activity.id || activity;
logger.info(`Accept: ${uri}`);
const resolver = new Resolver();
const object = await resolver.resolve(activity.object).catch((e)=>{
logger.error(`Resolution failed: ${e}`);
throw e;
});
if (isFollow(object)) return await acceptFollow(actor, object);
else if (isQuoteRequest(object)) return await acceptQuoteRequest(actor, object, activity.result);
return `skip: Unknown Accept type: ${getApType(object)}`;
});
@@ -0,0 +1,44 @@
import { resolveNote } from "../../models/note.js";
import { Notes } from "../../../../models/index.js";
import { parseUri } from "../../db-resolver.js";
import edit from "../../../../services/note/edit.js";
import { toPuny } from "../../../../misc/convert-host.js";
export async function acceptQuoteRequest(actor, activity, result) {
if (!result) return "skip: missing result";
const localParsed = parseUri(activity.instrument);
if (!localParsed.local) return "skip: note not local";
const resultUrl = new URL(result);
if (toPuny(resultUrl.host) !== actor.host) {
return "skip: result not on same host as actor";
}
const [note, targetNote] = await Promise.all([
Notes.findOne({
where: {
id: localParsed.id
},
relations: [
"user"
]
}),
resolveNote(activity.object)
]);
if (note === null) return "skip: note not found";
if (targetNote === null) return "skip: target note not found";
if (targetNote.userId !== actor.id) return "skip: tried to authorize note without ownership";
if (note.renoteId === null) return "skip: note not renote";
if (note.renoteId !== targetNote.id) return "skip: note not renoting target";
if (note.quoteAuthorization !== null) return "skip: quote already authorizated";
if (note.text == null && note.cw == null && !note.hasPoll && note.fileIds.length === 0) return "skip: note is plain renote";
note.quoteAuthorization = result;
await Notes.update({
id: note.id
}, {
quoteAuthorization: note.quoteAuthorization
});
await edit(note.user, note, {
text: note.text,
cw: note.cw,
quoteAuthorization: result
}, true);
return "ok";
}
@@ -0,0 +1,22 @@
import { resolveNote } from "../../models/note.js";
import { addPinned } from "../../../../services/i/pin.js";
import Resolver from "../../resolver.js";
import { Users } from "../../../../models/index.js";
export default (async (actor, activity)=>{
if ("actor" in activity && actor.uri !== activity.actor) {
throw new Error("invalid actor");
}
if (activity.target == null) {
throw new Error("target is null");
}
if (activity.target === actor.featured) {
const resolver = new Resolver();
const follower = await Users.getRandomFollower(actor.id);
if (follower) resolver.setUser(follower);
const note = await resolveNote(activity.object, resolver);
if (note == null) throw new Error("note not found");
await addPinned(actor, note.id);
return "ok";
}
throw new Error(`unknown target: ${activity.target}`);
});
@@ -0,0 +1,12 @@
import Resolver from "../../resolver.js";
import announceNote from "./note.js";
import { getApId } from "../../type.js";
import { apLogger } from "../../logger.js";
const logger = apLogger;
export default (async (actor, activity)=>{
const uri = getApId(activity);
logger.info(`Announce: ${uri}`);
const resolver = new Resolver();
const targetUri = getApId(activity.object);
return announceNote(resolver, actor, activity, targetUri);
});
@@ -0,0 +1,57 @@
import post from "../../../../services/note/create.js";
import { getApId } from "../../type.js";
import { fetchNote, resolveNote } from "../../models/note.js";
import { apLogger } from "../../logger.js";
import { extractDbHost } from "../../../../misc/convert-host.js";
import { getApLock } from "../../../../misc/app-lock.js";
import { parseAudience } from "../../audience.js";
import { StatusError } from "../../../../misc/fetch.js";
import { Notes } from "../../../../models/index.js";
import { shouldBlockInstance } from "../../../../misc/should-block-instance.js";
const logger = apLogger;
/**
* Handle announcement activities
*/ export default async function(resolver, actor, activity, targetUri) {
const uri = getApId(activity);
if (actor.isSuspended) {
return "skip: actor is suspended";
}
// Interrupt if you block the announcement destination
if (await shouldBlockInstance(extractDbHost(uri))) return "skip: instance is blocked";
const unlock = await getApLock(uri);
try {
// Check if something with the same URI is already registered
const exist = await fetchNote(uri);
if (exist) {
return "skip: note exists";
}
// Resolve Announce target
let renote;
try {
renote = await resolveNote(targetUri);
} catch (e) {
// Skip if target is 4xx
if (e instanceof StatusError) {
if (!e.isRetryable) {
logger.warn(`Ignored announce target ${targetUri} - ${e.statusCode}`);
return "skip: failed fetching note";
}
logger.warn(`Error in announce target ${targetUri} - ${e.statusCode || e}`);
}
throw e;
}
if (!await Notes.isVisibleForMe(renote, actor.id)) return "skip: invalid actor for this activity";
logger.info(`Creating the (Re)Note: ${uri}`);
const activityAudience = await parseAudience(actor, activity.to, activity.cc);
await post(actor, {
createdAt: activity.published ? new Date(activity.published) : null,
renote,
visibility: activityAudience.visibility,
visibleUsers: activityAudience.visibleUsers,
uri
});
return "ok";
} finally{
unlock();
}
}
@@ -0,0 +1,61 @@
import Resolver from "../resolver.js";
import { fetchPerson } from "../models/person.js";
import { createBite } from "../../../services/create-bite.js";
import { tickBiteIncoming } from "../../../metrics.js";
import { getNote } from "../../../server/api/common/getters.js";
import { parseUri } from "../db-resolver.js";
export default (async (actor, bite)=>{
if (actor.uri !== bite.actor) {
return "skip: actor uri mismatch";
}
if (bite.id === null) {
return "skip: bite id not specified";
}
const resolver = new Resolver();
const biteActor = await fetchPerson(bite.actor, resolver);
if (biteActor === null) {
return "skip: biteActor is null";
}
const targetParsed = parseUri(bite.target);
if (!targetParsed.local) {
return "skip: target is not local";
}
const targetDbId = targetParsed.id;
const targetPathType = targetParsed.type;
let targetType;
let targetId;
let fallback = false;
if (targetPathType === "users") {
targetType = "user";
targetId = targetDbId;
} else if (targetPathType === "bites") {
targetType = "bite";
targetId = targetDbId;
} else if (targetPathType === "notes") {
targetType = "note";
targetId = targetDbId;
try {
await getNote(targetDbId, actor);
} catch (err) {
if (err.id === "9725d0ce-ba28-4dde-95a7-2cbb2c15de24") {
// note either doesn't exist or the remote user shouldn't be able to access it
fallback = true;
}
}
} else {
fallback = true;
}
if (fallback) {
// fallback for unknown object types
targetType = "user";
if (bite.to !== undefined) {
const to = Array.isArray(bite.to) ? bite.to[0] : bite.to;
targetId = to.split("/").pop();
} else {
return "skip: unknown type missing to field";
}
}
await createBite(biteActor, targetType, targetId, bite.id, bite.published ? new Date(bite.published) : null);
tickBiteIncoming();
return "ok";
});
@@ -0,0 +1,20 @@
import block from "../../../../services/blocking/create.js";
import DbResolver from "../../db-resolver.js";
import { Users } from "../../../../models/index.js";
export default (async (actor, activity)=>{
// ※ There is a block target in activity.object, which should be a local user that exists.
const dbResolver = new DbResolver();
const blockee = await dbResolver.getUserFromApId(activity.object);
if (blockee == null) {
return "skip: blockee not found";
}
if (blockee.host != null) {
return "skip: The user you are trying to block is not a local user";
}
await block(await Users.findOneByOrFail({
id: actor.id
}), await Users.findOneByOrFail({
id: blockee.id
}));
return "ok";
});
@@ -0,0 +1,40 @@
import Resolver from "../../resolver.js";
import createNote from "./note.js";
import { getApId, isPost, getApType } from "../../type.js";
import { apLogger } from "../../logger.js";
import { toArray, concat, unique } from "../../../../prelude/array.js";
const logger = apLogger;
export default (async (actor, activity)=>{
const uri = getApId(activity);
logger.info(`Create: ${uri}`);
// copy audiences between activity <=> object.
if (typeof activity.object === "object") {
const to = unique(concat([
toArray(activity.to),
toArray(activity.object.to)
]));
const cc = unique(concat([
toArray(activity.cc),
toArray(activity.object.cc)
]));
activity.to = to;
activity.cc = cc;
activity.object.to = to;
activity.object.cc = cc;
}
// If there is no attributedTo, use Activity actor.
if (typeof activity.object === "object" && !activity.object.attributedTo) {
activity.object.attributedTo = activity.actor;
}
const resolver = new Resolver();
const object = await resolver.resolve(activity.object).catch((e)=>{
logger.error(`Resolution failed: ${e}`);
throw e;
});
if (isPost(object)) {
return createNote(resolver, actor, object, false, activity);
} else {
logger.warn(`Unknown type: ${getApType(object)}`);
return "skip: unknown create type";
}
});
@@ -0,0 +1,37 @@
import { createNote, fetchNote } from "../../models/note.js";
import { getApId } from "../../type.js";
import { getApLock } from "../../../../misc/app-lock.js";
import { extractDbHost } from "../../../../misc/convert-host.js";
import { StatusError } from "../../../../misc/fetch.js";
/**
* Handle post creation activity
*/ export default async function(resolver, actor, note, silent = false, activity) {
const uri = getApId(note);
if (typeof note === "object") {
if (actor.uri !== note.attributedTo) {
return "skip: actor.uri !== note.attributedTo";
}
if (typeof note.id === "string") {
if (extractDbHost(actor.uri) !== extractDbHost(note.id)) {
return "skip: host in actor.uri !== note.id";
}
} else {
return "skip: note.id is not a string";
}
}
const unlock = await getApLock(uri);
try {
const exist = await fetchNote(note);
if (exist) return "skip: note exists";
await createNote(note, resolver, silent);
return "ok";
} catch (e) {
if (e instanceof StatusError && !e.isRetryable) {
return `skip ${e.statusCode}`;
} else {
throw e;
}
} finally{
unlock();
}
}
@@ -0,0 +1,23 @@
import { apLogger } from "../../logger.js";
import { createDeleteAccountJob } from "../../../../queue/index.js";
import { Users } from "../../../../models/index.js";
const logger = apLogger;
export async function deleteActor(actor, uri) {
logger.info(`Deleting the Actor: ${uri}`);
if (actor.uri !== uri) {
return `skip: delete actor ${actor.uri} !== ${uri}`;
}
const user = await Users.findOneBy({
id: actor.id
});
if (!user) {
return `skip: actor ${actor.id} not found in the local database`;
} else if (user.isDeleted) {
return `skip: user ${user.id} already deleted`;
}
const job = await createDeleteAccountJob(actor);
await Users.update(actor.id, {
isDeleted: true
});
return `ok: queued ${job.name} ${job.id}`;
}
@@ -0,0 +1,42 @@
import { toSingle } from "../../../../prelude/array.js";
import { getApId, isTombstone, validPost, validActor } from "../../type.js";
import deleteNote from "./note.js";
import { deleteActor } from "./actor.js";
/**
* Handle delete activity
*/ export default (async (actor, activity)=>{
if ("actor" in activity && actor.uri !== activity.actor) {
throw new Error("invalid actor");
}
// Type of object to be deleted
let formerType;
if (typeof activity.object === "string") {
// The type is unknown, but it has disappeared
// anyway, so it does not remote resolve
formerType = undefined;
} else {
const object = activity.object;
if (isTombstone(object)) {
formerType = toSingle(object.formerType);
} else {
formerType = toSingle(object.type);
}
}
const uri = getApId(activity.object);
// Even if type is unknown, if actor and object are the same,
// it must be `Person`.
if (!formerType && actor.uri === uri) {
formerType = "Person";
}
// If not, fallback to `Note`.
if (!formerType) {
formerType = "Note";
}
if (validPost.includes(formerType)) {
return await deleteNote(actor, uri);
} else if (validActor.includes(formerType)) {
return await deleteActor(actor, uri);
} else {
return `Unknown type ${formerType}`;
}
});
@@ -0,0 +1,30 @@
import deleteNode from "../../../../services/note/delete.js";
import { apLogger } from "../../logger.js";
import DbResolver from "../../db-resolver.js";
import { getApLock } from "../../../../misc/app-lock.js";
import { deleteMessage } from "../../../../services/messages/delete.js";
const logger = apLogger;
export default async function(actor, uri) {
logger.info(`Deleting the Note: ${uri}`);
const unlock = await getApLock(uri);
try {
const dbResolver = new DbResolver();
const note = await dbResolver.getNoteFromApId(uri);
if (note == null) {
const message = await dbResolver.getMessageFromApId(uri);
if (message == null) return "message not found";
if (message.userId !== actor.id) {
return "The user trying to delete the post is not the post author";
}
await deleteMessage(message);
return "ok: message deleted";
}
if (note.userId !== actor.id) {
return "The user trying to delete the post is not the post author";
}
await deleteNode(actor, note);
return "ok: note deleted";
} finally{
unlock();
}
}
@@ -0,0 +1,26 @@
import config from "../../../../config/index.js";
import { getApIds } from "../../type.js";
import { AbuseUserReports, Users } from "../../../../models/index.js";
import { In } from "typeorm";
import { genId } from "../../../../misc/gen-id.js";
export default (async (actor, activity)=>{
// The object is `(User | Note) | (User | Note) []`, but it cannot be
// matched with all patterns of the DB schema, so the target user is the first
// user and it is stored as a comment.
const uris = getApIds(activity.object);
const userIds = uris.filter((uri)=>uri.startsWith(`${config.url}/users/`)).map((uri)=>uri.split("/").pop());
const users = await Users.findBy({
id: In(userIds)
});
if (users.length < 1) return "skip";
await AbuseUserReports.insert({
id: genId(),
createdAt: new Date(),
targetUserId: users[0].id,
targetUserHost: users[0].host,
reporterId: actor.id,
reporterHost: actor.host,
comment: `${activity.content}\n${JSON.stringify(uris, null, 2)}`
});
return "ok";
});
@@ -0,0 +1,14 @@
import follow from "../../../services/following/create.js";
import DbResolver from "../db-resolver.js";
export default (async (actor, activity)=>{
const dbResolver = new DbResolver();
const followee = await dbResolver.getUserFromApId(activity.object);
if (followee == null) {
return "skip: followee not found";
}
if (followee.host != null) {
return "skip: user you are trying to follow is not a local user";
}
await follow(actor, followee, activity.id);
return "ok";
});
@@ -0,0 +1,80 @@
import { isCreate, isDelete, isUpdate, isRead, isFollow, isAccept, isReject, isAdd, isRemove, isAnnounce, isLike, isUndo, isBlock, isCollectionOrOrderedCollection, isFlag, isMove, getApId, isBite, isQuoteRequest } from "../type.js";
import { apLogger } from "../logger.js";
import create from "./create/index.js";
import performDeleteActivity from "./delete/index.js";
import performUpdateActivity from "./update/index.js";
import { performReadActivity } from "./read.js";
import follow from "./follow.js";
import undo from "./undo/index.js";
import like from "./like.js";
import announce from "./announce/index.js";
import accept from "./accept/index.js";
import reject from "./reject/index.js";
import add from "./add/index.js";
import remove from "./remove/index.js";
import block from "./block/index.js";
import flag from "./flag/index.js";
import move from "./move/index.js";
import bite from "./bite.js";
import quoteRequest from "./quote-request.js";
import { extractDbHost } from "../../../misc/convert-host.js";
import { shouldBlockInstance } from "../../../misc/should-block-instance.js";
export async function performActivity(actor, activity) {
if (isCollectionOrOrderedCollection(activity)) {
apLogger.debug('Refusing to ingest collection as activity');
return "skip: activity is collection";
} else {
return await performOneActivity(actor, activity);
}
}
async function performOneActivity(actor, activity) {
if (actor.isSuspended) return "skip: actor suspended";
if (typeof activity.id !== "undefined") {
const host = extractDbHost(getApId(activity));
if (await shouldBlockInstance(host)) return "skip: instance blocked";
}
if (isCreate(activity)) {
return await create(actor, activity);
} else if (isDelete(activity)) {
return await performDeleteActivity(actor, activity);
} else if (isUpdate(activity)) {
return await performUpdateActivity(actor, activity);
} else if (isRead(activity)) {
return await performReadActivity(actor, activity);
} else if (isFollow(activity)) {
return await follow(actor, activity);
} else if (isAccept(activity)) {
return await accept(actor, activity);
} else if (isReject(activity)) {
return await reject(actor, activity);
} else if (isAdd(activity)) {
return await add(actor, activity).catch((err)=>{
apLogger.error(err);
return `skip: ${err}`;
});
} else if (isRemove(activity)) {
return await remove(actor, activity).catch((err)=>{
apLogger.error(err);
return `skip: ${err}`;
});
} else if (isAnnounce(activity)) {
return await announce(actor, activity);
} else if (isLike(activity)) {
return await like(actor, activity);
} else if (isUndo(activity)) {
return await undo(actor, activity);
} else if (isBlock(activity)) {
return await block(actor, activity);
} else if (isFlag(activity)) {
return await flag(actor, activity);
} else if (isMove(activity)) {
return await move(actor, activity);
} else if (isBite(activity)) {
return await bite(actor, activity);
} else if (isQuoteRequest(activity)) {
return await quoteRequest(actor, activity);
} else {
apLogger.warn(`unrecognized activity type: ${activity.type}`);
return `skip: unrecognized activity type: ${activity.type}`;
}
}
@@ -0,0 +1,16 @@
import { getApId } from "../type.js";
import create from "../../../services/note/reaction/create.js";
import { fetchNote, extractEmojis } from "../models/note.js";
export default (async (actor, activity)=>{
const targetUri = getApId(activity.object);
const note = await fetchNote(targetUri);
if (!note) return `skip: target note not found ${targetUri}`;
await extractEmojis(activity.tag || [], actor.host).catch(()=>null);
return await create(actor, note, activity._misskey_reaction || activity.content || activity.name).catch((e)=>{
if (e.id === "51c42bb4-931a-456b-bff7-e5a8a70dd298") {
return "skip: already reacted";
} else {
throw e;
}
}).then(()=>"ok");
});
@@ -0,0 +1,55 @@
import { Followings, Users } from "../../../../models/index.js";
import { resolvePerson, updatePerson } from "../../models/person.js";
import create from "../../../../services/following/create.js";
import deleteFollowing from "../../../../services/following/delete.js";
import { getApHrefNullable } from "../../type.js";
export default (async (actor, activity)=>{
// ※ There is a block target in activity.object, which should be a local user that exists.
// fetch the new and old accounts
const targetUri = getApHrefNullable(activity.target);
if (!targetUri) return "move: target uri is null";
let new_acc = await resolvePerson(targetUri);
if (!actor.uri) return "move: actor uri is null";
let old_acc = await resolvePerson(actor.uri);
// update them if they're remote
if (new_acc.uri) await updatePerson(new_acc.uri);
if (old_acc.uri) await updatePerson(old_acc.uri);
// retrieve updated users
new_acc = await resolvePerson(targetUri);
old_acc = await resolvePerson(actor.uri);
// check if alsoKnownAs of the new account is valid
let isValidMove = true;
if (old_acc.uri) {
if (!new_acc.alsoKnownAs?.includes(old_acc.uri)) {
isValidMove = false;
}
} else if (!new_acc.alsoKnownAs?.includes(old_acc.id)) {
isValidMove = false;
}
if (!isValidMove) {
return "skip: accounts invalid";
}
// add target uri to movedToUri in order to indicate that the user has moved
await Users.update(old_acc.id, {
movedToUri: targetUri
});
// follow the new account and unfollow the old one
const followings = await Followings.findBy({
followeeId: old_acc.id
});
followings.forEach(async (following)=>{
// If follower is local
if (!following.followerHost) {
try {
const follower = await Users.findOneBy({
id: following.followerId
});
if (!follower) return;
await create(follower, new_acc);
await deleteFollowing(follower, old_acc);
} catch {
/* empty */ }
}
});
return "ok";
});
@@ -0,0 +1,41 @@
import { resolveNote } from "../models/note.js";
import renderAcceptQuoteRequest from "../renderer/accept-quote-request.js";
import { deliverToUser } from "../deliver-manager.js";
import { renderActivity } from "../renderer/index.js";
import { parseUri } from "../db-resolver.js";
import { InteractionStamps, Notes } from "../../../models/index.js";
import { genId } from "../../../misc/gen-id.js";
export default (async (actor, activity)=>{
const localParsed = parseUri(activity.object);
if (!localParsed.local) return "skip: local note not local";
const [note, targetNote] = await Promise.all([
resolveNote(activity.instrument),
Notes.findOneBy({
id: localParsed.id
})
]);
if (note === null) return "skip: note not found";
if (note.userId !== actor.id) return "skip: actor is requesting authorization for a quote they didn't make";
if (targetNote === null) return "skip: target note not found";
if (!await Notes.isVisibleForMe(targetNote, actor.id)) return "skip: target note is not visible for remote user";
let stamp = await InteractionStamps.findOneBy({
noteId: note.id,
targetNoteId: targetNote.id
});
if (stamp === null) {
stamp = {
id: genId(),
type: "quote",
noteId: note.id,
targetNoteId: targetNote.id
};
await InteractionStamps.insert(stamp);
}
stamp.note = note;
stamp.targetNote = targetNote;
await deliverToUser({
id: targetNote.userId,
host: null
}, renderActivity(await renderAcceptQuoteRequest(activity, stamp)), actor);
return "ok";
});
@@ -0,0 +1,24 @@
import { getApId } from "../type.js";
import { isSelfHost, extractDbHost } from "../../../misc/convert-host.js";
import { MessagingMessages } from "../../../models/index.js";
import { readUserMessagingMessage } from "../../../server/api/common/read-messaging-message.js";
export const performReadActivity = async (actor, activity)=>{
const id = await getApId(activity.object);
if (!isSelfHost(extractDbHost(id))) {
return `skip: Read to foreign host (${id})`;
}
const messageId = id.split("/").pop();
const message = await MessagingMessages.findOneBy({
id: messageId
});
if (message == null) {
return "skip: message not found";
}
if (actor.id !== message.recipientId) {
return "skip: actor is not a message recipient";
}
await readUserMessagingMessage(message.recipientId, message.userId, [
message.id
]);
return `ok: mark as read (${message.userId} => ${message.recipientId} ${message.id})`;
};
@@ -0,0 +1,22 @@
import { remoteReject } from "../../../../services/following/reject.js";
import DbResolver from "../../db-resolver.js";
import { relayRejected } from "../../../../services/relay.js";
import { Users } from "../../../../models/index.js";
export default (async (actor, activity)=>{
// ※ `activity.actor` must be an existing local user, since `activity` is a follow request thrown from us.
const dbResolver = new DbResolver();
const follower = await dbResolver.getUserFromApId(activity.actor);
if (follower == null) {
return "skip: follower not found";
}
if (!Users.isLocalUser(follower)) {
return "skip: follower is not a local user";
}
// relay
const match = activity.id?.match(/follow-relay\/(\w+)/);
if (match) {
return await relayRejected(match[1]);
}
await remoteReject(actor, follower);
return "ok";
});
@@ -0,0 +1,16 @@
import Resolver from "../../resolver.js";
import rejectFollow from "./follow.js";
import { isFollow, getApType } from "../../type.js";
import { apLogger } from "../../logger.js";
const logger = apLogger;
export default (async (actor, activity)=>{
const uri = activity.id || activity;
logger.info(`Reject: ${uri}`);
const resolver = new Resolver();
const object = await resolver.resolve(activity.object).catch((e)=>{
logger.error(`Resolution failed: ${e}`);
throw e;
});
if (isFollow(object)) return await rejectFollow(actor, object);
return `skip: Unknown Reject type: ${getApType(object)}`;
});
@@ -0,0 +1,17 @@
import { fetchNote } from "../../models/note.js";
import { removePinned } from "../../../../services/i/pin.js";
export default (async (actor, activity)=>{
if ("actor" in activity && actor.uri !== activity.actor) {
throw new Error("invalid actor");
}
if (activity.target == null) {
throw new Error("target is null");
}
if (activity.target === actor.featured) {
const note = await fetchNote(activity.object);
if (note == null) return "skip: note not found"; // not pinned either way
await removePinned(actor, note.id);
return "ok";
}
throw new Error(`unknown target: ${activity.target}`);
});
@@ -0,0 +1,19 @@
import unfollow from "../../../../services/following/delete.js";
import { Followings } from "../../../../models/index.js";
import DbResolver from "../../db-resolver.js";
export default (async (actor, activity)=>{
const dbResolver = new DbResolver();
const follower = await dbResolver.getUserFromApId(activity.object);
if (follower == null) {
return "skip: follower not found";
}
const following = await Followings.findOneBy({
followerId: follower.id,
followeeId: actor.id
});
if (following) {
await unfollow(follower, actor);
return "ok: unfollowed";
}
return "skip: skip: not followed";
});
@@ -0,0 +1,13 @@
import { Notes } from "../../../../models/index.js";
import { getApId } from "../../type.js";
import deleteNote from "../../../../services/note/delete.js";
export const undoAnnounce = async (actor, activity)=>{
const uri = getApId(activity);
const note = await Notes.findOneBy({
uri,
userId: actor.id
});
if (!note) return "skip: no such Announce";
await deleteNote(actor, note);
return "ok: deleted";
};
@@ -0,0 +1,17 @@
import unblock from "../../../../services/blocking/delete.js";
import DbResolver from "../../db-resolver.js";
import { Users } from "../../../../models/index.js";
export default (async (actor, activity)=>{
const dbResolver = new DbResolver();
const blockee = await dbResolver.getUserFromApId(activity.object);
if (blockee == null) {
return "skip: blockee not found";
}
if (blockee.host != null) {
return "skip: The user you are trying to unblock is not a local user";
}
await unblock(await Users.findOneByOrFail({
id: actor.id
}), blockee);
return "ok";
});
@@ -0,0 +1,31 @@
import unfollow from "../../../../services/following/delete.js";
import cancelRequest from "../../../../services/following/requests/cancel.js";
import { FollowRequests, Followings } from "../../../../models/index.js";
import DbResolver from "../../db-resolver.js";
export default (async (actor, activity)=>{
const dbResolver = new DbResolver();
const followee = await dbResolver.getUserFromApId(activity.object);
if (followee == null) {
return "skip: followee not found";
}
if (followee.host != null) {
return "skip: The user you are trying to unfollow is not a local user";
}
const req = await FollowRequests.findOneBy({
followerId: actor.id,
followeeId: followee.id
});
const following = await Followings.findOneBy({
followerId: actor.id,
followeeId: followee.id
});
if (req) {
await cancelRequest(followee, actor);
return "ok: follow request canceled";
}
if (following) {
await unfollow(actor, followee);
return "ok: unfollowed";
}
return "skip: Not requested or followed";
});
@@ -0,0 +1,27 @@
import { isFollow, isBlock, isLike, isAnnounce, getApType, isAccept } from "../../type.js";
import unfollow from "./follow.js";
import unblock from "./block.js";
import undoLike from "./like.js";
import undoAccept from "./accept.js";
import { undoAnnounce } from "./announce.js";
import Resolver from "../../resolver.js";
import { apLogger } from "../../logger.js";
const logger = apLogger;
export default (async (actor, activity)=>{
if ("actor" in activity && actor.uri !== activity.actor) {
throw new Error("invalid actor");
}
const uri = activity.id || activity;
logger.info(`Undo: ${uri}`);
const resolver = new Resolver();
const object = await resolver.resolve(activity.object).catch((e)=>{
logger.error(`Resolution failed: ${e}`);
throw e;
});
if (isFollow(object)) return await unfollow(actor, object);
if (isBlock(object)) return await unblock(actor, object);
if (isLike(object)) return await undoLike(actor, object);
if (isAnnounce(object)) return await undoAnnounce(actor, object);
if (isAccept(object)) return await undoAccept(actor, object);
return `skip: unknown object type ${getApType(object)}`;
});
@@ -0,0 +1,15 @@
import { getApId } from "../../type.js";
import deleteReaction from "../../../../services/note/reaction/delete.js";
import { fetchNote } from "../../models/note.js";
/**
* Process Undo.Like activity
*/ export default (async (actor, activity)=>{
const targetUri = getApId(activity.object);
const note = await fetchNote(targetUri);
if (!note) return `skip: target note not found ${targetUri}`;
await deleteReaction(actor, note).catch((e)=>{
if (e.id === "60527ec9-b4cb-4a88-a6bd-32d3ad26817d") return;
throw e;
});
return "ok";
});
@@ -0,0 +1,41 @@
import { getApId } from "../../type.js";
import { getApType, isActor } from "../../type.js";
import { apLogger } from "../../logger.js";
import { updateNote } from "../../models/note.js";
import Resolver from "../../resolver.js";
import { updatePerson } from "../../models/person.js";
/**
* Handler for the Update activity
*/ export default (async (actor, activity)=>{
if (actor.uri == null || actor.uri !== getApId(activity.actor)) {
return "skip: invalid actor";
}
apLogger.debug("Update");
const resolver = new Resolver();
const object = await resolver.resolve(activity.object).catch((e)=>{
apLogger.error(`Resolution failed: ${e}`);
throw e;
});
if (isActor(object)) {
if (actor.uri !== object.id) {
return "skip: actor id mismatch";
}
await updatePerson(actor.uri, resolver, object);
return "ok: Person updated";
}
const objectType = getApType(object);
switch(objectType){
case "Question":
case "Note":
case "Article":
case "Document":
case "Page":
let failed = false;
await updateNote(object, actor, resolver).catch((e)=>{
failed = true;
});
return failed ? "skip: Note update failed" : "ok: Note updated";
default:
return `skip: Unknown type: ${objectType}`;
}
});
@@ -0,0 +1,2 @@
import { remoteLogger } from "../logger.js";
export const apLogger = remoteLogger.createSubLogger("ap", "magenta");
@@ -0,0 +1,693 @@
const id_v1 = {
"@context": {
id: "@id",
type: "@type",
cred: "https://w3id.org/credentials#",
dc: "http://purl.org/dc/terms/",
identity: "https://w3id.org/identity#",
perm: "https://w3id.org/permissions#",
ps: "https://w3id.org/payswarm#",
rdf: "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
rdfs: "http://www.w3.org/2000/01/rdf-schema#",
sec: "https://w3id.org/security#",
schema: "http://schema.org/",
xsd: "http://www.w3.org/2001/XMLSchema#",
Group: "https://www.w3.org/ns/activitystreams#Group",
claim: {
"@id": "cred:claim",
"@type": "@id"
},
credential: {
"@id": "cred:credential",
"@type": "@id"
},
issued: {
"@id": "cred:issued",
"@type": "xsd:dateTime"
},
issuer: {
"@id": "cred:issuer",
"@type": "@id"
},
recipient: {
"@id": "cred:recipient",
"@type": "@id"
},
Credential: "cred:Credential",
CryptographicKeyCredential: "cred:CryptographicKeyCredential",
about: {
"@id": "schema:about",
"@type": "@id"
},
address: {
"@id": "schema:address",
"@type": "@id"
},
addressCountry: "schema:addressCountry",
addressLocality: "schema:addressLocality",
addressRegion: "schema:addressRegion",
comment: "rdfs:comment",
created: {
"@id": "dc:created",
"@type": "xsd:dateTime"
},
creator: {
"@id": "dc:creator",
"@type": "@id"
},
description: "schema:description",
email: "schema:email",
familyName: "schema:familyName",
givenName: "schema:givenName",
image: {
"@id": "schema:image",
"@type": "@id"
},
label: "rdfs:label",
name: "schema:name",
postalCode: "schema:postalCode",
streetAddress: "schema:streetAddress",
title: "dc:title",
url: {
"@id": "schema:url",
"@type": "@id"
},
Person: "schema:Person",
PostalAddress: "schema:PostalAddress",
Organization: "schema:Organization",
identityService: {
"@id": "identity:identityService",
"@type": "@id"
},
idp: {
"@id": "identity:idp",
"@type": "@id"
},
Identity: "identity:Identity",
paymentProcessor: "ps:processor",
preferences: {
"@id": "ps:preferences",
"@type": "@vocab"
},
cipherAlgorithm: "sec:cipherAlgorithm",
cipherData: "sec:cipherData",
cipherKey: "sec:cipherKey",
digestAlgorithm: "sec:digestAlgorithm",
digestValue: "sec:digestValue",
domain: "sec:domain",
expires: {
"@id": "sec:expiration",
"@type": "xsd:dateTime"
},
initializationVector: "sec:initializationVector",
member: {
"@id": "schema:member",
"@type": "@id"
},
memberOf: {
"@id": "schema:memberOf",
"@type": "@id"
},
nonce: "sec:nonce",
normalizationAlgorithm: "sec:normalizationAlgorithm",
owner: {
"@id": "sec:owner",
"@type": "@id"
},
password: "sec:password",
privateKey: {
"@id": "sec:privateKey",
"@type": "@id"
},
privateKeyPem: "sec:privateKeyPem",
publicKey: {
"@id": "sec:publicKey",
"@type": "@id"
},
publicKeyPem: "sec:publicKeyPem",
publicKeyService: {
"@id": "sec:publicKeyService",
"@type": "@id"
},
revoked: {
"@id": "sec:revoked",
"@type": "xsd:dateTime"
},
signature: "sec:signature",
signatureAlgorithm: "sec:signatureAlgorithm",
signatureValue: "sec:signatureValue",
CryptographicKey: "sec:Key",
EncryptedMessage: "sec:EncryptedMessage",
GraphSignature2012: "sec:GraphSignature2012",
LinkedDataSignature2015: "sec:LinkedDataSignature2015",
accessControl: {
"@id": "perm:accessControl",
"@type": "@id"
},
writePermission: {
"@id": "perm:writePermission",
"@type": "@id"
}
}
};
const security_v1 = {
"@context": {
id: "@id",
type: "@type",
dc: "http://purl.org/dc/terms/",
sec: "https://w3id.org/security#",
xsd: "http://www.w3.org/2001/XMLSchema#",
EcdsaKoblitzSignature2016: "sec:EcdsaKoblitzSignature2016",
Ed25519Signature2018: "sec:Ed25519Signature2018",
EncryptedMessage: "sec:EncryptedMessage",
GraphSignature2012: "sec:GraphSignature2012",
LinkedDataSignature2015: "sec:LinkedDataSignature2015",
LinkedDataSignature2016: "sec:LinkedDataSignature2016",
CryptographicKey: "sec:Key",
authenticationTag: "sec:authenticationTag",
canonicalizationAlgorithm: "sec:canonicalizationAlgorithm",
cipherAlgorithm: "sec:cipherAlgorithm",
cipherData: "sec:cipherData",
cipherKey: "sec:cipherKey",
created: {
"@id": "dc:created",
"@type": "xsd:dateTime"
},
creator: {
"@id": "dc:creator",
"@type": "@id"
},
digestAlgorithm: "sec:digestAlgorithm",
digestValue: "sec:digestValue",
domain: "sec:domain",
encryptionKey: "sec:encryptionKey",
expiration: {
"@id": "sec:expiration",
"@type": "xsd:dateTime"
},
expires: {
"@id": "sec:expiration",
"@type": "xsd:dateTime"
},
initializationVector: "sec:initializationVector",
iterationCount: "sec:iterationCount",
nonce: "sec:nonce",
normalizationAlgorithm: "sec:normalizationAlgorithm",
owner: {
"@id": "sec:owner",
"@type": "@id"
},
password: "sec:password",
privateKey: {
"@id": "sec:privateKey",
"@type": "@id"
},
privateKeyPem: "sec:privateKeyPem",
publicKey: {
"@id": "sec:publicKey",
"@type": "@id"
},
publicKeyBase58: "sec:publicKeyBase58",
publicKeyPem: "sec:publicKeyPem",
publicKeyWif: "sec:publicKeyWif",
publicKeyService: {
"@id": "sec:publicKeyService",
"@type": "@id"
},
revoked: {
"@id": "sec:revoked",
"@type": "xsd:dateTime"
},
salt: "sec:salt",
signature: "sec:signature",
signatureAlgorithm: "sec:signingAlgorithm",
signatureValue: "sec:signatureValue"
}
};
const activitystreams = {
"@context": {
"@vocab": "_:",
xsd: "http://www.w3.org/2001/XMLSchema#",
as: "https://www.w3.org/ns/activitystreams#",
ldp: "http://www.w3.org/ns/ldp#",
vcard: "http://www.w3.org/2006/vcard/ns#",
id: "@id",
type: "@type",
Accept: "as:Accept",
Activity: "as:Activity",
IntransitiveActivity: "as:IntransitiveActivity",
Add: "as:Add",
Announce: "as:Announce",
Application: "as:Application",
Arrive: "as:Arrive",
Article: "as:Article",
Audio: "as:Audio",
Block: "as:Block",
Collection: "as:Collection",
CollectionPage: "as:CollectionPage",
Relationship: "as:Relationship",
Create: "as:Create",
Delete: "as:Delete",
Dislike: "as:Dislike",
Document: "as:Document",
Event: "as:Event",
Follow: "as:Follow",
Flag: "as:Flag",
Group: "as:Group",
Ignore: "as:Ignore",
Image: "as:Image",
Invite: "as:Invite",
Join: "as:Join",
Leave: "as:Leave",
Like: "as:Like",
Link: "as:Link",
Mention: "as:Mention",
Note: "as:Note",
Object: "as:Object",
Offer: "as:Offer",
OrderedCollection: "as:OrderedCollection",
OrderedCollectionPage: "as:OrderedCollectionPage",
Organization: "as:Organization",
Page: "as:Page",
Person: "as:Person",
Place: "as:Place",
Profile: "as:Profile",
Question: "as:Question",
Reject: "as:Reject",
Remove: "as:Remove",
Service: "as:Service",
TentativeAccept: "as:TentativeAccept",
TentativeReject: "as:TentativeReject",
Tombstone: "as:Tombstone",
Undo: "as:Undo",
Update: "as:Update",
Video: "as:Video",
View: "as:View",
Listen: "as:Listen",
Read: "as:Read",
Move: "as:Move",
Travel: "as:Travel",
IsFollowing: "as:IsFollowing",
IsFollowedBy: "as:IsFollowedBy",
IsContact: "as:IsContact",
IsMember: "as:IsMember",
subject: {
"@id": "as:subject",
"@type": "@id"
},
relationship: {
"@id": "as:relationship",
"@type": "@id"
},
actor: {
"@id": "as:actor",
"@type": "@id"
},
attributedTo: {
"@id": "as:attributedTo",
"@type": "@id"
},
attachment: {
"@id": "as:attachment",
"@type": "@id"
},
bcc: {
"@id": "as:bcc",
"@type": "@id"
},
bto: {
"@id": "as:bto",
"@type": "@id"
},
cc: {
"@id": "as:cc",
"@type": "@id"
},
context: {
"@id": "as:context",
"@type": "@id"
},
current: {
"@id": "as:current",
"@type": "@id"
},
first: {
"@id": "as:first",
"@type": "@id"
},
generator: {
"@id": "as:generator",
"@type": "@id"
},
icon: {
"@id": "as:icon",
"@type": "@id"
},
image: {
"@id": "as:image",
"@type": "@id"
},
inReplyTo: {
"@id": "as:inReplyTo",
"@type": "@id"
},
items: {
"@id": "as:items",
"@type": "@id"
},
instrument: {
"@id": "as:instrument",
"@type": "@id"
},
orderedItems: {
"@id": "as:items",
"@type": "@id",
"@container": "@list"
},
last: {
"@id": "as:last",
"@type": "@id"
},
location: {
"@id": "as:location",
"@type": "@id"
},
next: {
"@id": "as:next",
"@type": "@id"
},
object: {
"@id": "as:object",
"@type": "@id"
},
oneOf: {
"@id": "as:oneOf",
"@type": "@id"
},
anyOf: {
"@id": "as:anyOf",
"@type": "@id"
},
closed: {
"@id": "as:closed",
"@type": "xsd:dateTime"
},
origin: {
"@id": "as:origin",
"@type": "@id"
},
accuracy: {
"@id": "as:accuracy",
"@type": "xsd:float"
},
prev: {
"@id": "as:prev",
"@type": "@id"
},
preview: {
"@id": "as:preview",
"@type": "@id"
},
replies: {
"@id": "as:replies",
"@type": "@id"
},
result: {
"@id": "as:result",
"@type": "@id"
},
audience: {
"@id": "as:audience",
"@type": "@id"
},
partOf: {
"@id": "as:partOf",
"@type": "@id"
},
tag: {
"@id": "as:tag",
"@type": "@id"
},
target: {
"@id": "as:target",
"@type": "@id"
},
to: {
"@id": "as:to",
"@type": "@id"
},
url: {
"@id": "as:url",
"@type": "@id"
},
altitude: {
"@id": "as:altitude",
"@type": "xsd:float"
},
content: "as:content",
contentMap: {
"@id": "as:content",
"@container": "@language"
},
name: "as:name",
nameMap: {
"@id": "as:name",
"@container": "@language"
},
duration: {
"@id": "as:duration",
"@type": "xsd:duration"
},
endTime: {
"@id": "as:endTime",
"@type": "xsd:dateTime"
},
height: {
"@id": "as:height",
"@type": "xsd:nonNegativeInteger"
},
href: {
"@id": "as:href",
"@type": "@id"
},
hreflang: "as:hreflang",
latitude: {
"@id": "as:latitude",
"@type": "xsd:float"
},
longitude: {
"@id": "as:longitude",
"@type": "xsd:float"
},
mediaType: "as:mediaType",
published: {
"@id": "as:published",
"@type": "xsd:dateTime"
},
radius: {
"@id": "as:radius",
"@type": "xsd:float"
},
rel: "as:rel",
startIndex: {
"@id": "as:startIndex",
"@type": "xsd:nonNegativeInteger"
},
startTime: {
"@id": "as:startTime",
"@type": "xsd:dateTime"
},
summary: "as:summary",
summaryMap: {
"@id": "as:summary",
"@container": "@language"
},
totalItems: {
"@id": "as:totalItems",
"@type": "xsd:nonNegativeInteger"
},
units: "as:units",
updated: {
"@id": "as:updated",
"@type": "xsd:dateTime"
},
width: {
"@id": "as:width",
"@type": "xsd:nonNegativeInteger"
},
describes: {
"@id": "as:describes",
"@type": "@id"
},
formerType: {
"@id": "as:formerType",
"@type": "@id"
},
deleted: {
"@id": "as:deleted",
"@type": "xsd:dateTime"
},
inbox: {
"@id": "ldp:inbox",
"@type": "@id"
},
outbox: {
"@id": "as:outbox",
"@type": "@id"
},
following: {
"@id": "as:following",
"@type": "@id"
},
followers: {
"@id": "as:followers",
"@type": "@id"
},
streams: {
"@id": "as:streams",
"@type": "@id"
},
preferredUsername: "as:preferredUsername",
endpoints: {
"@id": "as:endpoints",
"@type": "@id"
},
uploadMedia: {
"@id": "as:uploadMedia",
"@type": "@id"
},
proxyUrl: {
"@id": "as:proxyUrl",
"@type": "@id"
},
liked: {
"@id": "as:liked",
"@type": "@id"
},
oauthAuthorizationEndpoint: {
"@id": "as:oauthAuthorizationEndpoint",
"@type": "@id"
},
oauthTokenEndpoint: {
"@id": "as:oauthTokenEndpoint",
"@type": "@id"
},
provideClientKey: {
"@id": "as:provideClientKey",
"@type": "@id"
},
signClientKey: {
"@id": "as:signClientKey",
"@type": "@id"
},
sharedInbox: {
"@id": "as:sharedInbox",
"@type": "@id"
},
Public: {
"@id": "as:Public",
"@type": "@id"
},
source: "as:source",
likes: {
"@id": "as:likes",
"@type": "@id"
},
shares: {
"@id": "as:shares",
"@type": "@id"
},
alsoKnownAs: {
"@id": "as:alsoKnownAs",
"@type": "@id"
}
}
};
export const WellKnownContext = {
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
{
// as non-standards
manuallyApprovesFollowers: "as:manuallyApprovesFollowers",
movedTo: {
"@id": "https://www.w3.org/ns/activitystreams#movedTo",
"@type": "@id"
},
movedToUri: "as:movedTo",
sensitive: "as:sensitive",
Hashtag: "as:Hashtag",
quoteUri: "fedibird:quoteUri",
quoteUrl: "as:quoteUrl",
// Mastodon
toot: "http://joinmastodon.org/ns#",
Emoji: "toot:Emoji",
featured: "toot:featured",
discoverable: "toot:discoverable",
// schema
schema: "http://schema.org#",
PropertyValue: "schema:PropertyValue",
value: "schema:value",
// Misskey
misskey: "https://misskey-hub.net/ns#",
_misskey_content: "misskey:_misskey_content",
_misskey_quote: "misskey:_misskey_quote",
_misskey_reaction: "misskey:_misskey_reaction",
_misskey_votes: "misskey:_misskey_votes",
_misskey_talk: "misskey:_misskey_talk",
_misskey_summary: "misskey:_misskey_summary",
isCat: "misskey:isCat",
// Fedibird
fedibird: "http://fedibird.com/ns#",
// vcard
vcard: "http://www.w3.org/2006/vcard/ns#",
// litepub
litepub: "http://litepub.social/ns#",
EmojiReact: "litepub:EmojiReact",
EmojiReaction: "litepub:EmojiReaction",
// mia
Bite: "https://ns.mia.jetzt/as#Bite",
canBite: {
"@id": "https://ns.mia.jetzt/as#canBite",
"@type": "@id"
},
// pancakes
pronouns: {
"@id": "https://ns.pancakes.gay/as#pronouns",
"@container": "@language"
},
// mastodon-style quotes
QuoteAuthorization: "https://w3id.org/fep/044f#QuoteAuthorization",
quote: {
"@id": "https://w3id.org/fep/044f#quote",
"@type": "@id"
},
gts: "https://gotosocial.org/ns#",
interactionPolicy: {
"@id": "gts:interactionPolicy",
"@type": "@id"
},
canQuote: {
"@id": "gts:canQuote",
"@type": "@id"
},
automaticApproval: {
"@id": "gts:automaticApproval",
"@type": "@id"
},
interactingObject: {
"@id": "gts:interactingObject",
"@type": "@id"
},
interactionTarget: {
"@id": "gts:interactionTarget",
"@type": "@id"
}
}
]
};
export const CONTEXTS = {
"https://w3id.org/identity/v1": id_v1,
"https://w3id.org/security/v1": security_v1,
"https://www.w3.org/ns/activitystreams": activitystreams
};
@@ -0,0 +1,6 @@
import * as mfm from "mfm-js";
import { toHtml } from "../../../mfm/to-html.js";
export default async function(note) {
if (!note.text) return "";
return toHtml(mfm.parse(note.text), JSON.parse(note.mentionedRemoteUsers), note.userHost);
}
@@ -0,0 +1,6 @@
import { extractApHashtagObjects } from "../models/tag.js";
import { fromHtml } from "../../../mfm/from-html.js";
export async function htmlToMfm(html, tag) {
const hashtagNames = extractApHashtagObjects(tag).map((x)=>x.name).filter((x)=>x != null);
return await fromHtml(html, hashtagNames);
}
@@ -0,0 +1,145 @@
import * as crypto from "node:crypto";
import jsonld from "jsonld";
import { CONTEXTS, WellKnownContext } from "./contexts.js";
import fetch from "node-fetch";
import { httpAgent, httpsAgent } from "../../../misc/fetch.js";
// RsaSignature2017 based from https://github.com/transmute-industries/RsaSignature2017
export class LdSignature {
debug = false;
preLoad = true;
loaderTimeout = 10 * 1000;
async signRsaSignature2017(data, privateKey, creator, domain, created) {
const options = {
type: "RsaSignature2017",
creator,
domain,
nonce: crypto.randomBytes(16).toString("hex"),
created: (created || new Date()).toISOString()
};
if (!domain) {
options.domain = undefined;
}
const toBeSigned = await this.createVerifyData(data, options);
const signer = crypto.createSign("sha256");
signer.update(toBeSigned);
signer.end();
const signature = signer.sign(privateKey);
return {
...data,
signature: {
...options,
signatureValue: signature.toString("base64")
}
};
}
async verifyRsaSignature2017(data, signature, publicKey) {
const toBeSigned = await this.createVerifyData(data, signature);
const verifier = crypto.createVerify("sha256");
verifier.update(toBeSigned);
return verifier.verify(publicKey, signature.signatureValue, "base64");
}
async createVerifyData(data, options) {
const transformedOptions = {
...options,
"@context": "https://w3id.org/identity/v1"
};
delete transformedOptions["type"];
delete transformedOptions["id"];
delete transformedOptions["signatureValue"];
const canonizedOptions = await this.normalize(transformedOptions);
const optionsHash = this.sha256(canonizedOptions);
const transformedData = {
...data
};
const cannonidedData = await this.normalize(transformedData);
if (this.debug) console.debug(`cannonidedData: ${cannonidedData}`);
const documentHash = this.sha256(cannonidedData);
const verifyData = `${optionsHash}${documentHash}`;
return verifyData;
}
async normalize(data) {
const customLoader = this.getLoader();
return await jsonld.normalize(data, {
documentLoader: customLoader
});
}
async compactToWellKnown(data) {
const options = {
documentLoader: this.getLoader()
};
const context = WellKnownContext;
return await jsonld.compact(data, context, options);
}
getLoader() {
return async (url)=>{
if (!url.match("^https?://")) throw new Error(`Invalid URL ${url}`);
if (this.preLoad) {
if (url in CONTEXTS) {
if (this.debug) console.debug(`HIT: ${url}`);
return {
contextUrl: null,
document: CONTEXTS[url],
documentUrl: url
};
}
}
if (this.debug) console.debug(`MISS: ${url}`);
const document = await this.fetchDocument(url);
return {
contextUrl: null,
document: document,
documentUrl: url
};
};
}
async fetchDocument(url) {
const json = await fetch(url, {
headers: {
Accept: "application/ld+json, application/json"
},
size: 1024 * 1024,
// TODO
//timeout: this.loaderTimeout,
agent: (u)=>u.protocol === "http:" ? httpAgent : httpsAgent
}).then((res)=>{
if (!res.ok) {
throw new Error(`${res.status} ${res.statusText}`);
} else {
return res.json();
}
});
return json;
}
sha256(data) {
const hash = crypto.createHash("sha256");
hash.update(data);
return hash.digest("hex");
}
containsForbiddenDirectives(doc) {
if (typeof doc === "object" && doc !== null) {
if (Array.isArray(doc)) {
for (const item of doc){
if (this.containsForbiddenDirectives(item)) {
return true;
}
}
} else {
for (const [key, value] of Object.entries(doc)){
if ([
"@included",
"@graph",
"@reverse"
].includes(key)) {
return true;
}
if (typeof value === "object" && value !== null) {
if (this.containsForbiddenDirectives(value)) {
return true;
}
}
}
}
}
return false;
}
}
@@ -0,0 +1 @@
export { };
@@ -0,0 +1 @@
export { };
@@ -0,0 +1,59 @@
import { uploadFromUrl } from "../../../services/drive/upload-from-url.js";
import Resolver from "../resolver.js";
import { fetchMeta } from "../../../misc/fetch-meta.js";
import { apLogger } from "../logger.js";
import { DriveFiles } from "../../../models/index.js";
import { truncate } from "../../../misc/truncate.js";
import { DB_MAX_IMAGE_COMMENT_LENGTH } from "../../../misc/hard-limits.js";
const logger = apLogger;
/**
* create an Image.
*/ export async function createImage(actor, value) {
// Skip if author is frozen.
if (actor.isSuspended) {
throw new Error("actor has been suspended");
}
const image = await new Resolver().resolve(value);
if (image.url == null) {
throw new Error("Invalid image, URL not provided");
}
if (!image.url.startsWith("https://") && !image.url.startsWith("http://")) {
throw new Error(`Invalid image, unexpected schema: ${image.url}`);
}
logger.info(`Creating the Image: ${image.url}`);
const instance = await fetchMeta();
let file = await uploadFromUrl({
url: image.url,
user: actor,
uri: image.url,
sensitive: image.sensitive,
isLink: !instance.cacheRemoteFiles,
comment: truncate(image.name, DB_MAX_IMAGE_COMMENT_LENGTH)
});
if (file.isLink) {
// If the URL is different, it means that the same image was previously
// registered with a different URL, so update the URL
if (file.url !== image.url) {
await DriveFiles.update({
id: file.id
}, {
url: image.url,
uri: image.url
});
file = await DriveFiles.findOneByOrFail({
id: file.id
});
}
}
return file;
}
/**
* Resolve Image.
*
* If the target Image is registered in Iceshrimp, return it, otherwise
* Fetch from remote server, register with Iceshrimp and return it.
*/ export async function resolveImage(actor, value) {
// TODO
// Fetch from remote server and register
return await createImage(actor, value);
}
@@ -0,0 +1,17 @@
import promiseLimit from "promise-limit";
import { toArray, unique } from "../../../prelude/array.js";
import { isMention } from "../type.js";
import Resolver from "../resolver.js";
import { resolvePerson } from "./person.js";
import { RecursionLimiter } from "../../../models/repositories/user-profile.js";
export async function extractApMentions(tags, limiter = new RecursionLimiter()) {
const hrefs = unique(extractApMentionObjects(tags).map((x)=>x.href));
const resolver = new Resolver();
const limit = promiseLimit(2);
const mentionedUsers = (await Promise.all(hrefs.map((x)=>limit(()=>resolvePerson(x, resolver, limiter).catch(()=>null))))).filter((x)=>x != null);
return mentionedUsers;
}
export function extractApMentionObjects(tags) {
if (tags == null) return [];
return toArray(tags).filter(isMention);
}
@@ -0,0 +1,563 @@
import promiseLimit from "promise-limit";
import * as mfm from "mfm-js";
import config from "../../../config/index.js";
import Resolver from "../resolver.js";
import post from "../../../services/note/create.js";
import { extractMentionedUsers } from "../../../services/note/create.js";
import { resolvePerson } from "./person.js";
import { resolveImage } from "./image.js";
import { htmlToMfm } from "../misc/html-to-mfm.js";
import { extractApHashtags } from "./tag.js";
import { unique, toArray, toSingle } from "../../../prelude/array.js";
import { extractPollFromQuestion } from "./question.js";
import vote from "../../../services/note/polls/vote.js";
import { apLogger } from "../logger.js";
import { extractDbHost, toPuny } from "../../../misc/convert-host.js";
import { Emojis, Polls, MessagingMessages, Notes, NoteEdits, DriveFiles, PollVotes } from "../../../models/index.js";
import { getOneApId, getApId, getOneApHrefNullable, validPost, isEmoji, getApType } from "../type.js";
import { genId } from "../../../misc/gen-id.js";
import { getApLock } from "../../../misc/app-lock.js";
import { createMessage } from "../../../services/messages/create.js";
import { parseAudience } from "../audience.js";
import { extractApMentions } from "./mention.js";
import DbResolver from "../db-resolver.js";
import { StatusError } from "../../../misc/fetch.js";
import { shouldBlockInstance } from "../../../misc/should-block-instance.js";
import { publishNoteStream, publishNoteUpdatesStream } from "../../../services/stream.js";
import { extractHashtags } from "../../../misc/extract-hashtags.js";
import { UserProfiles } from "../../../models/index.js";
import { In } from "typeorm";
import { DB_MAX_IMAGE_COMMENT_LENGTH } from "../../../misc/hard-limits.js";
import { truncate } from "../../../misc/truncate.js";
import { getEmojiSize } from "../../../misc/emoji-meta.js";
import { RecursionLimiter } from "../../../models/repositories/user-profile.js";
const logger = apLogger;
export function validateNote(object, uri) {
const expectHost = extractDbHost(uri);
if (object == null) {
return new Error("invalid Note: object is null");
}
if (!validPost.includes(getApType(object))) {
return new Error(`invalid Note: invalid object type ${getApType(object)}`);
}
if (object.id && extractDbHost(object.id) !== expectHost) {
return new Error(`invalid Note: id has different host. expected: ${expectHost}, actual: ${extractDbHost(object.id)}`);
}
if (object.attributedTo && extractDbHost(getOneApId(object.attributedTo)) !== expectHost) {
return new Error(`invalid Note: attributedTo has different host. expected: ${expectHost}, actual: ${extractDbHost(object.attributedTo)}`);
}
return null;
}
/**
* Fetch Notes.
*
* If the target Note is registered in Iceshrimp, it will be returned.
*/ export async function fetchNote(object) {
const dbResolver = new DbResolver();
return await dbResolver.getNoteFromApId(object);
}
/**
* Create a Note.
*/ export async function createNote(value, resolver, silent = false, limiter = new RecursionLimiter()) {
if (resolver == null) resolver = new Resolver();
const object = await resolver.resolve(value);
const entryUri = getApId(value);
const err = validateNote(object, entryUri);
if (err) {
logger.error(`${err.message}`, {
resolver: {
history: resolver.getHistory()
},
value: value,
object: object
});
throw new Error("invalid note");
}
const note = object;
if (note.id == null) {
throw new Error('Note must have an id');
}
const idUrl = new URL(note.id);
if (idUrl.protocol != 'https:') {
throw new Error(`unexpected schema of note.id: ${note.id}`);
}
let url = getOneApHrefNullable(note.url);
const urlUrl = url != null ? new URL(url) : null;
if (urlUrl != null && urlUrl.protocol != 'https:') {
throw new Error(`unexpected schema of note url: ${url}`);
}
logger.debug(`Note fetched: ${JSON.stringify(note, null, 2)}`);
logger.info(`Creating the Note: ${note.id}`);
// Skip if note is made before 2007 (1yr before Fedi was created)
// OR skip if note is made 3 days in advance
if (note.published) {
const DateChecker = new Date(note.published);
const FutureCheck = new Date();
FutureCheck.setDate(FutureCheck.getDate() + 3); // Allow some wiggle room for misconfigured hosts
if (DateChecker.getFullYear() < 2007) {
logger.warn("Note somehow made before Activitypub was created; discarding");
return null;
}
if (DateChecker > FutureCheck) {
logger.warn("Note somehow made after today; discarding");
return null;
}
}
// Fetch author
const actor = await resolvePerson(getOneApId(note.attributedTo), resolver, limiter);
if (actor.uri == null) {
logger.warn('Note actor uri is null, discarding');
return null;
}
const actorUri = new URL(actor.uri);
if (idUrl.host != actorUri.host) {
logger.warn("Note id host doesn't match actor host, discarding");
return null;
}
if (urlUrl != null && urlUrl.host != actorUri.host) {
logger.debug("Note url host doesn't match actor host, clearing variable");
url = undefined;
}
// Skip if author is suspended.
if (actor.isSuspended) {
logger.debug(`User ${actor.usernameLower}@${actor.host} suspended; discarding.`);
return null;
}
const noteAudience = await parseAudience(actor, note.to, note.cc, undefined, limiter);
let visibility = noteAudience.visibility;
const visibleUsers = noteAudience.visibleUsers;
// If Audience (to, cc) was not specified
if (visibility === "specified" && visibleUsers.length === 0) {
if (typeof value === "string") {
// If the input is a string, GET occurs in resolver
// Public if you can GET anonymously from here
visibility = "public";
}
}
let isTalk = note._misskey_talk && visibility === "specified";
const apMentions = await extractApMentions(note.tag, limiter);
const apHashtags = extractApHashtags(note.tag);
// Attachments
// TODO: attachmentは必ずしもImageではない
// TODO: attachmentは必ずしも配列ではない
// Noteがsensitiveなら添付もsensitiveにする
const limit = promiseLimit(2);
note.attachment = Array.isArray(note.attachment) ? note.attachment : note.attachment ? [
note.attachment
] : [];
note.attachment = note.attachment.filter((attach)=>[
"Document",
"Image",
"Audio",
"Video"
].includes(attach.type));
const files = note.attachment.map((attach)=>attach.sensitive = note.sensitive) ? (await Promise.all(note.attachment.map((x)=>limit(()=>resolveImage(actor, x))))).filter((image)=>image != null) : [];
// Reply
const reply = note.inReplyTo ? await resolveNote(note.inReplyTo, resolver, limiter).then((x)=>{
if (x == null) {
logger.warn("Specified inReplyTo, but nout found");
throw new Error("inReplyTo not found");
} else {
return x;
}
}).catch(async (e)=>{
// トークだったらinReplyToのエラーは無視
const uri = getApId(note.inReplyTo);
if (uri.startsWith(`${config.url}/`)) {
const id = uri.split("/").pop();
const talk = await MessagingMessages.findOneBy({
id
});
if (talk) {
isTalk = true;
return null;
}
}
logger.warn(`Error in inReplyTo ${note.inReplyTo} - ${e.statusCode || e}`);
throw e;
}) : null;
// Quote
let quote;
if (note._misskey_quote || note.quoteUrl || note.quoteUri || note.quote) {
const tryResolveNote = async (uri)=>{
if (typeof uri !== "string" || !uri.match(/^https?:/)) return {
status: "permerror"
};
try {
const res = await resolveNote(uri, undefined, limiter);
if (res) {
return {
status: "ok",
res
};
} else {
return {
status: "permerror"
};
}
} catch (e) {
return {
status: e instanceof StatusError && !e.isRetryable ? "permerror" : "temperror"
};
}
};
const uris = unique([
note._misskey_quote,
note.quoteUrl,
note.quoteUri,
note.quote
].filter((x)=>typeof x === "string"));
const results = await Promise.all(uris.map((uri)=>tryResolveNote(uri)));
quote = results.filter((x)=>x.status === "ok").map((x)=>x.res).find((x)=>x);
if (!quote) {
if (results.some((x)=>x.status === "temperror")) {
throw new Error("quote resolve failed");
}
}
}
const cw = note.summary === "" ? null : note.summary;
// Text parsing
let text = null;
if (note.source?.mediaType === "text/x.misskeymarkdown" && typeof note.source?.content === "string") {
text = note.source.content;
} else if (typeof note._misskey_content !== "undefined") {
text = note._misskey_content;
} else if (typeof note.content === "string") {
text = await htmlToMfm(note.content, note.tag);
}
// vote
if (reply?.hasPoll) {
const poll = await Polls.findOneByOrFail({
noteId: reply.id
});
const tryCreateVote = async (name, index)=>{
if (poll.expiresAt && Date.now() > new Date(poll.expiresAt).getTime()) {
logger.warn(`vote to expired poll from AP: actor=${actor.username}@${actor.host}, note=${note.id}, choice=${name}`);
} else if (index >= 0) {
logger.info(`vote from AP: actor=${actor.username}@${actor.host}, note=${note.id}, choice=${name}`);
await vote(actor, reply, index);
}
return null;
};
if (note.name) {
return await tryCreateVote(note.name, poll.choices.findIndex((x)=>x === note.name));
}
}
const emojis = await extractEmojis(note.tag || [], actor.host).catch((e)=>{
logger.info(`extractEmojis: ${e}`);
return [];
});
const apEmojis = emojis.map((emoji)=>emoji.name);
const poll = await extractPollFromQuestion(note, resolver).catch(()=>undefined);
if (isTalk) {
for (const recipient of visibleUsers){
await createMessage(actor, recipient, undefined, text || undefined, files && files.length > 0 ? files[0] : null, object.id);
return null;
}
}
return await post(actor, {
createdAt: note.published ? new Date(note.published) : null,
files,
reply,
renote: quote,
name: note.name,
cw,
text,
localOnly: false,
visibility,
visibleUsers,
apMentions,
apHashtags,
apEmojis,
poll,
uri: note.id,
url: url,
canQuote: !!note.interactionPolicy?.canQuote
}, silent, limiter);
}
/**
* Resolve Note.
*
* If the target Note is registered in Iceshrimp, return it, otherwise
* Fetch from remote server, register with Iceshrimp and return it.
*/ export async function resolveNote(value, resolver, limiter = new RecursionLimiter()) {
const uri = typeof value === "string" ? value : value.id;
if (uri == null) throw new Error("missing uri");
// Abort if origin host is blocked
if (await shouldBlockInstance(extractDbHost(uri))) throw new StatusError("host blocked", 451, `host ${extractDbHost(uri)} is blocked`);
const unlock = await getApLock(uri);
try {
//#region Returns if already registered with this server
const exist = await fetchNote(uri);
if (exist) {
return exist;
}
//#endregion
if (extractDbHost(uri) === toPuny(config.host)) {
throw new StatusError("cannot resolve local note", 400, "cannot resolve local note");
}
// Fetch from remote server and register
// If the attached `Note` Object is specified here instead of the uri, the note will be generated without going through the server fetch.
// Since the attached Note Object may be disguised, always specify the uri and fetch it from the server.
return await createNote(uri, resolver, true, limiter);
} finally{
unlock();
}
}
export async function extractEmojis(tags, host) {
host = toPuny(host);
if (!tags) return [];
const eomjiTags = toArray(tags).filter(isEmoji);
return await Promise.all(eomjiTags.map(async (tag)=>{
const name = tag.name.replace(/^:/, "").replace(/:$/, "");
tag.icon = toSingle(tag.icon);
const exists = await Emojis.findOneBy({
host,
name
});
if (exists) {
if (tag.updated != null && exists.updatedAt == null || tag.id != null && exists.uri == null || tag.updated != null && exists.updatedAt != null && new Date(tag.updated) > exists.updatedAt || tag.icon.url !== exists.originalUrl || !(exists.width && exists.height)) {
let size = {
width: 0,
height: 0
};
try {
size = await getEmojiSize(tag.icon.url);
} catch {
/* skip if any error happens */ }
await Emojis.update({
host,
name
}, {
uri: tag.id,
originalUrl: tag.icon.url,
publicUrl: tag.icon.url,
updatedAt: new Date(),
width: size.width || null,
height: size.height || null
});
return await Emojis.findOneBy({
host,
name
});
}
return exists;
}
logger.info(`register emoji host=${host}, name=${name}`);
let size = {
width: 0,
height: 0
};
try {
size = await getEmojiSize(tag.icon.url);
} catch {
/* skip if any error happens */ }
return await Emojis.insert({
id: genId(),
host,
name,
uri: tag.id,
originalUrl: tag.icon.url,
publicUrl: tag.icon.url,
updatedAt: new Date(),
aliases: [],
glyph: tag.icon?.type === "image/svg+xml",
width: size.width || null,
height: size.height || null
}).then((x)=>Emojis.findOneByOrFail(x.identifiers[0]));
}));
}
function notEmpty(partial) {
return Object.keys(partial).length > 0;
}
export async function updateNote(value, actor, resolver) {
const uri = typeof value === "string" ? value : value.id;
if (!uri) throw new Error("Missing note uri");
// Skip if URI points to this server
if (extractDbHost(uri) === toPuny(config.host)) throw new Error("uri points local");
// A new resolver is created if not specified
if (resolver == null) resolver = new Resolver();
// Resolve the updated Note object
const post1 = await resolver.resolve(value);
if (getOneApId(post1.attributedTo) !== actor.uri || actor.uri == null) {
throw new Error('Refusing to ingest update for note with mismatching actor');
}
// Already registered with this server?
const note = await Notes.findOneBy({
uri
});
if (note == null) {
return await createNote(post1, resolver);
}
if (note.userId !== actor.id) {
throw new Error('Refusing to ingest update for note of different user');
}
// Whether to tell clients the note has been updated and requires refresh.
let updating = false;
// Text parsing
let text = null;
if (post1.source?.mediaType === "text/x.misskeymarkdown" && typeof post1.source?.content === "string") {
text = post1.source.content;
} else if (typeof post1._misskey_content !== "undefined") {
text = post1._misskey_content;
} else if (typeof post1.content === "string") {
text = await htmlToMfm(post1.content, post1.tag);
}
const cw = post1.summary === "" ? null : post1.summary;
// File parsing
const fileList = post1.attachment ? Array.isArray(post1.attachment) ? post1.attachment : [
post1.attachment
] : [];
// Fetch files
const limit = promiseLimit(2);
const driveFiles = (await Promise.all(fileList.map((x)=>limit(async ()=>{
const file = await resolveImage(actor, x);
const update = {};
const altText = truncate(x.name, DB_MAX_IMAGE_COMMENT_LENGTH) ?? null;
if (file.comment !== altText) {
update.comment = altText;
}
// Don't unmark previously marked sensitive files,
// but if edited post contains sensitive marker, update it.
if (post1.sensitive && !file.isSensitive) {
update.isSensitive = post1.sensitive;
}
if (notEmpty(update)) {
await DriveFiles.update(file.id, update);
updating = true;
}
return file;
})))).filter((file)=>file != null);
const fileIds = driveFiles.map((file)=>file.id);
const fileTypes = driveFiles.map((file)=>file.type);
const apEmojis = (await extractEmojis(post1.tag || [], actor.host).catch((e)=>[])).map((emoji)=>emoji.name);
const apMentions = await extractApMentions(post1.tag);
const apHashtags = await extractApHashtags(post1.tag);
const poll = await extractPollFromQuestion(post1, resolver).catch(()=>undefined);
const choices = poll?.choices.flatMap((choice)=>mfm.parse(choice)) ?? [];
const tokens = mfm.parse(text || "").concat(mfm.parse(cw || "")).concat(choices);
const hashTags = apHashtags || extractHashtags(tokens);
const mentionUsers = apMentions || await extractMentionedUsers(actor, tokens);
const mentionUserIds = mentionUsers.map((user)=>user.id);
const remoteUsers = mentionUsers.filter((user)=>user.host != null);
const remoteUserIds = remoteUsers.map((user)=>user.id);
const remoteProfiles = await UserProfiles.findBy({
userId: In(remoteUserIds)
});
const mentionedRemoteUsers = remoteUsers.map((user)=>{
const profile = remoteProfiles.find((profile)=>profile.userId === user.id);
return {
username: user.username,
host: user.host ?? null,
uri: user.uri,
url: profile ? profile.url : undefined
};
});
const update = {};
if (text && text !== note.text) {
update.text = text;
}
if (cw !== note.cw) {
update.cw = cw ? cw : null;
}
if (fileIds.sort().join(",") !== note.fileIds.sort().join(",")) {
update.fileIds = fileIds;
update.attachedFileTypes = fileTypes;
}
if (hashTags.sort().join(",") !== note.tags.sort().join(",")) {
update.tags = hashTags;
}
if (mentionUserIds.sort().join(",") !== note.mentions.sort().join(",")) {
update.mentions = mentionUserIds;
update.mentionedRemoteUsers = JSON.stringify(mentionedRemoteUsers);
}
if (apEmojis.sort().join(",") !== note.emojis.sort().join(",")) {
update.emojis = apEmojis;
}
if (note.hasPoll !== !!poll) {
update.hasPoll = !!poll;
}
if (poll) {
const dbPoll = await Polls.findOneBy({
noteId: note.id
});
if (poll?.votes != null && poll.votes.find((p)=>!Number.isInteger(p) || p < 0) !== undefined) {
throw new Error('Refusing to ingest poll with non-integer or negative vote count');
}
if (dbPoll == null) {
await Polls.insert({
noteId: note.id,
choices: poll?.choices,
multiple: poll?.multiple,
votes: poll?.votes,
expiresAt: poll?.expiresAt,
noteVisibility: note.visibility === "hidden" ? "home" : note.visibility,
userId: actor.id,
userHost: actor.host
});
updating = true;
} else {
const choicesChanged = JSON.stringify(dbPoll.choices) !== JSON.stringify(poll.choices);
if (dbPoll.multiple !== poll.multiple || dbPoll.expiresAt !== poll.expiresAt || dbPoll.noteVisibility !== note.visibility || choicesChanged) {
await Polls.update({
noteId: note.id
}, {
choices: poll?.choices,
multiple: poll?.multiple,
votes: poll?.votes,
expiresAt: poll?.expiresAt,
noteVisibility: note.visibility === "hidden" ? "home" : note.visibility
});
// Reset votes
if (choicesChanged) {
await PollVotes.delete({
noteId: dbPoll.noteId
});
}
updating = true;
} else {
for(let i = 0; i < poll.choices.length; i++){
if (dbPoll.votes[i] !== poll.votes?.[i]) {
await Polls.update({
noteId: note.id
}, {
votes: poll?.votes
});
updating = true;
break;
}
}
}
}
}
// Update Note
if (notEmpty(update)) {
update.updatedAt = new Date();
// Save updated note to the database
await Notes.update({
uri
}, update);
// Save an edit history for the previous note
await NoteEdits.insert({
id: genId(),
noteId: note.id,
text: note.text,
cw: note.cw,
fileIds: note.fileIds,
updatedAt: update.updatedAt
});
updating = true;
}
if (updating) {
// Publish update event for the updated note details
publishNoteStream(note.id, "updated", {
updatedAt: update.updatedAt
});
const updatedNote = {
...note,
...update
};
publishNoteUpdatesStream("updated", updatedNote);
}
return null;
}
@@ -0,0 +1,638 @@
import { URL } from "node:url";
import promiseLimit from "promise-limit";
import config from "../../../config/index.js";
import { registerOrFetchInstanceDoc } from "../../../services/register-or-fetch-instance-doc.js";
import { updateUsertags } from "../../../services/update-hashtag.js";
import { Users, Instances, DriveFiles, Followings, UserProfiles, UserPublickeys } from "../../../models/index.js";
import { User } from "../../../models/entities/user.js";
import { UserNotePining } from "../../../models/entities/user-note-pining.js";
import { genId } from "../../../misc/gen-id.js";
import { instanceChart, usersChart } from "../../../services/chart/index.js";
import { UserPublickey } from "../../../models/entities/user-publickey.js";
import { isDuplicateKeyValueError } from "../../../misc/is-duplicate-key-value-error.js";
import { extractDbHost, toPuny } from "../../../misc/convert-host.js";
import { UserProfile } from "../../../models/entities/user-profile.js";
import { toArray } from "../../../prelude/array.js";
import { fetchInstanceMetadata } from "../../../services/fetch-instance-metadata.js";
import { normalizeForSearch } from "../../../misc/normalize-for-search.js";
import { truncate } from "../../../misc/truncate.js";
import { StatusError } from "../../../misc/fetch.js";
import { uriPersonCache } from "../../../services/user-cache.js";
import { publishInternalEvent } from "../../../services/stream.js";
import { db } from "../../../db/postgre.js";
import { apLogger } from "../logger.js";
import { htmlToMfm } from "../misc/html-to-mfm.js";
import { fromHtml } from "../../../mfm/from-html.js";
import { isCollectionOrOrderedCollection, isCollection, getApId, getOneApHrefNullable, isPropertyValue, getApType, isActor } from "../type.js";
import Resolver from "../resolver.js";
import { extractApHashtags } from "./tag.js";
import { resolveNote, extractEmojis } from "./note.js";
import { resolveImage } from "./image.js";
import { getSubjectHostFromUri, getSubjectHostFromRemoteUser, getSubjectHostFromAcctParts } from "../../resolve-user.js";
import { RecursionLimiter } from "../../../models/repositories/user-profile.js";
import { UserConverter } from "../../../server/api/mastodon/converters/user.js";
import fetch from "node-fetch";
const logger = apLogger;
const nameLength = 128;
const summaryLength = 2048;
/**
* Validate and convert to actor object
* @param x Fetched object
* @param uri Fetch target URI
*/ function validateActor(x, uri) {
const expectHost = extractDbHost(uri);
if (x == null) {
throw new Error("invalid Actor: object is null");
}
if (!isActor(x)) {
throw new Error(`invalid Actor type '${x.type}'`);
}
if (!(typeof x.id === "string" && x.id.length > 0)) {
throw new Error("invalid Actor: wrong id");
}
if (!(typeof x.inbox === "string" && x.inbox.length > 0 && extractDbHost(x.inbox) === expectHost)) {
throw new Error("invalid Actor: wrong inbox");
}
if (!(typeof x.outbox === "string" && x.outbox.length > 0 && extractDbHost(getApId(x.outbox)) === expectHost)) {
throw new Error("invalid Actor: wrong outbox");
}
const sharedInboxObject = x.sharedInbox ?? (x.endpoints ? x.endpoints.sharedInbox : undefined);
if (sharedInboxObject != null) {
const sharedInbox = getApId(sharedInboxObject);
if (!(typeof sharedInbox === "string" && sharedInbox.length > 0 && extractDbHost(sharedInbox) === expectHost)) {
throw new Error("invalid Actor: wrong shared inbox");
}
}
if (x.followers != null) {
x.followers = getApId(x.followers);
if (!(typeof x.followers === "string" && x.followers.length > 0 && extractDbHost(x.followers) === expectHost)) {
throw new Error("invalid Actor: wrong followers");
}
}
if (x.following != null) {
x.following = getApId(x.following);
if (!(typeof x.following === "string" && x.following.length > 0 && extractDbHost(x.following) === expectHost)) {
throw new Error("invalid Actor: wrong following");
}
}
if (!(typeof x.preferredUsername === "string" && x.preferredUsername.length > 0 && x.preferredUsername.length <= 128 && /^\w([\w-.]*\w)?$/.test(x.preferredUsername))) {
throw new Error("invalid Actor: wrong username");
}
// These fields are only informational, and some AP software allows these
// fields to be very long. If they are too long, we cut them off. This way
// we can at least see these users and their activities.
if (x.name) {
if (!(typeof x.name === "string" && x.name.length > 0)) {
throw new Error("invalid Actor: wrong name");
}
x.name = truncate(x.name, nameLength);
}
if (x.summary) {
if (!(typeof x.summary === "string" && x.summary.length > 0)) {
throw new Error("invalid Actor: wrong summary");
}
x.summary = truncate(x.summary, summaryLength);
}
const idHost = toPuny(new URL(x.id).host);
if (idHost !== expectHost) {
throw new Error("invalid Actor: id has different host");
}
if (x.publicKey) {
if (typeof x.publicKey.id !== "string") {
throw new Error("invalid Actor: publicKey.id is not a string");
}
const publicKeyIdHost = toPuny(new URL(x.publicKey.id).host);
if (publicKeyIdHost !== expectHost) {
throw new Error("invalid Actor: publicKey.id has different host");
}
}
if (x.pronouns) {
if (typeof x.pronouns !== "object") {
throw new Error("invalid Actor: pronouns is not an object");
}
for (const key of Object.keys(x.pronouns)){
if (typeof x.pronouns[key] !== "string") {
throw new Error(`invalid Actor: pronouns.${key} is not a string`);
}
}
}
if (x.canBite && typeof x.canBite !== "string") {
throw new Error("invalid Actor: canBite is not a string");
}
return x;
}
/**
* Fetch a Person.
*
* If the target Person is registered in Iceshrimp, it will be returned.
*/ export async function fetchPerson(uri, resolver) {
if (typeof uri !== "string") throw new Error("uri is not string");
const cached = await uriPersonCache.get(uri, true);
if (cached) return cached;
// Fetch from the database if the URI points to this server
if (extractDbHost(uri) === toPuny(config.host)) {
const id = uri.split("/").pop();
const u = await Users.findOneBy({
id
});
if (u) await uriPersonCache.set(uri, u);
return u;
}
//#region Returns if already registered with this server
const user = await Users.findOneBy({
uri
});
if (user != null) {
await uriPersonCache.set(uri, user);
return user;
}
//#endregion
return null;
}
/**
* Create Person.
*/ export async function createPerson(uri, resolver, subjectHost, limiter = new RecursionLimiter()) {
if (typeof uri !== "string") throw new Error("uri is not string");
if (extractDbHost(uri) === toPuny(config.host)) {
throw new StatusError("cannot resolve local user", 400, "cannot resolve local user");
}
if (resolver == null) resolver = new Resolver();
let object = await resolver.resolve(uri);
let person;
try {
person = validateActor(object, uri);
} catch (e) {
// Work around GoToSocial issue #1186 (ref: https://github.com/superseriousbusiness/gotosocial/issues/1186)
if (typeof object.publicKey?.owner !== 'string' || object.inbox != null) throw e;
logger.info(`Received stub actor, re-resolving with key owner uri: ${object.publicKey.owner}`);
object = await resolver.resolve(object.publicKey.owner);
person = validateActor(object, uri);
}
logger.info(`Creating the Person: ${person.id}`);
const usernameLower = person.preferredUsername?.toLowerCase();
const urlHostname = toPuny(new URL(object.id).hostname);
const host = subjectHost ?? await getSubjectHostFromUri(object.id) ?? await getSubjectHostFromAcctParts(usernameLower, urlHostname) ?? urlHostname;
if (usernameLower !== null) {
let checkUser = await Users.findOneBy({
usernameLower: usernameLower,
host: toPuny(new URL(object.id).hostname)
});
if (checkUser != null) {
logger.info('Person already exists');
if (host != checkUser.host) {
logger.info(`Updating existing person with canonical account domain (${usernameLower}@${checkUser.host} -> ${usernameLower}@${host})`);
await Users.update({
usernameLower: usernameLower,
host: checkUser.host
}, {
host: host
});
checkUser.host = host;
}
logger.info('Returning existing person');
return checkUser;
}
if (host != toPuny(new URL(object.id).hostname)) {
checkUser = await Users.findOneBy({
usernameLower: usernameLower,
host: host
});
if (checkUser != null) {
logger.info('Person already exists');
logger.info('Returning existing person');
return checkUser;
}
}
}
const { fields } = await analyzeAttachments(person.attachment || []);
const tags = extractApHashtags(person.tag).map((tag)=>normalizeForSearch(tag)).splice(0, 32);
const isBot = getApType(object) === "Service";
const bday = person["vcard:bday"]?.match(/^\d{4}-\d{2}-\d{2}/);
let url = getOneApHrefNullable(person.url);
const urlUrl = url != null ? new URL(url) : null;
const uriUrl = new URL(uri);
if (urlUrl != null && urlUrl.protocol != 'https:') {
throw new Error(`unexpected schema of person url: ${url}`);
}
if (urlUrl != null && urlUrl.host != uriUrl.host) {
logger.debug("Person url host doesn't match person uri host, clearing variable");
url = undefined;
}
let followersCount;
if (typeof person.followers === "string") {
try {
let data = await fetch(person.followers, {
headers: {
Accept: "application/json"
},
size: 1024 * 1024
});
let json_data = JSON.parse(await data.text());
followersCount = json_data.totalItems;
} catch {
followersCount = undefined;
}
}
let followingCount;
if (typeof person.following === "string") {
try {
let data = await fetch(person.following, {
headers: {
Accept: "application/json"
},
size: 1024 * 1024
});
let json_data = JSON.parse(await data.text());
followingCount = json_data.totalItems;
} catch (e) {
followingCount = undefined;
}
}
let canBite = "nobody";
if (person.canBite) {
if (person.canBite === "https://www.w3.org/ns/activitystreams#Public") {
canBite = "anyone";
} else if (person.followers && person.canBite === getApId(person.followers)) {
canBite = "followers";
}
}
// Prepare objects
let user = new User({
id: genId(),
avatarId: null,
bannerId: null,
createdAt: new Date(),
lastFetchedAt: new Date(),
name: truncate(person.name, nameLength),
isLocked: !!person.manuallyApprovesFollowers,
movedToUri: person.movedTo,
alsoKnownAs: person.alsoKnownAs,
isExplorable: !!person.discoverable,
username: person.preferredUsername,
usernameLower: person.preferredUsername.toLowerCase(),
host,
inbox: person.inbox,
sharedInbox: person.sharedInbox || (person.endpoints ? person.endpoints.sharedInbox : undefined),
followersUri: person.followers ? getApId(person.followers) : undefined,
followersCount: followersCount !== undefined ? followersCount : person.followers && typeof person.followers !== "string" && isCollectionOrOrderedCollection(person.followers) ? person.followers.totalItems : undefined,
followingCount: followingCount !== undefined ? followingCount : person.following && typeof person.following !== "string" && isCollectionOrOrderedCollection(person.following) ? person.following.totalItems : undefined,
featured: person.featured ? getApId(person.featured) : undefined,
uri: person.id,
tags,
isBot,
isCat: person.isCat === true,
canBite
});
const profile = new UserProfile({
userId: user.id,
description: person.summary ? await htmlToMfm(truncate(person.summary, summaryLength), person.tag) : null,
url: url,
fields,
birthday: bday ? bday[0] : null,
location: person["vcard:Address"] || null,
userHost: host,
pronouns: person.pronouns || {}
});
const publicKey = person.publicKey ? new UserPublickey({
userId: user.id,
keyId: person.publicKey.id,
keyPem: person.publicKey.publicKeyPem
}) : null;
try {
// Save the objects atomically using a db transaction, note that we should never run any code in a transaction block directly
await db.transaction(async (transactionalEntityManager)=>{
await transactionalEntityManager.save(user);
await transactionalEntityManager.save(profile);
if (publicKey) await transactionalEntityManager.save(publicKey);
});
} catch (e) {
// duplicate key error
if (isDuplicateKeyValueError(e)) {
// /users/@a => /users/:id Corresponds to an error that may occur when the input is an alias like
const u = await Users.findOneBy({
uri: person.id
});
if (u) {
user = u;
} else {
throw new Error("already registered");
}
} else {
logger.error(e instanceof Error ? e : new Error(e));
throw e;
}
}
// Register host
registerOrFetchInstanceDoc(host).then((i)=>{
Instances.increment({
id: i.id
}, "usersCount", 1);
instanceChart.newUser(i.host);
fetchInstanceMetadata(i);
});
usersChart.update(user, true);
// Hashtag update
updateUsertags(user, tags);
// Mentions update, then prewarm html cache
if (await limiter.shouldContinue()) UserProfiles.updateMentions(user.id, limiter).then((_)=>UserConverter.prewarmCacheById(user.id));
//#region Fetch avatar and header image
const [avatar, banner] = await Promise.all([
person.icon,
person.image
].map((img)=>img == null ? Promise.resolve(null) : resolveImage(user, img).catch(()=>null)));
const avatarId = avatar?.id ?? null;
const avatarBlurhash = avatar?.blurhash ?? null;
const avatarUrl = avatar ? DriveFiles.getDatabasePrefetchUrl(avatar, true) : null;
const bannerId = banner?.id ?? null;
const bannerBlurhash = banner?.blurhash ?? null;
const bannerUrl = banner ? DriveFiles.getDatabasePrefetchUrl(banner, false) : null;
await Users.update(user.id, {
avatarId,
avatarBlurhash,
avatarUrl,
bannerId,
bannerBlurhash,
bannerUrl
});
user.avatarId = avatarId;
user.avatarBlurhash = avatarBlurhash;
user.avatarUrl = avatarUrl;
user.bannerId = bannerId;
user.bannerBlurhash = bannerBlurhash;
user.bannerUrl = bannerUrl;
//#endregion
//#region Get custom emoji
const emojis = await extractEmojis(person.tag || [], host).catch((e)=>{
logger.info(`extractEmojis: ${e}`);
return [];
});
const emojiNames = emojis.map((emoji)=>emoji.name);
await Users.update(user.id, {
emojis: emojiNames
});
//#endregion
await updateFeatured(user.id, resolver, limiter).catch((err)=>logger.error(err));
return user;
}
/**
* Update Person data from remote.
* If the target Person is not registered in Iceshrimp, it is ignored.
* @param uri URI of Person
* @param resolver Resolver
* @param hint Hint of Person object (If this value is a valid Person, it is used for updating without Remote resolve)
* @param userHint Hint of IRemoteUser object, used for updating user information for remotes that only support webfinger with acct: query
*/ export async function updatePerson(uri, resolver, hint, userHint) {
if (typeof uri !== "string") throw new Error("uri is not string");
// Skip if the URI points to this server
if (extractDbHost(uri) === toPuny(config.host)) {
return;
}
//#region Already registered on this server?
const user = await Users.findOneBy({
uri
});
if (user == null) {
return;
}
//#endregion
if (resolver == null) resolver = new Resolver();
const object = hint || await resolver.resolve(uri);
const person = validateActor(object, uri);
logger.info(`Updating the Person: ${person.id}`);
const host = await getSubjectHostFromUri(uri) ?? await getSubjectHostFromRemoteUser(userHint);
// Fetch avatar and header image
const [avatar, banner] = await Promise.all([
person.icon,
person.image
].map((img)=>img == null ? Promise.resolve(null) : resolveImage(user, img).catch(()=>null)));
// Custom pictogram acquisition
const emojis = await extractEmojis(person.tag || [], user.host).catch((e)=>{
logger.info(`extractEmojis: ${e}`);
return [];
});
const emojiNames = emojis.map((emoji)=>emoji.name);
const { fields } = await analyzeAttachments(person.attachment || []);
const tags = extractApHashtags(person.tag).map((tag)=>normalizeForSearch(tag)).splice(0, 32);
const bday = person["vcard:bday"]?.match(/^\d{4}-\d{2}-\d{2}/);
const url = getOneApHrefNullable(person.url);
if (url && !url.startsWith("https://")) {
throw new Error(`unexpected schema of person url: ${url}`);
}
let followersCount;
if (typeof person.followers === "string") {
try {
let data = await fetch(person.followers, {
headers: {
Accept: "application/json"
},
size: 1024 * 1024
});
let json_data = JSON.parse(await data.text());
followersCount = json_data.totalItems;
} catch {
followersCount = undefined;
}
}
let followingCount;
if (typeof person.following === "string") {
try {
let data = await fetch(person.following, {
headers: {
Accept: "application/json"
},
size: 1024 * 1024
});
let json_data = JSON.parse(await data.text());
followingCount = json_data.totalItems;
} catch {
followingCount = undefined;
}
}
let canBite = "nobody";
if (person.canBite) {
if (person.canBite === "https://www.w3.org/ns/activitystreams#Public") {
canBite = "anyone";
} else if (person.followers && person.canBite === getApId(person.followers)) {
canBite = "followers";
}
}
const updates = {
lastFetchedAt: new Date(),
inbox: person.inbox,
sharedInbox: person.sharedInbox || (person.endpoints ? person.endpoints.sharedInbox : undefined),
followersUri: person.followers ? getApId(person.followers) : undefined,
followersCount: followersCount !== undefined ? followersCount : person.followers && typeof person.followers !== "string" && isCollectionOrOrderedCollection(person.followers) ? person.followers.totalItems : undefined,
followingCount: followingCount !== undefined ? followingCount : person.following && typeof person.following !== "string" && isCollectionOrOrderedCollection(person.following) ? person.following.totalItems : undefined,
featured: person.featured,
emojis: emojiNames,
name: truncate(person.name, nameLength),
tags,
isBot: getApType(object) === "Service",
isCat: person.isCat === true,
isLocked: !!person.manuallyApprovesFollowers,
movedToUri: person.movedTo || null,
alsoKnownAs: person.alsoKnownAs || null,
isExplorable: !!person.discoverable,
canBite
};
if (avatar) {
updates.avatarId = avatar.id;
updates.avatarUrl = DriveFiles.getDatabasePrefetchUrl(avatar, true);
updates.avatarBlurhash = avatar.blurhash;
}
if (banner) {
updates.bannerId = banner.id;
updates.bannerUrl = DriveFiles.getDatabasePrefetchUrl(banner, false);
updates.bannerBlurhash = banner.blurhash;
}
if (host) {
updates.host = host;
}
// Update user
await Users.update(user.id, updates);
if (person.publicKey) {
await UserPublickeys.update({
userId: user.id
}, {
keyId: person.publicKey.id,
keyPem: person.publicKey.publicKeyPem
});
}
// Get old profile to see if we need to update any matching html cache entries
const oldProfile = await UserProfiles.findOneBy({
userId: user.id
});
const newProfile = {
url: url,
fields,
description: person._misskey_summary ? truncate(person._misskey_summary, summaryLength) : person.summary ? await htmlToMfm(truncate(person.summary, summaryLength), person.tag) : null,
birthday: bday ? bday[0] : null,
location: person["vcard:Address"] || null,
pronouns: person.pronouns || {}
};
await UserProfiles.update({
userId: user.id
}, newProfile);
publishInternalEvent("remoteUserUpdated", {
id: user.id
});
// Hashtag Update
updateUsertags(user, tags);
// Mentions update, then prewarm html cache
UserProfiles.updateMentions(user.id).then((_)=>UserConverter.prewarmCacheById(user.id, oldProfile));
// If the user in question is a follower, followers will also be updated.
await Followings.update({
followerId: user.id
}, {
followerSharedInbox: person.sharedInbox || (person.endpoints ? person.endpoints.sharedInbox : null)
});
await updateFeatured(user.id, resolver).catch((err)=>logger.error(err));
}
/**
* Resolve Person.
*
* If the target person is registered in Iceshrimp, it returns it;
* otherwise, it fetches it from the remote server, registers it in Iceshrimp, and returns it.
*/ export async function resolvePerson(uri, resolver, limiter = new RecursionLimiter()) {
if (typeof uri !== "string") throw new Error("uri is not string");
//#region If already registered on this server, return it.
const user = await fetchPerson(uri);
if (user != null) {
return user;
}
//#endregion
// Fetched from remote server and registered
if (resolver == null) resolver = new Resolver();
return await createPerson(uri, resolver, undefined, limiter);
}
const services = {
"misskey:authentication:github": (id, login)=>({
id,
login
}),
"misskey:authentication:discord": (id, name)=>$discord(id, name)
};
const $discord = (id, name)=>{
if (typeof name !== "string") {
name = "unknown#0000";
}
const [username, discriminator] = name.split("#");
return {
id,
username,
discriminator
};
};
function addService(target, source) {
const service = services[source.name];
if (typeof source.value !== "string") {
source.value = "unknown";
}
const [id, username] = source.value.split("@");
if (service) {
target[source.name.split(":")[2]] = service(id, username);
}
}
export async function analyzeAttachments(attachments) {
const fields = [];
const services = {};
if (Array.isArray(attachments)) {
for (const attachment of attachments.filter(isPropertyValue)){
if (isPropertyValue(attachment.identifier)) {
addService(services, attachment.identifier);
} else {
fields.push({
name: attachment.name,
value: await fromHtml(attachment.value)
});
}
}
}
return {
fields,
services
};
}
export async function updateFeatured(userId, resolver, limiter = new RecursionLimiter()) {
const user = await Users.findOneByOrFail({
id: userId
});
if (!Users.isRemoteUser(user)) return;
if (!user.featured) return;
logger.info(`Updating the featured: ${user.uri}`);
if (resolver == null) resolver = new Resolver();
// Attempt to get a local user that follows the remote user
const follower = await Users.getRandomFollower(userId);
if (follower) resolver.setUser(follower);
// Resolve to (Ordered)Collection Object
const collection = await resolver.resolveCollection(user.featured);
if (!isCollectionOrOrderedCollection(collection)) throw new Error("Object is not Collection or OrderedCollection");
// Resolve to Object(may be Note) arrays
const unresolvedItems = isCollection(collection) ? collection.items : collection.orderedItems;
const items = await Promise.all(toArray(unresolvedItems).map((x)=>resolver.resolve(x)));
// Resolve and register Notes
resolver.reset();
const limit = promiseLimit(2);
const featuredNotes = await Promise.all(items.filter((item)=>getApType(item) === "Note") // TODO: Maybe it doesn't have to be a Note.
.slice(0, 5).map((item)=>limit(()=>resolveNote(item, resolver, limiter))));
// Prepare the objects
// For now, generate the id at a different time and maintain the order.
const data = [];
let td = 0;
for (const note of featuredNotes.filter((note)=>note != null)){
td -= 1000;
data.push({
id: genId(new Date(Date.now() + td)),
createdAt: new Date(),
userId: user.id,
noteId: note.id
});
}
// Save the objects atomically using a db transaction, note that we should never run any code in a transaction block directly
await db.transaction(async (transactionalEntityManager)=>{
await transactionalEntityManager.delete(UserNotePining, {
userId: user.id
});
await transactionalEntityManager.insert(UserNotePining, data);
});
}
@@ -0,0 +1,64 @@
import config from "../../../config/index.js";
import { getApId, isQuestion } from "../type.js";
import { apLogger } from "../logger.js";
import { Notes, Polls } from "../../../models/index.js";
import { extractDbHost, toPuny } from "../../../misc/convert-host.js";
export async function extractPollFromQuestion(source, resolver) {
const question = await resolver.resolve(source);
if (!isQuestion(question)) {
throw new Error("invalid type");
}
const multiple = !question.oneOf;
const expiresAt = question.endTime ? new Date(question.endTime) : question.closed ? new Date(question.closed) : null;
if (multiple && !question.anyOf) {
throw new Error("invalid question");
}
const choices = question[multiple ? "anyOf" : "oneOf"].map((x, i)=>x.name);
const votes = question[multiple ? "anyOf" : "oneOf"].map((x, i)=>x.replies?.totalItems || x._misskey_votes || 0);
return {
choices,
votes,
multiple,
expiresAt
};
}
/**
* Update votes of Question
* @param value URI of AP Question object or object itself
* @returns true if updated
*/ export async function updateQuestion(value, resolver) {
const uri = typeof value === "string" ? value : getApId(value);
// Skip if URI points to this server
if (extractDbHost(uri) === toPuny(config.host)) throw new Error("uri points local");
//#region Already registered with this server?
const note = await Notes.findOneBy({
uri
});
if (note == null) throw new Error("Question is not registed");
const poll = await Polls.findOneBy({
noteId: note.id
});
if (poll == null) throw new Error("Question is not registed");
//#endregion
// resolve new Question object
const question = await resolver.resolve(value);
apLogger.debug(`fetched question: ${JSON.stringify(question, null, 2)}`);
if (question.type !== "Question") throw new Error("object is not a Question");
const apChoices = question.oneOf || question.anyOf;
if (!apChoices) return false;
let changed = false;
for (const choice of poll.choices){
const oldCount = poll.votes[poll.choices.indexOf(choice)];
const newCount = apChoices.filter((ap)=>ap.name === choice)[0]?.replies?.totalItems;
if (newCount !== undefined && oldCount !== newCount) {
changed = true;
poll.votes[poll.choices.indexOf(choice)] = newCount;
}
}
await Polls.update({
noteId: note.id
}, {
votes: poll.votes
});
return changed;
}
@@ -0,0 +1,14 @@
import { toArray } from "../../../prelude/array.js";
import { isHashtag } from "../type.js";
export function extractApHashtags(tags) {
if (tags == null) return [];
const hashtags = extractApHashtagObjects(tags);
return hashtags.map((tag)=>{
const m = tag.name.match(/^#(.+)/);
return m ? m[1] : null;
}).filter((x)=>x != null);
}
export function extractApHashtagObjects(tags) {
if (tags == null) return [];
return toArray(tags).filter(isHashtag);
}
@@ -0,0 +1,14 @@
import { performActivity } from "./kernel/index.js";
import { updatePerson } from "./models/person.js";
export default (async (actor, activity)=>{
const ret = await performActivity(actor, activity);
// Update the remote user information if it is out of date
if (actor.uri) {
if (actor.lastFetchedAt == null || Date.now() - actor.lastFetchedAt.getTime() > 1000 * 60 * 60 * 24) {
setImmediate(()=>{
updatePerson(actor.uri);
});
}
}
return ret;
});
@@ -0,0 +1,9 @@
import config from "../../../config/index.js";
import renderFollow from "./follow.js";
export default ((follower, followee, requestId)=>{
return {
type: "Accept",
actor: `${config.url}/users/${followee.id}`,
object: renderFollow(follower, followee, requestId)
};
});
@@ -0,0 +1,15 @@
import config from "../../../config/index.js";
// assumes stamp.targetNote is populated
export default (async (request, stamp)=>({
type: "Accept",
to: request.actor,
actor: `${config.url}/users/${stamp.targetNote.userId}`,
object: {
type: "QuoteRequest",
id: request.id,
actor: request.actor,
object: request.object,
instrument: request.instrument
},
result: `${config.url}/stamp/${stamp.id}`
}));
@@ -0,0 +1,7 @@
import config from "../../../config/index.js";
export default ((user, target, object)=>({
type: "Add",
actor: `${config.url}/users/${user.id}`,
target,
object
}));
@@ -0,0 +1,39 @@
import config from "../../../config/index.js";
export default ((object, note)=>{
const attributedTo = `${config.url}/users/${note.userId}`;
const mentions = JSON.parse(note.mentionedRemoteUsers).map((x)=>x.uri);
let to = [];
let cc = [];
if (note.visibility === "public") {
to = [
"https://www.w3.org/ns/activitystreams#Public"
];
cc = [
`${attributedTo}/followers`
];
} else if (note.visibility === "home") {
to = [
`${attributedTo}/followers`
];
cc = [
"https://www.w3.org/ns/activitystreams#Public"
];
} else if (note.visibility === "followers") {
to = [
`${attributedTo}/followers`
];
} else if (note.visibility === "specified") {
to = mentions;
} else {
return null;
}
return {
id: `${config.url}/notes/${note.id}/activity`,
actor: `${config.url}/users/${note.userId}`,
type: "Announce",
published: note.createdAt.toISOString(),
to,
cc,
object
};
});
@@ -0,0 +1,10 @@
import config from "../../../config/index.js";
import { Bites } from "../../../models/index.js";
export default (async (bite)=>({
id: `${config.url}/bites/${bite.id}`,
type: "Bite",
actor: `${config.url}/users/${bite.userId}`,
target: await Bites.targetUri(bite),
published: bite.createdAt.toISOString(),
to: await Bites.targetUserUri(bite)
}));
@@ -0,0 +1,16 @@
import config from "../../../config/index.js";
/**
* Renders a block into its ActivityPub representation.
*
* @param block The block to be rendered. The blockee relation must be loaded.
*/ export function renderBlock(block) {
if (block.blockee?.uri == null) {
throw new Error("renderBlock: missing blockee uri");
}
return {
type: "Block",
id: `${config.url}/blocks/${block.id}`,
actor: `${config.url}/users/${block.blockerId}`,
object: block.blockee.uri
};
}
@@ -0,0 +1,13 @@
import config from "../../../config/index.js";
export default ((object, note)=>{
const activity = {
id: `${config.url}/notes/${note.id}/activity`,
actor: `${config.url}/users/${note.userId}`,
type: "Create",
published: note.createdAt.toISOString(),
object
};
if (object.to) activity.to = object.to;
if (object.cc) activity.cc = object.cc;
return activity;
});
@@ -0,0 +1,7 @@
import config from "../../../config/index.js";
export default ((object, user)=>({
type: "Delete",
actor: `${config.url}/users/${user.id}`,
object,
published: new Date().toISOString()
}));
@@ -0,0 +1,7 @@
import { DriveFiles } from "../../../models/index.js";
export default ((file)=>({
type: "Document",
mediaType: file.type,
url: DriveFiles.getPublicUrl(file),
name: file.comment
}));
@@ -0,0 +1,12 @@
import config from "../../../config/index.js";
export default ((emoji)=>({
id: `${config.url}/emojis/${emoji.name}`,
type: "Emoji",
name: `:${emoji.name}:`,
updated: emoji.updatedAt != null ? emoji.updatedAt.toISOString() : new Date().toISOString,
icon: {
type: "Image",
mediaType: emoji.glyph ? "image/svg+xml" : emoji.type || "image/png",
url: emoji.glyph ? emoji.originalUrl : emoji.publicUrl || emoji.originalUrl
}
}));
@@ -0,0 +1,11 @@
import config from "../../../config/index.js";
// to anonymise reporters, the reporting actor must be a system user
// object has to be a uri or array of uris
export const renderFlag = (user, object, content)=>{
return {
type: "Flag",
actor: `${config.url}/users/${user.id}`,
content,
object
};
};
@@ -0,0 +1,10 @@
import config from "../../../config/index.js";
export function renderFollowRelay(relay, relayActor) {
const follow = {
id: `${config.url}/activities/follow-relay/${relay.id}`,
type: "Follow",
actor: `${config.url}/users/${relayActor.id}`,
object: "https://www.w3.org/ns/activitystreams#Public"
};
return follow;
}
@@ -0,0 +1,11 @@
import config from "../../../config/index.js";
import { Users } from "../../../models/index.js";
/**
* Convert (local|remote)(Follower|Followee)ID to URL
* @param id Follower|Followee ID
*/ export default async function renderFollowUser(id) {
const user = await Users.findOneByOrFail({
id: id
});
return Users.isLocalUser(user) ? `${config.url}/users/${user.id}` : user.uri;
}
@@ -0,0 +1,11 @@
import config from "../../../config/index.js";
import { Users } from "../../../models/index.js";
export default ((follower, followee, requestId)=>{
const follow = {
id: requestId ?? `${config.url}/follows/${follower.id}/${followee.id}`,
type: "Follow",
actor: Users.isLocalUser(follower) ? `${config.url}/users/${follower.id}` : follower.uri,
object: Users.isLocalUser(followee) ? `${config.url}/users/${followee.id}` : followee.uri
};
return follow;
});
@@ -0,0 +1,6 @@
import config from "../../../config/index.js";
export default ((tag)=>({
type: "Hashtag",
href: `${config.url}/tags/${encodeURIComponent(tag)}`,
name: `#${tag}`
}));
@@ -0,0 +1,7 @@
import { DriveFiles } from "../../../models/index.js";
export default ((file)=>({
type: "Image",
url: DriveFiles.getPublicUrl(file),
sensitive: file.isSensitive,
name: file.comment
}));
@@ -0,0 +1,20 @@
import { v4 as uuid } from "uuid";
import config from "../../../config/index.js";
import { getUserKeypair } from "../../../misc/keypair-store.js";
import { LdSignature } from "../misc/ld-signature.js";
import { WellKnownContext } from "../misc/contexts.js";
export const renderActivity = (x)=>{
if (x == null) return null;
if (typeof x === "object" && x.id == null) {
x.id = `${config.url}/${uuid()}`;
}
return Object.assign({}, WellKnownContext, x);
};
export const attachLdSignature = async (activity, user)=>{
if (activity == null) return null;
const keypair = await getUserKeypair(user.id);
const ldSignature = new LdSignature();
ldSignature.debug = false;
activity = await ldSignature.signRsaSignature2017(activity, keypair.privateKey, `${config.url}/users/${user.id}#main-key`);
return activity;
};
@@ -0,0 +1,11 @@
import config from "../../../config/index.js";
import { createPublicKey } from "node:crypto";
export default ((user, key, postfix)=>({
id: `${config.url}/users/${user.id}${postfix || "/publickey"}`,
type: "Key",
owner: `${config.url}/users/${user.id}`,
publicKeyPem: createPublicKey(key.publicKey).export({
type: "spki",
format: "pem"
})
}));
@@ -0,0 +1,30 @@
import { IsNull } from "typeorm";
import config from "../../../config/index.js";
import { Emojis } from "../../../models/index.js";
import renderEmoji from "./emoji.js";
import { fetchMeta } from "../../../misc/fetch-meta.js";
export const renderLike = async (noteReaction, note)=>{
const reaction = noteReaction.reaction;
const meta = await fetchMeta();
const object = {
type: "Like",
id: `${config.url}/likes/${noteReaction.id}`,
actor: `${config.url}/users/${noteReaction.userId}`,
object: note.uri ? note.uri : `${config.url}/notes/${noteReaction.noteId}`,
...!meta.defaultReaction.includes(reaction) ? {
content: reaction,
_misskey_reaction: reaction
} : {}
};
if (reaction.startsWith(":")) {
const name = reaction.replace(/:/g, "");
const emoji = await Emojis.findOneBy({
name,
host: IsNull()
});
if (emoji) object.tag = [
renderEmoji(emoji)
];
}
return object;
};
@@ -0,0 +1,7 @@
import config from "../../../config/index.js";
import { Users } from "../../../models/index.js";
export default ((mention)=>({
type: "Mention",
href: Users.isRemoteUser(mention) ? mention.uri : `${config.url}/users/${mention.id}`,
name: Users.isRemoteUser(mention) ? `@${mention.username}@${mention.host}` : `@${mention.username}`
}));
@@ -0,0 +1,174 @@
import { In, IsNull } from "typeorm";
import config from "../../../config/index.js";
import { DriveFiles, Notes, Users, Emojis, Polls } from "../../../models/index.js";
import toHtml from "../misc/get-note-html.js";
import renderEmoji from "./emoji.js";
import renderMention from "./mention.js";
import renderHashtag from "./hashtag.js";
import renderDocument from "./document.js";
import sanitizeHtml from "sanitize-html";
export default async function renderNote(note, dive = true, isTalk = false) {
const getPromisedFiles = async (ids)=>{
if (!ids || ids.length === 0) return [];
const items = await DriveFiles.findBy({
id: In(ids)
});
return ids.map((id)=>items.find((item)=>item.id === id)).filter((item)=>item != null);
};
let inReplyTo;
let inReplyToNote;
if (note.replyId) {
inReplyToNote = await Notes.findOneBy({
id: note.replyId
});
if (inReplyToNote != null) {
const inReplyToUser = await Users.findOneBy({
id: inReplyToNote.userId
});
if (inReplyToUser != null) {
if (inReplyToNote.uri) {
inReplyTo = inReplyToNote.uri;
} else {
if (dive) {
inReplyTo = await renderNote(inReplyToNote, false);
} else {
inReplyTo = `${config.url}/notes/${inReplyToNote.id}`;
}
}
}
}
} else {
inReplyTo = null;
}
let quoteId;
let quoteUrl;
if (note.renoteId) {
const renote = await Notes.findOneBy({
id: note.renoteId
});
if (renote) {
if (renote.userHost) {
quoteId = renote.uri;
quoteUrl = renote.url ?? renote.uri;
} else {
quoteId = quoteUrl = `${config.url}/notes/${renote.id}`;
}
}
}
const attributedTo = `${config.url}/users/${note.userId}`;
const mentions = JSON.parse(note.mentionedRemoteUsers).map((x)=>x.uri);
let to = [];
let cc = [];
if (note.visibility === "public") {
to = [
"https://www.w3.org/ns/activitystreams#Public"
];
cc = [
`${attributedTo}/followers`
].concat(mentions);
} else if (note.visibility === "home") {
to = [
`${attributedTo}/followers`
];
cc = [
"https://www.w3.org/ns/activitystreams#Public"
].concat(mentions);
} else if (note.visibility === "followers") {
to = [
`${attributedTo}/followers`
];
cc = mentions;
} else {
to = mentions;
}
const mentionedUsers = note.mentions.length > 0 ? await Users.findBy({
id: In(note.mentions)
}) : [];
const hashtagTags = (note.tags || []).map((tag)=>renderHashtag(tag));
const mentionTags = mentionedUsers.map((u)=>renderMention(u));
const files = await getPromisedFiles(note.fileIds);
const text = note.text ?? "";
let poll = null;
if (note.hasPoll) {
poll = await Polls.findOneBy({
noteId: note.id
});
}
const summary = note.cw === "" ? String.fromCharCode(0x200b) : note.cw;
let content = await toHtml(Object.assign({}, note, {
text
}));
if (quoteId) {
// wrapping in p.quote-inline lets mastodon automatically strip the link
const quoteHREFSan = (quoteUrl || quoteId).replaceAll("&", "&amp;").replaceAll('"', "&quot;");
const quoteTextSan = sanitizeHtml(quoteUrl || quoteId);
content += `<p class="quote-inline">RE: <a href="${quoteHREFSan}">${quoteTextSan}</p>`;
}
const emojis = await getEmojis(note.emojis);
const apemojis = emojis.map((emoji)=>renderEmoji(emoji));
const tag = [
...hashtagTags,
...mentionTags,
...apemojis
];
const asPoll = poll ? {
type: "Question",
content: await toHtml(Object.assign({}, note, {
text: text
})),
[poll.expiresAt && poll.expiresAt < new Date() ? "closed" : "endTime"]: poll.expiresAt,
[poll.multiple ? "anyOf" : "oneOf"]: poll.choices.map((text, i)=>({
type: "Note",
name: text,
replies: {
type: "Collection",
totalItems: poll.votes[i]
}
}))
} : {};
const asTalk = isTalk ? {
_misskey_talk: true
} : {};
return {
id: `${config.url}/notes/${note.id}`,
type: "Note",
attributedTo,
summary,
content,
_misskey_content: text,
source: {
content: text,
mediaType: "text/x.misskeymarkdown"
},
_misskey_quote: quoteId,
quoteUri: quoteId,
quoteUrl: quoteId,
quote: note.canQuote ? quoteId : undefined,
published: note.createdAt.toISOString(),
to,
cc,
inReplyTo,
attachment: files.map(renderDocument),
sensitive: note.cw != null || files.some((file)=>file.isSensitive),
tag,
interactionPolicy: note.visibility === "public" || note.visibility === "home" ? {
canQuote: {
automaticApproval: "https://www.w3.org/ns/activitystreams#Public"
}
} : undefined,
quoteAuthorization: note.quoteAuthorization || undefined,
...asPoll,
...asTalk
};
}
export async function getEmojis(names) {
if (names == null || names.length === 0) return [];
const emojis = await Promise.all(names.map((name)=>{
const parts = name.split("@");
return Emojis.findOneBy({
name: parts[0],
host: parts.length === 2 ? parts[1] : IsNull()
});
}));
return emojis.filter((emoji)=>emoji != null);
}
@@ -0,0 +1,20 @@
/**
* Render OrderedCollectionPage
* @param id URL of self
* @param totalItems Number of total items
* @param orderedItems Items
* @param partOf URL of base
* @param prev URL of prev page (optional)
* @param next URL of next page (optional)
*/ export default function(id, totalItems, orderedItems, partOf, prev, next) {
const page = {
id,
partOf,
type: "OrderedCollectionPage",
totalItems,
orderedItems
};
if (prev) page.prev = prev;
if (next) page.next = next;
return page;
}
@@ -0,0 +1,18 @@
/**
* Render OrderedCollection
* @param id URL of self
* @param totalItems Total number of items
* @param first URL of first page (optional)
* @param last URL of last page (optional)
* @param orderedItems attached objects (optional)
*/ export default function(id, totalItems, first, last, orderedItems) {
const page = {
id,
type: "OrderedCollection",
totalItems
};
if (first) page.first = first;
if (last) page.last = last;
if (orderedItems) page.orderedItems = orderedItems;
return page;
}
@@ -0,0 +1,91 @@
import * as mfm from "mfm-js";
import config from "../../../config/index.js";
import { DriveFiles, UserProfiles } from "../../../models/index.js";
import { getUserKeypair } from "../../../misc/keypair-store.js";
import { toHtml } from "../../../mfm/to-html.js";
import renderImage from "./image.js";
import renderKey from "./key.js";
import { getEmojis } from "./note.js";
import renderEmoji from "./emoji.js";
import renderHashtag from "./hashtag.js";
export async function renderPerson(user) {
const id = `${config.url}/users/${user.id}`;
const isSystem = !!user.username.match(/\./);
const [avatar, banner, profile] = await Promise.all([
user.avatarId ? DriveFiles.findOneBy({
id: user.avatarId
}) : Promise.resolve(undefined),
user.bannerId ? DriveFiles.findOneBy({
id: user.bannerId
}) : Promise.resolve(undefined),
UserProfiles.findOneByOrFail({
userId: user.id
})
]);
const attachment = [];
if (profile.fields) {
for (const field of profile.fields){
const value = await toHtml(mfm.parse(field.value), profile.mentions, profile.userHost);
attachment.push({
type: "PropertyValue",
name: field.name,
value: value ?? field.value
});
}
}
const emojis = await getEmojis(user.emojis);
const apemojis = emojis.map((emoji)=>renderEmoji(emoji));
const hashtagTags = (user.tags || []).map((tag)=>renderHashtag(tag));
const tag = [
...apemojis,
...hashtagTags
];
const keypair = await getUserKeypair(user.id);
let canBite;
if (user.canBite === "anyone") {
canBite = "https://www.w3.org/ns/activitystreams#Public";
} else if (user.canBite === "followers") {
canBite = user.followersUri ?? `${config.url}/users/${user.id}/followers`;
}
const person = {
type: isSystem ? "Application" : user.isBot ? "Service" : "Person",
id,
inbox: `${id}/inbox`,
outbox: `${id}/outbox`,
followers: `${id}/followers`,
following: `${id}/following`,
featured: `${id}/collections/featured`,
sharedInbox: `${config.url}/inbox`,
endpoints: {
sharedInbox: `${config.url}/inbox`
},
url: `${config.url}/@${user.username}`,
preferredUsername: user.username,
name: user.name,
summary: profile.description ? await toHtml(mfm.parse(profile.description), profile.mentions, profile.userHost) : null,
_misskey_summary: profile.description,
icon: avatar ? renderImage(avatar) : null,
image: banner ? renderImage(banner) : null,
tag,
manuallyApprovesFollowers: user.isLocked,
discoverable: !!user.isExplorable,
publicKey: renderKey(user, keypair, "#main-key"),
isCat: user.isCat,
attachment: attachment.length ? attachment : undefined,
pronouns: profile.pronouns,
canBite
};
if (user.movedToUri) {
person.movedTo = user.movedToUri;
}
if (user.alsoKnownAs) {
person.alsoKnownAs = user.alsoKnownAs;
}
if (profile.birthday) {
person["vcard:bday"] = profile.birthday;
}
if (profile.location) {
person["vcard:Address"] = profile.location;
}
return person;
}
@@ -0,0 +1,18 @@
import config from "../../../config/index.js";
export default async function renderQuestion(user, note, poll) {
const question = {
type: "Question",
id: `${config.url}/questions/${note.id}`,
actor: `${config.url}/users/${user.id}`,
content: note.text || "",
[poll.multiple ? "anyOf" : "oneOf"]: poll.choices.map((text, i)=>({
name: text,
_misskey_votes: poll.votes[i],
replies: {
type: "Collection",
totalItems: poll.votes[i]
}
}))
};
return question;
}
@@ -0,0 +1,9 @@
import config from "../../../config/index.js";
// assumes stamp.note and stamp.targetNote are populated
export default ((stamp)=>({
id: `${config.url}/stamp/${stamp.id}`,
type: "QuoteAuthorization",
attributedTo: `${config.url}/users/${stamp.targetNote.userId}`,
interactingObject: stamp.note.userHost === null ? `${config.url}/notes/${stamp.note.id}` : stamp.note.uri,
interactionTarget: `${config.url}/notes/${stamp.targetNoteId}`
}));
@@ -0,0 +1,9 @@
import config from "../../../config/index.js";
export default function renderQuoteRequest(note, targetNote) {
return {
type: "QuoteRequest",
actor: `${config.url}/users/${note.userId}`,
object: targetNote.uri,
instrument: `${config.url}/notes/${note.id}`
};
}
@@ -0,0 +1,6 @@
import config from "../../../config/index.js";
export const renderReadActivity = (user, message)=>({
type: "Read",
actor: `${config.url}/users/${user.id}`,
object: message.uri
});
@@ -0,0 +1,6 @@
import config from "../../../config/index.js";
export default ((object, user)=>({
type: "Reject",
actor: `${config.url}/users/${user.id}`,
object
}));
@@ -0,0 +1,7 @@
import config from "../../../config/index.js";
export default ((user, target, object)=>({
type: "Remove",
actor: `${config.url}/users/${user.id}`,
target,
object
}));
@@ -0,0 +1,4 @@
export default ((id)=>({
id,
type: "Tombstone"
}));
@@ -0,0 +1,14 @@
import config from "../../../config/index.js";
export default ((object, user)=>{
if (object == null) return null;
const id = typeof object.id === "string" && object.id.startsWith(config.url) ? `${object.id}/undo` : undefined;
return {
type: "Undo",
...id ? {
id
} : {},
actor: `${config.url}/users/${user.id}`,
object,
published: new Date().toISOString()
};
});
@@ -0,0 +1,14 @@
import config from "../../../config/index.js";
export default ((object, user)=>{
const activity = {
id: `${config.url}/users/${user.id}#updates/${new Date().getTime()}`,
actor: `${config.url}/users/${user.id}`,
type: "Update",
to: [
"https://www.w3.org/ns/activitystreams#Public"
],
object,
published: new Date().toISOString()
};
return activity;
});
@@ -0,0 +1,22 @@
import config from "../../../config/index.js";
export default async function renderVote(user, vote, note, poll, pollOwner) {
return {
id: `${config.url}/users/${user.id}#votes/${vote.id}/activity`,
actor: `${config.url}/users/${user.id}`,
type: "Create",
to: [
pollOwner.uri
],
published: new Date().toISOString(),
object: {
id: `${config.url}/users/${user.id}#votes/${vote.id}`,
type: "Note",
attributedTo: `${config.url}/users/${user.id}`,
to: [
pollOwner.uri
],
inReplyTo: note.uri,
name: poll.choices[vote.choice]
}
};
}
@@ -0,0 +1,70 @@
import config from "../../config/index.js";
import { getUserKeypair } from "../../misc/keypair-store.js";
import { getResponse } from "../../misc/fetch.js";
import { createSignedPost, createSignedGet } from "./ap-request.js";
import { apLogger } from "./logger.js";
export default (async (user, url, object)=>{
const body = JSON.stringify(object);
const keypair = await getUserKeypair(user.id);
const req = createSignedPost({
key: {
privateKeyPem: keypair.privateKey,
keyId: `${config.url}/users/${user.id}#main-key`
},
url,
body,
additionalHeaders: {
"User-Agent": config.userAgent
}
});
await getResponse({
url,
method: req.request.method,
headers: req.request.headers,
body
});
});
/**
* Get AP object with http-signature
* @param user http-signature user
* @param url URL to fetch
* @param redirects whether or not to accept redirects
*/ export async function signedGet(url, user, redirects = true) {
apLogger.debug(`Running signedGet on url: ${url}`);
const keypair = await getUserKeypair(user.id);
const req = createSignedGet({
key: {
privateKeyPem: keypair.privateKey,
keyId: `${config.url}/users/${user.id}#main-key`
},
url,
additionalHeaders: {
"User-Agent": config.userAgent
}
});
const res = await getResponse({
url,
method: req.request.method,
headers: req.request.headers,
redirect: redirects ? "manual" : "error"
});
if (redirects && [
301,
302,
307,
308
].includes(res.status)) {
const newUrl = res.headers.get('location');
if (!newUrl) throw new Error('signedGet got redirect but no target location');
apLogger.debug(`signedGet is redirecting to ${newUrl}`);
return signedGet(newUrl, user, false);
}
const contentType = res.headers.get('content-type');
if (contentType == null || contentType !== 'application/activity+json' && !contentType.startsWith('application/activity+json;') && (!contentType.startsWith('application/ld+json;') || !contentType.includes('profile="https://www.w3.org/ns/activitystreams"'))) {
throw new Error(`signedGet response had unexpected content-type: ${contentType}`);
}
return {
finalUrl: res.url,
content: await res.json()
};
}
@@ -0,0 +1,233 @@
import config from "../../config/index.js";
import { getJsonActivity } from "../../misc/fetch.js";
import { getInstanceActor } from "../../services/instance-actor.js";
import { fetchMeta } from "../../misc/fetch-meta.js";
import { extractDbHost, isSelfHost } from "../../misc/convert-host.js";
import { signedGet } from "./request.js";
import { isCollectionOrOrderedCollection, getApId } from "./type.js";
import { FollowRequests, Notes, NoteReactions, Polls, Users, Bites, InteractionStamps } from "../../models/index.js";
import { parseUri } from "./db-resolver.js";
import renderNote from "./renderer/note.js";
import { renderLike } from "./renderer/like.js";
import { renderPerson } from "./renderer/person.js";
import renderQuestion from "./renderer/question.js";
import renderCreate from "./renderer/create.js";
import { renderActivity } from "./renderer/index.js";
import renderFollow from "./renderer/follow.js";
import { shouldBlockInstance } from "../../misc/should-block-instance.js";
import { apLogger } from "./logger.js";
import { IsNull, Not } from "typeorm";
import { tickResolve } from "../../metrics.js";
import renderBite from "./renderer/bite.js";
import renderQuoteAuthorization from "./renderer/quote-authorization.js";
export default class Resolver {
history;
user;
recursionLimit;
constructor(recursionLimit = 100){
this.history = new Set();
this.recursionLimit = recursionLimit;
}
setUser(user) {
this.user = user;
}
reset() {
this.history = new Set();
return this;
}
getHistory() {
return Array.from(this.history);
}
async resolveCollection(value) {
const collection = await this.resolve(value);
if (isCollectionOrOrderedCollection(collection)) {
return collection;
} else {
throw new Error(`unrecognized collection type: ${collection.type}`);
}
}
async resolve(value) {
if (value == null) {
throw new Error("resolvee is null (or undefined)");
}
if (typeof value !== "string") {
apLogger.debug("Object to resolve is not a string");
if (typeof value.id !== "undefined") {
const host = extractDbHost(getApId(value));
if (await shouldBlockInstance(host)) {
throw new Error("instance is blocked");
}
}
apLogger.debug("Returning existing object:");
apLogger.debug(JSON.stringify(value, null, 2));
return value;
}
apLogger.debug(`Resolving: ${value}`);
if (value.includes("#")) {
// URLs with fragment parts cannot be resolved correctly because
// the fragment part does not get transmitted over HTTP(S).
// Avoid strange behaviour by not trying to resolve these at all.
throw new Error(`cannot resolve URL with fragment: ${value}`);
}
if (this.history.has(value)) {
throw new Error("cannot resolve already resolved one");
}
if (this.recursionLimit && this.history.size > this.recursionLimit) {
throw new Error("hit recursion limit");
}
this.history.add(value);
const host = extractDbHost(value);
if (isSelfHost(host)) {
return await this.resolveLocal(value);
}
const meta = await fetchMeta();
if (await shouldBlockInstance(host, meta)) {
throw new Error("Instance is blocked");
}
if (meta.privateMode && config.host !== host && config.domain !== host && !meta.allowedHosts.includes(host)) {
throw new Error("Instance is not allowed");
}
if (!this.user) {
this.user = await getInstanceActor();
}
apLogger.debug("Getting object from remote, authenticated as user:");
apLogger.debug(JSON.stringify(this.user, null, 2));
const { res, object } = await this.doFetch(value);
if (object.id == null) throw new Error("Object has no ID");
const objectId = new URL(object.id);
const resFinalUrl = new URL(res.finalUrl);
if (resFinalUrl.toString() === objectId.toString()) {
tickResolve();
return object;
}
if (resFinalUrl.host !== objectId.host) throw new Error("Object ID host doesn't match final url host");
const { res: finalRes, object: finalObject } = await this.doFetch(object.id);
if (finalObject.id == null) throw new Error("Final object has no ID");
const finalObjectId = new URL(finalObject.id);
const finalResFinalUrl = new URL(finalRes.finalUrl);
if (finalResFinalUrl.toString() !== finalObjectId.toString()) throw new Error("Object ID still doesn't match final URL after second fetch attempt");
tickResolve();
return finalObject;
}
async doFetch(uri) {
let res = this.user ? await signedGet(uri, this.user) : await getJsonActivity(uri);
let object = res.content;
if (object == null || (Array.isArray(object["@context"]) ? !object["@context"].includes("https://www.w3.org/ns/activitystreams") : object["@context"] !== "https://www.w3.org/ns/activitystreams")) {
throw new Error("invalid response");
}
return {
res,
object
};
}
async resolveLocal(url) {
const parsed = parseUri(url);
if (!parsed.local) throw new Error("resolveLocal: not local");
switch(parsed.type){
case "notes":
{
const note = await Notes.findOneByOrFail({
id: parsed.id
});
if (parsed.rest === "activity") {
// this refers to the create activity and not the note itself
return renderActivity(renderCreate(await renderNote(note), note));
} else {
return renderActivity(await renderNote(note));
}
}
case "users":
{
const user = await Users.findOneByOrFail({
id: parsed.id
});
return await renderPerson(user);
}
case "questions":
{
// Polls are indexed by the note they are attached to.
const [note, poll] = await Promise.all([
Notes.findOneByOrFail({
id: parsed.id
}),
Polls.findOneByOrFail({
noteId: parsed.id
})
]);
return renderActivity(await renderQuestion({
id: note.userId
}, note, poll));
}
case "likes":
{
const reaction = await NoteReactions.findOneByOrFail({
id: parsed.id
});
return renderActivity(await renderLike(reaction, {
uri: null
}));
}
case "follows":
{
// if rest is a <followee id>
if (parsed.rest != null && /^\w+$/.test(parsed.rest)) {
const follower = await Users.findOneByOrFail({
id: parsed.id
});
const followee = await Users.findOneByOrFail({
id: parsed.rest
});
return renderActivity(renderFollow(follower, followee, url));
}
// Another situation is there is only requestId, then obtained object from database.
const followRequest = await FollowRequests.findOneBy({
id: parsed.id
});
if (followRequest == null) {
throw new Error("resolveLocal: invalid follow URI");
}
const follower = await Users.findOneBy({
id: followRequest.followerId,
host: IsNull()
});
const followee = await Users.findOneBy({
id: followRequest.followeeId,
host: Not(IsNull())
});
if (follower == null || followee == null) {
throw new Error("resolveLocal: invalid follow URI");
}
return renderActivity(renderFollow(follower, followee, url));
}
case "bites":
{
const bite = await Bites.findOneOrFail({
where: {
id: parsed.id
},
relations: [
"targetUser",
"targetBite",
"targetNote"
]
});
return renderActivity(await renderBite(bite));
}
case "stamp":
{
const stamp = await InteractionStamps.findOneOrFail({
where: {
id: parsed.id
},
relations: [
"note",
"targetNote"
]
});
return renderActivity(renderQuoteAuthorization(stamp));
}
default:
throw new Error(`resolveLocal: type ${parsed.type} unhandled`);
}
}
}
@@ -0,0 +1,84 @@
/**
* Get array of ActivityStreams Objects id
*/ export function getApIds(value) {
if (value == null) return [];
const array = Array.isArray(value) ? value : [
value
];
return array.map((x)=>getApId(x));
}
/**
* Get first ActivityStreams Object id
*/ export function getOneApId(value) {
const firstOne = Array.isArray(value) ? value[0] : value;
return getApId(firstOne);
}
/**
* Get ActivityStreams Object id
*/ export function getApId(value) {
if (typeof value === "string") return value;
if (typeof value.id === "string") return value.id;
throw new Error("cannot detemine id");
}
/**
* Get ActivityStreams Object type
*/ export function getApType(value) {
if (typeof value.type === "string") return value.type;
if (Array.isArray(value.type) && typeof value.type[0] === "string") return value.type[0];
throw new Error("cannot detect type");
}
export function getOneApHrefNullable(value) {
const firstOne = Array.isArray(value) ? value[0] : value;
return getApHrefNullable(firstOne);
}
export function getApHrefNullable(value) {
if (typeof value === "string") return value;
if (typeof value?.href === "string") return value.href;
return undefined;
}
export const validPost = [
"Note",
"Question",
"Article",
"Audio",
"Document",
"Image",
"Page",
"Video",
"Event"
];
export const isPost = (object)=>validPost.includes(getApType(object));
export const isQuestion = (object)=>getApType(object) === "Note" || getApType(object) === "Question";
export const isTombstone = (object)=>getApType(object) === "Tombstone";
export const validActor = [
"Person",
"Service",
"Group",
"Organization",
"Application"
];
export const isActor = (object)=>validActor.includes(getApType(object));
export const isCollection = (object)=>getApType(object) === "Collection";
export const isOrderedCollection = (object)=>getApType(object) === "OrderedCollection";
export const isCollectionOrOrderedCollection = (object)=>isCollection(object) || isOrderedCollection(object);
export const isPropertyValue = (object)=>object && getApType(object) === "PropertyValue" && typeof object.name === "string" && typeof object.value === "string";
export const isMention = (object)=>getApType(object) === "Mention" && typeof object.href === "string";
export const isHashtag = (object)=>getApType(object) === "Hashtag" && typeof object.name === "string";
export const isEmoji = (object)=>getApType(object) === "Emoji" && !Array.isArray(object.icon) && object.icon.url != null;
export const isCreate = (object)=>getApType(object) === "Create";
export const isDelete = (object)=>getApType(object) === "Delete";
export const isUpdate = (object)=>getApType(object) === "Update";
export const isRead = (object)=>getApType(object) === "Read";
export const isUndo = (object)=>getApType(object) === "Undo";
export const isFollow = (object)=>getApType(object) === "Follow";
export const isAccept = (object)=>getApType(object) === "Accept";
export const isReject = (object)=>getApType(object) === "Reject";
export const isAdd = (object)=>getApType(object) === "Add";
export const isRemove = (object)=>getApType(object) === "Remove";
export const isLike = (object)=>getApType(object) === "Like" || getApType(object) === "EmojiReaction" || getApType(object) === "EmojiReact";
export const isAnnounce = (object)=>getApType(object) === "Announce";
export const isBlock = (object)=>getApType(object) === "Block";
export const isFlag = (object)=>getApType(object) === "Flag";
export const isMove = (object)=>getApType(object) === "Move";
export const isBite = (object)=>getApType(object) === "Bite";
export const isQuoteRequest = (object)=>getApType(object) === "QuoteRequest";