diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-14 17:31:26 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:51 +0100 |
commit | 350e31d6b64e4973dfa5e9f7b46841cb09aeb1ad (patch) | |
tree | f4191f3c04a5230fcf8ca3d6ca3248643fc4151d /server/controllers | |
parent | e34c85e527100c0b5c44567bd951e95be41b8d7e (diff) | |
download | PeerTube-350e31d6b64e4973dfa5e9f7b46841cb09aeb1ad.tar.gz PeerTube-350e31d6b64e4973dfa5e9f7b46841cb09aeb1ad.tar.zst PeerTube-350e31d6b64e4973dfa5e9f7b46841cb09aeb1ad.zip |
Follow works
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/activitypub/client.ts | 4 | ||||
-rw-r--r-- | server/controllers/activitypub/inbox.ts | 4 | ||||
-rw-r--r-- | server/controllers/activitypub/index.ts | 10 | ||||
-rw-r--r-- | server/controllers/api/pods.ts | 68 | ||||
-rw-r--r-- | server/controllers/index.ts | 2 | ||||
-rw-r--r-- | server/controllers/webfinger.ts | 39 |
6 files changed, 102 insertions, 25 deletions
diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts index 461a619dd..56a4054fa 100644 --- a/server/controllers/activitypub/client.ts +++ b/server/controllers/activitypub/client.ts | |||
@@ -16,12 +16,12 @@ activityPubClientRouter.get('/account/:name', | |||
16 | executeIfActivityPub(asyncMiddleware(accountController)) | 16 | executeIfActivityPub(asyncMiddleware(accountController)) |
17 | ) | 17 | ) |
18 | 18 | ||
19 | activityPubClientRouter.get('/account/:nameWithHost/followers', | 19 | activityPubClientRouter.get('/account/:name/followers', |
20 | executeIfActivityPub(localAccountValidator), | 20 | executeIfActivityPub(localAccountValidator), |
21 | executeIfActivityPub(asyncMiddleware(accountFollowersController)) | 21 | executeIfActivityPub(asyncMiddleware(accountFollowersController)) |
22 | ) | 22 | ) |
23 | 23 | ||
24 | activityPubClientRouter.get('/account/:nameWithHost/following', | 24 | activityPubClientRouter.get('/account/:name/following', |
25 | executeIfActivityPub(localAccountValidator), | 25 | executeIfActivityPub(localAccountValidator), |
26 | executeIfActivityPub(asyncMiddleware(accountFollowingController)) | 26 | executeIfActivityPub(asyncMiddleware(accountFollowingController)) |
27 | ) | 27 | ) |
diff --git a/server/controllers/activitypub/inbox.ts b/server/controllers/activitypub/inbox.ts index eedb518b9..e62125d85 100644 --- a/server/controllers/activitypub/inbox.ts +++ b/server/controllers/activitypub/inbox.ts | |||
@@ -30,7 +30,7 @@ inboxRouter.post('/inbox', | |||
30 | asyncMiddleware(inboxController) | 30 | asyncMiddleware(inboxController) |
31 | ) | 31 | ) |
32 | 32 | ||
33 | inboxRouter.post('/:nameWithHost/inbox', | 33 | inboxRouter.post('/account/:name/inbox', |
34 | signatureValidator, | 34 | signatureValidator, |
35 | asyncMiddleware(checkSignature), | 35 | asyncMiddleware(checkSignature), |
36 | localAccountValidator, | 36 | localAccountValidator, |
@@ -59,7 +59,9 @@ async function inboxController (req: express.Request, res: express.Response, nex | |||
59 | } | 59 | } |
60 | 60 | ||
61 | // Only keep activities we are able to process | 61 | // Only keep activities we are able to process |
62 | logger.debug('Filtering activities...', { activities }) | ||
62 | activities = activities.filter(a => isActivityValid(a)) | 63 | activities = activities.filter(a => isActivityValid(a)) |
64 | logger.debug('We keep %d activities.', activities.length, { activities }) | ||
63 | 65 | ||
64 | await processActivities(activities, res.locals.account) | 66 | await processActivities(activities, res.locals.account) |
65 | 67 | ||
diff --git a/server/controllers/activitypub/index.ts b/server/controllers/activitypub/index.ts index 6c7bafc6e..0c8574ef7 100644 --- a/server/controllers/activitypub/index.ts +++ b/server/controllers/activitypub/index.ts | |||
@@ -4,14 +4,14 @@ import { badRequest } from '../../helpers' | |||
4 | import { inboxRouter } from './inbox' | 4 | import { inboxRouter } from './inbox' |
5 | import { activityPubClientRouter } from './client' | 5 | import { activityPubClientRouter } from './client' |
6 | 6 | ||
7 | const remoteRouter = express.Router() | 7 | const activityPubRouter = express.Router() |
8 | 8 | ||
9 | remoteRouter.use('/', inboxRouter) | 9 | activityPubRouter.use('/', inboxRouter) |
10 | remoteRouter.use('/', activityPubClientRouter) | 10 | activityPubRouter.use('/', activityPubClientRouter) |
11 | remoteRouter.use('/*', badRequest) | 11 | activityPubRouter.use('/*', badRequest) |
12 | 12 | ||
13 | // --------------------------------------------------------------------------- | 13 | // --------------------------------------------------------------------------- |
14 | 14 | ||
15 | export { | 15 | export { |
16 | remoteRouter | 16 | activityPubRouter |
17 | } | 17 | } |
diff --git a/server/controllers/api/pods.ts b/server/controllers/api/pods.ts index 2231a05fa..0bd6971bb 100644 --- a/server/controllers/api/pods.ts +++ b/server/controllers/api/pods.ts | |||
@@ -1,19 +1,19 @@ | |||
1 | import * as Bluebird from 'bluebird' | ||
2 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { UserRight } from '../../../shared/models/users/user-right.enum' | ||
3 | import { getFormattedObjects } from '../../helpers' | 3 | import { getFormattedObjects } from '../../helpers' |
4 | import { getOrCreateAccount } from '../../helpers/activitypub' | 4 | import { logger } from '../../helpers/logger' |
5 | import { getApplicationAccount } from '../../helpers/utils' | 5 | import { getApplicationAccount } from '../../helpers/utils' |
6 | import { REMOTE_SCHEME } from '../../initializers/constants' | 6 | import { getAccountFromWebfinger } from '../../helpers/webfinger' |
7 | import { SERVER_ACCOUNT_NAME } from '../../initializers/constants' | ||
7 | import { database as db } from '../../initializers/database' | 8 | import { database as db } from '../../initializers/database' |
9 | import { sendFollow } from '../../lib/activitypub/send-request' | ||
8 | import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../middlewares' | 10 | import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../middlewares' |
11 | import { authenticate } from '../../middlewares/oauth' | ||
9 | import { setBodyHostsPort } from '../../middlewares/pods' | 12 | import { setBodyHostsPort } from '../../middlewares/pods' |
10 | import { setFollowingSort } from '../../middlewares/sort' | 13 | import { setFollowingSort } from '../../middlewares/sort' |
14 | import { ensureUserHasRight } from '../../middlewares/user-right' | ||
11 | import { followValidator } from '../../middlewares/validators/pods' | 15 | import { followValidator } from '../../middlewares/validators/pods' |
12 | import { followersSortValidator, followingSortValidator } from '../../middlewares/validators/sort' | 16 | import { followersSortValidator, followingSortValidator } from '../../middlewares/validators/sort' |
13 | import { sendFollow } from '../../lib/activitypub/send-request' | ||
14 | import { authenticate } from '../../middlewares/oauth' | ||
15 | import { ensureUserHasRight } from '../../middlewares/user-right' | ||
16 | import { UserRight } from '../../../shared/models/users/user-right.enum' | ||
17 | 17 | ||
18 | const podsRouter = express.Router() | 18 | const podsRouter = express.Router() |
19 | 19 | ||
@@ -67,22 +67,43 @@ async function follow (req: express.Request, res: express.Response, next: expres | |||
67 | const hosts = req.body.hosts as string[] | 67 | const hosts = req.body.hosts as string[] |
68 | const fromAccount = await getApplicationAccount() | 68 | const fromAccount = await getApplicationAccount() |
69 | 69 | ||
70 | const tasks: Bluebird<any>[] = [] | 70 | const tasks: Promise<any>[] = [] |
71 | const accountName = SERVER_ACCOUNT_NAME | ||
72 | |||
71 | for (const host of hosts) { | 73 | for (const host of hosts) { |
72 | const url = REMOTE_SCHEME.HTTP + '://' + host | ||
73 | const targetAccount = await getOrCreateAccount(url) | ||
74 | 74 | ||
75 | // We process each host in a specific transaction | 75 | // We process each host in a specific transaction |
76 | // First, we add the follow request in the database | 76 | // First, we add the follow request in the database |
77 | // Then we send the follow request to other account | 77 | // Then we send the follow request to other account |
78 | const p = db.sequelize.transaction(async t => { | 78 | const p = loadLocalOrGetAccountFromWebfinger(accountName, host) |
79 | return db.AccountFollow.create({ | 79 | .then(accountResult => { |
80 | accountId: fromAccount.id, | 80 | let targetAccount = accountResult.account |
81 | targetAccountId: targetAccount.id, | 81 | |
82 | state: 'pending' | 82 | return db.sequelize.transaction(async t => { |
83 | if (accountResult.loadedFromDB === false) { | ||
84 | targetAccount = await targetAccount.save({ transaction: t }) | ||
85 | } | ||
86 | |||
87 | const [ accountFollow ] = await db.AccountFollow.findOrCreate({ | ||
88 | where: { | ||
89 | accountId: fromAccount.id, | ||
90 | targetAccountId: targetAccount.id | ||
91 | }, | ||
92 | defaults: { | ||
93 | state: 'pending', | ||
94 | accountId: fromAccount.id, | ||
95 | targetAccountId: targetAccount.id | ||
96 | }, | ||
97 | transaction: t | ||
98 | }) | ||
99 | |||
100 | // Send a notification to remote server | ||
101 | if (accountFollow.state === 'pending') { | ||
102 | await sendFollow(fromAccount, targetAccount, t) | ||
103 | } | ||
104 | }) | ||
83 | }) | 105 | }) |
84 | .then(() => sendFollow(fromAccount, targetAccount, t)) | 106 | .catch(err => logger.warn('Cannot follow server %s.', `${accountName}@${host}`, err)) |
85 | }) | ||
86 | 107 | ||
87 | tasks.push(p) | 108 | tasks.push(p) |
88 | } | 109 | } |
@@ -91,3 +112,16 @@ async function follow (req: express.Request, res: express.Response, next: expres | |||
91 | 112 | ||
92 | return res.status(204).end() | 113 | return res.status(204).end() |
93 | } | 114 | } |
115 | |||
116 | async function loadLocalOrGetAccountFromWebfinger (name: string, host: string) { | ||
117 | let loadedFromDB = true | ||
118 | let account = await db.Account.loadByNameAndHost(name, host) | ||
119 | |||
120 | if (!account) { | ||
121 | const nameWithDomain = name + '@' + host | ||
122 | account = await getAccountFromWebfinger(nameWithDomain) | ||
123 | loadedFromDB = false | ||
124 | } | ||
125 | |||
126 | return { account, loadedFromDB } | ||
127 | } | ||
diff --git a/server/controllers/index.ts b/server/controllers/index.ts index 51cb480a3..457d0a12e 100644 --- a/server/controllers/index.ts +++ b/server/controllers/index.ts | |||
@@ -1,4 +1,6 @@ | |||
1 | export * from './activitypub' | ||
1 | export * from './static' | 2 | export * from './static' |
2 | export * from './client' | 3 | export * from './client' |
3 | export * from './services' | 4 | export * from './services' |
4 | export * from './api' | 5 | export * from './api' |
6 | export * from './webfinger' | ||
diff --git a/server/controllers/webfinger.ts b/server/controllers/webfinger.ts new file mode 100644 index 000000000..1c726f0cb --- /dev/null +++ b/server/controllers/webfinger.ts | |||
@@ -0,0 +1,39 @@ | |||
1 | import * as express from 'express' | ||
2 | |||
3 | import { CONFIG, PREVIEWS_SIZE, EMBED_SIZE } from '../initializers' | ||
4 | import { oembedValidator } from '../middlewares' | ||
5 | import { VideoInstance } from '../models' | ||
6 | import { webfingerValidator } from '../middlewares/validators/webfinger' | ||
7 | import { AccountInstance } from '../models/account/account-interface' | ||
8 | |||
9 | const webfingerRouter = express.Router() | ||
10 | |||
11 | webfingerRouter.use('/.well-known/webfinger', | ||
12 | webfingerValidator, | ||
13 | webfingerController | ||
14 | ) | ||
15 | |||
16 | // --------------------------------------------------------------------------- | ||
17 | |||
18 | export { | ||
19 | webfingerRouter | ||
20 | } | ||
21 | |||
22 | // --------------------------------------------------------------------------- | ||
23 | |||
24 | function webfingerController (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
25 | const account: AccountInstance = res.locals.account | ||
26 | |||
27 | const json = { | ||
28 | subject: req.query.resource, | ||
29 | aliases: [ account.url ], | ||
30 | links: [ | ||
31 | { | ||
32 | rel: 'self', | ||
33 | href: account.url | ||
34 | } | ||
35 | ] | ||
36 | } | ||
37 | |||
38 | return res.json(json).end() | ||
39 | } | ||