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 { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
18 MVideoAPWithoutCaption,
21 } from '../../../types/models'
22 import { getServerActor } from '@server/models/application/application'
24 async function sendUpdateVideo (videoArg: MVideoAPWithoutCaption, t: Transaction, overrodeByActor?: MActor) {
25 const video = videoArg as MVideoAP
27 if (!video.hasPrivacyForFederation()) return undefined
29 logger.info('Creating job to update video %s.', video.url)
31 const byActor = overrodeByActor || video.VideoChannel.Account.Actor
33 const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString())
35 // Needed to build the AP object
36 if (!video.VideoCaptions) {
37 video.VideoCaptions = await video.$get('VideoCaptions', { transaction: t })
40 const videoObject = video.toActivityPubObject()
41 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
43 const updateActivity = buildUpdateActivity(url, byActor, videoObject, audience)
45 const actorsInvolved = await getActorsInvolvedInVideo(video, t)
46 if (overrodeByActor) actorsInvolved.push(overrodeByActor)
48 return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
51 async function sendUpdateActor (accountOrChannel: MChannelDefault | MAccountDefault, t: Transaction) {
52 const byActor = accountOrChannel.Actor
54 logger.info('Creating job to update actor %s.', byActor.url)
56 const url = getUpdateActivityPubUrl(byActor.url, byActor.updatedAt.toISOString())
57 const accountOrChannelObject = (accountOrChannel as any).toActivityPubObject() // FIXME: typescript bug?
58 const audience = getAudience(byActor)
59 const updateActivity = buildUpdateActivity(url, byActor, accountOrChannelObject, audience)
61 let actorsInvolved: MActor[]
62 if (accountOrChannel instanceof AccountModel) {
63 // Actors that shared my videos are involved too
64 actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, t)
66 // Actors that shared videos of my channel are involved too
67 actorsInvolved = await VideoShareModel.loadActorsByVideoChannel(accountOrChannel.id, t)
70 actorsInvolved.push(byActor)
72 return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
75 async function sendUpdateCacheFile (byActor: MActorLight, redundancyModel: MVideoRedundancyVideo) {
76 logger.info('Creating job to update cache file %s.', redundancyModel.url)
78 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(redundancyModel.getVideo().id)
80 const activityBuilder = (audience: ActivityAudience) => {
81 const redundancyObject = redundancyModel.toActivityPubObject()
82 const url = getUpdateActivityPubUrl(redundancyModel.url, redundancyModel.updatedAt.toISOString())
84 return buildUpdateActivity(url, byActor, redundancyObject, audience)
87 return sendVideoRelatedActivity(activityBuilder, { byActor, video, contextType: 'CacheFile' })
90 async function sendUpdateVideoPlaylist (videoPlaylist: MVideoPlaylistFull, t: Transaction) {
91 if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
93 const byActor = videoPlaylist.OwnerAccount.Actor
95 logger.info('Creating job to update video playlist %s.', videoPlaylist.url)
97 const url = getUpdateActivityPubUrl(videoPlaylist.url, videoPlaylist.updatedAt.toISOString())
99 const object = await videoPlaylist.toActivityPubObject(null, t)
100 const audience = getAudience(byActor, videoPlaylist.privacy === VideoPlaylistPrivacy.PUBLIC)
102 const updateActivity = buildUpdateActivity(url, byActor, object, audience)
104 const serverActor = await getServerActor()
105 const toFollowersOf = [ byActor, serverActor ]
107 if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
109 return broadcastToFollowers(updateActivity, byActor, toFollowersOf, t)
112 // ---------------------------------------------------------------------------
118 sendUpdateVideoPlaylist
121 // ---------------------------------------------------------------------------
123 function buildUpdateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityUpdate {
124 if (!audience) audience = getAudience(byActor)
128 type: 'Update' as 'Update',
131 object: audiencify(object, audience)