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, t: Transaction, overrodeByActor?: 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 = overrodeByActor || 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: t })
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, t)
45 if (overrodeByActor) actorsInvolved.push(overrodeByActor)
47 return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
50 async function sendUpdateActor (accountOrChannel: MChannelDefault | MAccountDefault, t: Transaction) {
51 const byActor = accountOrChannel.Actor
53 logger.info('Creating job to update actor %s.', byActor.url)
55 const url = getUpdateActivityPubUrl(byActor.url, byActor.updatedAt.toISOString())
56 const accountOrChannelObject = (accountOrChannel as any).toActivityPubObject() // FIXME: typescript bug?
57 const audience = getAudience(byActor)
58 const updateActivity = buildUpdateActivity(url, byActor, accountOrChannelObject, audience)
60 let actorsInvolved: MActor[]
61 if (accountOrChannel instanceof AccountModel) {
62 // Actors that shared my videos are involved too
63 actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, t)
65 // Actors that shared videos of my channel are involved too
66 actorsInvolved = await VideoShareModel.loadActorsByVideoChannel(accountOrChannel.id, t)
69 actorsInvolved.push(byActor)
71 return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
74 async function sendUpdateCacheFile (byActor: MActorLight, redundancyModel: MVideoRedundancyVideo) {
75 logger.info('Creating job to update cache file %s.', redundancyModel.url)
77 const associatedVideo = redundancyModel.getVideo()
78 if (!associatedVideo) {
79 logger.warn('Cannot send update activity for redundancy %s: no video files associated.', redundancyModel.url)
83 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(associatedVideo.id)
85 const activityBuilder = (audience: ActivityAudience) => {
86 const redundancyObject = redundancyModel.toActivityPubObject()
87 const url = getUpdateActivityPubUrl(redundancyModel.url, redundancyModel.updatedAt.toISOString())
89 return buildUpdateActivity(url, byActor, redundancyObject, audience)
92 return sendVideoRelatedActivity(activityBuilder, { byActor, video, contextType: 'CacheFile' })
95 async function sendUpdateVideoPlaylist (videoPlaylist: MVideoPlaylistFull, t: Transaction) {
96 if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
98 const byActor = videoPlaylist.OwnerAccount.Actor
100 logger.info('Creating job to update video playlist %s.', videoPlaylist.url)
102 const url = getUpdateActivityPubUrl(videoPlaylist.url, videoPlaylist.updatedAt.toISOString())
104 const object = await videoPlaylist.toActivityPubObject(null, t)
105 const audience = getAudience(byActor, videoPlaylist.privacy === VideoPlaylistPrivacy.PUBLIC)
107 const updateActivity = buildUpdateActivity(url, byActor, object, audience)
109 const serverActor = await getServerActor()
110 const toFollowersOf = [ byActor, serverActor ]
112 if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
114 return broadcastToFollowers(updateActivity, byActor, toFollowersOf, t)
117 // ---------------------------------------------------------------------------
123 sendUpdateVideoPlaylist
126 // ---------------------------------------------------------------------------
128 function buildUpdateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityUpdate {
129 if (!audience) audience = getAudience(byActor)
133 type: 'Update' as 'Update',
136 object: audiencify(object, audience)