aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/application
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-15 11:00:25 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:51 +0100
commit608624252466acf9f1d9ee1c1170bd4fe4d18d18 (patch)
tree47eab55bb5421b7fe88e0b2ac743a436fd9561cf /server/controllers/api/application
parent51548b31815c6f96f314ae96588a9adca150519d (diff)
downloadPeerTube-608624252466acf9f1d9ee1c1170bd4fe4d18d18.tar.gz
PeerTube-608624252466acf9f1d9ee1c1170bd4fe4d18d18.tar.zst
PeerTube-608624252466acf9f1d9ee1c1170bd4fe4d18d18.zip
Rename Pod -> Server
Diffstat (limited to 'server/controllers/api/application')
-rw-r--r--server/controllers/api/application/follows.ts127
-rw-r--r--server/controllers/api/application/index.ts12
2 files changed, 0 insertions, 139 deletions
diff --git a/server/controllers/api/application/follows.ts b/server/controllers/api/application/follows.ts
deleted file mode 100644
index 000bbd23e..000000000
--- a/server/controllers/api/application/follows.ts
+++ /dev/null
@@ -1,127 +0,0 @@
1import * as express from 'express'
2import { UserRight } from '../../../../shared/models/users/user-right.enum'
3import { getFormattedObjects } from '../../../helpers'
4import { logger } from '../../../helpers/logger'
5import { getApplicationAccount } from '../../../helpers/utils'
6import { getAccountFromWebfinger } from '../../../helpers/webfinger'
7import { SERVER_ACCOUNT_NAME } from '../../../initializers/constants'
8import { database as db } from '../../../initializers/database'
9import { sendFollow } from '../../../lib/activitypub/send-request'
10import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../../middlewares'
11import { authenticate } from '../../../middlewares/oauth'
12import { setBodyHostsPort } from '../../../middlewares/pods'
13import { setFollowingSort } from '../../../middlewares/sort'
14import { ensureUserHasRight } from '../../../middlewares/user-right'
15import { followValidator } from '../../../middlewares/validators/pods'
16import { followersSortValidator, followingSortValidator } from '../../../middlewares/validators/sort'
17
18const applicationFollowsRouter = express.Router()
19
20applicationFollowsRouter.get('/following',
21 paginationValidator,
22 followingSortValidator,
23 setFollowingSort,
24 setPagination,
25 asyncMiddleware(listFollowing)
26)
27
28applicationFollowsRouter.post('/follow',
29 authenticate,
30 ensureUserHasRight(UserRight.MANAGE_APPLICATION_FOLLOW),
31 followValidator,
32 setBodyHostsPort,
33 asyncMiddleware(follow)
34)
35
36applicationFollowsRouter.get('/followers',
37 paginationValidator,
38 followersSortValidator,
39 setFollowersSort,
40 setPagination,
41 asyncMiddleware(listFollowers)
42)
43
44// ---------------------------------------------------------------------------
45
46export {
47 applicationFollowsRouter
48}
49
50// ---------------------------------------------------------------------------
51
52async function listFollowing (req: express.Request, res: express.Response, next: express.NextFunction) {
53 const applicationAccount = await getApplicationAccount()
54 const resultList = await db.AccountFollow.listFollowingForApi(applicationAccount.id, req.query.start, req.query.count, req.query.sort)
55
56 return res.json(getFormattedObjects(resultList.data, resultList.total))
57}
58
59async function listFollowers (req: express.Request, res: express.Response, next: express.NextFunction) {
60 const applicationAccount = await getApplicationAccount()
61 const resultList = await db.AccountFollow.listFollowersForApi(applicationAccount.id, req.query.start, req.query.count, req.query.sort)
62
63 return res.json(getFormattedObjects(resultList.data, resultList.total))
64}
65
66async function follow (req: express.Request, res: express.Response, next: express.NextFunction) {
67 const hosts = req.body.hosts as string[]
68 const fromAccount = await getApplicationAccount()
69
70 const tasks: Promise<any>[] = []
71 const accountName = SERVER_ACCOUNT_NAME
72
73 for (const host of hosts) {
74
75 // We process each host in a specific transaction
76 // First, we add the follow request in the database
77 // Then we send the follow request to other account
78 const p = loadLocalOrGetAccountFromWebfinger(accountName, host)
79 .then(accountResult => {
80 let targetAccount = accountResult.account
81
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 })
105 })
106 .catch(err => logger.warn('Cannot follow server %s.', `${accountName}@${host}`, err))
107
108 tasks.push(p)
109 }
110
111 await Promise.all(tasks)
112
113 return res.status(204).end()
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}
diff --git a/server/controllers/api/application/index.ts b/server/controllers/api/application/index.ts
deleted file mode 100644
index 011b971ed..000000000
--- a/server/controllers/api/application/index.ts
+++ /dev/null
@@ -1,12 +0,0 @@
1import * as express from 'express'
2import { applicationFollowsRouter } from './follows'
3
4const applicationRouter = express.Router()
5
6applicationRouter.use('/', applicationFollowsRouter)
7
8// ---------------------------------------------------------------------------
9
10export {
11 applicationRouter
12}