1 import { Transaction } from 'sequelize'
2 import { ActivityAudience, ActivityUpdate } from '../../../../shared/models/activitypub'
3 import { VideoPrivacy } from '../../../../shared/models/videos'
4 import { AccountModel } from '../../../models/account/account'
5 import { ActorModel } from '../../../models/activitypub/actor'
6 import { VideoModel } from '../../../models/video/video'
7 import { VideoChannelModel } from '../../../models/video/video-channel'
8 import { VideoShareModel } from '../../../models/video/video-share'
9 import { getUpdateActivityPubUrl } from '../url'
10 import { broadcastToFollowers, sendVideoRelatedActivity } from './utils'
11 import { audiencify, getActorsInvolvedInVideo, getAudience } from '../audience'
12 import { logger } from '../../../helpers/logger'
13 import { VideoCaptionModel } from '../../../models/video/video-caption'
14 import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
15 import { VideoPlaylistModel } from '../../../models/video/video-playlist'
16 import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
17 import { getServerActor } from '../../../helpers/utils'
19 async function sendUpdateVideo (video: VideoModel, t: Transaction, overrodeByActor?: ActorModel) {
20 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
22 logger.info('Creating job to update video %s.', video.url)
24 const byActor = overrodeByActor ? overrodeByActor : video.VideoChannel.Account.Actor
26 const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString())
28 // Needed to build the AP object
29 if (!video.VideoCaptions) {
30 video.VideoCaptions = await video.$get('VideoCaptions', { transaction: t }) as VideoCaptionModel[]
33 const videoObject = video.toActivityPubObject()
34 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
36 const updateActivity = buildUpdateActivity(url, byActor, videoObject, audience)
38 const actorsInvolved = await getActorsInvolvedInVideo(video, t)
39 if (overrodeByActor) actorsInvolved.push(overrodeByActor)
41 return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
44 async function sendUpdateActor (accountOrChannel: AccountModel | VideoChannelModel, t: Transaction) {
45 const byActor = accountOrChannel.Actor
47 logger.info('Creating job to update actor %s.', byActor.url)
49 const url = getUpdateActivityPubUrl(byActor.url, byActor.updatedAt.toISOString())
50 const accountOrChannelObject = accountOrChannel.toActivityPubObject()
51 const audience = getAudience(byActor)
52 const updateActivity = buildUpdateActivity(url, byActor, accountOrChannelObject, audience)
54 let actorsInvolved: ActorModel[]
55 if (accountOrChannel instanceof AccountModel) {
56 // Actors that shared my videos are involved too
57 actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, t)
59 // Actors that shared videos of my channel are involved too
60 actorsInvolved = await VideoShareModel.loadActorsByVideoChannel(accountOrChannel.id, t)
63 actorsInvolved.push(byActor)
65 return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
68 async function sendUpdateCacheFile (byActor: ActorModel, redundancyModel: VideoRedundancyModel) {
69 logger.info('Creating job to update cache file %s.', redundancyModel.url)
71 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(redundancyModel.getVideo().id)
73 const activityBuilder = (audience: ActivityAudience) => {
74 const redundancyObject = redundancyModel.toActivityPubObject()
75 const url = getUpdateActivityPubUrl(redundancyModel.url, redundancyModel.updatedAt.toISOString())
77 return buildUpdateActivity(url, byActor, redundancyObject, audience)
80 return sendVideoRelatedActivity(activityBuilder, { byActor, video })
83 async function sendUpdateVideoPlaylist (videoPlaylist: VideoPlaylistModel, t: Transaction) {
84 if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
86 const byActor = videoPlaylist.OwnerAccount.Actor
88 logger.info('Creating job to update video playlist %s.', videoPlaylist.url)
90 const url = getUpdateActivityPubUrl(videoPlaylist.url, videoPlaylist.updatedAt.toISOString())
92 const object = await videoPlaylist.toActivityPubObject(null, t)
93 const audience = getAudience(byActor, videoPlaylist.privacy === VideoPlaylistPrivacy.PUBLIC)
95 const updateActivity = buildUpdateActivity(url, byActor, object, audience)
97 const serverActor = await getServerActor()
98 const toFollowersOf = [ byActor, serverActor ]
100 if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
102 return broadcastToFollowers(updateActivity, byActor, toFollowersOf, t)
105 // ---------------------------------------------------------------------------
111 sendUpdateVideoPlaylist
114 // ---------------------------------------------------------------------------
116 function buildUpdateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityUpdate {
117 if (!audience) audience = getAudience(byActor)
121 type: 'Update' as 'Update',
124 object: audiencify(object, audience