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