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

feat(*): support mute, deaf and suppress props #66

Merged
merged 1 commit into from
Jun 7, 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
2 changes: 1 addition & 1 deletion src/change.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface Change<T = unknown> {
export interface Change<T> {
previousValue: T;
currentValue: T;
}
33 changes: 33 additions & 0 deletions src/user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
64 changes: 32 additions & 32 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<User, 'session'>]?: Change<User[P]>;
[P in keyof UserWritableProps]?: Change<User[P]>;
};

/**
Expand All @@ -21,6 +26,9 @@ export class User {
readonly session: number;
name?: string;
channelId = 0;
mute = false;
deaf = false;
suppress = false;
selfMute = false;
selfDeaf = false;

Expand Down Expand Up @@ -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) {
/**
Expand Down Expand Up @@ -147,4 +131,20 @@ export class User {
await setSelfDeaf(this.client.socket, this.session, selfDeaf);
return this;
}

private syncProperty<R extends keyof UserWritableProps>(
propertyName: R,
newValue: this[R] | undefined,
changes: UserChanges,
) {
if (newValue === undefined) {
return;
}

(changes[propertyName] as Change<User[R]>) = {
previousValue: this[propertyName],
currentValue: newValue,
};
this[propertyName] = newValue;
}
}