1 import { Transaction } from 'sequelize'
2 import { getServerActor } from '@server/models/application/application'
3 import { ActivityAudience, ActivityUpdate, VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models'
4 import { logger } from '../../../helpers/logger'
5 import { AccountModel } from '../../../models/account/account'
6 import { VideoModel } from '../../../models/video/video'
7 import { VideoShareModel } from '../../../models/video/video-share'
14 MVideoAPWithoutCaption,
17 } from '../../../types/models'
18 import { audiencify, getAudience } from '../audience'
19 import { getUpdateActivityPubUrl } from '../url'
20 import { getActorsInvolvedInVideo } from './shared'
21 import { broadcastToFollowers, sendVideoRelatedActivity } from './shared/send-utils'
23 async function sendUpdateVideo (videoArg: MVideoAPWithoutCaption, transaction: Transaction, overriddenByActor?: MActor) {
24 const video = videoArg as MVideoAP
26 if (!video.hasPrivacyForFederation()) return undefined
28 logger.info('Creating job to update video %s.', video.url)
30 const byActor = overriddenByActor || video.VideoChannel.Account.Actor
32 const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString())
34 // Needed to build the AP object
35 if (!video.VideoCaptions) {
36 video.VideoCaptions = await video.$get('VideoCaptions', { transaction })
39 const videoObject = video.toActivityPubObject()
40 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
42 const updateActivity = buildUpdateActivity(url, byActor, videoObject, audience)
44 const actorsInvolved = await getActorsInvolvedInVideo(video, transaction)
45 if (overriddenByActor) actorsInvolved.push(overriddenByActor)
47 return broadcastToFollowers({
50 toFollowersOf: actorsInvolved,
56 async function sendUpdateActor (accountOrChannel: MChannelDefault | MAccountDefault, transaction: Transaction) {
57 const byActor = accountOrChannel.Actor
59 logger.info('Creating job to update actor %s.', byActor.url)
61 const url = getUpdateActivityPubUrl(byActor.url, byActor.updatedAt.toISOString())
62 const accountOrChannelObject = (accountOrChannel as any).toActivityPubObject() // FIXME: typescript bug?
63 const audience = getAudience(byActor)
64 const updateActivity = buildUpdateActivity(url, byActor, accountOrChannelObject, audience)
66 let actorsInvolved: MActor[]
67 if (accountOrChannel instanceof AccountModel) {
68 // Actors that shared my videos are involved too
69 actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, transaction)
71 // Actors that shared videos of my channel are involved too
72 actorsInvolved = await VideoShareModel.loadActorsByVideoChannel(accountOrChannel.id, transaction)
75 actorsInvolved.push(byActor)
77 return broadcastToFollowers({
80 toFollowersOf: actorsInvolved,
86 async function sendUpdateCacheFile (byActor: MActorLight, redundancyModel: MVideoRedundancyVideo) {
87 logger.info('Creating job to update cache file %s.', redundancyModel.url)
89 const associatedVideo = redundancyModel.getVideo()
90 if (!associatedVideo) {
91 logger.warn('Cannot send update activity for redundancy %s: no video files associated.', redundancyModel.url)
95 const video = await VideoModel.loadFull(associatedVideo.id)
97 const activityBuilder = (audience: ActivityAudience) => {
98 const redundancyObject = redundancyModel.toActivityPubObject()
99 const url = getUpdateActivityPubUrl(redundancyModel.url, redundancyModel.updatedAt.toISOString())
101 return buildUpdateActivity(url, byActor, redundancyObject, audience)
104 return sendVideoRelatedActivity(activityBuilder, { byActor, video, contextType: 'CacheFile' })
107 async function sendUpdateVideoPlaylist (videoPlaylist: MVideoPlaylistFull, transaction: Transaction) {
108 if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
110 const byActor = videoPlaylist.OwnerAccount.Actor
112 logger.info('Creating job to update video playlist %s.', videoPlaylist.url)
114 const url = getUpdateActivityPubUrl(videoPlaylist.url, videoPlaylist.updatedAt.toISOString())
116 const object = await videoPlaylist.toActivityPubObject(null, transaction)
117 const audience = getAudience(byActor, videoPlaylist.privacy === VideoPlaylistPrivacy.PUBLIC)
119 const updateActivity = buildUpdateActivity(url, byActor, object, audience)
121 const serverActor = await getServerActor()
122 const toFollowersOf = [ byActor, serverActor ]
124 if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
126 return broadcastToFollowers({
127 data: updateActivity,
131 contextType: 'Playlist'
135 // ---------------------------------------------------------------------------
141 sendUpdateVideoPlaylist
144 // ---------------------------------------------------------------------------
146 function buildUpdateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityUpdate {
147 if (!audience) audience = getAudience(byActor)
151 type: 'Update' as 'Update',
154 object: audiencify(object, audience)