aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/videos/shared/trackers.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-02 09:35:01 +0200
committerChocobozzz <me@florianbigard.com>2021-06-02 16:57:53 +0200
commit69290ab37b8aead01477b9b98fdfad0e69b08582 (patch)
tree4b93d349dfce5014925e7f060b3b158ac9b3bbc2 /server/lib/activitypub/videos/shared/trackers.ts
parent81628e5069e0168b11857f276fe8e03b93102dde (diff)
downloadPeerTube-69290ab37b8aead01477b9b98fdfad0e69b08582.tar.gz
PeerTube-69290ab37b8aead01477b9b98fdfad0e69b08582.tar.zst
PeerTube-69290ab37b8aead01477b9b98fdfad0e69b08582.zip
Refactor AP video update
Diffstat (limited to 'server/lib/activitypub/videos/shared/trackers.ts')
-rw-r--r--server/lib/activitypub/videos/shared/trackers.ts43
1 files changed, 43 insertions, 0 deletions
diff --git a/server/lib/activitypub/videos/shared/trackers.ts b/server/lib/activitypub/videos/shared/trackers.ts
new file mode 100644
index 000000000..fcb2a5091
--- /dev/null
+++ b/server/lib/activitypub/videos/shared/trackers.ts
@@ -0,0 +1,43 @@
1import { Transaction } from 'sequelize/types'
2import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub'
3import { isAPVideoTrackerUrlObject } from '@server/helpers/custom-validators/activitypub/videos'
4import { isArray } from '@server/helpers/custom-validators/misc'
5import { REMOTE_SCHEME } from '@server/initializers/constants'
6import { TrackerModel } from '@server/models/server/tracker'
7import { MVideo, MVideoWithHost } from '@server/types/models'
8import { ActivityTrackerUrlObject, VideoObject } from '@shared/models'
9
10function getTrackerUrls (object: VideoObject, video: MVideoWithHost) {
11 let wsFound = false
12
13 const trackers = object.url.filter(u => isAPVideoTrackerUrlObject(u))
14 .map((u: ActivityTrackerUrlObject) => {
15 if (isArray(u.rel) && u.rel.includes('websocket')) wsFound = true
16
17 return u.href
18 })
19
20 if (wsFound) return trackers
21
22 return [
23 buildRemoteVideoBaseUrl(video, '/tracker/socket', REMOTE_SCHEME.WS),
24 buildRemoteVideoBaseUrl(video, '/tracker/announce')
25 ]
26}
27
28async function setVideoTrackers (options: {
29 video: MVideo
30 trackers: string[]
31 transaction?: Transaction
32}) {
33 const { video, trackers, transaction } = options
34
35 const trackerInstances = await TrackerModel.findOrCreateTrackers(trackers, transaction)
36
37 await video.$set('Trackers', trackerInstances, { transaction })
38}
39
40export {
41 getTrackerUrls,
42 setVideoTrackers
43}