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