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(*): cache channel permissions #45

Merged
merged 3 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
21 changes: 2 additions & 19 deletions src/channel-manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChannelRemove, ChannelState, PermissionQuery } from '@tf2pickup-org/mumble-protocol';
import { ChannelRemove, ChannelState } from '@tf2pickup-org/mumble-protocol';
import { tap } from 'rxjs';
import { Channel } from './channel';
import { Client } from './client';
Expand All @@ -25,13 +25,6 @@ export class ChannelManager {
tap(channelRemove => this.removeChannel(channelRemove)),
)
.subscribe();

socket.packet
.pipe(
filterPacket(PermissionQuery),
tap(permissionQuery => this.syncChannelPermissions(permissionQuery)),
)
.subscribe();
});
}

Expand Down Expand Up @@ -108,18 +101,8 @@ export class ChannelManager {
*/
this.client.emit('channelCreate', channel);
} else {
channel.sync(channelState);
}
}

/**
* @internal
*/
private syncChannelPermissions(permissionQuery: PermissionQuery) {
if (permissionQuery.channelId === undefined) {
return;
channel.syncState(channelState);
}
this.byId(permissionQuery.channelId)?.sync(permissionQuery);
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/channel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ChannelManager } from './channel-manager';
import { Client } from './client';
import { createChannel, removeChannel } from './commands';
import { MumbleSocket } from './mumble-socket';
import { Permissions } from './permissions';

jest.mock('./client');
jest.mock('./commands', () => ({
Expand All @@ -30,6 +31,8 @@ describe('Channel', () => {
byPath: jest.fn().mockResolvedValue({}),
findAll: jest.fn().mockReturnValue([]),
} as unknown as ChannelManager;
(client as { permissions: Map<number, Permissions> }).permissions =
new Map();
});

it('should assign properties', () => {
Expand Down Expand Up @@ -58,12 +61,12 @@ describe('Channel', () => {
});

it('should update name', () => {
channel.sync(ChannelState.create({ name: 'NEW_CHANNEL_NAME' }));
channel.syncState(ChannelState.create({ name: 'NEW_CHANNEL_NAME' }));
expect(channel.name).toEqual('NEW_CHANNEL_NAME');
});

it('should update parent', () => {
channel.sync(ChannelState.create({ parent: 10 }));
channel.syncState(ChannelState.create({ parent: 10 }));
expect(channel.parent).toEqual(10);
});
});
Expand Down
47 changes: 22 additions & 25 deletions src/channel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChannelState, PermissionQuery } from '@tf2pickup-org/mumble-protocol';
import { ChannelState } from '@tf2pickup-org/mumble-protocol';
import { Client } from './client';
import {
createChannel,
Expand All @@ -16,7 +16,6 @@ export class Channel {
name?: string;
parent?: number;
temporary: boolean;
private permissions?: Permissions;
private links: number[] = [];

constructor(
Expand All @@ -33,28 +32,26 @@ export class Channel {
/**
* @internal
*/
sync(message: unknown) {
if (ChannelState.is(message)) {
if (message.name !== undefined) {
this.name = message.name;
}

if (message.parent !== undefined) {
this.parent = message.parent;
}

if (message.temporary !== undefined) {
this.temporary = message.temporary;
}

this.links = [
...new Set([...this.links, ...message.links, ...message.linksAdd]),
].filter(l => !message.linksRemove.includes(l));
} else if (PermissionQuery.is(message)) {
if (message.permissions !== undefined) {
this.permissions = new Permissions(message.permissions);
}
syncState(channelState: ChannelState) {
if (channelState.name !== undefined) {
this.name = channelState.name;
}

if (channelState.parent !== undefined) {
this.parent = channelState.parent;
}

if (channelState.temporary !== undefined) {
this.temporary = channelState.temporary;
}

this.links = [
...new Set([
...this.links,
...channelState.links,
...channelState.linksAdd,
]),
].filter(l => !channelState.linksRemove.includes(l));
}

get users(): User[] {
Expand Down Expand Up @@ -100,8 +97,8 @@ export class Channel {
}

async getPermissions(): Promise<Permissions> {
if (this.permissions) {
return this.permissions;
if (this.client.permissions.has(this.id)) {
return this.client.permissions.get(this.id) as Permissions;
}

if (!this.client.socket) {
Expand Down
30 changes: 29 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { tlsConnect } from './tls-connect';
import { MumbleSocket } from './mumble-socket';
import { delay, exhaustMap, interval, map, race, take, tap, zip } from 'rxjs';
import {
delay,
exhaustMap,
filter,
interval,
map,
race,
take,
tap,
zip,
} from 'rxjs';
import {
Authenticate,
PermissionQuery,
Ping,
Reject,
ServerConfig,
Expand All @@ -18,6 +29,7 @@ import { ClientOptions } from './client-options';
import { ConnectionRejectedError } from './errors';
import { filterPacket } from './rxjs-operators/filter-packet';
import { platform, release } from 'os';
import { Permissions } from './permissions';

const defaultOptions: Partial<ClientOptions> = {
port: 64738,
Expand All @@ -35,6 +47,9 @@ export class Client extends EventEmitter {
welcomeText?: string;
readonly options: ClientOptions;

// Channel permission cache
readonly permissions = new Map<number, Permissions>();

constructor(options: ClientOptions) {
super();
this.options = { ...defaultOptions, ...options };
Expand All @@ -46,6 +61,19 @@ export class Client extends EventEmitter {
);
this.emit('socketConnected', this.socket);

this.socket.packet
.pipe(
filterPacket(PermissionQuery),
filter(permissionQuery => permissionQuery.channelId !== undefined),
map(permissionQuery => ({
channelId: permissionQuery.channelId as number,
permissions: new Permissions(permissionQuery.permissions ?? 0),
})),
)
.subscribe(({ channelId, permissions }) => {
this.permissions.set(channelId, permissions);
});

const initialize: Promise<this> = new Promise((resolve, reject) =>
race(
zip(
Expand Down