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