]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/send-update.ts
Add hls support on server
[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'
50d6de9c 5import { ActorModel } from '../../../models/activitypub/actor'
3fd3ab2d 6import { VideoModel } from '../../../models/video/video'
2422c46b 7import { VideoChannelModel } from '../../../models/video/video-channel'
3fd3ab2d 8import { VideoShareModel } from '../../../models/video/video-share'
892211e8 9import { getUpdateActivityPubUrl } from '../url'
e5565833
C
10import { broadcastToFollowers, sendVideoRelatedActivity } from './utils'
11import { audiencify, getActorsInvolvedInVideo, getAudience } from '../audience'
8e0fd45e 12import { logger } from '../../../helpers/logger'
5cf84858 13import { VideoCaptionModel } from '../../../models/video/video-caption'
c48e82b5 14import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
54141398 15
5cf84858 16async function sendUpdateVideo (video: VideoModel, t: Transaction, overrodeByActor?: ActorModel) {
8e0fd45e
C
17 logger.info('Creating job to update video %s.', video.url)
18
5cf84858 19 const byActor = overrodeByActor ? overrodeByActor : video.VideoChannel.Account.Actor
54141398
C
20
21 const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString())
5cf84858
C
22
23 // Needed to build the AP object
24 if (!video.VideoCaptions) video.VideoCaptions = await video.$get('VideoCaptions') as VideoCaptionModel[]
25
54141398 26 const videoObject = video.toActivityPubObject()
2186386c 27 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
50d6de9c 28
c48e82b5 29 const updateActivity = buildUpdateActivity(url, byActor, videoObject, audience)
54141398 30
c48e82b5
C
31 const actorsInvolved = await getActorsInvolvedInVideo(video, t)
32 if (overrodeByActor) actorsInvolved.push(overrodeByActor)
54141398 33
c48e82b5 34 return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
54141398
C
35}
36
2422c46b
C
37async function sendUpdateActor (accountOrChannel: AccountModel | VideoChannelModel, t: Transaction) {
38 const byActor = accountOrChannel.Actor
265ba139 39
8e0fd45e
C
40 logger.info('Creating job to update actor %s.', byActor.url)
41
265ba139 42 const url = getUpdateActivityPubUrl(byActor.url, byActor.updatedAt.toISOString())
2422c46b 43 const accountOrChannelObject = accountOrChannel.toActivityPubObject()
2186386c 44 const audience = getAudience(byActor)
c48e82b5 45 const updateActivity = buildUpdateActivity(url, byActor, accountOrChannelObject, audience)
2422c46b
C
46
47 let actorsInvolved: ActorModel[]
48 if (accountOrChannel instanceof AccountModel) {
49 // Actors that shared my videos are involved too
50 actorsInvolved = await VideoShareModel.loadActorsByVideoOwner(byActor.id, t)
51 } else {
52 // Actors that shared videos of my channel are involved too
53 actorsInvolved = await VideoShareModel.loadActorsByVideoChannel(accountOrChannel.id, t)
54 }
265ba139 55
265ba139
C
56 actorsInvolved.push(byActor)
57
c48e82b5
C
58 return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
59}
60
61async function sendUpdateCacheFile (byActor: ActorModel, redundancyModel: VideoRedundancyModel) {
62 logger.info('Creating job to update cache file %s.', redundancyModel.url)
63
09209296 64 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(redundancyModel.getVideo().id)
c48e82b5 65
a2377d15
C
66 const activityBuilder = (audience: ActivityAudience) => {
67 const redundancyObject = redundancyModel.toActivityPubObject()
68 const url = getUpdateActivityPubUrl(redundancyModel.url, redundancyModel.updatedAt.toISOString())
c48e82b5 69
a2377d15
C
70 return buildUpdateActivity(url, byActor, redundancyObject, audience)
71 }
c48e82b5 72
a2377d15 73 return sendVideoRelatedActivity(activityBuilder, { byActor, video })
265ba139
C
74}
75
54141398
C
76// ---------------------------------------------------------------------------
77
78export {
2422c46b 79 sendUpdateActor,
c48e82b5
C
80 sendUpdateVideo,
81 sendUpdateCacheFile
54141398
C
82}
83
84// ---------------------------------------------------------------------------
85
c48e82b5 86function buildUpdateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityUpdate {
2186386c 87 if (!audience) audience = getAudience(byActor)
50d6de9c 88
2186386c
C
89 return audiencify(
90 {
91 type: 'Update' as 'Update',
92 id: url,
93 actor: byActor.url,
94 object: audiencify(object, audience
95 )
96 },
97 audience
98 )
54141398 99}