-async function follow (fromAccount: AccountModel, targetAccount: AccountModel, targetAlreadyInDB: boolean) {
- try {
- await sequelizeTypescript.transaction(async t => {
- if (targetAlreadyInDB === false) {
- await saveAccountAndServerIfNotExist(targetAccount, t)
- }
-
- const [ accountFollow ] = await AccountFollowModel.findOrCreate({
- where: {
- accountId: fromAccount.id,
- targetAccountId: targetAccount.id
- },
- defaults: {
- state: 'pending',
- accountId: fromAccount.id,
- targetAccountId: targetAccount.id
- },
- transaction: t
- })
- accountFollow.AccountFollowing = targetAccount
- accountFollow.AccountFollower = fromAccount
-
- // Send a notification to remote server
- if (accountFollow.state === 'pending') {
- await sendFollow(accountFollow, t)
- }
- })
- } catch (err) {
- // Reset target account
- targetAccount.isNewRecord = !targetAlreadyInDB
- throw err
- }
-}
-
-async function removeFollow (req: express.Request, res: express.Response, next: express.NextFunction) {
- const follow: AccountFollowModel = res.locals.follow