mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 19:42:24 +02:00
Add follow tests
This commit is contained in:
parent
81de19482b
commit
0f91ae62df
23 changed files with 736 additions and 522 deletions
|
@ -1,27 +1,65 @@
|
|||
import * as Bluebird from 'bluebird'
|
||||
import * as url from 'url'
|
||||
import { ActivityPubActor } from '../../../shared/models/activitypub/activitypub-actor'
|
||||
import { isRemoteAccountValid } from '../../helpers/custom-validators/activitypub/account'
|
||||
import { retryTransactionWrapper } from '../../helpers/database-utils'
|
||||
import { logger } from '../../helpers/logger'
|
||||
import { doRequest } from '../../helpers/requests'
|
||||
import { ACTIVITY_PUB } from '../../initializers/constants'
|
||||
import { database as db } from '../../initializers/database'
|
||||
import { AccountInstance } from '../../models/account/account-interface'
|
||||
import { Transaction } from 'sequelize'
|
||||
|
||||
async function getOrCreateAccount (accountUrl: string) {
|
||||
async function getOrCreateAccountAndServer (accountUrl: string) {
|
||||
let account = await db.Account.loadByUrl(accountUrl)
|
||||
|
||||
// We don't have this account in our database, fetch it on remote
|
||||
if (!account) {
|
||||
const res = await fetchRemoteAccountAndCreateServer(accountUrl)
|
||||
if (res === undefined) throw new Error('Cannot fetch remote account.')
|
||||
account = await fetchRemoteAccount(accountUrl)
|
||||
if (account === undefined) throw new Error('Cannot fetch remote account.')
|
||||
|
||||
// Save our new account in database
|
||||
account = await res.account.save()
|
||||
const options = {
|
||||
arguments: [ account ],
|
||||
errorMessage: 'Cannot save account and server with many retries.'
|
||||
}
|
||||
account = await retryTransactionWrapper(saveAccountAndServerIfNotExist, options)
|
||||
}
|
||||
|
||||
return account
|
||||
}
|
||||
|
||||
async function fetchRemoteAccountAndCreateServer (accountUrl: string) {
|
||||
function saveAccountAndServerIfNotExist (account: AccountInstance, t?: Transaction): Bluebird<AccountInstance> | Promise<AccountInstance> {
|
||||
if (t !== undefined) {
|
||||
return save(t)
|
||||
} else {
|
||||
return db.sequelize.transaction(t => {
|
||||
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
|
||||
}
|
||||
const [ server ] = await db.Server.findOrCreate(serverOptions)
|
||||
|
||||
// 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',
|
||||
|
@ -64,24 +102,13 @@ async function fetchRemoteAccountAndCreateServer (accountUrl: string) {
|
|||
followingUrl: accountJSON.following
|
||||
})
|
||||
|
||||
const accountHost = url.parse(account.url).host
|
||||
const serverOptions = {
|
||||
where: {
|
||||
host: accountHost
|
||||
},
|
||||
defaults: {
|
||||
host: accountHost
|
||||
}
|
||||
}
|
||||
const [ server ] = await db.Server.findOrCreate(serverOptions)
|
||||
account.set('serverId', server.id)
|
||||
|
||||
return { account, server }
|
||||
return account
|
||||
}
|
||||
|
||||
export {
|
||||
getOrCreateAccount,
|
||||
fetchRemoteAccountAndCreateServer
|
||||
getOrCreateAccountAndServer,
|
||||
fetchRemoteAccount,
|
||||
saveAccountAndServerIfNotExist
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
|
|
@ -6,7 +6,7 @@ import { logger } from '../../../helpers/logger'
|
|||
import { database as db } from '../../../initializers'
|
||||
import { AccountInstance } from '../../../models/account/account-interface'
|
||||
import { VideoChannelInstance } from '../../../models/video/video-channel-interface'
|
||||
import { getOrCreateAccount } from '../account'
|
||||
import { getOrCreateAccountAndServer } from '../account'
|
||||
import { getOrCreateVideoChannel } from '../video-channels'
|
||||
import { generateThumbnailFromUrl } from '../videos'
|
||||
import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
|
||||
|
@ -14,7 +14,7 @@ import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes }
|
|||
async function processAddActivity (activity: ActivityAdd) {
|
||||
const activityObject = activity.object
|
||||
const activityType = activityObject.type
|
||||
const account = await getOrCreateAccount(activity.actor)
|
||||
const account = await getOrCreateAccountAndServer(activity.actor)
|
||||
|
||||
if (activityType === 'Video') {
|
||||
const videoChannelUrl = activity.target
|
||||
|
|
|
@ -5,11 +5,11 @@ import { VideoInstance } from '../../../models/index'
|
|||
import { VideoChannelInstance } from '../../../models/video/video-channel-interface'
|
||||
import { processAddActivity } from './process-add'
|
||||
import { processCreateActivity } from './process-create'
|
||||
import { getOrCreateAccount } from '../account'
|
||||
import { getOrCreateAccountAndServer } from '../account'
|
||||
|
||||
async function processAnnounceActivity (activity: ActivityAnnounce) {
|
||||
const announcedActivity = activity.object
|
||||
const accountAnnouncer = await getOrCreateAccount(activity.actor)
|
||||
const accountAnnouncer = await getOrCreateAccountAndServer(activity.actor)
|
||||
|
||||
if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'VideoChannel') {
|
||||
// Add share entry
|
||||
|
|
|
@ -3,14 +3,14 @@ import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects/
|
|||
import { logger, retryTransactionWrapper } from '../../../helpers'
|
||||
import { database as db } from '../../../initializers'
|
||||
import { AccountInstance } from '../../../models/account/account-interface'
|
||||
import { getOrCreateAccount } from '../account'
|
||||
import { getOrCreateAccountAndServer } from '../account'
|
||||
import { getVideoChannelActivityPubUrl } from '../url'
|
||||
import { videoChannelActivityObjectToDBAttributes } from './misc'
|
||||
|
||||
async function processCreateActivity (activity: ActivityCreate) {
|
||||
const activityObject = activity.object
|
||||
const activityType = activityObject.type
|
||||
const account = await getOrCreateAccount(activity.actor)
|
||||
const account = await getOrCreateAccountAndServer(activity.actor)
|
||||
|
||||
if (activityType === 'VideoChannel') {
|
||||
return processCreateVideoChannel(account, activityObject as VideoChannelObject)
|
||||
|
|
|
@ -5,10 +5,10 @@ import { database as db } from '../../../initializers'
|
|||
import { AccountInstance } from '../../../models/account/account-interface'
|
||||
import { VideoChannelInstance } from '../../../models/video/video-channel-interface'
|
||||
import { VideoInstance } from '../../../models/video/video-interface'
|
||||
import { getOrCreateAccount } from '../account'
|
||||
import { getOrCreateAccountAndServer } from '../account'
|
||||
|
||||
async function processDeleteActivity (activity: ActivityDelete) {
|
||||
const account = await getOrCreateAccount(activity.actor)
|
||||
const account = await getOrCreateAccountAndServer(activity.actor)
|
||||
|
||||
if (account.url === activity.id) {
|
||||
return processDeleteAccount(account)
|
||||
|
|
|
@ -4,11 +4,11 @@ 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'
|
||||
import { getOrCreateAccountAndServer } from '../account'
|
||||
|
||||
async function processFollowActivity (activity: ActivityFollow) {
|
||||
const activityObject = activity.object
|
||||
const account = await getOrCreateAccount(activity.actor)
|
||||
const account = await getOrCreateAccountAndServer(activity.actor)
|
||||
|
||||
return processFollow(account, activityObject)
|
||||
}
|
||||
|
|
|
@ -8,10 +8,10 @@ import { AccountInstance } from '../../../models/account/account-interface'
|
|||
import { VideoInstance } from '../../../models/video/video-interface'
|
||||
import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
|
||||
import Bluebird = require('bluebird')
|
||||
import { getOrCreateAccount } from '../account'
|
||||
import { getOrCreateAccountAndServer } from '../account'
|
||||
|
||||
async function processUpdateActivity (activity: ActivityUpdate) {
|
||||
const account = await getOrCreateAccount(activity.actor)
|
||||
const account = await getOrCreateAccountAndServer(activity.actor)
|
||||
|
||||
if (activity.object.type === 'Video') {
|
||||
return processUpdateVideo(account, activity.object)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue