aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-04-07 10:36:13 +0200
committerChocobozzz <chocobozzz@cpy.re>2021-04-08 10:07:53 +0200
commit213e30ef90806369529684ac9c247d73b8dc7928 (patch)
tree7f834f2485a074b1d3052745fa5236d34c0f26db /server/lib/activitypub
parent2cb03dc1f4e01ba491c36caff30c33fe9c5bad89 (diff)
downloadPeerTube-213e30ef90806369529684ac9c247d73b8dc7928.tar.gz
PeerTube-213e30ef90806369529684ac9c247d73b8dc7928.tar.zst
PeerTube-213e30ef90806369529684ac9c247d73b8dc7928.zip
Add banner tests
Diffstat (limited to 'server/lib/activitypub')
-rw-r--r--server/lib/activitypub/actor.ts72
-rw-r--r--server/lib/activitypub/process/process-update.ts9
2 files changed, 35 insertions, 46 deletions
diff --git a/server/lib/activitypub/actor.ts b/server/lib/activitypub/actor.ts
index fe4796a3d..917fed6ec 100644
--- a/server/lib/activitypub/actor.ts
+++ b/server/lib/activitypub/actor.ts
@@ -34,6 +34,7 @@ import {
34 MActorFull, 34 MActorFull,
35 MActorFullActor, 35 MActorFullActor,
36 MActorId, 36 MActorId,
37 MActorImage,
37 MActorImages, 38 MActorImages,
38 MChannel 39 MChannel
39} from '../../types/models' 40} from '../../types/models'
@@ -169,38 +170,34 @@ async function updateActorInstance (actorInstance: ActorModel, attributes: Activ
169 } 170 }
170} 171}
171 172
172type AvatarInfo = { name: string, onDisk: boolean, fileUrl: string, type: ActorImageType } 173type ImageInfo = { name: string, onDisk?: boolean, fileUrl: string }
173async function updateActorImageInstance (actor: MActorImages, info: AvatarInfo, t: Transaction) { 174async function updateActorImageInstance (actor: MActorImages, type: ActorImageType, imageInfo: ImageInfo | null, t: Transaction) {
174 if (!info.name) return actor 175 const oldImageModel = type === ActorImageType.AVATAR
175
176 const oldImageModel = info.type === ActorImageType.AVATAR
177 ? actor.Avatar 176 ? actor.Avatar
178 : actor.Banner 177 : actor.Banner
179 178
180 if (oldImageModel) { 179 if (oldImageModel) {
181 // Don't update the avatar if the file URL did not change 180 // Don't update the avatar if the file URL did not change
182 if (info.fileUrl && oldImageModel.fileUrl === info.fileUrl) return actor 181 if (imageInfo?.fileUrl && oldImageModel.fileUrl === imageInfo.fileUrl) return actor
183 182
184 try { 183 try {
185 await oldImageModel.destroy({ transaction: t }) 184 await oldImageModel.destroy({ transaction: t })
185
186 setActorImage(actor, type, null)
186 } catch (err) { 187 } catch (err) {
187 logger.error('Cannot remove old actor image of actor %s.', actor.url, { err }) 188 logger.error('Cannot remove old actor image of actor %s.', actor.url, { err })
188 } 189 }
189 } 190 }
190 191
191 const imageModel = await ActorImageModel.create({ 192 if (imageInfo) {
192 filename: info.name, 193 const imageModel = await ActorImageModel.create({
193 onDisk: info.onDisk, 194 filename: imageInfo.name,
194 fileUrl: info.fileUrl, 195 onDisk: imageInfo.onDisk ?? false,
195 type: info.type 196 fileUrl: imageInfo.fileUrl,
196 }, { transaction: t }) 197 type: type
198 }, { transaction: t })
197 199
198 if (info.type === ActorImageType.AVATAR) { 200 setActorImage(actor, type, imageModel)
199 actor.avatarId = imageModel.id
200 actor.Avatar = imageModel
201 } else {
202 actor.bannerId = imageModel.id
203 actor.Banner = imageModel
204 } 201 }
205 202
206 return actor 203 return actor
@@ -310,27 +307,8 @@ async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannel
310 return sequelizeTypescript.transaction(async t => { 307 return sequelizeTypescript.transaction(async t => {
311 updateInstanceWithAnother(actor, result.actor) 308 updateInstanceWithAnother(actor, result.actor)
312 309
313 if (result.avatar !== undefined) { 310 await updateActorImageInstance(actor, ActorImageType.AVATAR, result.avatar, t)
314 const avatarInfo = { 311 await updateActorImageInstance(actor, ActorImageType.BANNER, result.banner, t)
315 name: result.avatar.name,
316 fileUrl: result.avatar.fileUrl,
317 onDisk: false,
318 type: ActorImageType.AVATAR
319 }
320
321 await updateActorImageInstance(actor, avatarInfo, t)
322 }
323
324 if (result.banner !== undefined) {
325 const bannerInfo = {
326 name: result.banner.name,
327 fileUrl: result.banner.fileUrl,
328 onDisk: false,
329 type: ActorImageType.BANNER
330 }
331
332 await updateActorImageInstance(actor, bannerInfo, t)
333 }
334 312
335 // Force update 313 // Force update
336 actor.setDataValue('updatedAt', new Date()) 314 actor.setDataValue('updatedAt', new Date())
@@ -381,6 +359,22 @@ export {
381 359
382// --------------------------------------------------------------------------- 360// ---------------------------------------------------------------------------
383 361
362function setActorImage (actorModel: MActorImages, type: ActorImageType, imageModel: MActorImage) {
363 const id = imageModel
364 ? imageModel.id
365 : null
366
367 if (type === ActorImageType.AVATAR) {
368 actorModel.avatarId = id
369 actorModel.Avatar = imageModel
370 } else {
371 actorModel.bannerId = id
372 actorModel.Banner = imageModel
373 }
374
375 return actorModel
376}
377
384function saveActorAndServerAndModelIfNotExist ( 378function saveActorAndServerAndModelIfNotExist (
385 result: FetchRemoteActorResult, 379 result: FetchRemoteActorResult,
386 ownerActor?: MActorFullActor, 380 ownerActor?: MActorFullActor,
diff --git a/server/lib/activitypub/process/process-update.ts b/server/lib/activitypub/process/process-update.ts
index ad3bb392d..6df9b93b2 100644
--- a/server/lib/activitypub/process/process-update.ts
+++ b/server/lib/activitypub/process/process-update.ts
@@ -134,13 +134,8 @@ async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate)
134 134
135 await updateActorInstance(actor, actorAttributesToUpdate) 135 await updateActorInstance(actor, actorAttributesToUpdate)
136 136
137 for (const imageInfo of [ avatarInfo, bannerInfo ]) { 137 await updateActorImageInstance(actor, ActorImageType.AVATAR, avatarInfo, t)
138 if (!imageInfo) continue 138 await updateActorImageInstance(actor, ActorImageType.BANNER, bannerInfo, t)
139
140 const imageOptions = Object.assign({}, imageInfo, { onDisk: false })
141
142 await updateActorImageInstance(actor, imageOptions, t)
143 }
144 139
145 await actor.save({ transaction: t }) 140 await actor.save({ transaction: t })
146 141