PeerTube/server/lib/activitypub/videos/shared/trackers.ts

44 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-06-02 09:35:01 +02:00
import { Transaction } from 'sequelize/types'
import { isAPVideoTrackerUrlObject } from '@server/helpers/custom-validators/activitypub/videos'
import { isArray } from '@server/helpers/custom-validators/misc'
import { REMOTE_SCHEME } from '@server/initializers/constants'
import { TrackerModel } from '@server/models/server/tracker'
import { MVideo, MVideoWithHost } from '@server/types/models'
import { ActivityTrackerUrlObject, VideoObject } from '@shared/models'
import { buildRemoteVideoBaseUrl } from '../../url'
2021-06-02 09:35:01 +02:00
function getTrackerUrls (object: VideoObject, video: MVideoWithHost) {
let wsFound = false
const trackers = object.url.filter(u => isAPVideoTrackerUrlObject(u))
.map((u: ActivityTrackerUrlObject) => {
if (isArray(u.rel) && u.rel.includes('websocket')) wsFound = true
return u.href
})
if (wsFound) return trackers
return [
buildRemoteVideoBaseUrl(video, '/tracker/socket', REMOTE_SCHEME.WS),
buildRemoteVideoBaseUrl(video, '/tracker/announce')
]
}
async function setVideoTrackers (options: {
video: MVideo
trackers: string[]
2021-06-08 17:29:45 +02:00
transaction: Transaction
2021-06-02 09:35:01 +02:00
}) {
const { video, trackers, transaction } = options
const trackerInstances = await TrackerModel.findOrCreateTrackers(trackers, transaction)
await video.$set('Trackers', trackerInstances, { transaction })
}
export {
getTrackerUrls,
setVideoTrackers
}