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