refactor: ♻️ code quality on AP kernel index

This commit is contained in:
ThatOneCalculator 2023-08-23 20:00:35 -07:00
parent 7047ddce3e
commit 92d1d22228
No known key found for this signature in database
GPG key ID: 8703CACD01000000

View file

@ -1,25 +1,6 @@
import type { CacheableRemoteUser } from "@/models/entities/user.js"; import type { CacheableRemoteUser } from "@/models/entities/user.js";
import { toArray } from "@/prelude/array.js"; import { toArray } from "@/prelude/array.js";
import { import * as activityTypes from "../type.js";
isCreate,
isDelete,
isUpdate,
isRead,
isFollow,
isAccept,
isReject,
isAdd,
isRemove,
isAnnounce,
isLike,
isUndo,
isBlock,
isCollectionOrOrderedCollection,
isCollection,
isFlag,
isMove,
getApId,
} from "../type.js";
import { apLogger } from "../logger.js"; import { apLogger } from "../logger.js";
import Resolver from "../resolver.js"; import Resolver from "../resolver.js";
import create from "./create/index.js"; import create from "./create/index.js";
@ -37,7 +18,7 @@ import remove from "./remove/index.js";
import block from "./block/index.js"; import block from "./block/index.js";
import flag from "./flag/index.js"; import flag from "./flag/index.js";
import move from "./move/index.js"; import move from "./move/index.js";
import type { IObject, IActivity } from "../type.js"; import type { IObject } from "../type.js";
import { extractDbHost } from "@/misc/convert-host.js"; import { extractDbHost } from "@/misc/convert-host.js";
import { shouldBlockInstance } from "@/misc/should-block-instance.js"; import { shouldBlockInstance } from "@/misc/should-block-instance.js";
@ -45,10 +26,12 @@ export async function performActivity(
actor: CacheableRemoteUser, actor: CacheableRemoteUser,
activity: IObject, activity: IObject,
) { ) {
if (isCollectionOrOrderedCollection(activity)) { if (activityTypes.isCollectionOrOrderedCollection(activity)) {
const resolver = new Resolver(); const resolver = new Resolver();
for (const item of toArray( for (const item of toArray(
isCollection(activity) ? activity.items : activity.orderedItems, activityTypes.isCollection(activity)
? activity.items
: activity.orderedItems,
)) { )) {
const act = await resolver.resolve(item); const act = await resolver.resolve(item);
try { try {
@ -71,41 +54,37 @@ async function performOneActivity(
if (actor.isSuspended) return; if (actor.isSuspended) return;
if (typeof activity.id !== "undefined") { if (typeof activity.id !== "undefined") {
const host = extractDbHost(getApId(activity)); const host = extractDbHost(activityTypes.getApId(activity));
if (await shouldBlockInstance(host)) return; if (await shouldBlockInstance(host)) return;
} }
if (isCreate(activity)) { const activityHandlers: { [key: string]: Function } = {
await create(actor, activity); Create: create,
} else if (isDelete(activity)) { Delete: performDeleteActivity,
await performDeleteActivity(actor, activity); Update: performUpdateActivity,
} else if (isUpdate(activity)) { Read: performReadActivity,
await performUpdateActivity(actor, activity); Follow: follow,
} else if (isRead(activity)) { Accept: accept,
await performReadActivity(actor, activity); Reject: reject,
} else if (isFollow(activity)) { Add: add,
await follow(actor, activity); Remove: remove,
} else if (isAccept(activity)) { Announce: announce,
await accept(actor, activity); Like: like,
} else if (isReject(activity)) { Undo: undo,
await reject(actor, activity); Block: block,
} else if (isAdd(activity)) { Flag: flag,
await add(actor, activity).catch((err) => apLogger.error(err)); Move: move,
} else if (isRemove(activity)) { };
await remove(actor, activity).catch((err) => apLogger.error(err));
} else if (isAnnounce(activity)) { const handler = activityHandlers[activityTypes.getApType(activity)];
await announce(actor, activity);
} else if (isLike(activity)) { if (handler) {
await like(actor, activity); await handler(actor, activity);
} else if (isUndo(activity)) {
await undo(actor, activity);
} else if (isBlock(activity)) {
await block(actor, activity);
} else if (isFlag(activity)) {
await flag(actor, activity);
} else if (isMove(activity)) {
await move(actor, activity);
} else { } else {
apLogger.warn(`Unrecognized activity type: ${(activity as IActivity).type}`); apLogger.warn(
`Unrecognized activity type: ${
(activity as activityTypes.IActivity).type
}`,
);
} }
} }