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