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