]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/activitypub/client.ts
Split files in activitypub server
[github/Chocobozzz/PeerTube.git] / server / controllers / activitypub / client.ts
CommitLineData
e4f97bab
C
1// Intercept ActivityPub client requests
2import * as express from 'express'
2ccaeeb3 3import { VideoPrivacy } from '../../../shared/models/videos'
d6e99e53 4import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
da854ddd 5import { pageToStartAndCount } from '../../helpers/core-utils'
fd4484f1 6import { ACTIVITY_PUB, CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers'
07197db4 7import { buildVideoAnnounce } from '../../lib/activitypub/send'
e251f170 8import { audiencify, getAudience } from '../../lib/activitypub/audience'
296c0905 9import { createActivityData } from '../../lib/activitypub/send/send-create'
3fd3ab2d 10import { asyncMiddleware, executeIfActivityPub, localAccountValidator } from '../../middlewares'
50d6de9c 11import { videoChannelsGetValidator, videosGetValidator, videosShareValidator } from '../../middlewares/validators'
da854ddd 12import { videoCommentGetValidator } from '../../middlewares/validators/video-comments'
3fd3ab2d 13import { AccountModel } from '../../models/account/account'
7006bc63 14import { ActorModel } from '../../models/activitypub/actor'
50d6de9c 15import { ActorFollowModel } from '../../models/activitypub/actor-follow'
3fd3ab2d
C
16import { VideoModel } from '../../models/video/video'
17import { VideoChannelModel } from '../../models/video/video-channel'
da854ddd 18import { VideoCommentModel } from '../../models/video/video-comment'
3fd3ab2d 19import { VideoShareModel } from '../../models/video/video-share'
fd4484f1 20import { cacheRoute } from '../../middlewares/cache'
e4f97bab
C
21
22const activityPubClientRouter = express.Router()
23
108af661 24activityPubClientRouter.get('/accounts?/:name',
a2431b7d 25 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
4e50b6a1 26 executeIfActivityPub(accountController)
e4f97bab 27)
7006bc63 28activityPubClientRouter.get('/accounts?/:name/followers',
a2431b7d 29 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
e4f97bab
C
30 executeIfActivityPub(asyncMiddleware(accountFollowersController))
31)
7006bc63 32activityPubClientRouter.get('/accounts?/:name/following',
a2431b7d 33 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
e4f97bab
C
34 executeIfActivityPub(asyncMiddleware(accountFollowingController))
35)
36
20494f12 37activityPubClientRouter.get('/videos/watch/:id',
fd4484f1 38 executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))),
a2431b7d 39 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
da854ddd 40 executeIfActivityPub(asyncMiddleware(videoController))
4e50b6a1 41)
296c0905
C
42activityPubClientRouter.get('/videos/watch/:id/activity',
43 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
44 executeIfActivityPub(asyncMiddleware(videoController))
45)
46531a0a
C
46activityPubClientRouter.get('/videos/watch/:id/announces',
47 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
48 executeIfActivityPub(asyncMiddleware(videoAnnouncesController))
49)
4e50b6a1
C
50activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
51 executeIfActivityPub(asyncMiddleware(videosShareValidator)),
52 executeIfActivityPub(asyncMiddleware(videoAnnounceController))
20494f12 53)
46531a0a
C
54activityPubClientRouter.get('/videos/watch/:id/likes',
55 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
56 executeIfActivityPub(asyncMiddleware(videoLikesController))
57)
58activityPubClientRouter.get('/videos/watch/:id/dislikes',
59 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
60 executeIfActivityPub(asyncMiddleware(videoDislikesController))
61)
62activityPubClientRouter.get('/videos/watch/:id/comments',
63 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
64 executeIfActivityPub(asyncMiddleware(videoCommentsController))
65)
da854ddd
C
66activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
67 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
68 executeIfActivityPub(asyncMiddleware(videoCommentController))
69)
296c0905
C
70activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
71 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
72 executeIfActivityPub(asyncMiddleware(videoCommentController))
73)
da854ddd 74
20494f12 75activityPubClientRouter.get('/video-channels/:id',
a2431b7d 76 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
20494f12
C
77 executeIfActivityPub(asyncMiddleware(videoChannelController))
78)
7006bc63
C
79activityPubClientRouter.get('/video-channels/:id/followers',
80 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
81 executeIfActivityPub(asyncMiddleware(videoChannelFollowersController))
82)
83activityPubClientRouter.get('/video-channels/:id/following',
84 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
85 executeIfActivityPub(asyncMiddleware(videoChannelFollowingController))
86)
20494f12 87
e4f97bab
C
88// ---------------------------------------------------------------------------
89
90export {
91 activityPubClientRouter
92}
93
94// ---------------------------------------------------------------------------
95
4e50b6a1 96function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 97 const account: AccountModel = res.locals.account
e4f97bab 98
4b8f09fa 99 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
e4f97bab
C
100}
101
102async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 103 const account: AccountModel = res.locals.account
7006bc63 104 const activityPubResult = await actorFollowers(req, account.Actor)
e4f97bab 105
4b8f09fa 106 return activityPubResponse(activityPubContextify(activityPubResult), res)
e4f97bab
C
107}
108
109async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 110 const account: AccountModel = res.locals.account
7006bc63 111 const activityPubResult = await actorFollowing(req, account.Actor)
e4f97bab 112
4b8f09fa 113 return activityPubResponse(activityPubContextify(activityPubResult), res)
e4f97bab 114}
20494f12 115
da854ddd 116async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 117 const video: VideoModel = res.locals.video
20494f12 118
da854ddd
C
119 // We need more attributes
120 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
2ccaeeb3 121 const audience = await getAudience(video.VideoChannel.Account.Actor, undefined, video.privacy === VideoPrivacy.PUBLIC)
d6e99e53 122 const videoObject = audiencify(videoAll.toActivityPubObject(), audience)
2ccaeeb3 123
296c0905
C
124 if (req.path.endsWith('/activity')) {
125 const data = await createActivityData(video.url, video.VideoChannel.Account.Actor, videoObject, undefined, audience)
4b8f09fa 126 return activityPubResponse(activityPubContextify(data), res)
296c0905
C
127 }
128
4b8f09fa 129 return activityPubResponse(activityPubContextify(videoObject), res)
20494f12
C
130}
131
4e50b6a1 132async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 133 const share = res.locals.videoShare as VideoShareModel
07197db4 134 const object = await buildVideoAnnounce(share.Actor, share, res.locals.video, undefined)
4e50b6a1 135
4b8f09fa 136 return activityPubResponse(activityPubContextify(object), res)
4e50b6a1
C
137}
138
46531a0a
C
139async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) {
140 const video: VideoModel = res.locals.video
141
142 // We need more attributes
143 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
144 const object = videoAll.toAnnouncesActivityPubObject()
145
4b8f09fa 146 return activityPubResponse(activityPubContextify(object), res)
46531a0a
C
147}
148
149async function videoLikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
150 const video: VideoModel = res.locals.video
151
152 // We need more attributes
153 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
154 const { likesObject } = videoAll.toRatesActivityPubObjects()
155
4b8f09fa 156 return activityPubResponse(activityPubContextify(likesObject), res)
46531a0a
C
157}
158
159async function videoDislikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
160 const video: VideoModel = res.locals.video
161
162 // We need more attributes
163 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
164 const { dislikesObject } = videoAll.toRatesActivityPubObjects()
165
4b8f09fa 166 return activityPubResponse(activityPubContextify(dislikesObject), res)
46531a0a
C
167}
168
169async function videoCommentsController (req: express.Request, res: express.Response, next: express.NextFunction) {
170 const video: VideoModel = res.locals.video
171
172 // We need more attributes
173 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
174 const commentsObject = videoAll.toCommentsActivityPubObject()
175
4b8f09fa 176 return activityPubResponse(activityPubContextify(commentsObject), res)
46531a0a
C
177}
178
20494f12 179async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 180 const videoChannel: VideoChannelModel = res.locals.videoChannel
20494f12 181
4b8f09fa 182 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
20494f12 183}
da854ddd 184
7006bc63
C
185async function videoChannelFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
186 const videoChannel: VideoChannelModel = res.locals.videoChannel
187 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
188
4b8f09fa 189 return activityPubResponse(activityPubContextify(activityPubResult), res)
7006bc63
C
190}
191
192async function videoChannelFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
193 const videoChannel: VideoChannelModel = res.locals.videoChannel
194 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
195
4b8f09fa 196 return activityPubResponse(activityPubContextify(activityPubResult), res)
7006bc63
C
197}
198
da854ddd
C
199async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) {
200 const videoComment: VideoCommentModel = res.locals.videoComment
201
d7e70384 202 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
d6e99e53
C
203 const isPublic = true // Comments are always public
204 const audience = await getAudience(videoComment.Account.Actor, undefined, isPublic)
205
206 const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
207
296c0905
C
208 if (req.path.endsWith('/activity')) {
209 const data = await createActivityData(videoComment.url, videoComment.Account.Actor, videoCommentObject, undefined, audience)
4b8f09fa 210 return activityPubResponse(activityPubContextify(data), res)
296c0905
C
211 }
212
4b8f09fa 213 return activityPubResponse(activityPubContextify(videoCommentObject), res)
da854ddd 214}
7006bc63
C
215
216// ---------------------------------------------------------------------------
217
218async function actorFollowing (req: express.Request, actor: ActorModel) {
219 const page = req.query.page || 1
220 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
221
222 const result = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
223 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
224}
225
226async function actorFollowers (req: express.Request, actor: ActorModel) {
227 const page = req.query.page || 1
228 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
229
230 const result = await ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count)
231 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
232}
4b8f09fa
C
233
234function activityPubResponse (data: any, res: express.Response) {
235 return res.type('application/activity+json; charset=utf-8')
236 .json(data)
237 .end()
238}