PeerTube/server/lib/activitypub/account.ts

128 lines
3.6 KiB
TypeScript
Raw Normal View History

2017-11-21 13:43:29 +01:00
import * as Bluebird from 'bluebird'
import { Transaction } from 'sequelize'
2017-12-12 17:53:50 +01:00
import * as url from 'url'
import { ActivityPubActor } from '../../../shared/models/activitypub'
import { doRequest, logger, retryTransactionWrapper } from '../../helpers'
import { isRemoteAccountValid } from '../../helpers/custom-validators/activitypub'
import { ACTIVITY_PUB, sequelizeTypescript } from '../../initializers'
import { AccountModel } from '../../models/account/account'
import { ServerModel } from '../../models/server/server'
2017-11-21 13:43:29 +01:00
async function getOrCreateAccountAndServer (accountUrl: string) {
2017-12-12 17:53:50 +01:00
let account = await AccountModel.loadByUrl(accountUrl)
// We don't have this account in our database, fetch it on remote
if (!account) {
2017-11-21 13:43:29 +01:00
account = await fetchRemoteAccount(accountUrl)
if (account === undefined) throw new Error('Cannot fetch remote account.')
2017-11-21 13:43:29 +01:00
const options = {
arguments: [ account ],
errorMessage: 'Cannot save account and server with many retries.'
}
account = await retryTransactionWrapper(saveAccountAndServerIfNotExist, options)
}
return account
}
2017-12-12 17:53:50 +01:00
function saveAccountAndServerIfNotExist (account: AccountModel, t?: Transaction): Bluebird<AccountModel> | Promise<AccountModel> {
2017-11-21 13:43:29 +01:00
if (t !== undefined) {
return save(t)
} else {
2017-12-12 17:53:50 +01:00
return sequelizeTypescript.transaction(t => {
2017-11-21 13:43:29 +01:00
return save(t)
})
}
async function save (t: Transaction) {
const accountHost = url.parse(account.url).host
const serverOptions = {
where: {
host: accountHost
},
defaults: {
host: accountHost
},
transaction: t
}
2017-12-12 17:53:50 +01:00
const [ server ] = await ServerModel.findOrCreate(serverOptions)
2017-11-21 13:43:29 +01:00
// Save our new account in database
account.set('serverId', server.id)
account = await account.save({ transaction: t })
return account
}
}
async function fetchRemoteAccount (accountUrl: string) {
const options = {
uri: accountUrl,
method: 'GET',
headers: {
'Accept': ACTIVITY_PUB.ACCEPT_HEADER
}
}
logger.info('Fetching remote account %s.', accountUrl)
let requestResult
try {
requestResult = await doRequest(options)
} catch (err) {
logger.warn('Cannot fetch remote account %s.', accountUrl, err)
return undefined
}
const accountJSON: ActivityPubActor = JSON.parse(requestResult.body)
if (isRemoteAccountValid(accountJSON) === false) {
logger.debug('Remote account JSON is not valid.', { accountJSON })
return undefined
}
const followersCount = await fetchAccountCount(accountJSON.followers)
const followingCount = await fetchAccountCount(accountJSON.following)
2017-12-12 17:53:50 +01:00
return new AccountModel({
uuid: accountJSON.uuid,
name: accountJSON.preferredUsername,
url: accountJSON.url,
publicKey: accountJSON.publicKey.publicKeyPem,
privateKey: null,
followersCount: followersCount,
followingCount: followingCount,
inboxUrl: accountJSON.inbox,
outboxUrl: accountJSON.outbox,
sharedInboxUrl: accountJSON.endpoints.sharedInbox,
followersUrl: accountJSON.followers,
followingUrl: accountJSON.following
})
}
export {
2017-11-21 13:43:29 +01:00
getOrCreateAccountAndServer,
fetchRemoteAccount,
saveAccountAndServerIfNotExist
}
// ---------------------------------------------------------------------------
async function fetchAccountCount (url: string) {
const options = {
uri: url,
method: 'GET'
}
let requestResult
try {
requestResult = await doRequest(options)
} catch (err) {
logger.warn('Cannot fetch remote account count %s.', url, err)
return undefined
}
return requestResult.totalItems ? requestResult.totalItems : 0
}