]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/send-create.ts
Federate video views
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-create.ts
CommitLineData
54141398
C
1import { Transaction } from 'sequelize'
2import { ActivityCreate } from '../../../../shared/models/activitypub/activity'
3import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../../models'
4import { VideoAbuseInstance } from '../../../models/video/video-abuse-interface'
5import { broadcastToFollowers, getAudience, unicastTo } from './misc'
40ff5707
C
6import { getVideoAbuseActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
7import { getServerAccount } from '../../../helpers/utils'
8import { database as db } from '../../../initializers'
54141398
C
9
10async 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
19async function sendVideoAbuse (byAccount: AccountInstance, videoAbuse: VideoAbuseInstance, video: VideoInstance, t: Transaction) {
20 const url = getVideoAbuseActivityPubUrl(videoAbuse)
40ff5707
C
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
28async 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)
54141398
C
34
35 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
36}
37
40ff5707
C
38async 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
57async function createActivityData (url: string, byAccount: AccountInstance, object: any, audience?: { to: string[], cc: string[] }) {
58 if (!audience) {
59 audience = await getAudience(byAccount)
60 }
c986175d 61
54141398
C
62 const activity: ActivityCreate = {
63 type: 'Create',
64 id: url,
65 actor: byAccount.url,
40ff5707
C
66 to: audience.to,
67 cc: audience.cc,
54141398
C
68 object
69 }
70
71 return activity
72}
73
74// ---------------------------------------------------------------------------
75
76export {
77 sendCreateVideoChannel,
78 sendVideoAbuse,
40ff5707
C
79 createActivityData,
80 sendCreateViewToOrigin,
81 sendCreateViewToVideoFollowers
82}
83
84// ---------------------------------------------------------------------------
85
86function createViewActivityData (byAccount: AccountInstance, video: VideoInstance) {
87 const obj = {
88 type: 'View',
89 actor: byAccount.url,
90 object: video.url
91 }
92
93 return obj
54141398 94}