]>
Commit | Line | Data |
---|---|---|
e4f97bab C |
1 | // Intercept ActivityPub client requests |
2 | import * as express from 'express' | |
2ccaeeb3 | 3 | import { VideoPrivacy } from '../../../shared/models/videos' |
da854ddd C |
4 | import { activityPubCollectionPagination } from '../../helpers/activitypub' |
5 | import { pageToStartAndCount } from '../../helpers/core-utils' | |
3fd3ab2d | 6 | import { ACTIVITY_PUB, CONFIG } from '../../initializers' |
50d6de9c | 7 | import { buildVideoAnnounceToFollowers } from '../../lib/activitypub/send' |
2ccaeeb3 | 8 | import { audiencify, getAudience } from '../../lib/activitypub/send/misc' |
3fd3ab2d | 9 | import { asyncMiddleware, executeIfActivityPub, localAccountValidator } from '../../middlewares' |
50d6de9c | 10 | import { videoChannelsGetValidator, videosGetValidator, videosShareValidator } from '../../middlewares/validators' |
da854ddd | 11 | import { videoCommentGetValidator } from '../../middlewares/validators/video-comments' |
3fd3ab2d | 12 | import { AccountModel } from '../../models/account/account' |
50d6de9c | 13 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' |
3fd3ab2d C |
14 | import { VideoModel } from '../../models/video/video' |
15 | import { VideoChannelModel } from '../../models/video/video-channel' | |
da854ddd | 16 | import { VideoCommentModel } from '../../models/video/video-comment' |
3fd3ab2d | 17 | import { VideoShareModel } from '../../models/video/video-share' |
e4f97bab C |
18 | |
19 | const activityPubClientRouter = express.Router() | |
20 | ||
108af661 | 21 | activityPubClientRouter.get('/accounts?/:name', |
a2431b7d | 22 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
4e50b6a1 | 23 | executeIfActivityPub(accountController) |
e4f97bab C |
24 | ) |
25 | ||
c5911fd3 | 26 | activityPubClientRouter.get('/accounts/:name/followers', |
a2431b7d | 27 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
e4f97bab C |
28 | executeIfActivityPub(asyncMiddleware(accountFollowersController)) |
29 | ) | |
30 | ||
c5911fd3 | 31 | activityPubClientRouter.get('/accounts/:name/following', |
a2431b7d | 32 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
e4f97bab C |
33 | executeIfActivityPub(asyncMiddleware(accountFollowingController)) |
34 | ) | |
35 | ||
20494f12 | 36 | activityPubClientRouter.get('/videos/watch/:id', |
a2431b7d | 37 | executeIfActivityPub(asyncMiddleware(videosGetValidator)), |
da854ddd | 38 | executeIfActivityPub(asyncMiddleware(videoController)) |
4e50b6a1 C |
39 | ) |
40 | ||
41 | activityPubClientRouter.get('/videos/watch/:id/announces/:accountId', | |
42 | executeIfActivityPub(asyncMiddleware(videosShareValidator)), | |
43 | executeIfActivityPub(asyncMiddleware(videoAnnounceController)) | |
20494f12 C |
44 | ) |
45 | ||
da854ddd C |
46 | activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId', |
47 | executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)), | |
48 | executeIfActivityPub(asyncMiddleware(videoCommentController)) | |
49 | ) | |
50 | ||
20494f12 | 51 | activityPubClientRouter.get('/video-channels/:id', |
a2431b7d | 52 | executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)), |
20494f12 C |
53 | executeIfActivityPub(asyncMiddleware(videoChannelController)) |
54 | ) | |
55 | ||
e4f97bab C |
56 | // --------------------------------------------------------------------------- |
57 | ||
58 | export { | |
59 | activityPubClientRouter | |
60 | } | |
61 | ||
62 | // --------------------------------------------------------------------------- | |
63 | ||
4e50b6a1 | 64 | function accountController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 65 | const account: AccountModel = res.locals.account |
e4f97bab | 66 | |
da854ddd C |
67 | return res.json(account.toActivityPubObject()) |
68 | .end() | |
e4f97bab C |
69 | } |
70 | ||
71 | async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
3fd3ab2d | 72 | const account: AccountModel = res.locals.account |
e4f97bab | 73 | |
c46edbc2 | 74 | const page = req.query.page || 1 |
e4f97bab C |
75 | const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) |
76 | ||
50d6de9c | 77 | const result = await ActorFollowModel.listAcceptedFollowerUrlsForApi([ account.Actor.id ], undefined, start, count) |
e71bcc0f | 78 | const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result) |
e4f97bab C |
79 | |
80 | return res.json(activityPubResult) | |
81 | } | |
82 | ||
83 | async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
3fd3ab2d | 84 | const account: AccountModel = res.locals.account |
e4f97bab | 85 | |
c46edbc2 | 86 | const page = req.query.page || 1 |
e4f97bab C |
87 | const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) |
88 | ||
50d6de9c | 89 | const result = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ account.Actor.id ], undefined, start, count) |
e71bcc0f | 90 | const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result) |
e4f97bab C |
91 | |
92 | return res.json(activityPubResult) | |
93 | } | |
20494f12 | 94 | |
da854ddd | 95 | async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 96 | const video: VideoModel = res.locals.video |
20494f12 | 97 | |
da854ddd C |
98 | // We need more attributes |
99 | const videoAll = await VideoModel.loadAndPopulateAll(video.id) | |
2ccaeeb3 C |
100 | const audience = await getAudience(video.VideoChannel.Account.Actor, undefined, video.privacy === VideoPrivacy.PUBLIC) |
101 | ||
102 | return res.json(audiencify(videoAll.toActivityPubObject(), audience)) | |
20494f12 C |
103 | } |
104 | ||
4e50b6a1 | 105 | async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 106 | const share = res.locals.videoShare as VideoShareModel |
50d6de9c | 107 | const object = await buildVideoAnnounceToFollowers(share.Actor, res.locals.video, undefined) |
4e50b6a1 C |
108 | |
109 | return res.json(object) | |
110 | } | |
111 | ||
20494f12 | 112 | async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 113 | const videoChannel: VideoChannelModel = res.locals.videoChannel |
20494f12 C |
114 | |
115 | return res.json(videoChannel.toActivityPubObject()) | |
116 | } | |
da854ddd C |
117 | |
118 | async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
119 | const videoComment: VideoCommentModel = res.locals.videoComment | |
120 | ||
d7e70384 C |
121 | const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined) |
122 | return res.json(videoComment.toActivityPubObject(threadParentComments)) | |
da854ddd | 123 | } |