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