PeerTube/server/lib/activitypub/actors/shared/object-to-model-attributes.ts

85 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-06-03 16:02:29 +02:00
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 { getLowercaseExtension } from '@shared/core-utils'
import { buildUUID } from '@shared/extra-utils'
import { ActivityIconObject, ActivityPubActor, ActorImageType } from '@shared/models'
2021-06-03 16:02:29 +02:00
function getActorAttributesFromObject (
actorObject: ActivityPubActor,
followersCount: number,
followingCount: number
): FilteredModelAttributes<ActorModel> {
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 getImagesInfoFromObject (actorObject: ActivityPubActor, type: ActorImageType) {
const iconsOrImages = type === ActorImageType.AVATAR
? actorObject.icons || actorObject.icon
2021-06-03 16:02:29 +02:00
: actorObject.image
return normalizeIconOrImage(iconsOrImages)
.map(iconOrImage => {
const mimetypes = MIMETYPES.IMAGE
2021-06-03 16:02:29 +02:00
if (iconOrImage.type !== 'Image' || !isActivityPubUrlValid(iconOrImage.url)) return undefined
2021-06-03 16:02:29 +02:00
let extension: string
2021-06-03 16:02:29 +02:00
if (iconOrImage.mediaType) {
extension = mimetypes.MIMETYPE_EXT[iconOrImage.mediaType]
} else {
const tmp = getLowercaseExtension(iconOrImage.url)
2021-06-03 16:02:29 +02:00
if (mimetypes.EXT_MIMETYPE[tmp] !== undefined) extension = tmp
}
2021-06-03 16:02:29 +02:00
if (!extension) return undefined
return {
name: buildUUID() + extension,
fileUrl: iconOrImage.url,
height: iconOrImage.height,
width: iconOrImage.width,
type
}
})
.filter(i => !!i)
2021-06-03 16:02:29 +02:00
}
function getActorDisplayNameFromObject (actorObject: ActivityPubActor) {
return actorObject.name || actorObject.preferredUsername
}
export {
getActorAttributesFromObject,
getImagesInfoFromObject,
2021-06-03 16:02:29 +02:00
getActorDisplayNameFromObject
}
// ---------------------------------------------------------------------------
function normalizeIconOrImage (icon: ActivityIconObject | ActivityIconObject[]): ActivityIconObject[] {
if (Array.isArray(icon)) return icon
if (icon) return [ icon ]
return []
}