From 87e2635a50ac2decb1c330e55c2ff7b6d07a85de Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 7 Jul 2021 11:55:16 +0200 Subject: [PATCH] Introduce socket io command --- server/tests/api/live/live-socket-messages.ts | 11 +++++------ shared/extra-utils/index.ts | 1 + shared/extra-utils/server/servers.ts | 3 +++ shared/extra-utils/socket/index.ts | 1 + shared/extra-utils/socket/socket-io-command.ts | 15 +++++++++++++++ shared/extra-utils/socket/socket-io.ts | 18 ------------------ shared/extra-utils/users/user-notifications.ts | 7 +++---- 7 files changed, 28 insertions(+), 28 deletions(-) create mode 100644 shared/extra-utils/socket/index.ts create mode 100644 shared/extra-utils/socket/socket-io-command.ts delete mode 100644 shared/extra-utils/socket/socket-io.ts diff --git a/server/tests/api/live/live-socket-messages.ts b/server/tests/api/live/live-socket-messages.ts index 0159d5199..20fec16a9 100644 --- a/server/tests/api/live/live-socket-messages.ts +++ b/server/tests/api/live/live-socket-messages.ts @@ -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 }) diff --git a/shared/extra-utils/index.ts b/shared/extra-utils/index.ts index f69d7241e..ed7d7ab04 100644 --- a/shared/extra-utils/index.ts +++ b/shared/extra-utils/index.ts @@ -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' diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index c33b68316..8bd4446be 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts @@ -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 index 000000000..594329b2f --- /dev/null +++ b/shared/extra-utils/socket/index.ts @@ -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 index 000000000..545561bed --- /dev/null +++ b/shared/extra-utils/socket/socket-io-command.ts @@ -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 index 4ca93f453..000000000 --- a/shared/extra-utils/socket/socket-io.ts +++ /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 -} diff --git a/shared/extra-utils/users/user-notifications.ts b/shared/extra-utils/users/user-notifications.ts index e75946eec..961cfcc0f 100644 --- a/shared/extra-utils/users/user-notifications.ts +++ b/shared/extra-utils/users/user-notifications.ts @@ -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)) }