]>
Commit | Line | Data |
---|---|---|
e4f97bab C |
1 | import * as Sequelize from 'sequelize' |
2 | ||
571389d4 | 3 | import { database as db } from '../../initializers' |
e4f97bab C |
4 | import { |
5 | AccountInstance, | |
6 | VideoInstance, | |
7 | VideoChannelInstance | |
8 | } from '../../models' | |
9 | import { httpRequestJobScheduler } from '../jobs' | |
10 | import { signObject, activityPubContextify } from '../../helpers' | |
11 | import { Activity } from '../../../shared' | |
8e13fa7d C |
12 | import { VideoAbuseInstance } from '../../models/video/video-abuse-interface' |
13 | import { getActivityPubUrl } from '../../helpers/activitypub' | |
e4f97bab | 14 | |
350e31d6 | 15 | async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) { |
e4f97bab | 16 | const videoChannelObject = videoChannel.toActivityPubObject() |
350e31d6 | 17 | const data = await createActivityData(videoChannel.url, videoChannel.Account, videoChannelObject) |
e4f97bab | 18 | |
571389d4 | 19 | return broadcastToFollowers(data, videoChannel.Account, t) |
e4f97bab C |
20 | } |
21 | ||
350e31d6 | 22 | async function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) { |
e4f97bab | 23 | const videoChannelObject = videoChannel.toActivityPubObject() |
350e31d6 | 24 | const data = await updateActivityData(videoChannel.url, videoChannel.Account, videoChannelObject) |
e4f97bab | 25 | |
571389d4 | 26 | return broadcastToFollowers(data, videoChannel.Account, t) |
e4f97bab C |
27 | } |
28 | ||
350e31d6 C |
29 | async function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) { |
30 | const data = await deleteActivityData(videoChannel.url, videoChannel.Account) | |
e4f97bab | 31 | |
571389d4 | 32 | return broadcastToFollowers(data, videoChannel.Account, t) |
e4f97bab C |
33 | } |
34 | ||
350e31d6 | 35 | async function sendAddVideo (video: VideoInstance, t: Sequelize.Transaction) { |
e4f97bab | 36 | const videoObject = video.toActivityPubObject() |
350e31d6 | 37 | const data = await addActivityData(video.url, video.VideoChannel.Account, video.VideoChannel.url, videoObject) |
e4f97bab | 38 | |
571389d4 | 39 | return broadcastToFollowers(data, video.VideoChannel.Account, t) |
e4f97bab C |
40 | } |
41 | ||
350e31d6 | 42 | async function sendUpdateVideo (video: VideoInstance, t: Sequelize.Transaction) { |
e4f97bab | 43 | const videoObject = video.toActivityPubObject() |
350e31d6 | 44 | const data = await updateActivityData(video.url, video.VideoChannel.Account, videoObject) |
e4f97bab | 45 | |
571389d4 | 46 | return broadcastToFollowers(data, video.VideoChannel.Account, t) |
e4f97bab C |
47 | } |
48 | ||
350e31d6 C |
49 | async function sendDeleteVideo (video: VideoInstance, t: Sequelize.Transaction) { |
50 | const data = await deleteActivityData(video.url, video.VideoChannel.Account) | |
e4f97bab | 51 | |
571389d4 | 52 | return broadcastToFollowers(data, video.VideoChannel.Account, t) |
e4f97bab C |
53 | } |
54 | ||
350e31d6 C |
55 | async function sendDeleteAccount (account: AccountInstance, t: Sequelize.Transaction) { |
56 | const data = await deleteActivityData(account.url, account) | |
7a7724e6 C |
57 | |
58 | return broadcastToFollowers(data, account, t) | |
59 | } | |
60 | ||
8e13fa7d C |
61 | async function sendVideoAbuse ( |
62 | fromAccount: AccountInstance, | |
63 | videoAbuse: VideoAbuseInstance, | |
64 | video: VideoInstance, | |
65 | t: Sequelize.Transaction | |
66 | ) { | |
67 | const url = getActivityPubUrl('videoAbuse', videoAbuse.id.toString()) | |
68 | const data = await createActivityData(url, fromAccount, video.url) | |
69 | ||
70 | return unicastTo(data, video.VideoChannel.Account.sharedInboxUrl, t) | |
71 | } | |
72 | ||
350e31d6 C |
73 | async function sendAccept (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) { |
74 | const data = await acceptActivityData(fromAccount) | |
ce548a10 | 75 | |
8e13fa7d | 76 | return unicastTo(data, toAccount.inboxUrl, t) |
ce548a10 C |
77 | } |
78 | ||
350e31d6 C |
79 | async function sendFollow (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) { |
80 | const data = await followActivityData(toAccount.url, fromAccount) | |
ce548a10 | 81 | |
8e13fa7d | 82 | return unicastTo(data, toAccount.inboxUrl, t) |
ce548a10 C |
83 | } |
84 | ||
e4f97bab C |
85 | // --------------------------------------------------------------------------- |
86 | ||
87 | export { | |
571389d4 C |
88 | sendCreateVideoChannel, |
89 | sendUpdateVideoChannel, | |
90 | sendDeleteVideoChannel, | |
91 | sendAddVideo, | |
92 | sendUpdateVideo, | |
7a7724e6 | 93 | sendDeleteVideo, |
ce548a10 C |
94 | sendDeleteAccount, |
95 | sendAccept, | |
8e13fa7d C |
96 | sendFollow, |
97 | sendVideoAbuse | |
e4f97bab C |
98 | } |
99 | ||
100 | // --------------------------------------------------------------------------- | |
101 | ||
571389d4 | 102 | async function broadcastToFollowers (data: any, fromAccount: AccountInstance, t: Sequelize.Transaction) { |
51548b31 | 103 | const result = await db.AccountFollow.listAcceptedFollowerUrlsForApi(fromAccount.id, 0) |
571389d4 C |
104 | |
105 | const jobPayload = { | |
106 | uris: result.data, | |
107 | body: data | |
108 | } | |
109 | ||
110 | return httpRequestJobScheduler.createJob(t, 'httpRequestBroadcastHandler', jobPayload) | |
e4f97bab C |
111 | } |
112 | ||
8e13fa7d | 113 | async function unicastTo (data: any, toAccountUrl: string, t: Sequelize.Transaction) { |
ce548a10 | 114 | const jobPayload = { |
8e13fa7d | 115 | uris: [ toAccountUrl ], |
ce548a10 C |
116 | body: data |
117 | } | |
118 | ||
119 | return httpRequestJobScheduler.createJob(t, 'httpRequestUnicastHandler', jobPayload) | |
120 | } | |
121 | ||
e4f97bab C |
122 | function buildSignedActivity (byAccount: AccountInstance, data: Object) { |
123 | const activity = activityPubContextify(data) | |
124 | ||
125 | return signObject(byAccount, activity) as Promise<Activity> | |
126 | } | |
127 | ||
128 | async function getPublicActivityTo (account: AccountInstance) { | |
129 | const inboxUrls = await account.getFollowerSharedInboxUrls() | |
130 | ||
131 | return inboxUrls.concat('https://www.w3.org/ns/activitystreams#Public') | |
132 | } | |
133 | ||
134 | async function createActivityData (url: string, byAccount: AccountInstance, object: any) { | |
135 | const to = await getPublicActivityTo(byAccount) | |
136 | const base = { | |
137 | type: 'Create', | |
138 | id: url, | |
139 | actor: byAccount.url, | |
140 | to, | |
141 | object | |
142 | } | |
143 | ||
144 | return buildSignedActivity(byAccount, base) | |
145 | } | |
146 | ||
147 | async function updateActivityData (url: string, byAccount: AccountInstance, object: any) { | |
148 | const to = await getPublicActivityTo(byAccount) | |
149 | const base = { | |
150 | type: 'Update', | |
151 | id: url, | |
152 | actor: byAccount.url, | |
153 | to, | |
154 | object | |
155 | } | |
156 | ||
157 | return buildSignedActivity(byAccount, base) | |
158 | } | |
159 | ||
7a7724e6 | 160 | async function deleteActivityData (url: string, byAccount: AccountInstance) { |
e4f97bab | 161 | const base = { |
7a7724e6 | 162 | type: 'Delete', |
e4f97bab | 163 | id: url, |
7a7724e6 | 164 | actor: byAccount.url |
e4f97bab C |
165 | } |
166 | ||
167 | return buildSignedActivity(byAccount, base) | |
168 | } | |
169 | ||
170 | async function addActivityData (url: string, byAccount: AccountInstance, target: string, object: any) { | |
171 | const to = await getPublicActivityTo(byAccount) | |
172 | const base = { | |
173 | type: 'Add', | |
174 | id: url, | |
175 | actor: byAccount.url, | |
176 | to, | |
177 | object, | |
178 | target | |
179 | } | |
180 | ||
181 | return buildSignedActivity(byAccount, base) | |
182 | } | |
ce548a10 C |
183 | |
184 | async function followActivityData (url: string, byAccount: AccountInstance) { | |
185 | const base = { | |
186 | type: 'Follow', | |
187 | id: byAccount.url, | |
188 | actor: byAccount.url, | |
189 | object: url | |
190 | } | |
191 | ||
192 | return buildSignedActivity(byAccount, base) | |
193 | } | |
194 | ||
195 | async function acceptActivityData (byAccount: AccountInstance) { | |
196 | const base = { | |
197 | type: 'Accept', | |
198 | id: byAccount.url, | |
199 | actor: byAccount.url | |
200 | } | |
201 | ||
202 | return buildSignedActivity(byAccount, base) | |
203 | } |