]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Introduce socket io command
authorChocobozzz <me@florianbigard.com>
Wed, 7 Jul 2021 09:55:16 +0000 (11:55 +0200)
committerChocobozzz <me@florianbigard.com>
Tue, 20 Jul 2021 13:27:17 +0000 (15:27 +0200)
server/tests/api/live/live-socket-messages.ts
shared/extra-utils/index.ts
shared/extra-utils/server/servers.ts
shared/extra-utils/socket/index.ts [new file with mode: 0644]
shared/extra-utils/socket/socket-io-command.ts [new file with mode: 0644]
shared/extra-utils/socket/socket-io.ts [deleted file]
shared/extra-utils/users/user-notifications.ts

index 0159d5199ba1693a063a1f101898693bce2302ca..20fec16a9998261d49c7612ac8f3adb0b7a8e0e2 100644 (file)
@@ -2,7 +2,6 @@
 
 import 'mocha'
 import * as chai from 'chai'
-import { getLiveNotificationSocket } from '@shared/extra-utils/socket/socket-io'
 import { VideoPrivacy, VideoState } from '@shared/models'
 import {
   cleanupTests,
@@ -77,7 +76,7 @@ describe('Test live', function () {
       {
         const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
 
-        const localSocket = getLiveNotificationSocket(servers[0].url)
+        const localSocket = servers[0].socketIOCommand.getLiveNotificationSocket()
         localSocket.on('state-change', data => localStateChanges.push(data.state))
         localSocket.emit('subscribe', { videoId })
       }
@@ -85,7 +84,7 @@ describe('Test live', function () {
       {
         const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
 
-        const remoteSocket = getLiveNotificationSocket(servers[1].url)
+        const remoteSocket = servers[1].socketIOCommand.getLiveNotificationSocket()
         remoteSocket.on('state-change', data => remoteStateChanges.push(data.state))
         remoteSocket.emit('subscribe', { videoId })
       }
@@ -125,7 +124,7 @@ describe('Test live', function () {
       {
         const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
 
-        const localSocket = getLiveNotificationSocket(servers[0].url)
+        const localSocket = servers[0].socketIOCommand.getLiveNotificationSocket()
         localSocket.on('views-change', data => { localLastVideoViews = data.views })
         localSocket.emit('subscribe', { videoId })
       }
@@ -133,7 +132,7 @@ describe('Test live', function () {
       {
         const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID)
 
-        const remoteSocket = getLiveNotificationSocket(servers[1].url)
+        const remoteSocket = servers[1].socketIOCommand.getLiveNotificationSocket()
         remoteSocket.on('views-change', data => { remoteLastVideoViews = data.views })
         remoteSocket.emit('subscribe', { videoId })
       }
@@ -169,7 +168,7 @@ describe('Test live', function () {
 
       const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID)
 
-      const socket = getLiveNotificationSocket(servers[0].url)
+      const socket = servers[0].socketIOCommand.getLiveNotificationSocket()
       socket.on('state-change', data => stateChanges.push(data.state))
       socket.emit('subscribe', { videoId })
 
index f69d7241e698b60ac9f21f7576b1db7cc5bda391..ed7d7ab04d7efbc44c8a453405c0518d01c45677 100644 (file)
@@ -9,6 +9,7 @@ export * from './moderation'
 export * from './overviews'
 export * from './search'
 export * from './server'
+export * from './socket'
 
 export * from './requests/check-api-params'
 export * from './requests/requests'
index c33b68316beced95c9c8bf96b7c6f1aa24dc9e48..8bd4446be1f8e09cad0564b6ae8db276ea69484e 100644 (file)
@@ -16,6 +16,7 @@ import { AbusesCommand } from '../moderation'
 import { OverviewsCommand } from '../overviews'
 import { makeGetRequest } from '../requests/requests'
 import { SearchCommand } from '../search'
+import { SocketIOCommand } from '../socket'
 import { ConfigCommand } from './config-command'
 import { ContactFormCommand } from './contact-form-command'
 import { DebugCommand } from './debug-command'
@@ -93,6 +94,7 @@ interface ServerInfo {
   redundancyCommand?: RedundancyCommand
   statsCommand?: StatsCommand
   configCommand?: ConfigCommand
+  socketIOCommand?: SocketIOCommand
 }
 
 function parallelTests () {
@@ -314,6 +316,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = []
       server.redundancyCommand = new RedundancyCommand(server)
       server.statsCommand = new StatsCommand(server)
       server.configCommand = new ConfigCommand(server)
+      server.socketIOCommand = new SocketIOCommand(server)
 
       res(server)
     })
diff --git a/shared/extra-utils/socket/index.ts b/shared/extra-utils/socket/index.ts
new file mode 100644 (file)
index 0000000..594329b
--- /dev/null
@@ -0,0 +1 @@
+export * from './socket-io-command'
diff --git a/shared/extra-utils/socket/socket-io-command.ts b/shared/extra-utils/socket/socket-io-command.ts
new file mode 100644 (file)
index 0000000..545561b
--- /dev/null
@@ -0,0 +1,15 @@
+import { io } from 'socket.io-client'
+import { AbstractCommand, OverrideCommandOptions } from '../shared'
+
+export class SocketIOCommand extends AbstractCommand {
+
+  getUserNotificationSocket (options: OverrideCommandOptions = {}) {
+    return io(this.server.url + '/user-notifications', {
+      query: { accessToken: this.server.accessToken }
+    })
+  }
+
+  getLiveNotificationSocket () {
+    return io(this.server.url + '/live-videos')
+  }
+}
diff --git a/shared/extra-utils/socket/socket-io.ts b/shared/extra-utils/socket/socket-io.ts
deleted file mode 100644 (file)
index 4ca93f4..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-import { io } from 'socket.io-client'
-
-function getUserNotificationSocket (serverUrl: string, accessToken: string) {
-  return io(serverUrl + '/user-notifications', {
-    query: { accessToken }
-  })
-}
-
-function getLiveNotificationSocket (serverUrl: string) {
-  return io(serverUrl + '/live-videos')
-}
-
-// ---------------------------------------------------------------------------
-
-export {
-  getUserNotificationSocket,
-  getLiveNotificationSocket
-}
index e75946eec1aae5cf4a786a7a071e1f9c2b53b429..961cfcc0f5d0514d1df863c786d7a01f4c31dc50 100644 (file)
@@ -9,7 +9,6 @@ import { MockSmtpServer } from '../mock-servers/mock-email'
 import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests'
 import { doubleFollow } from '../server/follows'
 import { flushAndRunMultipleServers, ServerInfo } from '../server/servers'
-import { getUserNotificationSocket } from '../socket/socket-io'
 import { setAccessTokensToServers, userLogin } from './login'
 import { createUser, getMyUserInformation } from './users'
 
@@ -748,16 +747,16 @@ async function prepareNotificationsTest (serversCount = 3, overrideConfigArg: an
   }
 
   {
-    const socket = getUserNotificationSocket(servers[0].url, userAccessToken)
+    const socket = servers[0].socketIOCommand.getUserNotificationSocket({ token: userAccessToken })
     socket.on('new-notification', n => userNotifications.push(n))
   }
   {
-    const socket = getUserNotificationSocket(servers[0].url, servers[0].accessToken)
+    const socket = servers[0].socketIOCommand.getUserNotificationSocket()
     socket.on('new-notification', n => adminNotifications.push(n))
   }
 
   if (serversCount > 1) {
-    const socket = getUserNotificationSocket(servers[1].url, servers[1].accessToken)
+    const socket = servers[1].socketIOCommand.getUserNotificationSocket()
     socket.on('new-notification', n => adminNotificationsServer2.push(n))
   }