]>
Commit | Line | Data |
---|---|---|
e4f97bab C |
1 | // Intercept ActivityPub client requests |
2 | import * as express from 'express' | |
8fffe21a | 3 | import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos' |
d6e99e53 | 4 | import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub' |
8fffe21a | 5 | import { CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers' |
c48e82b5 | 6 | import { buildAnnounceWithVideoAudience } from '../../lib/activitypub/send' |
e251f170 | 7 | import { audiencify, getAudience } from '../../lib/activitypub/audience' |
c48e82b5 | 8 | import { buildCreateActivity } from '../../lib/activitypub/send/send-create' |
96f29c0f C |
9 | import { |
10 | asyncMiddleware, | |
11 | executeIfActivityPub, | |
12 | localAccountValidator, | |
13 | localVideoChannelValidator, | |
14 | videosCustomGetValidator | |
15 | } from '../../middlewares' | |
8a19bee1 | 16 | import { videosGetValidator, videosShareValidator } from '../../middlewares/validators' |
da854ddd | 17 | import { videoCommentGetValidator } from '../../middlewares/validators/video-comments' |
3fd3ab2d | 18 | import { AccountModel } from '../../models/account/account' |
7006bc63 | 19 | import { ActorModel } from '../../models/activitypub/actor' |
50d6de9c | 20 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' |
3fd3ab2d C |
21 | import { VideoModel } from '../../models/video/video' |
22 | import { VideoChannelModel } from '../../models/video/video-channel' | |
da854ddd | 23 | import { VideoCommentModel } from '../../models/video/video-comment' |
3fd3ab2d | 24 | import { VideoShareModel } from '../../models/video/video-share' |
98d3324d | 25 | import { cacheRoute } from '../../middlewares/cache' |
8fffe21a C |
26 | import { activityPubResponse } from './utils' |
27 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' | |
28 | import { | |
29 | getVideoCommentsActivityPubUrl, | |
30 | getVideoDislikesActivityPubUrl, | |
31 | getVideoLikesActivityPubUrl, | |
32 | getVideoSharesActivityPubUrl | |
33 | } from '../../lib/activitypub' | |
40e87e9e | 34 | import { VideoCaptionModel } from '../../models/video/video-caption' |
c48e82b5 C |
35 | import { videoRedundancyGetValidator } from '../../middlewares/validators/redundancy' |
36 | import { getServerActor } from '../../helpers/utils' | |
e4f97bab C |
37 | |
38 | const activityPubClientRouter = express.Router() | |
39 | ||
108af661 | 40 | activityPubClientRouter.get('/accounts?/:name', |
a2431b7d | 41 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
4e50b6a1 | 42 | executeIfActivityPub(accountController) |
e4f97bab | 43 | ) |
7006bc63 | 44 | activityPubClientRouter.get('/accounts?/:name/followers', |
a2431b7d | 45 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
e4f97bab C |
46 | executeIfActivityPub(asyncMiddleware(accountFollowersController)) |
47 | ) | |
7006bc63 | 48 | activityPubClientRouter.get('/accounts?/:name/following', |
a2431b7d | 49 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
e4f97bab C |
50 | executeIfActivityPub(asyncMiddleware(accountFollowingController)) |
51 | ) | |
52 | ||
20494f12 | 53 | activityPubClientRouter.get('/videos/watch/:id', |
98d3324d | 54 | executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))), |
a2431b7d | 55 | executeIfActivityPub(asyncMiddleware(videosGetValidator)), |
da854ddd | 56 | executeIfActivityPub(asyncMiddleware(videoController)) |
4e50b6a1 | 57 | ) |
296c0905 C |
58 | activityPubClientRouter.get('/videos/watch/:id/activity', |
59 | executeIfActivityPub(asyncMiddleware(videosGetValidator)), | |
60 | executeIfActivityPub(asyncMiddleware(videoController)) | |
61 | ) | |
46531a0a | 62 | activityPubClientRouter.get('/videos/watch/:id/announces', |
96f29c0f | 63 | executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))), |
46531a0a C |
64 | executeIfActivityPub(asyncMiddleware(videoAnnouncesController)) |
65 | ) | |
4e50b6a1 C |
66 | activityPubClientRouter.get('/videos/watch/:id/announces/:accountId', |
67 | executeIfActivityPub(asyncMiddleware(videosShareValidator)), | |
68 | executeIfActivityPub(asyncMiddleware(videoAnnounceController)) | |
20494f12 | 69 | ) |
46531a0a | 70 | activityPubClientRouter.get('/videos/watch/:id/likes', |
96f29c0f | 71 | executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))), |
46531a0a C |
72 | executeIfActivityPub(asyncMiddleware(videoLikesController)) |
73 | ) | |
74 | activityPubClientRouter.get('/videos/watch/:id/dislikes', | |
96f29c0f | 75 | executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))), |
46531a0a C |
76 | executeIfActivityPub(asyncMiddleware(videoDislikesController)) |
77 | ) | |
78 | activityPubClientRouter.get('/videos/watch/:id/comments', | |
96f29c0f | 79 | executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))), |
46531a0a C |
80 | executeIfActivityPub(asyncMiddleware(videoCommentsController)) |
81 | ) | |
da854ddd C |
82 | activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId', |
83 | executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)), | |
84 | executeIfActivityPub(asyncMiddleware(videoCommentController)) | |
85 | ) | |
296c0905 C |
86 | activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity', |
87 | executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)), | |
88 | executeIfActivityPub(asyncMiddleware(videoCommentController)) | |
89 | ) | |
da854ddd | 90 | |
8a19bee1 C |
91 | activityPubClientRouter.get('/video-channels/:name', |
92 | executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)), | |
20494f12 C |
93 | executeIfActivityPub(asyncMiddleware(videoChannelController)) |
94 | ) | |
8a19bee1 C |
95 | activityPubClientRouter.get('/video-channels/:name/followers', |
96 | executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)), | |
7006bc63 C |
97 | executeIfActivityPub(asyncMiddleware(videoChannelFollowersController)) |
98 | ) | |
8a19bee1 C |
99 | activityPubClientRouter.get('/video-channels/:name/following', |
100 | executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)), | |
7006bc63 C |
101 | executeIfActivityPub(asyncMiddleware(videoChannelFollowingController)) |
102 | ) | |
20494f12 | 103 | |
c48e82b5 C |
104 | activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?', |
105 | executeIfActivityPub(asyncMiddleware(videoRedundancyGetValidator)), | |
106 | executeIfActivityPub(asyncMiddleware(videoRedundancyController)) | |
107 | ) | |
108 | ||
e4f97bab C |
109 | // --------------------------------------------------------------------------- |
110 | ||
111 | export { | |
112 | activityPubClientRouter | |
113 | } | |
114 | ||
115 | // --------------------------------------------------------------------------- | |
116 | ||
4e50b6a1 | 117 | function accountController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 118 | const account: AccountModel = res.locals.account |
e4f97bab | 119 | |
4b8f09fa | 120 | return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res) |
e4f97bab C |
121 | } |
122 | ||
123 | async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
3fd3ab2d | 124 | const account: AccountModel = res.locals.account |
7006bc63 | 125 | const activityPubResult = await actorFollowers(req, account.Actor) |
e4f97bab | 126 | |
4b8f09fa | 127 | return activityPubResponse(activityPubContextify(activityPubResult), res) |
e4f97bab C |
128 | } |
129 | ||
130 | async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
3fd3ab2d | 131 | const account: AccountModel = res.locals.account |
7006bc63 | 132 | const activityPubResult = await actorFollowing(req, account.Actor) |
e4f97bab | 133 | |
4b8f09fa | 134 | return activityPubResponse(activityPubContextify(activityPubResult), res) |
e4f97bab | 135 | } |
20494f12 | 136 | |
da854ddd | 137 | async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 138 | const video: VideoModel = res.locals.video |
20494f12 | 139 | |
40e87e9e C |
140 | // We need captions to render AP object |
141 | video.VideoCaptions = await VideoCaptionModel.listVideoCaptions(video.id) | |
142 | ||
2186386c | 143 | const audience = getAudience(video.VideoChannel.Account.Actor, video.privacy === VideoPrivacy.PUBLIC) |
8fffe21a | 144 | const videoObject = audiencify(video.toActivityPubObject(), audience) |
2ccaeeb3 | 145 | |
296c0905 | 146 | if (req.path.endsWith('/activity')) { |
c48e82b5 | 147 | const data = buildCreateActivity(video.url, video.VideoChannel.Account.Actor, videoObject, audience) |
4b8f09fa | 148 | return activityPubResponse(activityPubContextify(data), res) |
296c0905 C |
149 | } |
150 | ||
4b8f09fa | 151 | return activityPubResponse(activityPubContextify(videoObject), res) |
20494f12 C |
152 | } |
153 | ||
4e50b6a1 | 154 | async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 155 | const share = res.locals.videoShare as VideoShareModel |
c48e82b5 | 156 | const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.video, undefined) |
4e50b6a1 | 157 | |
c48e82b5 | 158 | return activityPubResponse(activityPubContextify(activity), res) |
4e50b6a1 C |
159 | } |
160 | ||
46531a0a C |
161 | async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) { |
162 | const video: VideoModel = res.locals.video | |
163 | ||
8fffe21a C |
164 | const handler = async (start: number, count: number) => { |
165 | const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count) | |
166 | return { | |
167 | total: result.count, | |
168 | data: result.rows.map(r => r.url) | |
169 | } | |
170 | } | |
171 | const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page) | |
46531a0a | 172 | |
8fffe21a | 173 | return activityPubResponse(activityPubContextify(json), res) |
46531a0a C |
174 | } |
175 | ||
176 | async function videoLikesController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
177 | const video: VideoModel = res.locals.video | |
8fffe21a | 178 | const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video)) |
46531a0a | 179 | |
8fffe21a | 180 | return activityPubResponse(activityPubContextify(json), res) |
46531a0a C |
181 | } |
182 | ||
183 | async function videoDislikesController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
184 | const video: VideoModel = res.locals.video | |
8fffe21a | 185 | const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video)) |
46531a0a | 186 | |
8fffe21a | 187 | return activityPubResponse(activityPubContextify(json), res) |
46531a0a C |
188 | } |
189 | ||
190 | async function videoCommentsController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
191 | const video: VideoModel = res.locals.video | |
192 | ||
8fffe21a C |
193 | const handler = async (start: number, count: number) => { |
194 | const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count) | |
195 | return { | |
196 | total: result.count, | |
197 | data: result.rows.map(r => r.url) | |
198 | } | |
199 | } | |
200 | const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page) | |
46531a0a | 201 | |
8fffe21a | 202 | return activityPubResponse(activityPubContextify(json), res) |
46531a0a C |
203 | } |
204 | ||
20494f12 | 205 | async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 206 | const videoChannel: VideoChannelModel = res.locals.videoChannel |
20494f12 | 207 | |
4b8f09fa | 208 | return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res) |
20494f12 | 209 | } |
da854ddd | 210 | |
7006bc63 C |
211 | async function videoChannelFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) { |
212 | const videoChannel: VideoChannelModel = res.locals.videoChannel | |
213 | const activityPubResult = await actorFollowers(req, videoChannel.Actor) | |
214 | ||
4b8f09fa | 215 | return activityPubResponse(activityPubContextify(activityPubResult), res) |
7006bc63 C |
216 | } |
217 | ||
218 | async function videoChannelFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
219 | const videoChannel: VideoChannelModel = res.locals.videoChannel | |
220 | const activityPubResult = await actorFollowing(req, videoChannel.Actor) | |
221 | ||
4b8f09fa | 222 | return activityPubResponse(activityPubContextify(activityPubResult), res) |
7006bc63 C |
223 | } |
224 | ||
da854ddd C |
225 | async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) { |
226 | const videoComment: VideoCommentModel = res.locals.videoComment | |
227 | ||
d7e70384 | 228 | const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined) |
d6e99e53 | 229 | const isPublic = true // Comments are always public |
2186386c | 230 | const audience = getAudience(videoComment.Account.Actor, isPublic) |
d6e99e53 C |
231 | |
232 | const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience) | |
233 | ||
296c0905 | 234 | if (req.path.endsWith('/activity')) { |
c48e82b5 | 235 | const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience) |
4b8f09fa | 236 | return activityPubResponse(activityPubContextify(data), res) |
296c0905 C |
237 | } |
238 | ||
4b8f09fa | 239 | return activityPubResponse(activityPubContextify(videoCommentObject), res) |
da854ddd | 240 | } |
7006bc63 | 241 | |
c48e82b5 C |
242 | async function videoRedundancyController (req: express.Request, res: express.Response) { |
243 | const videoRedundancy = res.locals.videoRedundancy | |
244 | const serverActor = await getServerActor() | |
245 | ||
246 | const audience = getAudience(serverActor) | |
247 | const object = audiencify(videoRedundancy.toActivityPubObject(), audience) | |
248 | ||
249 | if (req.path.endsWith('/activity')) { | |
250 | const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience) | |
251 | return activityPubResponse(activityPubContextify(data), res) | |
252 | } | |
253 | ||
254 | return activityPubResponse(activityPubContextify(object), res) | |
255 | } | |
256 | ||
7006bc63 C |
257 | // --------------------------------------------------------------------------- |
258 | ||
259 | async function actorFollowing (req: express.Request, actor: ActorModel) { | |
8fffe21a C |
260 | const handler = (start: number, count: number) => { |
261 | return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count) | |
262 | } | |
7006bc63 | 263 | |
8fffe21a | 264 | return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, handler, req.query.page) |
7006bc63 C |
265 | } |
266 | ||
267 | async function actorFollowers (req: express.Request, actor: ActorModel) { | |
8fffe21a C |
268 | const handler = (start: number, count: number) => { |
269 | return ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count) | |
270 | } | |
7006bc63 | 271 | |
8fffe21a | 272 | return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, handler, req.query.page) |
7006bc63 | 273 | } |
4b8f09fa | 274 | |
8fffe21a C |
275 | function videoRates (req: express.Request, rateType: VideoRateType, video: VideoModel, url: string) { |
276 | const handler = async (start: number, count: number) => { | |
277 | const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count) | |
278 | return { | |
279 | total: result.count, | |
280 | data: result.rows.map(r => r.Account.Actor.url) | |
281 | } | |
282 | } | |
283 | return activityPubCollectionPagination(url, handler, req.query.page) | |
4b8f09fa | 284 | } |