PeerTube/server/lib/activitypub/process/process-follow.ts

61 lines
2.0 KiB
TypeScript
Raw Normal View History

2017-11-20 02:43:39 -06:00
import { ActivityFollow } from '../../../../shared/models/activitypub/activity'
import { retryTransactionWrapper } from '../../../helpers'
2017-11-20 02:43:39 -06:00
import { database as db } from '../../../initializers'
import { AccountInstance } from '../../../models/account/account-interface'
import { logger } from '../../../helpers/logger'
import { sendAccept } from '../send/send-accept'
import { getOrCreateAccount } from '../account'
2017-11-13 10:39:41 -06: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 11:48:28 -06:00
function processFollow (account: AccountInstance, targetAccountURL: string) {
const options = {
arguments: [ account, targetAccountURL ],
errorMessage: 'Cannot follow with many retries.'
}
2017-11-13 10:39:41 -06:00
2017-11-13 11:48:28 -06: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)
2017-11-20 02:43:39 -06:00
if (!targetAccount) throw new Error('Unknown account')
2017-11-13 11:48:28 -06:00
if (targetAccount.isOwned() === false) throw new Error('This is not a local account.')
2017-11-13 10:39:41 -06:00
2017-11-20 02:43:39 -06:00
const [ accountFollow ] = await db.AccountFollow.findOrCreate({
2017-11-14 10:31:26 -06:00
where: {
accountId: account.id,
targetAccountId: targetAccount.id
},
defaults: {
accountId: account.id,
targetAccountId: targetAccount.id,
state: 'accepted'
},
2017-11-13 11:48:28 -06:00
transaction: t
2017-11-14 10:31:26 -06:00
})
2017-11-20 02:43:39 -06:00
accountFollow.AccountFollower = account
accountFollow.AccountFollowing = targetAccount
2017-11-13 11:48:28 -06:00
// Target sends to account he accepted the follow request
2017-11-20 02:43:39 -06:00
return sendAccept(accountFollow, t)
2017-11-13 10:39:41 -06:00
})
2017-11-13 11:48:28 -06:00
logger.info('Account uuid %s is followed by account %s.', account.url, targetAccountURL)
2017-11-13 10:39:41 -06:00
}