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