]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/activitypub/client.ts
Refractor and optimize AP collections
[github/Chocobozzz/PeerTube.git] / server / controllers / activitypub / client.ts
CommitLineData
e4f97bab
C
1// Intercept ActivityPub client requests
2import * as express from 'express'
8fffe21a 3import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos'
d6e99e53 4import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
8fffe21a 5import { CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers'
07197db4 6import { buildVideoAnnounce } from '../../lib/activitypub/send'
e251f170 7import { audiencify, getAudience } from '../../lib/activitypub/audience'
296c0905 8import { createActivityData } from '../../lib/activitypub/send/send-create'
3fd3ab2d 9import { asyncMiddleware, executeIfActivityPub, localAccountValidator } from '../../middlewares'
50d6de9c 10import { videoChannelsGetValidator, videosGetValidator, videosShareValidator } from '../../middlewares/validators'
da854ddd 11import { videoCommentGetValidator } from '../../middlewares/validators/video-comments'
3fd3ab2d 12import { AccountModel } from '../../models/account/account'
7006bc63 13import { ActorModel } from '../../models/activitypub/actor'
50d6de9c 14import { ActorFollowModel } from '../../models/activitypub/actor-follow'
3fd3ab2d
C
15import { VideoModel } from '../../models/video/video'
16import { VideoChannelModel } from '../../models/video/video-channel'
da854ddd 17import { VideoCommentModel } from '../../models/video/video-comment'
3fd3ab2d 18import { VideoShareModel } from '../../models/video/video-share'
fd4484f1 19import { cacheRoute } from '../../middlewares/cache'
8fffe21a
C
20import { activityPubResponse } from './utils'
21import { AccountVideoRateModel } from '../../models/account/account-video-rate'
22import {
23 getVideoCommentsActivityPubUrl,
24 getVideoDislikesActivityPubUrl,
25 getVideoLikesActivityPubUrl,
26 getVideoSharesActivityPubUrl
27} from '../../lib/activitypub'
e4f97bab
C
28
29const activityPubClientRouter = express.Router()
30
108af661 31activityPubClientRouter.get('/accounts?/:name',
a2431b7d 32 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
4e50b6a1 33 executeIfActivityPub(accountController)
e4f97bab 34)
7006bc63 35activityPubClientRouter.get('/accounts?/:name/followers',
a2431b7d 36 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
e4f97bab
C
37 executeIfActivityPub(asyncMiddleware(accountFollowersController))
38)
7006bc63 39activityPubClientRouter.get('/accounts?/:name/following',
a2431b7d 40 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
e4f97bab
C
41 executeIfActivityPub(asyncMiddleware(accountFollowingController))
42)
43
20494f12 44activityPubClientRouter.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
49activityPubClientRouter.get('/videos/watch/:id/activity',
50 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
51 executeIfActivityPub(asyncMiddleware(videoController))
52)
46531a0a
C
53activityPubClientRouter.get('/videos/watch/:id/announces',
54 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
55 executeIfActivityPub(asyncMiddleware(videoAnnouncesController))
56)
4e50b6a1
C
57activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
58 executeIfActivityPub(asyncMiddleware(videosShareValidator)),
59 executeIfActivityPub(asyncMiddleware(videoAnnounceController))
20494f12 60)
46531a0a
C
61activityPubClientRouter.get('/videos/watch/:id/likes',
62 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
63 executeIfActivityPub(asyncMiddleware(videoLikesController))
64)
65activityPubClientRouter.get('/videos/watch/:id/dislikes',
66 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
67 executeIfActivityPub(asyncMiddleware(videoDislikesController))
68)
69activityPubClientRouter.get('/videos/watch/:id/comments',
70 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
71 executeIfActivityPub(asyncMiddleware(videoCommentsController))
72)
da854ddd
C
73activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
74 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
75 executeIfActivityPub(asyncMiddleware(videoCommentController))
76)
296c0905
C
77activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
78 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
79 executeIfActivityPub(asyncMiddleware(videoCommentController))
80)
da854ddd 81
20494f12 82activityPubClientRouter.get('/video-channels/:id',
a2431b7d 83 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
20494f12
C
84 executeIfActivityPub(asyncMiddleware(videoChannelController))
85)
7006bc63
C
86activityPubClientRouter.get('/video-channels/:id/followers',
87 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
88 executeIfActivityPub(asyncMiddleware(videoChannelFollowersController))
89)
90activityPubClientRouter.get('/video-channels/:id/following',
91 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
92 executeIfActivityPub(asyncMiddleware(videoChannelFollowingController))
93)
20494f12 94
e4f97bab
C
95// ---------------------------------------------------------------------------
96
97export {
98 activityPubClientRouter
99}
100
101// ---------------------------------------------------------------------------
102
4e50b6a1 103function 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
109async 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
116async 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 123async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 124 const video: VideoModel = res.locals.video
20494f12 125
2ccaeeb3 126 const audience = await getAudience(video.VideoChannel.Account.Actor, undefined, video.privacy === VideoPrivacy.PUBLIC)
8fffe21a 127 const videoObject = audiencify(video.toActivityPubObject(), audience)
2ccaeeb3 128
296c0905
C
129 if (req.path.endsWith('/activity')) {
130 const data = await createActivityData(video.url, video.VideoChannel.Account.Actor, videoObject, undefined, audience)
4b8f09fa 131 return activityPubResponse(activityPubContextify(data), res)
296c0905
C
132 }
133
4b8f09fa 134 return activityPubResponse(activityPubContextify(videoObject), res)
20494f12
C
135}
136
4e50b6a1 137async 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
144async 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
159async 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
166async 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
173async 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 188async 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
194async 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
201async 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
208async 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
C
212 const isPublic = true // Comments are always public
213 const audience = await getAudience(videoComment.Account.Actor, undefined, isPublic)
214
215 const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
216
296c0905
C
217 if (req.path.endsWith('/activity')) {
218 const data = await createActivityData(videoComment.url, videoComment.Account.Actor, videoCommentObject, undefined, 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
227async 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
235async 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
243function 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}