]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/activitypub/client.ts
rename manifest
[github/Chocobozzz/PeerTube.git] / server / controllers / activitypub / client.ts
1 // Intercept ActivityPub client requests
2 import * as express from 'express'
3 import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos'
4 import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
5 import { CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers'
6 import { buildAnnounceWithVideoAudience } from '../../lib/activitypub/send'
7 import { audiencify, getAudience } from '../../lib/activitypub/audience'
8 import { buildCreateActivity } from '../../lib/activitypub/send/send-create'
9 import {
10 asyncMiddleware,
11 executeIfActivityPub,
12 localAccountValidator,
13 localVideoChannelValidator,
14 videosCustomGetValidator
15 } from '../../middlewares'
16 import { videosGetValidator, videosShareValidator } from '../../middlewares/validators'
17 import { videoCommentGetValidator } from '../../middlewares/validators/video-comments'
18 import { AccountModel } from '../../models/account/account'
19 import { ActorModel } from '../../models/activitypub/actor'
20 import { ActorFollowModel } from '../../models/activitypub/actor-follow'
21 import { VideoModel } from '../../models/video/video'
22 import { VideoChannelModel } from '../../models/video/video-channel'
23 import { VideoCommentModel } from '../../models/video/video-comment'
24 import { VideoShareModel } from '../../models/video/video-share'
25 import { cacheRoute } from '../../middlewares/cache'
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'
34 import { VideoCaptionModel } from '../../models/video/video-caption'
35 import { videoRedundancyGetValidator } from '../../middlewares/validators/redundancy'
36 import { getServerActor } from '../../helpers/utils'
37
38 const activityPubClientRouter = express.Router()
39
40 activityPubClientRouter.get('/accounts?/:name',
41 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
42 executeIfActivityPub(accountController)
43 )
44 activityPubClientRouter.get('/accounts?/:name/followers',
45 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
46 executeIfActivityPub(asyncMiddleware(accountFollowersController))
47 )
48 activityPubClientRouter.get('/accounts?/:name/following',
49 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
50 executeIfActivityPub(asyncMiddleware(accountFollowingController))
51 )
52
53 activityPubClientRouter.get('/videos/watch/:id',
54 executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))),
55 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
56 executeIfActivityPub(asyncMiddleware(videoController))
57 )
58 activityPubClientRouter.get('/videos/watch/:id/activity',
59 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
60 executeIfActivityPub(asyncMiddleware(videoController))
61 )
62 activityPubClientRouter.get('/videos/watch/:id/announces',
63 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))),
64 executeIfActivityPub(asyncMiddleware(videoAnnouncesController))
65 )
66 activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
67 executeIfActivityPub(asyncMiddleware(videosShareValidator)),
68 executeIfActivityPub(asyncMiddleware(videoAnnounceController))
69 )
70 activityPubClientRouter.get('/videos/watch/:id/likes',
71 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))),
72 executeIfActivityPub(asyncMiddleware(videoLikesController))
73 )
74 activityPubClientRouter.get('/videos/watch/:id/dislikes',
75 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))),
76 executeIfActivityPub(asyncMiddleware(videoDislikesController))
77 )
78 activityPubClientRouter.get('/videos/watch/:id/comments',
79 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))),
80 executeIfActivityPub(asyncMiddleware(videoCommentsController))
81 )
82 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
83 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
84 executeIfActivityPub(asyncMiddleware(videoCommentController))
85 )
86 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
87 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
88 executeIfActivityPub(asyncMiddleware(videoCommentController))
89 )
90
91 activityPubClientRouter.get('/video-channels/:name',
92 executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)),
93 executeIfActivityPub(asyncMiddleware(videoChannelController))
94 )
95 activityPubClientRouter.get('/video-channels/:name/followers',
96 executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)),
97 executeIfActivityPub(asyncMiddleware(videoChannelFollowersController))
98 )
99 activityPubClientRouter.get('/video-channels/:name/following',
100 executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)),
101 executeIfActivityPub(asyncMiddleware(videoChannelFollowingController))
102 )
103
104 activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?',
105 executeIfActivityPub(asyncMiddleware(videoRedundancyGetValidator)),
106 executeIfActivityPub(asyncMiddleware(videoRedundancyController))
107 )
108
109 // ---------------------------------------------------------------------------
110
111 export {
112 activityPubClientRouter
113 }
114
115 // ---------------------------------------------------------------------------
116
117 function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
118 const account: AccountModel = res.locals.account
119
120 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
121 }
122
123 async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
124 const account: AccountModel = res.locals.account
125 const activityPubResult = await actorFollowers(req, account.Actor)
126
127 return activityPubResponse(activityPubContextify(activityPubResult), res)
128 }
129
130 async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
131 const account: AccountModel = res.locals.account
132 const activityPubResult = await actorFollowing(req, account.Actor)
133
134 return activityPubResponse(activityPubContextify(activityPubResult), res)
135 }
136
137 async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) {
138 const video: VideoModel = res.locals.video
139
140 // We need captions to render AP object
141 video.VideoCaptions = await VideoCaptionModel.listVideoCaptions(video.id)
142
143 const audience = getAudience(video.VideoChannel.Account.Actor, video.privacy === VideoPrivacy.PUBLIC)
144 const videoObject = audiencify(video.toActivityPubObject(), audience)
145
146 if (req.path.endsWith('/activity')) {
147 const data = buildCreateActivity(video.url, video.VideoChannel.Account.Actor, videoObject, audience)
148 return activityPubResponse(activityPubContextify(data), res)
149 }
150
151 return activityPubResponse(activityPubContextify(videoObject), res)
152 }
153
154 async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) {
155 const share = res.locals.videoShare as VideoShareModel
156 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.video, undefined)
157
158 return activityPubResponse(activityPubContextify(activity), res)
159 }
160
161 async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) {
162 const video: VideoModel = res.locals.video
163
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)
172
173 return activityPubResponse(activityPubContextify(json), res)
174 }
175
176 async function videoLikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
177 const video: VideoModel = res.locals.video
178 const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))
179
180 return activityPubResponse(activityPubContextify(json), res)
181 }
182
183 async function videoDislikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
184 const video: VideoModel = res.locals.video
185 const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))
186
187 return activityPubResponse(activityPubContextify(json), res)
188 }
189
190 async function videoCommentsController (req: express.Request, res: express.Response, next: express.NextFunction) {
191 const video: VideoModel = res.locals.video
192
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)
201
202 return activityPubResponse(activityPubContextify(json), res)
203 }
204
205 async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) {
206 const videoChannel: VideoChannelModel = res.locals.videoChannel
207
208 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
209 }
210
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
215 return activityPubResponse(activityPubContextify(activityPubResult), res)
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
222 return activityPubResponse(activityPubContextify(activityPubResult), res)
223 }
224
225 async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) {
226 const videoComment: VideoCommentModel = res.locals.videoComment
227
228 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
229 const isPublic = true // Comments are always public
230 const audience = getAudience(videoComment.Account.Actor, isPublic)
231
232 const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
233
234 if (req.path.endsWith('/activity')) {
235 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
236 return activityPubResponse(activityPubContextify(data), res)
237 }
238
239 return activityPubResponse(activityPubContextify(videoCommentObject), res)
240 }
241
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
257 // ---------------------------------------------------------------------------
258
259 async function actorFollowing (req: express.Request, actor: ActorModel) {
260 const handler = (start: number, count: number) => {
261 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
262 }
263
264 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, handler, req.query.page)
265 }
266
267 async function actorFollowers (req: express.Request, actor: ActorModel) {
268 const handler = (start: number, count: number) => {
269 return ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count)
270 }
271
272 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, handler, req.query.page)
273 }
274
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)
284 }