aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-09 17:51:58 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:51 +0100
commite4f97babf701481b55cc10fb3448feab5f97c867 (patch)
treeaf37402a594dc5ff09f71ecb0687e8cfe4cdb471 /server/controllers
parent343ad675f2a26c15b86150a9a3552e619d5d44f4 (diff)
downloadPeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.tar.gz
PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.tar.zst
PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.zip
Begin activitypub
Diffstat (limited to 'server/controllers')
-rw-r--r--server/controllers/activitypub/client.ts65
-rw-r--r--server/controllers/activitypub/inbox.ts72
-rw-r--r--server/controllers/activitypub/index.ts15
-rw-r--r--server/controllers/activitypub/pods.ts (renamed from server/controllers/api/remote/pods.ts)0
-rw-r--r--server/controllers/activitypub/videos.ts (renamed from server/controllers/api/remote/videos.ts)0
-rw-r--r--server/controllers/api/remote/index.ts18
6 files changed, 152 insertions, 18 deletions
diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts
new file mode 100644
index 000000000..28d08b3f4
--- /dev/null
+++ b/server/controllers/activitypub/client.ts
@@ -0,0 +1,65 @@
1// Intercept ActivityPub client requests
2import * as express from 'express'
3
4import { database as db } from '../../initializers'
5import { executeIfActivityPub, localAccountValidator } from '../../middlewares'
6import { pageToStartAndCount } from '../../helpers'
7import { AccountInstance } from '../../models'
8import { activityPubCollectionPagination } from '../../helpers/activitypub'
9import { ACTIVITY_PUB } from '../../initializers/constants'
10import { asyncMiddleware } from '../../middlewares/async'
11
12const activityPubClientRouter = express.Router()
13
14activityPubClientRouter.get('/account/:name',
15 executeIfActivityPub(localAccountValidator),
16 executeIfActivityPub(asyncMiddleware(accountController))
17)
18
19activityPubClientRouter.get('/account/:name/followers',
20 executeIfActivityPub(localAccountValidator),
21 executeIfActivityPub(asyncMiddleware(accountFollowersController))
22)
23
24activityPubClientRouter.get('/account/:name/following',
25 executeIfActivityPub(localAccountValidator),
26 executeIfActivityPub(asyncMiddleware(accountFollowingController))
27)
28
29// ---------------------------------------------------------------------------
30
31export {
32 activityPubClientRouter
33}
34
35// ---------------------------------------------------------------------------
36
37async function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
38 const account: AccountInstance = res.locals.account
39
40 return res.json(account.toActivityPubObject()).end()
41}
42
43async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
44 const account: AccountInstance = res.locals.account
45
46 const page = req.params.page || 1
47 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
48
49 const result = await db.Account.listFollowerUrlsForApi(account.name, start, count)
50 const activityPubResult = activityPubCollectionPagination(req.url, page, result)
51
52 return res.json(activityPubResult)
53}
54
55async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
56 const account: AccountInstance = res.locals.account
57
58 const page = req.params.page || 1
59 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
60
61 const result = await db.Account.listFollowingUrlsForApi(account.name, start, count)
62 const activityPubResult = activityPubCollectionPagination(req.url, page, result)
63
64 return res.json(activityPubResult)
65}
diff --git a/server/controllers/activitypub/inbox.ts b/server/controllers/activitypub/inbox.ts
new file mode 100644
index 000000000..79d989c2c
--- /dev/null
+++ b/server/controllers/activitypub/inbox.ts
@@ -0,0 +1,72 @@
1import * as express from 'express'
2
3import {
4 processCreateActivity,
5 processUpdateActivity,
6 processFlagActivity
7} from '../../lib'
8import {
9 Activity,
10 ActivityType,
11 RootActivity,
12 ActivityPubCollection,
13 ActivityPubOrderedCollection
14} from '../../../shared'
15import {
16 signatureValidator,
17 checkSignature,
18 asyncMiddleware
19} from '../../middlewares'
20import { logger } from '../../helpers'
21
22const processActivity: { [ P in ActivityType ]: (activity: Activity) => Promise<any> } = {
23 Create: processCreateActivity,
24 Update: processUpdateActivity,
25 Flag: processFlagActivity
26}
27
28const inboxRouter = express.Router()
29
30inboxRouter.post('/',
31 signatureValidator,
32 asyncMiddleware(checkSignature),
33 // inboxValidator,
34 asyncMiddleware(inboxController)
35)
36
37// ---------------------------------------------------------------------------
38
39export {
40 inboxRouter
41}
42
43// ---------------------------------------------------------------------------
44
45async function inboxController (req: express.Request, res: express.Response, next: express.NextFunction) {
46 const rootActivity: RootActivity = req.body
47 let activities: Activity[] = []
48
49 if ([ 'Collection', 'CollectionPage' ].indexOf(rootActivity.type) !== -1) {
50 activities = (rootActivity as ActivityPubCollection).items
51 } else if ([ 'OrderedCollection', 'OrderedCollectionPage' ].indexOf(rootActivity.type) !== -1) {
52 activities = (rootActivity as ActivityPubOrderedCollection).orderedItems
53 } else {
54 activities = [ rootActivity as Activity ]
55 }
56
57 await processActivities(activities)
58
59 res.status(204).end()
60}
61
62async function processActivities (activities: Activity[]) {
63 for (const activity of activities) {
64 const activityProcessor = processActivity[activity.type]
65 if (activityProcessor === undefined) {
66 logger.warn('Unknown activity type %s.', activity.type, { activityId: activity.id })
67 continue
68 }
69
70 await activityProcessor(activity)
71 }
72}
diff --git a/server/controllers/activitypub/index.ts b/server/controllers/activitypub/index.ts
new file mode 100644
index 000000000..7a4602b37
--- /dev/null
+++ b/server/controllers/activitypub/index.ts
@@ -0,0 +1,15 @@
1import * as express from 'express'
2
3import { badRequest } from '../../helpers'
4import { inboxRouter } from './inbox'
5
6const remoteRouter = express.Router()
7
8remoteRouter.use('/inbox', inboxRouter)
9remoteRouter.use('/*', badRequest)
10
11// ---------------------------------------------------------------------------
12
13export {
14 remoteRouter
15}
diff --git a/server/controllers/api/remote/pods.ts b/server/controllers/activitypub/pods.ts
index 326eb61ac..326eb61ac 100644
--- a/server/controllers/api/remote/pods.ts
+++ b/server/controllers/activitypub/pods.ts
diff --git a/server/controllers/api/remote/videos.ts b/server/controllers/activitypub/videos.ts
index cba47f0a1..cba47f0a1 100644
--- a/server/controllers/api/remote/videos.ts
+++ b/server/controllers/activitypub/videos.ts
diff --git a/server/controllers/api/remote/index.ts b/server/controllers/api/remote/index.ts
deleted file mode 100644
index d3522772b..000000000
--- a/server/controllers/api/remote/index.ts
+++ /dev/null
@@ -1,18 +0,0 @@
1import * as express from 'express'
2
3import { badRequest } from '../../../helpers'
4
5import { remotePodsRouter } from './pods'
6import { remoteVideosRouter } from './videos'
7
8const remoteRouter = express.Router()
9
10remoteRouter.use('/pods', remotePodsRouter)
11remoteRouter.use('/videos', remoteVideosRouter)
12remoteRouter.use('/*', badRequest)
13
14// ---------------------------------------------------------------------------
15
16export {
17 remoteRouter
18}