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