]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/videos.ts
Generate a name for thumbnails
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / videos.ts
index ab4aac0a1eed905ed246cab0a0d5013827dd5cab..b5a199e675c2809c44613c4f3c4b47bc08b3cfac 100644 (file)
@@ -1,10 +1,11 @@
-import { VideoLiveModel } from '@server/models/video/video-live'
 import * as Bluebird from 'bluebird'
 import { maxBy, minBy } from 'lodash'
 import * as magnetUtil from 'magnet-uri'
 import { join } from 'path'
 import * as request from 'request'
 import * as sequelize from 'sequelize'
+import { VideoLiveModel } from '@server/models/video/video-live'
+import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
 import {
   ActivityHashTagObject,
   ActivityMagnetUrlObject,
@@ -13,10 +14,9 @@ import {
   ActivitypubHttpFetcherPayload,
   ActivityTagObject,
   ActivityUrlObject,
-  ActivityVideoUrlObject,
-  VideoState
+  ActivityVideoUrlObject
 } from '../../../shared/index'
-import { VideoObject } from '../../../shared/models/activitypub/objects'
+import { ActivityIconObject, VideoObject } from '../../../shared/models/activitypub/objects'
 import { VideoPrivacy } from '../../../shared/models/videos'
 import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type'
 import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
@@ -352,12 +352,15 @@ async function updateVideoFromAP (options: {
       video.views = videoData.views
       video.isLive = videoData.isLive
 
+      // Ensures we update the updated video attribute
+      video.changed('updatedAt', true)
+
       const videoUpdated = await video.save(sequelizeOptions) as MVideoFullLight
 
       if (thumbnailModel) await videoUpdated.addAndSaveThumbnail(thumbnailModel, t)
 
       if (videoUpdated.getPreview()) {
-        const previewUrl = videoUpdated.getPreview().getFileUrl(videoUpdated)
+        const previewUrl = getPreviewUrl(getPreviewFromIcons(videoObject), video)
         const previewModel = createPlaceholderThumbnail(previewUrl, video, ThumbnailType.PREVIEW, PREVIEWS_SIZE)
         await videoUpdated.addAndSaveThumbnail(previewModel, t)
       }
@@ -430,6 +433,7 @@ async function updateVideoFromAP (options: {
         if (video.isLive) {
           const [ videoLive ] = await VideoLiveModel.upsert({
             saveReplay: videoObject.liveSaveReplay,
+            permanentLive: videoObject.permanentLive,
             videoId: video.id
           }, { transaction: t, returning: true })
 
@@ -457,8 +461,13 @@ async function updateVideoFromAP (options: {
       transaction: undefined
     })
 
-    if (wasPrivateVideo || wasUnlistedVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoUpdated) // Notify our users?
-    if (videoUpdated.isLive) PeerTubeSocket.Instance.sendVideoLiveNewState(videoUpdated)
+    // Notify our users?
+    if (wasPrivateVideo || wasUnlistedVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoUpdated)
+
+    if (videoUpdated.isLive) {
+      PeerTubeSocket.Instance.sendVideoLiveNewState(videoUpdated)
+      PeerTubeSocket.Instance.sendVideoViewsUpdate(videoUpdated)
+    }
 
     logger.info('Remote video with uuid %s updated', videoObject.uuid)
 
@@ -488,7 +497,7 @@ async function refreshVideoIfNeeded (options: {
 
   try {
     const { response, videoObject } = await fetchRemoteVideo(video.url)
-    if (response.statusCode === 404) {
+    if (response.statusCode === HttpStatusCode.NOT_FOUND_404) {
       logger.info('Cannot refresh remote video %s: video does not exist anymore. Deleting it.', video.url)
 
       // Video does not exist anymore
@@ -562,8 +571,6 @@ function isAPHashTagObject (url: any): url is ActivityHashTagObject {
   return url && url.type === 'Hashtag'
 }
 
-
-
 async function createVideo (videoObject: VideoObject, channel: MChannelAccountLight, waitThumbnail = false) {
   logger.debug('Adding remote video %s.', videoObject.id)
 
@@ -590,9 +597,7 @@ async function createVideo (videoObject: VideoObject, channel: MChannelAccountLi
     if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
 
     const previewIcon = getPreviewFromIcons(videoObject)
-    const previewUrl = previewIcon
-      ? previewIcon.url
-      : buildRemoteVideoBaseUrl(videoCreated, join(STATIC_PATHS.PREVIEWS, video.generatePreviewName()))
+    const previewUrl = getPreviewUrl(previewIcon, videoCreated)
     const previewModel = createPlaceholderThumbnail(previewUrl, videoCreated, ThumbnailType.PREVIEW, PREVIEWS_SIZE)
 
     if (thumbnailModel) await videoCreated.addAndSaveThumbnail(previewModel, t)
@@ -634,6 +639,7 @@ async function createVideo (videoObject: VideoObject, channel: MChannelAccountLi
       const videoLive = new VideoLiveModel({
         streamKey: null,
         saveReplay: videoObject.liveSaveReplay,
+        permanentLive: videoObject.permanentLive,
         videoId: videoCreated.id
       })
 
@@ -814,7 +820,11 @@ function getThumbnailFromIcons (videoObject: VideoObject) {
 function getPreviewFromIcons (videoObject: VideoObject) {
   const validIcons = videoObject.icon.filter(i => i.width > PREVIEWS_SIZE.minWidth)
 
-  // FIXME: don't put a fallback here for compatibility with PeerTube <2.2
-
   return maxBy(validIcons, 'width')
 }
+
+function getPreviewUrl (previewIcon: ActivityIconObject, video: MVideoAccountLight) {
+  return previewIcon
+    ? previewIcon.url
+    : buildRemoteVideoBaseUrl(video, join(STATIC_PATHS.PREVIEWS, video.generatePreviewName()))
+}