]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/activitypub/client.ts
Add context on activitypub responses
[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'
3fd3ab2d 6import { ACTIVITY_PUB, CONFIG } from '../../initializers'
50d6de9c 7import { buildVideoAnnounceToFollowers } from '../../lib/activitypub/send'
2ccaeeb3 8import { audiencify, getAudience } from '../../lib/activitypub/send/misc'
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'
e4f97bab
C
19
20const activityPubClientRouter = express.Router()
21
108af661 22activityPubClientRouter.get('/accounts?/:name',
a2431b7d 23 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
4e50b6a1 24 executeIfActivityPub(accountController)
e4f97bab 25)
7006bc63 26activityPubClientRouter.get('/accounts?/:name/followers',
a2431b7d 27 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
e4f97bab
C
28 executeIfActivityPub(asyncMiddleware(accountFollowersController))
29)
7006bc63 30activityPubClientRouter.get('/accounts?/:name/following',
a2431b7d 31 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
e4f97bab
C
32 executeIfActivityPub(asyncMiddleware(accountFollowingController))
33)
34
20494f12 35activityPubClientRouter.get('/videos/watch/:id',
a2431b7d 36 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
da854ddd 37 executeIfActivityPub(asyncMiddleware(videoController))
4e50b6a1 38)
4e50b6a1
C
39activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
40 executeIfActivityPub(asyncMiddleware(videosShareValidator)),
41 executeIfActivityPub(asyncMiddleware(videoAnnounceController))
20494f12 42)
da854ddd
C
43activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
44 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
45 executeIfActivityPub(asyncMiddleware(videoCommentController))
46)
47
20494f12 48activityPubClientRouter.get('/video-channels/:id',
a2431b7d 49 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
20494f12
C
50 executeIfActivityPub(asyncMiddleware(videoChannelController))
51)
7006bc63
C
52activityPubClientRouter.get('/video-channels/:id/followers',
53 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
54 executeIfActivityPub(asyncMiddleware(videoChannelFollowersController))
55)
56activityPubClientRouter.get('/video-channels/:id/following',
57 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
58 executeIfActivityPub(asyncMiddleware(videoChannelFollowingController))
59)
20494f12 60
e4f97bab
C
61// ---------------------------------------------------------------------------
62
63export {
64 activityPubClientRouter
65}
66
67// ---------------------------------------------------------------------------
68
4e50b6a1 69function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 70 const account: AccountModel = res.locals.account
e4f97bab 71
d6e99e53 72 return res.json(activityPubContextify(account.toActivityPubObject()))
da854ddd 73 .end()
e4f97bab
C
74}
75
76async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 77 const account: AccountModel = res.locals.account
7006bc63 78 const activityPubResult = await actorFollowers(req, account.Actor)
e4f97bab 79
d6e99e53 80 return res.json(activityPubContextify(activityPubResult))
e4f97bab
C
81}
82
83async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 84 const account: AccountModel = res.locals.account
7006bc63 85 const activityPubResult = await actorFollowing(req, account.Actor)
e4f97bab 86
d6e99e53 87 return res.json(activityPubContextify(activityPubResult))
e4f97bab 88}
20494f12 89
da854ddd 90async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 91 const video: VideoModel = res.locals.video
20494f12 92
da854ddd
C
93 // We need more attributes
94 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
2ccaeeb3 95 const audience = await getAudience(video.VideoChannel.Account.Actor, undefined, video.privacy === VideoPrivacy.PUBLIC)
d6e99e53 96 const videoObject = audiencify(videoAll.toActivityPubObject(), audience)
2ccaeeb3 97
d6e99e53 98 return res.json(activityPubContextify(videoObject))
20494f12
C
99}
100
4e50b6a1 101async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 102 const share = res.locals.videoShare as VideoShareModel
50d6de9c 103 const object = await buildVideoAnnounceToFollowers(share.Actor, res.locals.video, undefined)
4e50b6a1 104
d6e99e53 105 return res.json(activityPubContextify(object))
4e50b6a1
C
106}
107
20494f12 108async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 109 const videoChannel: VideoChannelModel = res.locals.videoChannel
20494f12 110
d6e99e53 111 return res.json(activityPubContextify(videoChannel.toActivityPubObject()))
20494f12 112}
da854ddd 113
7006bc63
C
114async function videoChannelFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
115 const videoChannel: VideoChannelModel = res.locals.videoChannel
116 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
117
d6e99e53 118 return res.json(activityPubContextify(activityPubResult))
7006bc63
C
119}
120
121async function videoChannelFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
122 const videoChannel: VideoChannelModel = res.locals.videoChannel
123 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
124
d6e99e53 125 return res.json(activityPubContextify(activityPubResult))
7006bc63
C
126}
127
da854ddd
C
128async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) {
129 const videoComment: VideoCommentModel = res.locals.videoComment
130
d7e70384 131 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
d6e99e53
C
132 const isPublic = true // Comments are always public
133 const audience = await getAudience(videoComment.Account.Actor, undefined, isPublic)
134
135 const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
136
137 return res.json(activityPubContextify(videoCommentObject))
da854ddd 138}
7006bc63
C
139
140// ---------------------------------------------------------------------------
141
142async function actorFollowing (req: express.Request, actor: ActorModel) {
143 const page = req.query.page || 1
144 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
145
146 const result = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
147 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
148}
149
150async function actorFollowers (req: express.Request, actor: ActorModel) {
151 const page = req.query.page || 1
152 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
153
154 const result = await ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count)
155 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
156}