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