]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/videos.ts
Stronger actor association typing in AP functions
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / videos.ts
index 67b433165b4c445735d26f45ff971aa078660c5a..3a8451a326116c91c78bfcf04a40dc4d2efdebc9 100644 (file)
@@ -27,7 +27,6 @@ import {
 import { ActorModel } from '../../models/activitypub/actor'
 import { TagModel } from '../../models/video/tag'
 import { VideoModel } from '../../models/video/video'
-import { VideoChannelModel } from '../../models/video/video-channel'
 import { VideoFileModel } from '../../models/video/video-file'
 import { getOrCreateActorAndServerAndModel } from './actor'
 import { addVideoComments } from './video-comments'
@@ -54,12 +53,17 @@ import { ThumbnailModel } from '../../models/video/thumbnail'
 import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type'
 import { join } from 'path'
 import { FilteredModelAttributes } from '../../typings/sequelize'
-import { Hooks } from '../plugins/hooks'
 import { autoBlacklistVideoIfNeeded } from '../video-blacklist'
+import { ActorFollowScoreCache } from '../files-cache'
+import { AccountModelIdActor, VideoChannelModelId, VideoChannelModelIdActor } from '../../typings/models'
 
 async function federateVideoIfNeeded (video: VideoModel, isNewVideo: boolean, transaction?: sequelize.Transaction) {
-  // If the video is not private and is published, we federate it
-  if (video.privacy !== VideoPrivacy.PRIVATE && video.state === VideoState.PUBLISHED) {
+  if (
+    // Check this is not a blacklisted video, or unfederated blacklisted video
+    (video.isBlacklisted() === false || (isNewVideo === false && video.VideoBlacklist.unfederated === false)) &&
+    // Check the video is public/unlisted and published
+    video.privacy !== VideoPrivacy.PRIVATE && video.state === VideoState.PUBLISHED
+  ) {
     // Fetch more attributes that we will need to serialize in AP object
     if (isArray(video.VideoCaptions) === false) {
       video.VideoCaptions = await video.$get('VideoCaptions', {
@@ -178,7 +182,7 @@ async function syncVideoExternalAttributes (video: VideoModel, fetchedVideo: Vid
   }
 
   if (syncParam.comments === true) {
-    const handler = items => addVideoComments(items, video)
+    const handler = items => addVideoComments(items)
     const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate)
 
     await crawlCollectionPage<string>(fetchedVideo.comments, handler, cleaner)
@@ -234,8 +238,8 @@ async function getOrCreateVideoAndAccountAndChannel (options: {
 async function updateVideoFromAP (options: {
   video: VideoModel,
   videoObject: VideoTorrentObject,
-  account: AccountModel,
-  channel: VideoChannelModel,
+  account: AccountModelIdActor,
+  channel: VideoChannelModelIdActor,
   overrideTo?: string[]
 }) {
   const { video, videoObject, account, channel, overrideTo } = options
@@ -354,7 +358,7 @@ async function updateVideoFromAP (options: {
       }
     })
 
-    const autoBlacklisted = await autoBlacklistVideoIfNeeded({
+    await autoBlacklistVideoIfNeeded({
       video,
       user: undefined,
       isRemote: true,
@@ -362,8 +366,7 @@ async function updateVideoFromAP (options: {
       transaction: undefined
     })
 
-    if (autoBlacklisted) Notifier.Instance.notifyOnVideoAutoBlacklist(video)
-    else if (!wasPrivateVideo || wasUnlistedVideo) Notifier.Instance.notifyOnNewVideo(video) // Notify our users?
+    if (wasPrivateVideo || wasUnlistedVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(video) // Notify our users?
 
     logger.info('Remote video with uuid %s updated', videoObject.uuid)
   } catch (err) {
@@ -418,10 +421,14 @@ async function refreshVideoIfNeeded (options: {
     await retryTransactionWrapper(updateVideoFromAP, updateOptions)
     await syncVideoExternalAttributes(video, videoObject, options.syncParam)
 
+    ActorFollowScoreCache.Instance.addGoodServerId(video.VideoChannel.Actor.serverId)
+
     return video
   } catch (err) {
     logger.warn('Cannot refresh video %s.', options.video.url, { err })
 
+    ActorFollowScoreCache.Instance.addBadServerId(video.VideoChannel.Actor.serverId)
+
     // Don't refresh in loop
     await video.setAsRefreshed()
     return video
@@ -497,7 +504,7 @@ async function createVideo (videoObject: VideoTorrentObject, channelActor: Actor
 
     const videoStreamingPlaylists = streamingPlaylistActivityUrlToDBAttributes(videoCreated, videoObject, videoFiles)
     const playlistPromises = videoStreamingPlaylists.map(p => VideoStreamingPlaylistModel.create(p, { transaction: t }))
-    await Promise.all(playlistPromises)
+    const streamingPlaylists = await Promise.all(playlistPromises)
 
     // Process tags
     const tags = videoObject.tag
@@ -510,7 +517,12 @@ async function createVideo (videoObject: VideoTorrentObject, channelActor: Actor
     const videoCaptionsPromises = videoObject.subtitleLanguage.map(c => {
       return VideoCaptionModel.insertOrReplaceLanguage(videoCreated.id, c.identifier, t)
     })
-    await Promise.all(videoCaptionsPromises)
+    const captions = await Promise.all(videoCaptionsPromises)
+
+    video.VideoFiles = videoFiles
+    video.VideoStreamingPlaylists = streamingPlaylists
+    video.Tags = tagInstances
+    video.VideoCaptions = captions
 
     const autoBlacklisted = await autoBlacklistVideoIfNeeded({
       video,
@@ -537,7 +549,7 @@ async function createVideo (videoObject: VideoTorrentObject, channelActor: Actor
 }
 
 async function videoActivityObjectToDBAttributes (
-  videoChannel: VideoChannelModel,
+  videoChannel: VideoChannelModelId,
   videoObject: VideoTorrentObject,
   to: string[] = []
 ) {