diff options
author | Chocobozzz <me@florianbigard.com> | 2021-06-03 16:02:29 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-06-03 16:40:32 +0200 |
commit | 136d7efde798d3dc0ec0dd18aac674365f7d162e (patch) | |
tree | 3a0e2a7a5d04dedf0d8ffda99c2787cecb838891 /server/lib/activitypub/actors/image.ts | |
parent | 49af5ac8c2653cb0ef23479c9d3256c5b724d49d (diff) | |
download | PeerTube-136d7efde798d3dc0ec0dd18aac674365f7d162e.tar.gz PeerTube-136d7efde798d3dc0ec0dd18aac674365f7d162e.tar.zst PeerTube-136d7efde798d3dc0ec0dd18aac674365f7d162e.zip |
Refactor AP actors
Diffstat (limited to 'server/lib/activitypub/actors/image.ts')
-rw-r--r-- | server/lib/activitypub/actors/image.ts | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/server/lib/activitypub/actors/image.ts b/server/lib/activitypub/actors/image.ts new file mode 100644 index 000000000..443ad0a63 --- /dev/null +++ b/server/lib/activitypub/actors/image.ts | |||
@@ -0,0 +1,94 @@ | |||
1 | import { Transaction } from 'sequelize/types' | ||
2 | import { logger } from '@server/helpers/logger' | ||
3 | import { ActorImageModel } from '@server/models/actor/actor-image' | ||
4 | import { MActorImage, MActorImages } from '@server/types/models' | ||
5 | import { ActorImageType } from '@shared/models' | ||
6 | |||
7 | type ImageInfo = { | ||
8 | name: string | ||
9 | fileUrl: string | ||
10 | height: number | ||
11 | width: number | ||
12 | onDisk?: boolean | ||
13 | } | ||
14 | |||
15 | async function updateActorImageInstance (actor: MActorImages, type: ActorImageType, imageInfo: ImageInfo | null, t: Transaction) { | ||
16 | const oldImageModel = type === ActorImageType.AVATAR | ||
17 | ? actor.Avatar | ||
18 | : actor.Banner | ||
19 | |||
20 | if (oldImageModel) { | ||
21 | // Don't update the avatar if the file URL did not change | ||
22 | if (imageInfo?.fileUrl && oldImageModel.fileUrl === imageInfo.fileUrl) return actor | ||
23 | |||
24 | try { | ||
25 | await oldImageModel.destroy({ transaction: t }) | ||
26 | |||
27 | setActorImage(actor, type, null) | ||
28 | } catch (err) { | ||
29 | logger.error('Cannot remove old actor image of actor %s.', actor.url, { err }) | ||
30 | } | ||
31 | } | ||
32 | |||
33 | if (imageInfo) { | ||
34 | const imageModel = await ActorImageModel.create({ | ||
35 | filename: imageInfo.name, | ||
36 | onDisk: imageInfo.onDisk ?? false, | ||
37 | fileUrl: imageInfo.fileUrl, | ||
38 | height: imageInfo.height, | ||
39 | width: imageInfo.width, | ||
40 | type | ||
41 | }, { transaction: t }) | ||
42 | |||
43 | setActorImage(actor, type, imageModel) | ||
44 | } | ||
45 | |||
46 | return actor | ||
47 | } | ||
48 | |||
49 | async function deleteActorImageInstance (actor: MActorImages, type: ActorImageType, t: Transaction) { | ||
50 | try { | ||
51 | if (type === ActorImageType.AVATAR) { | ||
52 | await actor.Avatar.destroy({ transaction: t }) | ||
53 | |||
54 | actor.avatarId = null | ||
55 | actor.Avatar = null | ||
56 | } else { | ||
57 | await actor.Banner.destroy({ transaction: t }) | ||
58 | |||
59 | actor.bannerId = null | ||
60 | actor.Banner = null | ||
61 | } | ||
62 | } catch (err) { | ||
63 | logger.error('Cannot remove old image of actor %s.', actor.url, { err }) | ||
64 | } | ||
65 | |||
66 | return actor | ||
67 | } | ||
68 | |||
69 | // --------------------------------------------------------------------------- | ||
70 | |||
71 | export { | ||
72 | ImageInfo, | ||
73 | |||
74 | updateActorImageInstance, | ||
75 | deleteActorImageInstance | ||
76 | } | ||
77 | |||
78 | // --------------------------------------------------------------------------- | ||
79 | |||
80 | function setActorImage (actorModel: MActorImages, type: ActorImageType, imageModel: MActorImage) { | ||
81 | const id = imageModel | ||
82 | ? imageModel.id | ||
83 | : null | ||
84 | |||
85 | if (type === ActorImageType.AVATAR) { | ||
86 | actorModel.avatarId = id | ||
87 | actorModel.Avatar = imageModel | ||
88 | } else { | ||
89 | actorModel.bannerId = id | ||
90 | actorModel.Banner = imageModel | ||
91 | } | ||
92 | |||
93 | return actorModel | ||
94 | } | ||