]>
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' |
3fd3ab2d | 5 | import { VideoModel } from '../../../models/video/video' |
3fd3ab2d | 6 | import { VideoShareModel } from '../../../models/video/video-share' |
892211e8 | 7 | import { getUpdateActivityPubUrl } from '../url' |
e5565833 C |
8 | import { broadcastToFollowers, sendVideoRelatedActivity } from './utils' |
9 | import { audiencify, getActorsInvolvedInVideo, getAudience } from '../audience' | |
8e0fd45e | 10 | import { logger } from '../../../helpers/logger' |
418d092a | 11 | import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model' |
453e83ea | 12 | import { |
b5fecbf4 | 13 | MAccountDefault, |
453e83ea C |
14 | MActor, |
15 | MActorLight, | |
b5fecbf4 | 16 | MChannelDefault, |
453e83ea C |
17 | MVideoAP, |
18 | MVideoAPWithoutCaption, | |
19 | MVideoPlaylistFull, | |
20 | MVideoRedundancyVideo | |
21 | } from '../../../typings/models' | |
8dc8a34e | 22 | import { getServerActor } from '@server/models/application/application' |
453e83ea C |
23 | |
24 | async 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 | 51 | async 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 | 75 | async 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 | 90 | async 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 | ||
114 | export { | |
2422c46b | 115 | sendUpdateActor, |
c48e82b5 | 116 | sendUpdateVideo, |
418d092a C |
117 | sendUpdateCacheFile, |
118 | sendUpdateVideoPlaylist | |
54141398 C |
119 | } |
120 | ||
121 | // --------------------------------------------------------------------------- | |
122 | ||
453e83ea | 123 | function 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 | } |