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