]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-update.ts
3eb2704fd6882b544eb615ca4b6b559eb7ea84b5
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-update.ts
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 { ActorModel } from '../../../models/activitypub/actor'
6 import { VideoModel } from '../../../models/video/video'
7 import { VideoChannelModel } from '../../../models/video/video-channel'
8 import { VideoShareModel } from '../../../models/video/video-share'
9 import { getUpdateActivityPubUrl } from '../url'
10 import { broadcastToFollowers, sendVideoRelatedActivity } from './utils'
11 import { audiencify, getActorsInvolvedInVideo, getAudience } from '../audience'
12 import { logger } from '../../../helpers/logger'
13 import { VideoCaptionModel } from '../../../models/video/video-caption'
14 import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
15 import { VideoPlaylistModel } from '../../../models/video/video-playlist'
16 import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
17 import { getServerActor } from '../../../helpers/utils'
18
19 async function sendUpdateVideo (video: VideoModel, t: Transaction, overrodeByActor?: ActorModel) {
20 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
21
22 logger.info('Creating job to update video %s.', video.url)
23
24 const byActor = overrodeByActor ? overrodeByActor : video.VideoChannel.Account.Actor
25
26 const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString())
27
28 // Needed to build the AP object
29 if (!video.VideoCaptions) video.VideoCaptions = await video.$get('VideoCaptions') as VideoCaptionModel[]
30
31 const videoObject = video.toActivityPubObject()
32 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
33
34 const updateActivity = buildUpdateActivity(url, byActor, videoObject, audience)
35
36 const actorsInvolved = await getActorsInvolvedInVideo(video, t)
37 if (overrodeByActor) actorsInvolved.push(overrodeByActor)
38
39 return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
40 }
41
42 async function sendUpdateActor (accountOrChannel: AccountModel | VideoChannelModel, t: Transaction) {
43 const byActor = accountOrChannel.Actor
44
45 logger.info('Creating job to update actor %s.', byActor.url)
46
47 const url = getUpdateActivityPubUrl(byActor.url, byActor.updatedAt.toISOString())
48 const accountOrChannelObject = accountOrChannel.toActivityPubObject()
49 const audience = getAudience(byActor)
50 const updateActivity = buildUpdateActivity(url, byActor, accountOrChannelObject, audience)
51
52 let actorsInvolved: ActorModel[]
53 if (accountOrChannel instanceof AccountModel) {
54 // Actors that shared my videos are involved too
55 actorsInvolved = await VideoShareModel.loadActorsByVideoOwner(byActor.id, t)
56 } else {
57 // Actors that shared videos of my channel are involved too
58 actorsInvolved = await VideoShareModel.loadActorsByVideoChannel(accountOrChannel.id, t)
59 }
60
61 actorsInvolved.push(byActor)
62
63 return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
64 }
65
66 async function sendUpdateCacheFile (byActor: ActorModel, redundancyModel: VideoRedundancyModel) {
67 logger.info('Creating job to update cache file %s.', redundancyModel.url)
68
69 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(redundancyModel.getVideo().id)
70
71 const activityBuilder = (audience: ActivityAudience) => {
72 const redundancyObject = redundancyModel.toActivityPubObject()
73 const url = getUpdateActivityPubUrl(redundancyModel.url, redundancyModel.updatedAt.toISOString())
74
75 return buildUpdateActivity(url, byActor, redundancyObject, audience)
76 }
77
78 return sendVideoRelatedActivity(activityBuilder, { byActor, video })
79 }
80
81 async function sendUpdateVideoPlaylist (videoPlaylist: VideoPlaylistModel, t: Transaction) {
82 if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
83
84 const byActor = videoPlaylist.OwnerAccount.Actor
85
86 logger.info('Creating job to update video playlist %s.', videoPlaylist.url)
87
88 const url = getUpdateActivityPubUrl(videoPlaylist.url, videoPlaylist.updatedAt.toISOString())
89
90 const object = await videoPlaylist.toActivityPubObject()
91 const audience = getAudience(byActor, videoPlaylist.privacy === VideoPlaylistPrivacy.PUBLIC)
92
93 const updateActivity = buildUpdateActivity(url, byActor, object, audience)
94
95 const serverActor = await getServerActor()
96 const toFollowersOf = [ byActor, serverActor ]
97
98 if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
99
100 return broadcastToFollowers(updateActivity, byActor, toFollowersOf, t)
101 }
102
103 // ---------------------------------------------------------------------------
104
105 export {
106 sendUpdateActor,
107 sendUpdateVideo,
108 sendUpdateCacheFile,
109 sendUpdateVideoPlaylist
110 }
111
112 // ---------------------------------------------------------------------------
113
114 function buildUpdateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityUpdate {
115 if (!audience) audience = getAudience(byActor)
116
117 return audiencify(
118 {
119 type: 'Update' as 'Update',
120 id: url,
121 actor: byActor.url,
122 object: audiencify(object, audience
123 )
124 },
125 audience
126 )
127 }