aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/send/send-create.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-22 16:25:03 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:53 +0100
commit40ff57078e15d5b86ee6b71e198b95d3feb78eaf (patch)
tree88031d4eac6a26597e8a1f2fc63674664e3eae26 /server/lib/activitypub/send/send-create.ts
parentc46edbc2f6ca310b2f0331f979ac6caf27f6eb92 (diff)
downloadPeerTube-40ff57078e15d5b86ee6b71e198b95d3feb78eaf.tar.gz
PeerTube-40ff57078e15d5b86ee6b71e198b95d3feb78eaf.tar.zst
PeerTube-40ff57078e15d5b86ee6b71e198b95d3feb78eaf.zip
Federate video views
Diffstat (limited to 'server/lib/activitypub/send/send-create.ts')
-rw-r--r--server/lib/activitypub/send/send-create.ts64
1 files changed, 56 insertions, 8 deletions
diff --git a/server/lib/activitypub/send/send-create.ts b/server/lib/activitypub/send/send-create.ts
index df8e0a642..e5fb212b7 100644
--- a/server/lib/activitypub/send/send-create.ts
+++ b/server/lib/activitypub/send/send-create.ts
@@ -3,7 +3,9 @@ import { ActivityCreate } from '../../../../shared/models/activitypub/activity'
3import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../../models' 3import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../../models'
4import { VideoAbuseInstance } from '../../../models/video/video-abuse-interface' 4import { VideoAbuseInstance } from '../../../models/video/video-abuse-interface'
5import { broadcastToFollowers, getAudience, unicastTo } from './misc' 5import { broadcastToFollowers, getAudience, unicastTo } from './misc'
6import { getVideoAbuseActivityPubUrl } from '../url' 6import { getVideoAbuseActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
7import { getServerAccount } from '../../../helpers/utils'
8import { database as db } from '../../../initializers'
7 9
8async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) { 10async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) {
9 const byAccount = videoChannel.Account 11 const byAccount = videoChannel.Account
@@ -16,21 +18,53 @@ async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Tr
16 18
17async function sendVideoAbuse (byAccount: AccountInstance, videoAbuse: VideoAbuseInstance, video: VideoInstance, t: Transaction) { 19async function sendVideoAbuse (byAccount: AccountInstance, videoAbuse: VideoAbuseInstance, video: VideoInstance, t: Transaction) {
18 const url = getVideoAbuseActivityPubUrl(videoAbuse) 20 const url = getVideoAbuseActivityPubUrl(videoAbuse)
19 const data = await createActivityData(url, byAccount, videoAbuse.toActivityPubObject()) 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)
20 34
21 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) 35 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
22} 36}
23 37
24// async function sendCreateView () 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 }
25 61
26async function createActivityData (url: string, byAccount: AccountInstance, object: any) {
27 const { to, cc } = await getAudience(byAccount)
28 const activity: ActivityCreate = { 62 const activity: ActivityCreate = {
29 type: 'Create', 63 type: 'Create',
30 id: url, 64 id: url,
31 actor: byAccount.url, 65 actor: byAccount.url,
32 to, 66 to: audience.to,
33 cc, 67 cc: audience.cc,
34 object 68 object
35 } 69 }
36 70
@@ -42,5 +76,19 @@ async function createActivityData (url: string, byAccount: AccountInstance, obje
42export { 76export {
43 sendCreateVideoChannel, 77 sendCreateVideoChannel,
44 sendVideoAbuse, 78 sendVideoAbuse,
45 createActivityData 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
46} 94}