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