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 { VideoModel } from '../../../models/video/video'
6 import { VideoShareModel } from '../../../models/video/video-share'
7 import { getUpdateActivityPubUrl } from '../url'
8 import { broadcastToFollowers, sendVideoRelatedActivity } from './utils'
9 import { audiencify, getActorsInvolvedInVideo, getAudience } from '../audience'
10 import { logger } from '../../../helpers/logger'
11 import { VideoCaptionModel } from '../../../models/video/video-caption'
12 import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
13 import { getServerActor } from '../../../helpers/utils'
20 MVideoAPWithoutCaption,
23 } from '../../../typings/models'
25 async function sendUpdateVideo (videoArg: MVideoAPWithoutCaption, t: Transaction, overrodeByActor?: MActor) {
26 const video = videoArg as MVideoAP
28 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
30 logger.info('Creating job to update video %s.', video.url)
32 const byActor = overrodeByActor ? overrodeByActor : video.VideoChannel.Account.Actor
34 const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString())
36 // Needed to build the AP object
37 if (!video.VideoCaptions) {
38 video.VideoCaptions = await video.$get('VideoCaptions', { transaction: t }) as VideoCaptionModel[]
41 const videoObject = video.toActivityPubObject()
42 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
44 const updateActivity = buildUpdateActivity(url, byActor, videoObject, audience)
46 const actorsInvolved = await getActorsInvolvedInVideo(video, t)
47 if (overrodeByActor) actorsInvolved.push(overrodeByActor)
49 return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
52 async function sendUpdateActor (accountOrChannel: MChannelDefault | MAccountDefault, t: Transaction) {
53 const byActor = accountOrChannel.Actor
55 logger.info('Creating job to update actor %s.', byActor.url)
57 const url = getUpdateActivityPubUrl(byActor.url, byActor.updatedAt.toISOString())
58 const accountOrChannelObject = (accountOrChannel as any).toActivityPubObject() // FIXME: typescript bug?
59 const audience = getAudience(byActor)
60 const updateActivity = buildUpdateActivity(url, byActor, accountOrChannelObject, audience)
62 let actorsInvolved: MActor[]
63 if (accountOrChannel instanceof AccountModel) {
64 // Actors that shared my videos are involved too
65 actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, t)
67 // Actors that shared videos of my channel are involved too
68 actorsInvolved = await VideoShareModel.loadActorsByVideoChannel(accountOrChannel.id, t)
71 actorsInvolved.push(byActor)
73 return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
76 async function sendUpdateCacheFile (byActor: MActorLight, redundancyModel: MVideoRedundancyVideo) {
77 logger.info('Creating job to update cache file %s.', redundancyModel.url)
79 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(redundancyModel.getVideo().id)
81 const activityBuilder = (audience: ActivityAudience) => {
82 const redundancyObject = redundancyModel.toActivityPubObject()
83 const url = getUpdateActivityPubUrl(redundancyModel.url, redundancyModel.updatedAt.toISOString())
85 return buildUpdateActivity(url, byActor, redundancyObject, audience)
88 return sendVideoRelatedActivity(activityBuilder, { byActor, video })
91 async function sendUpdateVideoPlaylist (videoPlaylist: MVideoPlaylistFull, t: Transaction) {
92 if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
94 const byActor = videoPlaylist.OwnerAccount.Actor
96 logger.info('Creating job to update video playlist %s.', videoPlaylist.url)
98 const url = getUpdateActivityPubUrl(videoPlaylist.url, videoPlaylist.updatedAt.toISOString())
100 const object = await videoPlaylist.toActivityPubObject(null, t)
101 const audience = getAudience(byActor, videoPlaylist.privacy === VideoPlaylistPrivacy.PUBLIC)
103 const updateActivity = buildUpdateActivity(url, byActor, object, audience)
105 const serverActor = await getServerActor()
106 const toFollowersOf = [ byActor, serverActor ]
108 if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
110 return broadcastToFollowers(updateActivity, byActor, toFollowersOf, t)
113 // ---------------------------------------------------------------------------
119 sendUpdateVideoPlaylist
122 // ---------------------------------------------------------------------------
124 function buildUpdateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityUpdate {
125 if (!audience) audience = getAudience(byActor)
129 type: 'Update' as 'Update',
132 object: audiencify(object, audience)