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'
|
2021-12-29 14:44:58 +01:00
|
|
|
import { getLowercaseExtension } from '@shared/core-utils'
|
|
|
|
import { buildUUID } from '@shared/extra-utils'
|
2022-02-28 08:34:43 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 08:34:43 +01:00
|
|
|
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
|
|
|
|
|
2022-11-23 16:23:22 +01:00
|
|
|
return normalizeIconOrImage(iconsOrImages)
|
|
|
|
.map(iconOrImage => {
|
|
|
|
const mimetypes = MIMETYPES.IMAGE
|
2021-06-03 16:02:29 +02:00
|
|
|
|
2022-11-23 16:23:22 +01:00
|
|
|
if (iconOrImage.type !== 'Image' || !isActivityPubUrlValid(iconOrImage.url)) return undefined
|
2021-06-03 16:02:29 +02:00
|
|
|
|
2022-11-23 16:23:22 +01:00
|
|
|
let extension: string
|
2021-06-03 16:02:29 +02:00
|
|
|
|
2022-11-23 16:23:22 +01:00
|
|
|
if (iconOrImage.mediaType) {
|
|
|
|
extension = mimetypes.MIMETYPE_EXT[iconOrImage.mediaType]
|
|
|
|
} else {
|
|
|
|
const tmp = getLowercaseExtension(iconOrImage.url)
|
2021-06-03 16:02:29 +02:00
|
|
|
|
2022-11-23 16:23:22 +01:00
|
|
|
if (mimetypes.EXT_MIMETYPE[tmp] !== undefined) extension = tmp
|
|
|
|
}
|
2021-06-03 16:02:29 +02:00
|
|
|
|
2022-11-23 16:23:22 +01:00
|
|
|
if (!extension) return undefined
|
2022-02-28 08:34:43 +01:00
|
|
|
|
2022-11-23 16:23:22 +01:00
|
|
|
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,
|
2022-02-28 08:34:43 +01:00
|
|
|
getImagesInfoFromObject,
|
2021-06-03 16:02:29 +02:00
|
|
|
getActorDisplayNameFromObject
|
|
|
|
}
|
2022-02-28 08:34:43 +01:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function normalizeIconOrImage (icon: ActivityIconObject | ActivityIconObject[]): ActivityIconObject[] {
|
|
|
|
if (Array.isArray(icon)) return icon
|
|
|
|
if (icon) return [ icon ]
|
|
|
|
|
|
|
|
return []
|
|
|
|
}
|