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'
21 const activityPubClientRouter = express.Router()
23 activityPubClientRouter.get('/accounts?/:name',
24 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
25 executeIfActivityPub(accountController)
27 activityPubClientRouter.get('/accounts?/:name/followers',
28 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
29 executeIfActivityPub(asyncMiddleware(accountFollowersController))
31 activityPubClientRouter.get('/accounts?/:name/following',
32 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
33 executeIfActivityPub(asyncMiddleware(accountFollowingController))
36 activityPubClientRouter.get('/videos/watch/:id',
37 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
38 executeIfActivityPub(asyncMiddleware(videoController))
40 activityPubClientRouter.get('/videos/watch/:id/activity',
41 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
42 executeIfActivityPub(asyncMiddleware(videoController))
44 activityPubClientRouter.get('/videos/watch/:id/announces',
45 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
46 executeIfActivityPub(asyncMiddleware(videoAnnouncesController))
48 activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
49 executeIfActivityPub(asyncMiddleware(videosShareValidator)),
50 executeIfActivityPub(asyncMiddleware(videoAnnounceController))
52 activityPubClientRouter.get('/videos/watch/:id/likes',
53 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
54 executeIfActivityPub(asyncMiddleware(videoLikesController))
56 activityPubClientRouter.get('/videos/watch/:id/dislikes',
57 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
58 executeIfActivityPub(asyncMiddleware(videoDislikesController))
60 activityPubClientRouter.get('/videos/watch/:id/comments',
61 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
62 executeIfActivityPub(asyncMiddleware(videoCommentsController))
64 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
65 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
66 executeIfActivityPub(asyncMiddleware(videoCommentController))
68 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
69 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
70 executeIfActivityPub(asyncMiddleware(videoCommentController))
73 activityPubClientRouter.get('/video-channels/:id',
74 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
75 executeIfActivityPub(asyncMiddleware(videoChannelController))
77 activityPubClientRouter.get('/video-channels/:id/followers',
78 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
79 executeIfActivityPub(asyncMiddleware(videoChannelFollowersController))
81 activityPubClientRouter.get('/video-channels/:id/following',
82 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
83 executeIfActivityPub(asyncMiddleware(videoChannelFollowingController))
86 // ---------------------------------------------------------------------------
89 activityPubClientRouter
92 // ---------------------------------------------------------------------------
94 function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
95 const account: AccountModel = res.locals.account
97 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
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)
104 return activityPubResponse(activityPubContextify(activityPubResult), res)
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)
111 return activityPubResponse(activityPubContextify(activityPubResult), res)
114 async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) {
115 const video: VideoModel = res.locals.video
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)
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)
127 return activityPubResponse(activityPubContextify(videoObject), res)
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)
134 return activityPubResponse(activityPubContextify(object), res)
137 async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) {
138 const video: VideoModel = res.locals.video
140 // We need more attributes
141 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
142 const object = videoAll.toAnnouncesActivityPubObject()
144 return activityPubResponse(activityPubContextify(object), res)
147 async function videoLikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
148 const video: VideoModel = res.locals.video
150 // We need more attributes
151 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
152 const { likesObject } = videoAll.toRatesActivityPubObjects()
154 return activityPubResponse(activityPubContextify(likesObject), res)
157 async function videoDislikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
158 const video: VideoModel = res.locals.video
160 // We need more attributes
161 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
162 const { dislikesObject } = videoAll.toRatesActivityPubObjects()
164 return activityPubResponse(activityPubContextify(dislikesObject), res)
167 async function videoCommentsController (req: express.Request, res: express.Response, next: express.NextFunction) {
168 const video: VideoModel = res.locals.video
170 // We need more attributes
171 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
172 const commentsObject = videoAll.toCommentsActivityPubObject()
174 return activityPubResponse(activityPubContextify(commentsObject), res)
177 async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) {
178 const videoChannel: VideoChannelModel = res.locals.videoChannel
180 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
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)
187 return activityPubResponse(activityPubContextify(activityPubResult), res)
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)
194 return activityPubResponse(activityPubContextify(activityPubResult), res)
197 async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) {
198 const videoComment: VideoCommentModel = res.locals.videoComment
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)
204 const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
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)
211 return activityPubResponse(activityPubContextify(videoCommentObject), res)
214 // ---------------------------------------------------------------------------
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)
220 const result = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
221 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
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)
228 const result = await ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count)
229 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
232 function activityPubResponse (data: any, res: express.Response) {
233 return res.type('application/activity+json; charset=utf-8')