X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fvideos.ts;h=d935e3f90224a89a8668bc1d2e444073692a7f34;hb=7ccddd7b5250bd25a917a6e77e58b87b9484a2a4;hp=edd01234fade0d5900c3eb397abcb817577b2b7f;hpb=092092969633bbcf6d4891a083ea497a7d5c3154;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/videos.ts b/server/lib/activitypub/videos.ts index edd01234f..d935e3f90 100644 --- a/server/lib/activitypub/videos.ts +++ b/server/lib/activitypub/videos.ts @@ -40,9 +40,12 @@ import { Notifier } from '../notifier' import { VideoStreamingPlaylistModel } from '../../models/video/video-streaming-playlist' import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' import { FilteredModelAttributes } from 'sequelize-typescript/lib/models/Model' +import { AccountVideoRateModel } from '../../models/account/account-video-rate' +import { VideoShareModel } from '../../models/video/video-share' +import { VideoCommentModel } from '../../models/video/video-comment' async function federateVideoIfNeeded (video: VideoModel, isNewVideo: boolean, transaction?: sequelize.Transaction) { - // If the video is not private and published, we federate it + // If the video is not private and is published, we federate it if (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) { @@ -134,31 +137,43 @@ async function syncVideoExternalAttributes (video: VideoModel, fetchedVideo: Vid const jobPayloads: ActivitypubHttpFetcherPayload[] = [] if (syncParam.likes === true) { - await crawlCollectionPage(fetchedVideo.likes, items => createRates(items, video, 'like')) + const handler = items => createRates(items, video, 'like') + const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'like' as 'like', crawlStartDate) + + await crawlCollectionPage(fetchedVideo.likes, handler, cleaner) .catch(err => logger.error('Cannot add likes of video %s.', video.uuid, { err })) } else { jobPayloads.push({ uri: fetchedVideo.likes, videoId: video.id, type: 'video-likes' as 'video-likes' }) } if (syncParam.dislikes === true) { - await crawlCollectionPage(fetchedVideo.dislikes, items => createRates(items, video, 'dislike')) + const handler = items => createRates(items, video, 'dislike') + const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'dislike' as 'dislike', crawlStartDate) + + await crawlCollectionPage(fetchedVideo.dislikes, handler, cleaner) .catch(err => logger.error('Cannot add dislikes of video %s.', video.uuid, { err })) } else { jobPayloads.push({ uri: fetchedVideo.dislikes, videoId: video.id, type: 'video-dislikes' as 'video-dislikes' }) } if (syncParam.shares === true) { - await crawlCollectionPage(fetchedVideo.shares, items => addVideoShares(items, video)) + const handler = items => addVideoShares(items, video) + const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate) + + await crawlCollectionPage(fetchedVideo.shares, handler, cleaner) .catch(err => logger.error('Cannot add shares of video %s.', video.uuid, { err })) } else { jobPayloads.push({ uri: fetchedVideo.shares, videoId: video.id, type: 'video-shares' as 'video-shares' }) } if (syncParam.comments === true) { - await crawlCollectionPage(fetchedVideo.comments, items => addVideoComments(items, video)) + const handler = items => addVideoComments(items, video) + const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate) + + await crawlCollectionPage(fetchedVideo.comments, handler, cleaner) .catch(err => logger.error('Cannot add comments of video %s.', video.uuid, { err })) } else { - jobPayloads.push({ uri: fetchedVideo.shares, videoId: video.id, type: 'video-shares' as 'video-shares' }) + jobPayloads.push({ uri: fetchedVideo.comments, videoId: video.id, type: 'video-comments' as 'video-comments' }) } await Bluebird.map(jobPayloads, payload => JobQueue.Instance.createJob({ type: 'activitypub-http-fetcher', payload })) @@ -180,8 +195,7 @@ async function getOrCreateVideoAndAccountAndChannel (options: { let videoFromDatabase = await fetchVideoByUrl(videoUrl, fetchType) if (videoFromDatabase) { - - if (allowRefresh === true) { + if (videoFromDatabase.isOutdated() && allowRefresh === true) { const refreshOptions = { video: videoFromDatabase, fetchedType: fetchType, @@ -243,11 +257,13 @@ async function updateVideoFromAP (options: { options.video.set('support', videoData.support) options.video.set('nsfw', videoData.nsfw) options.video.set('commentsEnabled', videoData.commentsEnabled) + options.video.set('downloadEnabled', videoData.downloadEnabled) options.video.set('waitTranscoding', videoData.waitTranscoding) options.video.set('state', videoData.state) options.video.set('duration', videoData.duration) options.video.set('createdAt', videoData.createdAt) options.video.set('publishedAt', videoData.publishedAt) + options.video.set('originallyPublishedAt', videoData.originallyPublishedAt) options.video.set('privacy', videoData.privacy) options.video.set('channelId', videoData.channelId) options.video.set('views', videoData.views) @@ -503,12 +519,14 @@ async function videoActivityObjectToDBAttributes ( support, nsfw: videoObject.sensitive, commentsEnabled: videoObject.commentsEnabled, + downloadEnabled: videoObject.downloadEnabled, waitTranscoding: videoObject.waitTranscoding, state: videoObject.state, channelId: videoChannel.id, duration: parseInt(duration, 10), createdAt: new Date(videoObject.published), publishedAt: new Date(videoObject.published), + originallyPublishedAt: videoObject.originallyPublishedAt ? new Date(videoObject.originallyPublishedAt) : null, // FIXME: updatedAt does not seems to be considered by Sequelize updatedAt: new Date(videoObject.updated), views: videoObject.views,