From 136d7efde798d3dc0ec0dd18aac674365f7d162e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 3 Jun 2021 16:02:29 +0200 Subject: Refactor AP actors --- .../actors/shared/object-to-model-attributes.ts | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 server/lib/activitypub/actors/shared/object-to-model-attributes.ts (limited to 'server/lib/activitypub/actors/shared/object-to-model-attributes.ts') diff --git a/server/lib/activitypub/actors/shared/object-to-model-attributes.ts b/server/lib/activitypub/actors/shared/object-to-model-attributes.ts new file mode 100644 index 000000000..66b22c952 --- /dev/null +++ b/server/lib/activitypub/actors/shared/object-to-model-attributes.ts @@ -0,0 +1,70 @@ +import { extname } from 'path' +import { v4 as uuidv4 } from 'uuid' +import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc' +import { MIMETYPES } from '@server/initializers/constants' +import { ActorModel } from '@server/models/actor/actor' +import { FilteredModelAttributes } from '@server/types' +import { ActivityPubActor, ActorImageType } from '@shared/models' + +function getActorAttributesFromObject ( + actorObject: ActivityPubActor, + followersCount: number, + followingCount: number +): FilteredModelAttributes { + return { + type: actorObject.type, + preferredUsername: actorObject.preferredUsername, + url: actorObject.id, + publicKey: actorObject.publicKey.publicKeyPem, + privateKey: null, + followersCount, + followingCount, + inboxUrl: actorObject.inbox, + outboxUrl: actorObject.outbox, + followersUrl: actorObject.followers, + followingUrl: actorObject.following, + + sharedInboxUrl: actorObject.endpoints?.sharedInbox + ? actorObject.endpoints.sharedInbox + : null + } +} + +function getImageInfoFromObject (actorObject: ActivityPubActor, type: ActorImageType) { + const mimetypes = MIMETYPES.IMAGE + const icon = type === ActorImageType.AVATAR + ? actorObject.icon + : actorObject.image + + if (!icon || icon.type !== 'Image' || !isActivityPubUrlValid(icon.url)) return undefined + + let extension: string + + if (icon.mediaType) { + extension = mimetypes.MIMETYPE_EXT[icon.mediaType] + } else { + const tmp = extname(icon.url) + + if (mimetypes.EXT_MIMETYPE[tmp] !== undefined) extension = tmp + } + + if (!extension) return undefined + + return { + name: uuidv4() + extension, + fileUrl: icon.url, + height: icon.height, + width: icon.width, + type + } +} + +function getActorDisplayNameFromObject (actorObject: ActivityPubActor) { + return actorObject.name || actorObject.preferredUsername +} + +export { + getActorAttributesFromObject, + getImageInfoFromObject, + getActorDisplayNameFromObject +} -- cgit v1.2.3