]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/actors/shared/object-to-model-attributes.ts
Move test functions outside extra-utils
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / actors / shared / object-to-model-attributes.ts
CommitLineData
136d7efd
C
1import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
2import { MIMETYPES } from '@server/initializers/constants'
3import { ActorModel } from '@server/models/actor/actor'
4import { FilteredModelAttributes } from '@server/types'
c55e3d72 5import { buildUUID, getLowercaseExtension } from '@shared/core-utils'
136d7efd
C
6import { ActivityPubActor, ActorImageType } from '@shared/models'
7
8function getActorAttributesFromObject (
9 actorObject: ActivityPubActor,
10 followersCount: number,
11 followingCount: number
12): FilteredModelAttributes<ActorModel> {
13 return {
14 type: actorObject.type,
15 preferredUsername: actorObject.preferredUsername,
16 url: actorObject.id,
17 publicKey: actorObject.publicKey.publicKeyPem,
18 privateKey: null,
19 followersCount,
20 followingCount,
21 inboxUrl: actorObject.inbox,
22 outboxUrl: actorObject.outbox,
23 followersUrl: actorObject.followers,
24 followingUrl: actorObject.following,
25
26 sharedInboxUrl: actorObject.endpoints?.sharedInbox
27 ? actorObject.endpoints.sharedInbox
28 : null
29 }
30}
31
32function getImageInfoFromObject (actorObject: ActivityPubActor, type: ActorImageType) {
33 const mimetypes = MIMETYPES.IMAGE
34 const icon = type === ActorImageType.AVATAR
35 ? actorObject.icon
36 : actorObject.image
37
38 if (!icon || icon.type !== 'Image' || !isActivityPubUrlValid(icon.url)) return undefined
39
40 let extension: string
41
42 if (icon.mediaType) {
43 extension = mimetypes.MIMETYPE_EXT[icon.mediaType]
44 } else {
ea54cd04 45 const tmp = getLowercaseExtension(icon.url)
136d7efd
C
46
47 if (mimetypes.EXT_MIMETYPE[tmp] !== undefined) extension = tmp
48 }
49
50 if (!extension) return undefined
51
52 return {
d4a8e7a6 53 name: buildUUID() + extension,
136d7efd
C
54 fileUrl: icon.url,
55 height: icon.height,
56 width: icon.width,
57 type
58 }
59}
60
61function getActorDisplayNameFromObject (actorObject: ActivityPubActor) {
62 return actorObject.name || actorObject.preferredUsername
63}
64
65export {
66 getActorAttributesFromObject,
67 getImageInfoFromObject,
68 getActorDisplayNameFromObject
69}