2017-11-13 11:39:41 -05:00
|
|
|
import { ActivityFollow } from '../../../shared/models/activitypub/activity'
|
2017-11-13 12:48:28 -05:00
|
|
|
import { getOrCreateAccount, retryTransactionWrapper } from '../../helpers'
|
2017-11-13 11:39:41 -05:00
|
|
|
import { database as db } from '../../initializers'
|
|
|
|
import { AccountInstance } from '../../models/account/account-interface'
|
2017-11-13 12:48:28 -05:00
|
|
|
import { sendAccept } from './send-request'
|
|
|
|
import { logger } from '../../helpers/logger'
|
2017-11-13 11:39:41 -05:00
|
|
|
|
|
|
|
async function processFollowActivity (activity: ActivityFollow) {
|
|
|
|
const activityObject = activity.object
|
|
|
|
const account = await getOrCreateAccount(activity.actor)
|
|
|
|
|
|
|
|
return processFollow(account, activityObject)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processFollowActivity
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-11-13 12:48:28 -05:00
|
|
|
function processFollow (account: AccountInstance, targetAccountURL: string) {
|
|
|
|
const options = {
|
|
|
|
arguments: [ account, targetAccountURL ],
|
|
|
|
errorMessage: 'Cannot follow with many retries.'
|
|
|
|
}
|
2017-11-13 11:39:41 -05:00
|
|
|
|
2017-11-13 12:48:28 -05:00
|
|
|
return retryTransactionWrapper(follow, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function follow (account: AccountInstance, targetAccountURL: string) {
|
|
|
|
await db.sequelize.transaction(async t => {
|
|
|
|
const targetAccount = await db.Account.loadByUrl(targetAccountURL, t)
|
|
|
|
|
|
|
|
if (targetAccount === undefined) throw new Error('Unknown account')
|
|
|
|
if (targetAccount.isOwned() === false) throw new Error('This is not a local account.')
|
2017-11-13 11:39:41 -05:00
|
|
|
|
2017-11-14 11:31:26 -05:00
|
|
|
await db.AccountFollow.findOrCreate({
|
|
|
|
where: {
|
|
|
|
accountId: account.id,
|
|
|
|
targetAccountId: targetAccount.id
|
|
|
|
},
|
|
|
|
defaults: {
|
|
|
|
accountId: account.id,
|
|
|
|
targetAccountId: targetAccount.id,
|
|
|
|
state: 'accepted'
|
|
|
|
},
|
2017-11-13 12:48:28 -05:00
|
|
|
transaction: t
|
2017-11-14 11:31:26 -05:00
|
|
|
})
|
2017-11-13 12:48:28 -05:00
|
|
|
|
|
|
|
// Target sends to account he accepted the follow request
|
|
|
|
return sendAccept(targetAccount, account, t)
|
2017-11-13 11:39:41 -05:00
|
|
|
})
|
2017-11-13 12:48:28 -05:00
|
|
|
|
|
|
|
logger.info('Account uuid %s is followed by account %s.', account.url, targetAccountURL)
|
2017-11-13 11:39:41 -05:00
|
|
|
}
|