Fix lint
[oweals/peertube.git] / server / controllers / api / server / follows.ts
1 import * as express from 'express'
2 import { UserRight } from '../../../../shared/models/users/user-right.enum'
3 import { getFormattedObjects } from '../../../helpers'
4 import { retryTransactionWrapper } from '../../../helpers/database-utils'
5 import { logger } from '../../../helpers/logger'
6 import { getServerAccount } from '../../../helpers/utils'
7 import { getAccountFromWebfinger } from '../../../helpers/webfinger'
8 import { SERVER_ACCOUNT_NAME } from '../../../initializers/constants'
9 import { database as db } from '../../../initializers/database'
10 import { saveAccountAndServerIfNotExist } from '../../../lib/activitypub/account'
11 import { sendUndoFollow } from '../../../lib/activitypub/send/send-undo'
12 import { sendFollow } from '../../../lib/index'
13 import { asyncMiddleware, paginationValidator, removeFollowingValidator, setFollowersSort, setPagination } from '../../../middlewares'
14 import { authenticate } from '../../../middlewares/oauth'
15 import { setBodyHostsPort } from '../../../middlewares/servers'
16 import { setFollowingSort } from '../../../middlewares/sort'
17 import { ensureUserHasRight } from '../../../middlewares/user-right'
18 import { followValidator } from '../../../middlewares/validators/follows'
19 import { followersSortValidator, followingSortValidator } from '../../../middlewares/validators/sort'
20 import { AccountInstance } from '../../../models/account/account-interface'
21 import { AccountFollowInstance } from '../../../models/index'
22
23 const serverFollowsRouter = express.Router()
24
25 serverFollowsRouter.get('/following',
26   paginationValidator,
27   followingSortValidator,
28   setFollowingSort,
29   setPagination,
30   asyncMiddleware(listFollowing)
31 )
32
33 serverFollowsRouter.post('/following',
34   authenticate,
35   ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
36   followValidator,
37   setBodyHostsPort,
38   asyncMiddleware(followRetry)
39 )
40
41 serverFollowsRouter.delete('/following/:accountId',
42   authenticate,
43   ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
44   removeFollowingValidator,
45   asyncMiddleware(removeFollow)
46 )
47
48 serverFollowsRouter.get('/followers',
49   paginationValidator,
50   followersSortValidator,
51   setFollowersSort,
52   setPagination,
53   asyncMiddleware(listFollowers)
54 )
55
56 // ---------------------------------------------------------------------------
57
58 export {
59   serverFollowsRouter
60 }
61
62 // ---------------------------------------------------------------------------
63
64 async function listFollowing (req: express.Request, res: express.Response, next: express.NextFunction) {
65   const serverAccount = await getServerAccount()
66   const resultList = await db.AccountFollow.listFollowingForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort)
67
68   return res.json(getFormattedObjects(resultList.data, resultList.total))
69 }
70
71 async function listFollowers (req: express.Request, res: express.Response, next: express.NextFunction) {
72   const serverAccount = await getServerAccount()
73   const resultList = await db.AccountFollow.listFollowersForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort)
74
75   return res.json(getFormattedObjects(resultList.data, resultList.total))
76 }
77
78 async function followRetry (req: express.Request, res: express.Response, next: express.NextFunction) {
79   const hosts = req.body.hosts as string[]
80   const fromAccount = await getServerAccount()
81
82   const tasks: Promise<any>[] = []
83   const accountName = SERVER_ACCOUNT_NAME
84
85   for (const host of hosts) {
86
87     // We process each host in a specific transaction
88     // First, we add the follow request in the database
89     // Then we send the follow request to other account
90     const p = loadLocalOrGetAccountFromWebfinger(accountName, host)
91       .then(accountResult => {
92         let targetAccount = accountResult.account
93
94         const options = {
95           arguments: [ fromAccount, targetAccount, accountResult.loadedFromDB ],
96           errorMessage: 'Cannot follow with many retries.'
97         }
98
99         return retryTransactionWrapper(follow, options)
100       })
101       .catch(err => logger.warn('Cannot follow server %s.', `${accountName}@${host}`, err))
102
103     tasks.push(p)
104   }
105
106   // Don't make the client wait the tasks
107   Promise.all(tasks)
108     .catch(err => logger.error('Error in follow.', err))
109
110   return res.status(204).end()
111 }
112
113 async function follow (fromAccount: AccountInstance, targetAccount: AccountInstance, targetAlreadyInDB: boolean) {
114   try {
115     await db.sequelize.transaction(async t => {
116       if (targetAlreadyInDB === false) {
117         await saveAccountAndServerIfNotExist(targetAccount, t)
118       }
119
120       const [ accountFollow ] = await db.AccountFollow.findOrCreate({
121         where: {
122           accountId: fromAccount.id,
123           targetAccountId: targetAccount.id
124         },
125         defaults: {
126           state: 'pending',
127           accountId: fromAccount.id,
128           targetAccountId: targetAccount.id
129         },
130         transaction: t
131       })
132       accountFollow.AccountFollowing = targetAccount
133       accountFollow.AccountFollower = fromAccount
134
135       // Send a notification to remote server
136       if (accountFollow.state === 'pending') {
137         await sendFollow(accountFollow, t)
138       }
139     })
140   } catch (err) {
141     // Reset target account
142     targetAccount.isNewRecord = !targetAlreadyInDB
143     throw err
144   }
145 }
146
147 async function removeFollow (req: express.Request, res: express.Response, next: express.NextFunction) {
148   const follow: AccountFollowInstance = res.locals.follow
149
150   await db.sequelize.transaction(async t => {
151     if (follow.state === 'accepted') await sendUndoFollow(follow, t)
152
153     await follow.destroy({ transaction: t })
154   })
155
156   // Destroy the account that will destroy video channels, videos and video files too
157   // This could be long so don't wait this task
158   const following = follow.AccountFollowing
159   following.destroy()
160     .catch(err => logger.error('Cannot destroy account that we do not follow anymore %s.', following.url, err))
161
162   return res.status(204).end()
163 }
164
165 async function loadLocalOrGetAccountFromWebfinger (name: string, host: string) {
166   let loadedFromDB = true
167   let account = await db.Account.loadByNameAndHost(name, host)
168
169   if (!account) {
170     const nameWithDomain = name + '@' + host
171     account = await getAccountFromWebfinger(nameWithDomain)
172     loadedFromDB = false
173   }
174
175   return { account, loadedFromDB }
176 }