]>
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' |
7006bc63 | 13 | import { ActorModel } from '../../models/activitypub/actor' |
50d6de9c | 14 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' |
3fd3ab2d C |
15 | import { VideoModel } from '../../models/video/video' |
16 | import { VideoChannelModel } from '../../models/video/video-channel' | |
da854ddd | 17 | import { VideoCommentModel } from '../../models/video/video-comment' |
3fd3ab2d | 18 | import { VideoShareModel } from '../../models/video/video-share' |
e4f97bab C |
19 | |
20 | const activityPubClientRouter = express.Router() | |
21 | ||
108af661 | 22 | activityPubClientRouter.get('/accounts?/:name', |
a2431b7d | 23 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
4e50b6a1 | 24 | executeIfActivityPub(accountController) |
e4f97bab | 25 | ) |
7006bc63 | 26 | activityPubClientRouter.get('/accounts?/:name/followers', |
a2431b7d | 27 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
e4f97bab C |
28 | executeIfActivityPub(asyncMiddleware(accountFollowersController)) |
29 | ) | |
7006bc63 | 30 | activityPubClientRouter.get('/accounts?/:name/following', |
a2431b7d | 31 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
e4f97bab C |
32 | executeIfActivityPub(asyncMiddleware(accountFollowingController)) |
33 | ) | |
34 | ||
20494f12 | 35 | activityPubClientRouter.get('/videos/watch/:id', |
a2431b7d | 36 | executeIfActivityPub(asyncMiddleware(videosGetValidator)), |
da854ddd | 37 | executeIfActivityPub(asyncMiddleware(videoController)) |
4e50b6a1 | 38 | ) |
4e50b6a1 C |
39 | activityPubClientRouter.get('/videos/watch/:id/announces/:accountId', |
40 | executeIfActivityPub(asyncMiddleware(videosShareValidator)), | |
41 | executeIfActivityPub(asyncMiddleware(videoAnnounceController)) | |
20494f12 | 42 | ) |
da854ddd C |
43 | activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId', |
44 | executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)), | |
45 | executeIfActivityPub(asyncMiddleware(videoCommentController)) | |
46 | ) | |
47 | ||
20494f12 | 48 | activityPubClientRouter.get('/video-channels/:id', |
a2431b7d | 49 | executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)), |
20494f12 C |
50 | executeIfActivityPub(asyncMiddleware(videoChannelController)) |
51 | ) | |
7006bc63 C |
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 | ) | |
20494f12 | 60 | |
e4f97bab C |
61 | // --------------------------------------------------------------------------- |
62 | ||
63 | export { | |
64 | activityPubClientRouter | |
65 | } | |
66 | ||
67 | // --------------------------------------------------------------------------- | |
68 | ||
4e50b6a1 | 69 | function accountController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 70 | const account: AccountModel = res.locals.account |
e4f97bab | 71 | |
da854ddd C |
72 | return res.json(account.toActivityPubObject()) |
73 | .end() | |
e4f97bab C |
74 | } |
75 | ||
76 | async 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 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 |
7006bc63 | 85 | const activityPubResult = await actorFollowing(req, account.Actor) |
e4f97bab C |
86 | |
87 | return res.json(activityPubResult) | |
88 | } | |
20494f12 | 89 | |
da854ddd | 90 | async 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 C |
95 | const audience = await getAudience(video.VideoChannel.Account.Actor, undefined, video.privacy === VideoPrivacy.PUBLIC) |
96 | ||
97 | return res.json(audiencify(videoAll.toActivityPubObject(), audience)) | |
20494f12 C |
98 | } |
99 | ||
4e50b6a1 | 100 | async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 101 | const share = res.locals.videoShare as VideoShareModel |
50d6de9c | 102 | const object = await buildVideoAnnounceToFollowers(share.Actor, res.locals.video, undefined) |
4e50b6a1 C |
103 | |
104 | return res.json(object) | |
105 | } | |
106 | ||
20494f12 | 107 | async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 108 | const videoChannel: VideoChannelModel = res.locals.videoChannel |
20494f12 C |
109 | |
110 | return res.json(videoChannel.toActivityPubObject()) | |
111 | } | |
da854ddd | 112 | |
7006bc63 C |
113 | async function videoChannelFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) { |
114 | const videoChannel: VideoChannelModel = res.locals.videoChannel | |
115 | const activityPubResult = await actorFollowers(req, videoChannel.Actor) | |
116 | ||
117 | return res.json(activityPubResult) | |
118 | } | |
119 | ||
120 | async function videoChannelFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
121 | const videoChannel: VideoChannelModel = res.locals.videoChannel | |
122 | const activityPubResult = await actorFollowing(req, videoChannel.Actor) | |
123 | ||
124 | return res.json(activityPubResult) | |
125 | } | |
126 | ||
da854ddd C |
127 | async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) { |
128 | const videoComment: VideoCommentModel = res.locals.videoComment | |
129 | ||
d7e70384 C |
130 | const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined) |
131 | return res.json(videoComment.toActivityPubObject(threadParentComments)) | |
da854ddd | 132 | } |
7006bc63 C |
133 | |
134 | // --------------------------------------------------------------------------- | |
135 | ||
136 | async function actorFollowing (req: express.Request, actor: ActorModel) { | |
137 | const page = req.query.page || 1 | |
138 | const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) | |
139 | ||
140 | const result = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count) | |
141 | return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result) | |
142 | } | |
143 | ||
144 | async function actorFollowers (req: express.Request, actor: ActorModel) { | |
145 | const page = req.query.page || 1 | |
146 | const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) | |
147 | ||
148 | const result = await ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count) | |
149 | return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result) | |
150 | } |