From e0a1da222a982243fb4a8cb37c27927b5c474dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Garapich?= Date: Tue, 7 Jun 2022 17:52:46 +0200 Subject: [PATCH] feat(*): support mute, deaf and suppress props --- src/change.ts | 2 +- src/user.spec.ts | 33 +++++++++++++++++++++++++ src/user.ts | 64 ++++++++++++++++++++++++------------------------ 3 files changed, 66 insertions(+), 33 deletions(-) diff --git a/src/change.ts b/src/change.ts index d127230..9cf921b 100644 --- a/src/change.ts +++ b/src/change.ts @@ -1,4 +1,4 @@ -export interface Change { +export interface Change { previousValue: T; currentValue: T; } diff --git a/src/user.spec.ts b/src/user.spec.ts index 111a054..2cb4245 100644 --- a/src/user.spec.ts +++ b/src/user.spec.ts @@ -64,6 +64,39 @@ describe('User', () => { }); }); + it('should update mute', () => { + user.syncState(UserState.create({ mute: true })); + expect(user.mute).toBe(true); + expect(client.emit).toHaveBeenCalledWith(EventNames.userUpdate, user, { + mute: { + previousValue: false, + currentValue: true, + }, + }); + }); + + it('should update deaf', () => { + user.syncState(UserState.create({ deaf: true })); + expect(user.deaf).toBe(true); + expect(client.emit).toHaveBeenCalledWith(EventNames.userUpdate, user, { + deaf: { + previousValue: false, + currentValue: true, + }, + }); + }); + + it('should update suppress', () => { + user.syncState(UserState.create({ suppress: true })); + expect(user.suppress).toBe(true); + expect(client.emit).toHaveBeenCalledWith(EventNames.userUpdate, user, { + suppress: { + previousValue: false, + currentValue: true, + }, + }); + }); + it('should update selfMute', () => { user.syncState(UserState.create({ selfMute: true })); expect(user.selfMute).toBe(true); diff --git a/src/user.ts b/src/user.ts index 66ccf39..be1fac3 100644 --- a/src/user.ts +++ b/src/user.ts @@ -10,8 +10,13 @@ import { moveUserToChannel, setSelfDeaf, setSelfMute } from './commands'; import { Change } from './change'; import { EventNames } from './event-names'; +type UserWritableProps = Pick< + User, + 'name' | 'channelId' | 'mute' | 'deaf' | 'suppress' | 'selfMute' | 'selfDeaf' +>; + export type UserChanges = { - [P in keyof Omit]?: Change; + [P in keyof UserWritableProps]?: Change; }; /** @@ -21,6 +26,9 @@ export class User { readonly session: number; name?: string; channelId = 0; + mute = false; + deaf = false; + suppress = false; selfMute = false; selfDeaf = false; @@ -48,37 +56,13 @@ export class User { syncState(userState: UserState, emitUpdate = true) { const changes: UserChanges = {}; - if (userState.name !== undefined) { - changes.name = { - previousValue: `${this.name}`, - currentValue: `${userState.name}`, - }; - this.name = userState.name; - } - - if (userState.channelId !== undefined) { - changes.channelId = { - previousValue: this.channelId, - currentValue: userState.channelId, - }; - this.channelId = userState.channelId; - } - - if (userState.selfMute !== undefined) { - changes.selfMute = { - previousValue: this.selfMute, - currentValue: userState.selfMute, - }; - this.selfMute = userState.selfMute; - } - - if (userState.selfDeaf !== undefined) { - changes.selfDeaf = { - previousValue: this.selfDeaf, - currentValue: userState.selfDeaf, - }; - this.selfDeaf = userState.selfDeaf; - } + this.syncProperty('name', userState.name, changes); + this.syncProperty('channelId', userState.channelId, changes); + this.syncProperty('mute', userState.mute, changes); + this.syncProperty('deaf', userState.deaf, changes); + this.syncProperty('suppress', userState.suppress, changes); + this.syncProperty('selfMute', userState.selfMute, changes); + this.syncProperty('selfDeaf', userState.selfDeaf, changes); if (emitUpdate && Object.keys(changes).length > 0) { /** @@ -147,4 +131,20 @@ export class User { await setSelfDeaf(this.client.socket, this.session, selfDeaf); return this; } + + private syncProperty( + propertyName: R, + newValue: this[R] | undefined, + changes: UserChanges, + ) { + if (newValue === undefined) { + return; + } + + (changes[propertyName] as Change) = { + previousValue: this[propertyName], + currentValue: newValue, + }; + this[propertyName] = newValue; + } }