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