Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(*): add setSelfDeaf command #48

Merged
merged 4 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,16 @@ jobs:
uses: actions/checkout@v3

- name: Start mumble server
run: docker run --rm --detach --name mumble-server -p 64738:64738/tcp -p 64738:64738/udp --volume ${{ github.workspace }}/e2e/mumble-data:/data --user root mumblevoip/mumble-server:latest
run: |
docker run \
--rm --detach \
--name mumble-server \
-p 64738:64738/tcp \
-p 64738:64738/udp \
-e MUMBLE_CONFIG_AUTOBAN_ATTEMPTS=0 \
--volume ${{ github.workspace }}/e2e/mumble-data:/data \
--user root \
mumblevoip/mumble-server:latest

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
Expand Down
29 changes: 29 additions & 0 deletions e2e/self-deafens.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { Client } from '@';
import { waitABit } from './utils/wait-a-bit';

describe('Sets self-deaf (e2e)', () => {
let client: Client;

beforeAll(async () => {
client = new Client({
host: 'localhost',
port: 64738,
username: 'tester',
rejectUnauthorized: false,
});
await client.connect();
await waitABit(1000);
});

afterAll(async () => {
await waitABit(1000);
client.disconnect();
});

it('should set self deaf', async () => {
expect(client.user).toBeTruthy();
await client.user!.setSelfDeaf(true);
expect(client.user!.selfDeaf).toBe(true);
});
});
1 change: 1 addition & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export { fetchChannelPermissions } from './fetch-channel-permissions';
export { linkChannels } from './link-channels';
export { moveUserToChannel } from './move-user-to-channel';
export { removeChannel } from './remove-channel';
export { setSelfDeaf } from './set-self-deaf';
export { setSelfMute } from './set-self-mute';
export { unlinkChannels } from './unlink-channels';
28 changes: 28 additions & 0 deletions src/commands/set-self-deaf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { CommandTimeout } from '@/config';
import { CommandTimedOutError } from '@/errors';
import { MumbleSocket } from '@/mumble-socket';
import { filterPacket } from '@/rxjs-operators/filter-packet';
import { UserState } from '@tf2pickup-org/mumble-protocol';
import { filter, lastValueFrom, map, take, throwError, timeout } from 'rxjs';

export const setSelfDeaf = async (
socket: MumbleSocket,
userSession: number,
selfDeaf: boolean,
): Promise<boolean> => {
const ret = lastValueFrom(
socket.packet.pipe(
filterPacket(UserState),
filter(userState => userState.session === userSession),
filter(userState => userState.selfDeaf !== undefined),
take(1),
map(userState => userState.selfDeaf as boolean),
timeout({
first: CommandTimeout,
with: () => throwError(() => new CommandTimedOutError('setSelfDeaf')),
}),
),
);
socket.send(UserState, UserState.create({ session: userSession, selfDeaf }));
return ret;
};
24 changes: 7 additions & 17 deletions src/user.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { filter, takeWhile } from 'rxjs';
import { UserState } from '@tf2pickup-org/mumble-protocol';
import { Client } from './client';
import { Channel } from './channel';
import { InsufficientPermissionsError, NoSuchChannelError } from './errors';
import { filterPacket } from './rxjs-operators/filter-packet';
import { moveUserToChannel, setSelfMute } from './commands';
import { moveUserToChannel, setSelfDeaf, setSelfMute } from './commands';

export class User {
readonly session: number;
Expand Down Expand Up @@ -84,20 +82,12 @@ export class User {
return this;
}

setSelfDeaf(selfDeaf: boolean): Promise<void> {
return new Promise(resolve => {
this.client.socket?.packet
.pipe(
filterPacket(UserState),
filter(userState => userState.session === this.session),
takeWhile(userState => userState.selfDeaf === selfDeaf, true),
)
.subscribe(() => resolve());
async setSelfDeaf(selfDeaf: boolean): Promise<this> {
if (!this.client.socket) {
throw new Error('no socket');
}

this.client.socket?.send(
UserState,
UserState.create({ session: this.session, selfDeaf }),
);
});
await setSelfDeaf(this.client.socket, this.session, selfDeaf);
return this;
}
}