]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-create.ts
Federate video views
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-create.ts
1 import { Transaction } from 'sequelize'
2 import { ActivityCreate } from '../../../../shared/models/activitypub/activity'
3 import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../../models'
4 import { VideoAbuseInstance } from '../../../models/video/video-abuse-interface'
5 import { broadcastToFollowers, getAudience, unicastTo } from './misc'
6 import { getVideoAbuseActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
7 import { getServerAccount } from '../../../helpers/utils'
8 import { database as db } from '../../../initializers'
9
10 async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) {
11 const byAccount = videoChannel.Account
12
13 const videoChannelObject = videoChannel.toActivityPubObject()
14 const data = await createActivityData(videoChannel.url, byAccount, videoChannelObject)
15
16 return broadcastToFollowers(data, byAccount, [ byAccount ], t)
17 }
18
19 async function sendVideoAbuse (byAccount: AccountInstance, videoAbuse: VideoAbuseInstance, video: VideoInstance, t: Transaction) {
20 const url = getVideoAbuseActivityPubUrl(videoAbuse)
21
22 const audience = { to: [ video.VideoChannel.Account.url ], cc: [] }
23 const data = await createActivityData(url, byAccount, videoAbuse.toActivityPubObject(), audience)
24
25 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
26 }
27
28 async function sendCreateViewToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
29 const url = getVideoViewActivityPubUrl(byAccount, video)
30 const viewActivity = createViewActivityData(byAccount, video)
31
32 const audience = { to: [ video.VideoChannel.Account.url ], cc: [ video.VideoChannel.Account.url + '/followers' ] }
33 const data = await createActivityData(url, byAccount, viewActivity, audience)
34
35 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
36 }
37
38 async function sendCreateViewToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
39 const url = getVideoViewActivityPubUrl(byAccount, video)
40 const viewActivity = createViewActivityData(byAccount, video)
41
42 const audience = { to: [ video.VideoChannel.Account.url + '/followers' ], cc: [] }
43 const data = await createActivityData(url, byAccount, viewActivity, audience)
44
45 const serverAccount = await getServerAccount()
46 const accountsToForwardView = await db.VideoShare.loadAccountsByShare(video.id)
47 accountsToForwardView.push(video.VideoChannel.Account)
48
49 // Don't forward view to server that sent it to us
50 const index = accountsToForwardView.findIndex(a => a.id === byAccount.id)
51 if (index) accountsToForwardView.splice(index, 1)
52
53 const followersException = [ byAccount ]
54 return broadcastToFollowers(data, serverAccount, accountsToForwardView, t, followersException)
55 }
56
57 async function createActivityData (url: string, byAccount: AccountInstance, object: any, audience?: { to: string[], cc: string[] }) {
58 if (!audience) {
59 audience = await getAudience(byAccount)
60 }
61
62 const activity: ActivityCreate = {
63 type: 'Create',
64 id: url,
65 actor: byAccount.url,
66 to: audience.to,
67 cc: audience.cc,
68 object
69 }
70
71 return activity
72 }
73
74 // ---------------------------------------------------------------------------
75
76 export {
77 sendCreateVideoChannel,
78 sendVideoAbuse,
79 createActivityData,
80 sendCreateViewToOrigin,
81 sendCreateViewToVideoFollowers
82 }
83
84 // ---------------------------------------------------------------------------
85
86 function createViewActivityData (byAccount: AccountInstance, video: VideoInstance) {
87 const obj = {
88 type: 'View',
89 actor: byAccount.url,
90 object: video.url
91 }
92
93 return obj
94 }