aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/pods.ts
blob: 0bd6971bb65ce07ab8ef18f376c0ff2f56e51f67 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import * as express from 'express'
import { UserRight } from '../../../shared/models/users/user-right.enum'
import { getFormattedObjects } from '../../helpers'
import { logger } from '../../helpers/logger'
import { getApplicationAccount } from '../../helpers/utils'
import { getAccountFromWebfinger } from '../../helpers/webfinger'
import { SERVER_ACCOUNT_NAME } from '../../initializers/constants'
import { database as db } from '../../initializers/database'
import { sendFollow } from '../../lib/activitypub/send-request'
import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../middlewares'
import { authenticate } from '../../middlewares/oauth'
import { setBodyHostsPort } from '../../middlewares/pods'
import { setFollowingSort } from '../../middlewares/sort'
import { ensureUserHasRight } from '../../middlewares/user-right'
import { followValidator } from '../../middlewares/validators/pods'
import { followersSortValidator, followingSortValidator } from '../../middlewares/validators/sort'

const podsRouter = express.Router()

podsRouter.get('/following',
  paginationValidator,
  followingSortValidator,
  setFollowingSort,
  setPagination,
  asyncMiddleware(listFollowing)
)

podsRouter.post('/follow',
  authenticate,
  ensureUserHasRight(UserRight.MANAGE_PEERTUBE_FOLLOW),
  followValidator,
  setBodyHostsPort,
  asyncMiddleware(follow)
)

podsRouter.get('/followers',
  paginationValidator,
  followersSortValidator,
  setFollowersSort,
  setPagination,
  asyncMiddleware(listFollowers)
)

// ---------------------------------------------------------------------------

export {
  podsRouter
}

// ---------------------------------------------------------------------------

async function listFollowing (req: express.Request, res: express.Response, next: express.NextFunction) {
  const applicationAccount = await getApplicationAccount()
  const resultList = await db.Account.listFollowingForApi(applicationAccount.id, req.query.start, req.query.count, req.query.sort)

  return res.json(getFormattedObjects(resultList.data, resultList.total))
}

async function listFollowers (req: express.Request, res: express.Response, next: express.NextFunction) {
  const applicationAccount = await getApplicationAccount()
  const resultList = await db.Account.listFollowersForApi(applicationAccount.id, req.query.start, req.query.count, req.query.sort)

  return res.json(getFormattedObjects(resultList.data, resultList.total))
}

async function follow (req: express.Request, res: express.Response, next: express.NextFunction) {
  const hosts = req.body.hosts as string[]
  const fromAccount = await getApplicationAccount()

  const tasks: Promise<any>[] = []
  const accountName = SERVER_ACCOUNT_NAME

  for (const host of hosts) {

    // We process each host in a specific transaction
    // First, we add the follow request in the database
    // Then we send the follow request to other account
    const p = loadLocalOrGetAccountFromWebfinger(accountName, host)
      .then(accountResult => {
        let targetAccount = accountResult.account

        return db.sequelize.transaction(async t => {
          if (accountResult.loadedFromDB === false) {
            targetAccount = await targetAccount.save({ transaction: t })
          }

          const [ accountFollow ] = await db.AccountFollow.findOrCreate({
            where: {
              accountId: fromAccount.id,
              targetAccountId: targetAccount.id
            },
            defaults: {
              state: 'pending',
              accountId: fromAccount.id,
              targetAccountId: targetAccount.id
            },
            transaction: t
          })

          // Send a notification to remote server
          if (accountFollow.state === 'pending') {
            await sendFollow(fromAccount, targetAccount, t)
          }
        })
      })
      .catch(err => logger.warn('Cannot follow server %s.', `${accountName}@${host}`, err))

    tasks.push(p)
  }

  await Promise.all(tasks)

  return res.status(204).end()
}

async function loadLocalOrGetAccountFromWebfinger (name: string, host: string) {
  let loadedFromDB = true
  let account = await db.Account.loadByNameAndHost(name, host)

  if (!account) {
    const nameWithDomain = name + '@' + host
    account = await getAccountFromWebfinger(nameWithDomain)
    loadedFromDB = false
  }

  return { account, loadedFromDB }
}