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