aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/pods.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/pods.ts')
-rw-r--r--server/controllers/api/pods.ts68
1 files changed, 51 insertions, 17 deletions
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 @@
1import * as Bluebird from 'bluebird'
2import * as express from 'express' 1import * as express from 'express'
2import { UserRight } from '../../../shared/models/users/user-right.enum'
3import { getFormattedObjects } from '../../helpers' 3import { getFormattedObjects } from '../../helpers'
4import { getOrCreateAccount } from '../../helpers/activitypub' 4import { logger } from '../../helpers/logger'
5import { getApplicationAccount } from '../../helpers/utils' 5import { getApplicationAccount } from '../../helpers/utils'
6import { REMOTE_SCHEME } from '../../initializers/constants' 6import { getAccountFromWebfinger } from '../../helpers/webfinger'
7import { SERVER_ACCOUNT_NAME } from '../../initializers/constants'
7import { database as db } from '../../initializers/database' 8import { database as db } from '../../initializers/database'
9import { sendFollow } from '../../lib/activitypub/send-request'
8import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../middlewares' 10import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../middlewares'
11import { authenticate } from '../../middlewares/oauth'
9import { setBodyHostsPort } from '../../middlewares/pods' 12import { setBodyHostsPort } from '../../middlewares/pods'
10import { setFollowingSort } from '../../middlewares/sort' 13import { setFollowingSort } from '../../middlewares/sort'
14import { ensureUserHasRight } from '../../middlewares/user-right'
11import { followValidator } from '../../middlewares/validators/pods' 15import { followValidator } from '../../middlewares/validators/pods'
12import { followersSortValidator, followingSortValidator } from '../../middlewares/validators/sort' 16import { followersSortValidator, followingSortValidator } from '../../middlewares/validators/sort'
13import { sendFollow } from '../../lib/activitypub/send-request'
14import { authenticate } from '../../middlewares/oauth'
15import { ensureUserHasRight } from '../../middlewares/user-right'
16import { UserRight } from '../../../shared/models/users/user-right.enum'
17 17
18const podsRouter = express.Router() 18const 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
116async 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}