]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-update.ts
d64b88343b73c4bafc9cb03cd0e754dcc10adc90
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-update.ts
1 import { Transaction } from 'sequelize'
2 import { ActivityAudience, ActivityUpdate } from '../../../../shared/models/activitypub'
3 import { VideoPrivacy } from '../../../../shared/models/videos'
4 import { AccountModel } from '../../../models/account/account'
5 import { ActorModel } from '../../../models/activitypub/actor'
6 import { VideoModel } from '../../../models/video/video'
7 import { VideoChannelModel } from '../../../models/video/video-channel'
8 import { VideoShareModel } from '../../../models/video/video-share'
9 import { getUpdateActivityPubUrl } from '../url'
10 import { broadcastToFollowers } from './utils'
11 import { audiencify, getAudience } from '../audience'
12
13 async function sendUpdateVideo (video: VideoModel, t: Transaction) {
14 const byActor = video.VideoChannel.Account.Actor
15
16 const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString())
17 const videoObject = video.toActivityPubObject()
18 const audience = await getAudience(byActor, t, video.privacy === VideoPrivacy.PUBLIC)
19
20 const data = await updateActivityData(url, byActor, videoObject, t, audience)
21
22 const actorsInvolved = await VideoShareModel.loadActorsByShare(video.id, t)
23 actorsInvolved.push(byActor)
24
25 return broadcastToFollowers(data, byActor, actorsInvolved, t)
26 }
27
28 async function sendUpdateActor (accountOrChannel: AccountModel | VideoChannelModel, t: Transaction) {
29 const byActor = accountOrChannel.Actor
30
31 const url = getUpdateActivityPubUrl(byActor.url, byActor.updatedAt.toISOString())
32 const accountOrChannelObject = accountOrChannel.toActivityPubObject()
33 const audience = await getAudience(byActor, t)
34 const data = await updateActivityData(url, byActor, accountOrChannelObject, t, audience)
35
36 let actorsInvolved: ActorModel[]
37 if (accountOrChannel instanceof AccountModel) {
38 // Actors that shared my videos are involved too
39 actorsInvolved = await VideoShareModel.loadActorsByVideoOwner(byActor.id, t)
40 } else {
41 // Actors that shared videos of my channel are involved too
42 actorsInvolved = await VideoShareModel.loadActorsByVideoChannel(accountOrChannel.id, t)
43 }
44
45 actorsInvolved.push(byActor)
46
47 return broadcastToFollowers(data, byActor, actorsInvolved, t)
48 }
49
50 // ---------------------------------------------------------------------------
51
52 export {
53 sendUpdateActor,
54 sendUpdateVideo
55 }
56
57 // ---------------------------------------------------------------------------
58
59 async function updateActivityData (
60 url: string,
61 byActor: ActorModel,
62 object: any,
63 t: Transaction,
64 audience?: ActivityAudience
65 ): Promise<ActivityUpdate> {
66 if (!audience) {
67 audience = await getAudience(byActor, t)
68 }
69
70 return audiencify({
71 type: 'Update' as 'Update',
72 id: url,
73 actor: byActor.url,
74 object: audiencify(object, audience)
75 }, audience)
76 }