]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/activitypub/client.ts
Add context on activitypub responses
[github/Chocobozzz/PeerTube.git] / server / controllers / activitypub / client.ts
1 // Intercept ActivityPub client requests
2 import * as express from 'express'
3 import { VideoPrivacy } from '../../../shared/models/videos'
4 import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
5 import { pageToStartAndCount } from '../../helpers/core-utils'
6 import { ACTIVITY_PUB, CONFIG } from '../../initializers'
7 import { buildVideoAnnounceToFollowers } from '../../lib/activitypub/send'
8 import { audiencify, getAudience } from '../../lib/activitypub/send/misc'
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
20 const activityPubClientRouter = express.Router()
21
22 activityPubClientRouter.get('/accounts?/:name',
23 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
24 executeIfActivityPub(accountController)
25 )
26 activityPubClientRouter.get('/accounts?/:name/followers',
27 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
28 executeIfActivityPub(asyncMiddleware(accountFollowersController))
29 )
30 activityPubClientRouter.get('/accounts?/:name/following',
31 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
32 executeIfActivityPub(asyncMiddleware(accountFollowingController))
33 )
34
35 activityPubClientRouter.get('/videos/watch/:id',
36 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
37 executeIfActivityPub(asyncMiddleware(videoController))
38 )
39 activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
40 executeIfActivityPub(asyncMiddleware(videosShareValidator)),
41 executeIfActivityPub(asyncMiddleware(videoAnnounceController))
42 )
43 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
44 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
45 executeIfActivityPub(asyncMiddleware(videoCommentController))
46 )
47
48 activityPubClientRouter.get('/video-channels/:id',
49 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
50 executeIfActivityPub(asyncMiddleware(videoChannelController))
51 )
52 activityPubClientRouter.get('/video-channels/:id/followers',
53 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
54 executeIfActivityPub(asyncMiddleware(videoChannelFollowersController))
55 )
56 activityPubClientRouter.get('/video-channels/:id/following',
57 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
58 executeIfActivityPub(asyncMiddleware(videoChannelFollowingController))
59 )
60
61 // ---------------------------------------------------------------------------
62
63 export {
64 activityPubClientRouter
65 }
66
67 // ---------------------------------------------------------------------------
68
69 function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
70 const account: AccountModel = res.locals.account
71
72 return res.json(activityPubContextify(account.toActivityPubObject()))
73 .end()
74 }
75
76 async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
77 const account: AccountModel = res.locals.account
78 const activityPubResult = await actorFollowers(req, account.Actor)
79
80 return res.json(activityPubContextify(activityPubResult))
81 }
82
83 async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
84 const account: AccountModel = res.locals.account
85 const activityPubResult = await actorFollowing(req, account.Actor)
86
87 return res.json(activityPubContextify(activityPubResult))
88 }
89
90 async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) {
91 const video: VideoModel = res.locals.video
92
93 // We need more attributes
94 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
95 const audience = await getAudience(video.VideoChannel.Account.Actor, undefined, video.privacy === VideoPrivacy.PUBLIC)
96 const videoObject = audiencify(videoAll.toActivityPubObject(), audience)
97
98 return res.json(activityPubContextify(videoObject))
99 }
100
101 async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) {
102 const share = res.locals.videoShare as VideoShareModel
103 const object = await buildVideoAnnounceToFollowers(share.Actor, res.locals.video, undefined)
104
105 return res.json(activityPubContextify(object))
106 }
107
108 async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) {
109 const videoChannel: VideoChannelModel = res.locals.videoChannel
110
111 return res.json(activityPubContextify(videoChannel.toActivityPubObject()))
112 }
113
114 async 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
118 return res.json(activityPubContextify(activityPubResult))
119 }
120
121 async 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
125 return res.json(activityPubContextify(activityPubResult))
126 }
127
128 async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) {
129 const videoComment: VideoCommentModel = res.locals.videoComment
130
131 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
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))
138 }
139
140 // ---------------------------------------------------------------------------
141
142 async 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
150 async 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 }