]>
Commit | Line | Data |
---|---|---|
54141398 | 1 | import { Transaction } from 'sequelize' |
63c93323 | 2 | import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub/activity' |
0032ebe9 | 3 | import { getServerAccount } from '../../../helpers/utils' |
54141398 C |
4 | import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../../models' |
5 | import { VideoAbuseInstance } from '../../../models/video/video-abuse-interface' | |
0032ebe9 C |
6 | import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url' |
7 | import { | |
8 | broadcastToFollowers, | |
63c93323 | 9 | getAccountsInvolvedInVideo, |
0032ebe9 C |
10 | getAudience, |
11 | getOriginVideoAudience, | |
4e50b6a1 | 12 | getObjectFollowersAudience, |
0032ebe9 C |
13 | unicastTo |
14 | } from './misc' | |
54141398 C |
15 | |
16 | async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) { | |
17 | const byAccount = videoChannel.Account | |
18 | ||
19 | const videoChannelObject = videoChannel.toActivityPubObject() | |
20 | const data = await createActivityData(videoChannel.url, byAccount, videoChannelObject) | |
21 | ||
22 | return broadcastToFollowers(data, byAccount, [ byAccount ], t) | |
23 | } | |
24 | ||
25 | async function sendVideoAbuse (byAccount: AccountInstance, videoAbuse: VideoAbuseInstance, video: VideoInstance, t: Transaction) { | |
26 | const url = getVideoAbuseActivityPubUrl(videoAbuse) | |
40ff5707 C |
27 | |
28 | const audience = { to: [ video.VideoChannel.Account.url ], cc: [] } | |
29 | const data = await createActivityData(url, byAccount, videoAbuse.toActivityPubObject(), audience) | |
30 | ||
31 | return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) | |
32 | } | |
33 | ||
34 | async function sendCreateViewToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { | |
35 | const url = getVideoViewActivityPubUrl(byAccount, video) | |
36 | const viewActivity = createViewActivityData(byAccount, video) | |
37 | ||
63c93323 C |
38 | const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video) |
39 | const audience = getOriginVideoAudience(video, accountsInvolvedInVideo) | |
40ff5707 | 40 | const data = await createActivityData(url, byAccount, viewActivity, audience) |
54141398 C |
41 | |
42 | return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) | |
43 | } | |
44 | ||
40ff5707 C |
45 | async function sendCreateViewToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { |
46 | const url = getVideoViewActivityPubUrl(byAccount, video) | |
47 | const viewActivity = createViewActivityData(byAccount, video) | |
48 | ||
63c93323 | 49 | const accountsToForwardView = await getAccountsInvolvedInVideo(video) |
4e50b6a1 | 50 | const audience = getObjectFollowersAudience(accountsToForwardView) |
40ff5707 C |
51 | const data = await createActivityData(url, byAccount, viewActivity, audience) |
52 | ||
63c93323 | 53 | // Use the server account to send the view, because it could be an unregistered account |
40ff5707 | 54 | const serverAccount = await getServerAccount() |
0032ebe9 C |
55 | const followersException = [ byAccount ] |
56 | return broadcastToFollowers(data, serverAccount, accountsToForwardView, t, followersException) | |
57 | } | |
58 | ||
59 | async function sendCreateDislikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { | |
60 | const url = getVideoDislikeActivityPubUrl(byAccount, video) | |
61 | const dislikeActivity = createDislikeActivityData(byAccount, video) | |
62 | ||
63c93323 C |
63 | const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video) |
64 | const audience = getOriginVideoAudience(video, accountsInvolvedInVideo) | |
0032ebe9 C |
65 | const data = await createActivityData(url, byAccount, dislikeActivity, audience) |
66 | ||
67 | return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) | |
68 | } | |
40ff5707 | 69 | |
0032ebe9 C |
70 | async function sendCreateDislikeToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { |
71 | const url = getVideoDislikeActivityPubUrl(byAccount, video) | |
72 | const dislikeActivity = createDislikeActivityData(byAccount, video) | |
73 | ||
63c93323 | 74 | const accountsToForwardView = await getAccountsInvolvedInVideo(video) |
4e50b6a1 | 75 | const audience = getObjectFollowersAudience(accountsToForwardView) |
0032ebe9 C |
76 | const data = await createActivityData(url, byAccount, dislikeActivity, audience) |
77 | ||
40ff5707 | 78 | const followersException = [ byAccount ] |
63c93323 | 79 | return broadcastToFollowers(data, byAccount, accountsToForwardView, t, followersException) |
40ff5707 C |
80 | } |
81 | ||
63c93323 | 82 | async function createActivityData (url: string, byAccount: AccountInstance, object: any, audience?: ActivityAudience) { |
40ff5707 C |
83 | if (!audience) { |
84 | audience = await getAudience(byAccount) | |
85 | } | |
c986175d | 86 | |
54141398 C |
87 | const activity: ActivityCreate = { |
88 | type: 'Create', | |
89 | id: url, | |
90 | actor: byAccount.url, | |
40ff5707 C |
91 | to: audience.to, |
92 | cc: audience.cc, | |
54141398 C |
93 | object |
94 | } | |
95 | ||
96 | return activity | |
97 | } | |
98 | ||
0032ebe9 C |
99 | function createDislikeActivityData (byAccount: AccountInstance, video: VideoInstance) { |
100 | const obj = { | |
101 | type: 'Dislike', | |
102 | actor: byAccount.url, | |
103 | object: video.url | |
104 | } | |
105 | ||
106 | return obj | |
107 | } | |
108 | ||
54141398 C |
109 | // --------------------------------------------------------------------------- |
110 | ||
111 | export { | |
112 | sendCreateVideoChannel, | |
113 | sendVideoAbuse, | |
40ff5707 C |
114 | createActivityData, |
115 | sendCreateViewToOrigin, | |
0032ebe9 C |
116 | sendCreateViewToVideoFollowers, |
117 | sendCreateDislikeToOrigin, | |
118 | sendCreateDislikeToVideoFollowers, | |
119 | createDislikeActivityData | |
40ff5707 C |
120 | } |
121 | ||
122 | // --------------------------------------------------------------------------- | |
123 | ||
124 | function createViewActivityData (byAccount: AccountInstance, video: VideoInstance) { | |
125 | const obj = { | |
126 | type: 'View', | |
127 | actor: byAccount.url, | |
128 | object: video.url | |
129 | } | |
130 | ||
131 | return obj | |
54141398 | 132 | } |