]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-update.ts
Add banners support
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-update.ts
index 1bdf23d6ffbd3ff5b5fa85f06d37519b95a34407..ad3bb392dd8cf4ae121519eaaef939507829b835 100644 (file)
@@ -1,4 +1,4 @@
-import { ActivityUpdate, CacheFileObject, VideoTorrentObject } from '../../../../shared/models/activitypub'
+import { ActivityUpdate, CacheFileObject, VideoObject } from '../../../../shared/models/activitypub'
 import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor'
 import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers/database-utils'
 import { logger } from '../../../helpers/logger'
@@ -6,7 +6,7 @@ import { sequelizeTypescript } from '../../../initializers/database'
 import { AccountModel } from '../../../models/account/account'
 import { ActorModel } from '../../../models/activitypub/actor'
 import { VideoChannelModel } from '../../../models/video/video-channel'
-import { getAvatarInfoIfExists, updateActorAvatarInstance, updateActorInstance } from '../actor'
+import { getImageInfoIfExists, updateActorImageInstance, updateActorInstance } from '../actor'
 import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannelFromVideoObject, updateVideoFromAP } from '../videos'
 import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos'
 import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file'
@@ -17,6 +17,7 @@ import { createOrUpdateVideoPlaylist } from '../playlist'
 import { APProcessorOptions } from '../../../types/activitypub-processor.model'
 import { MActorSignature, MAccountIdActor } from '../../../types/models'
 import { isRedundancyAccepted } from '@server/lib/redundancy'
+import { ActorImageType } from '@shared/models'
 
 async function processUpdateActivity (options: APProcessorOptions<ActivityUpdate>) {
   const { activity, byActor } = options
@@ -55,14 +56,22 @@ export {
 // ---------------------------------------------------------------------------
 
 async function processUpdateVideo (actor: MActorSignature, activity: ActivityUpdate) {
-  const videoObject = activity.object as VideoTorrentObject
+  const videoObject = activity.object as VideoObject
 
   if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
     logger.debug('Video sent by update is not valid.', { videoObject })
     return undefined
   }
 
-  const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id, allowRefresh: false, fetchType: 'all' })
+  const { video, created } = await getOrCreateVideoAndAccountAndChannel({
+    videoObject: videoObject.id,
+    allowRefresh: false,
+    fetchType: 'all'
+  })
+  // We did not have this video, it has been created so no need to update
+  if (created) return
+
+  // Load new channel
   const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
 
   const account = actor.Account as MAccountIdActor
@@ -111,7 +120,8 @@ async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate)
   let accountOrChannelFieldsSave: object
 
   // Fetch icon?
-  const avatarInfo = await getAvatarInfoIfExists(actorAttributesToUpdate)
+  const avatarInfo = getImageInfoIfExists(actorAttributesToUpdate, ActorImageType.AVATAR)
+  const bannerInfo = getImageInfoIfExists(actorAttributesToUpdate, ActorImageType.BANNER)
 
   try {
     await sequelizeTypescript.transaction(async t => {
@@ -124,10 +134,12 @@ async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate)
 
       await updateActorInstance(actor, actorAttributesToUpdate)
 
-      if (avatarInfo !== undefined) {
-        const avatarOptions = Object.assign({}, avatarInfo, { onDisk: false })
+      for (const imageInfo of [ avatarInfo, bannerInfo ]) {
+        if (!imageInfo) continue
+
+        const imageOptions = Object.assign({}, imageInfo, { onDisk: false })
 
-        await updateActorAvatarInstance(actor, avatarOptions, t)
+        await updateActorImageInstance(actor, imageOptions, t)
       }
 
       await actor.save({ transaction: t })