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 associatedVideo = redundancyModel.getVideo()
79 if (!associatedVideo) {
80 logger.warn('Cannot send update activity for redundancy %s: no video files associated.', redundancyModel.url)
84 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(associatedVideo.id)
86 const activityBuilder = (audience: ActivityAudience) => {
87 const redundancyObject = redundancyModel.toActivityPubObject()
88 const url = getUpdateActivityPubUrl(redundancyModel.url, redundancyModel.updatedAt.toISOString())
90 return buildUpdateActivity(url, byActor, redundancyObject, audience)
93 return sendVideoRelatedActivity(activityBuilder, { byActor, video, contextType: 'CacheFile' })
96 async function sendUpdateVideoPlaylist (videoPlaylist: MVideoPlaylistFull, t: Transaction) {
97 if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
99 const byActor = videoPlaylist.OwnerAccount.Actor
101 logger.info('Creating job to update video playlist %s.', videoPlaylist.url)
103 const url = getUpdateActivityPubUrl(videoPlaylist.url, videoPlaylist.updatedAt.toISOString())
105 const object = await videoPlaylist.toActivityPubObject(null, t)
106 const audience = getAudience(byActor, videoPlaylist.privacy === VideoPlaylistPrivacy.PUBLIC)
108 const updateActivity = buildUpdateActivity(url, byActor, object, audience)
110 const serverActor = await getServerActor()
111 const toFollowersOf = [ byActor, serverActor ]
113 if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
115 return broadcastToFollowers(updateActivity, byActor, toFollowersOf, t)
118 // ---------------------------------------------------------------------------
124 sendUpdateVideoPlaylist
127 // ---------------------------------------------------------------------------
129 function buildUpdateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityUpdate {
130 if (!audience) audience = getAudience(byActor)
134 type: 'Update' as 'Update',
137 object: audiencify(object, audience)