]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-update.ts
7c9e72cbcec2b464879e0fd1c68e2de6c2a258ef
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-update.ts
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'
8 import {
9 MAccountDefault,
10 MActor,
11 MActorLight,
12 MChannelDefault,
13 MVideoAP,
14 MVideoAPWithoutCaption,
15 MVideoPlaylistFull,
16 MVideoRedundancyVideo
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'
22
23 async function sendUpdateVideo (videoArg: MVideoAPWithoutCaption, t: Transaction, overrodeByActor?: MActor) {
24 const video = videoArg as MVideoAP
25
26 if (!video.hasPrivacyForFederation()) return undefined
27
28 logger.info('Creating job to update video %s.', video.url)
29
30 const byActor = overrodeByActor || video.VideoChannel.Account.Actor
31
32 const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString())
33
34 // Needed to build the AP object
35 if (!video.VideoCaptions) {
36 video.VideoCaptions = await video.$get('VideoCaptions', { transaction: t })
37 }
38
39 const videoObject = video.toActivityPubObject()
40 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
41
42 const updateActivity = buildUpdateActivity(url, byActor, videoObject, audience)
43
44 const actorsInvolved = await getActorsInvolvedInVideo(video, t)
45 if (overrodeByActor) actorsInvolved.push(overrodeByActor)
46
47 return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
48 }
49
50 async function sendUpdateActor (accountOrChannel: MChannelDefault | MAccountDefault, t: Transaction) {
51 const byActor = accountOrChannel.Actor
52
53 logger.info('Creating job to update actor %s.', byActor.url)
54
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)
59
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)
64 } else {
65 // Actors that shared videos of my channel are involved too
66 actorsInvolved = await VideoShareModel.loadActorsByVideoChannel(accountOrChannel.id, t)
67 }
68
69 actorsInvolved.push(byActor)
70
71 return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
72 }
73
74 async function sendUpdateCacheFile (byActor: MActorLight, redundancyModel: MVideoRedundancyVideo) {
75 logger.info('Creating job to update cache file %s.', redundancyModel.url)
76
77 const associatedVideo = redundancyModel.getVideo()
78 if (!associatedVideo) {
79 logger.warn('Cannot send update activity for redundancy %s: no video files associated.', redundancyModel.url)
80 return
81 }
82
83 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(associatedVideo.id)
84
85 const activityBuilder = (audience: ActivityAudience) => {
86 const redundancyObject = redundancyModel.toActivityPubObject()
87 const url = getUpdateActivityPubUrl(redundancyModel.url, redundancyModel.updatedAt.toISOString())
88
89 return buildUpdateActivity(url, byActor, redundancyObject, audience)
90 }
91
92 return sendVideoRelatedActivity(activityBuilder, { byActor, video, contextType: 'CacheFile' })
93 }
94
95 async function sendUpdateVideoPlaylist (videoPlaylist: MVideoPlaylistFull, t: Transaction) {
96 if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
97
98 const byActor = videoPlaylist.OwnerAccount.Actor
99
100 logger.info('Creating job to update video playlist %s.', videoPlaylist.url)
101
102 const url = getUpdateActivityPubUrl(videoPlaylist.url, videoPlaylist.updatedAt.toISOString())
103
104 const object = await videoPlaylist.toActivityPubObject(null, t)
105 const audience = getAudience(byActor, videoPlaylist.privacy === VideoPlaylistPrivacy.PUBLIC)
106
107 const updateActivity = buildUpdateActivity(url, byActor, object, audience)
108
109 const serverActor = await getServerActor()
110 const toFollowersOf = [ byActor, serverActor ]
111
112 if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
113
114 return broadcastToFollowers(updateActivity, byActor, toFollowersOf, t)
115 }
116
117 // ---------------------------------------------------------------------------
118
119 export {
120 sendUpdateActor,
121 sendUpdateVideo,
122 sendUpdateCacheFile,
123 sendUpdateVideoPlaylist
124 }
125
126 // ---------------------------------------------------------------------------
127
128 function buildUpdateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityUpdate {
129 if (!audience) audience = getAudience(byActor)
130
131 return audiencify(
132 {
133 type: 'Update' as 'Update',
134 id: url,
135 actor: byActor.url,
136 object: audiencify(object, audience)
137 },
138 audience
139 )
140 }