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