1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-04 10:19:35 +02:00

Refractor activity pub lib/helpers

This commit is contained in:
Chocobozzz 2017-11-20 09:43:39 +01:00
parent eb8b27c93e
commit 5414139835
No known key found for this signature in database
GPG key ID: 583A612D890159BE
47 changed files with 844 additions and 498 deletions

View file

@ -0,0 +1,59 @@
import { ActivityFollow } from '../../../../shared/models/activitypub/activity'
import { getOrCreateAccount, retryTransactionWrapper } from '../../../helpers'
import { database as db } from '../../../initializers'
import { AccountInstance } from '../../../models/account/account-interface'
import { logger } from '../../../helpers/logger'
import { sendAccept } from '../send/send-accept'
async function processFollowActivity (activity: ActivityFollow) {
const activityObject = activity.object
const account = await getOrCreateAccount(activity.actor)
return processFollow(account, activityObject)
}
// ---------------------------------------------------------------------------
export {
processFollowActivity
}
// ---------------------------------------------------------------------------
function processFollow (account: AccountInstance, targetAccountURL: string) {
const options = {
arguments: [ account, targetAccountURL ],
errorMessage: 'Cannot follow with many retries.'
}
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) throw new Error('Unknown account')
if (targetAccount.isOwned() === false) throw new Error('This is not a local account.')
const [ accountFollow ] = await db.AccountFollow.findOrCreate({
where: {
accountId: account.id,
targetAccountId: targetAccount.id
},
defaults: {
accountId: account.id,
targetAccountId: targetAccount.id,
state: 'accepted'
},
transaction: t
})
accountFollow.AccountFollower = account
accountFollow.AccountFollowing = targetAccount
// Target sends to account he accepted the follow request
return sendAccept(accountFollow, t)
})
logger.info('Account uuid %s is followed by account %s.', account.url, targetAccountURL)
}