From c5aae31973ae79bc885a3842ee6741c353a3104b Mon Sep 17 00:00:00 2001 From: emerson Date: Mon, 6 Dec 2021 12:07:40 -0500 Subject: [PATCH] move sync state to user and add message sending --- src/Client.ts | 42 ++++++++++++++++++++++-------------------- src/IRCUser.ts | 35 +++++++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/src/Client.ts b/src/Client.ts index 773d1dc..c457e5a 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -15,7 +15,6 @@ export class Client { localUsername: string localRealname: string deviceId: string - initialSync: boolean constructor(private socket: Socket, public server: Server) { this.user = null; this.capVersion = '301'; @@ -36,7 +35,6 @@ export class Client { this.localUsername = 'none'; this.localRealname = 'none'; this.deviceId = ""; - this.initialSync = false; this.socket.on('data', (data) => this.receiveData(data)); //this.socket.on('close', (e) => {if (this.user) this.user.handleClientClose(this, e)}); } @@ -179,24 +177,28 @@ export class Client { this.user = thisIRCUser; this.sendMessage(this.server.name, '900', numerics['900'](this.user.getMask(), this.user.nick), new Map()); this.sendMessage(this.server.name, '903', numerics['903'](this.user.nick), new Map()); - axios.get(`https://matrix.org/_matrix/client/v3/sync?access_token=${accessToken}`).then(response => { - const data = response.data; - const rooms = data.rooms; - if (this.user === null) - return; - if (rooms['join']) { - for (const roomId of Object.keys(rooms.join)) { - const targetChannel = this.server.matrixRooms.get(roomId) || new Channel(roomId, this.server); - this.user.channels.set(targetChannel.name, targetChannel); - rooms.join[roomId].state.events.forEach((nextEvent: any) => targetChannel.routeMatrixEvent(nextEvent)); - } - } - if (this.user === null) - return; - this.user.nextBatch = data.next_batch; - console.log(this.user.nextBatch); + if (this.user.isSynced()) { this.user.addClient(this, message.tags); - }) + } else { + axios.get(`https://matrix.org/_matrix/client/v3/sync?access_token=${accessToken}`).then(response => { + const data = response.data; + const rooms = data.rooms; + if (this.user === null) + return; + if (rooms['join']) { + for (const roomId of Object.keys(rooms.join)) { + const targetChannel = this.server.matrixRooms.get(roomId) || new Channel(roomId, this.server); + this.user.channels.set(targetChannel.name, targetChannel); + rooms.join[roomId].state.events.forEach((nextEvent: any) => targetChannel.routeMatrixEvent(nextEvent)); + } + } + if (this.user === null) + return; + this.user.nextBatch = data.next_batch; + console.log(this.user.nextBatch); + this.user.addClient(this, message.tags); + }) + } } }) } @@ -228,7 +230,7 @@ export class Client { this.sendMessage(this.server.name, '376', numerics['376'](this.user.nick), message.tags); this.sendMessage(this.user.nick, 'MODE', [this.user.nick, '+i'], message.tags); - if (!this.user.isUserSynced()) + if (!this.user.isSynced()) this.sendMessage(this.server.name, 'NOTICE', [this.user.nick, 'Please wait for initial sync, this may take a while if you are in many large channels'], message.tags); } diff --git a/src/IRCUser.ts b/src/IRCUser.ts index 4eea59d..22f65d3 100644 --- a/src/IRCUser.ts +++ b/src/IRCUser.ts @@ -13,6 +13,7 @@ export class IRCUser { public realname: string private txnIdStore: Set public nextBatch: string + private initialSync: boolean constructor(public mxid: string, private accessToken: string, private server: Server) { this.clients = new Set(); this.channels = new Map(); @@ -24,10 +25,11 @@ export class IRCUser { this.realname = this.accountName; this.txnIdStore = new Set(); this.nextBatch = ""; + this.initialSync = false; } - isUserSynced() { - return this.nextBatch !== ""; + isSynced() { + return this.initialSync; } getVerification() { @@ -51,10 +53,7 @@ export class IRCUser { } doSync(): void { - let endpoint = `https://matrix.org/_matrix/client/v3/sync?access_token=${this.accessToken}`; - if (this.nextBatch !== "") - endpoint = `${endpoint}&since=${this.nextBatch}`; - + const endpoint = `https://matrix.org/_matrix/client/v3/sync?access_token=${this.accessToken}&since=${this.nextBatch}`; axios.get(endpoint).then(response => { const data = response.data; this.nextBatch = data.next_batch; @@ -67,4 +66,28 @@ export class IRCUser { } }) } + + sendToAll(prefix: string, command: string, params: string[], tags: Map = new Map(), skipClient: Client|null = null) { + this.clients.forEach(client => { + if (client !== skipClient) { + client.sendMessage(prefix, command, params, tags); + } + }) + } + + sendToAllWithCap(cap: string, prefix: string, command: string, params: string[], tags: Map = new Map(), skipClient: Client|null = null) { + this.clients.forEach(client => { + if (client !== skipClient && client.enabledCaps.has(cap)) { + client.sendMessage(prefix, command, params, tags); + } + }) + } + + sendToAllWithoutCap(cap: string, prefix: string, command: string, params: string[], tags: Map = new Map(), skipClient: Client|null = null) { + this.clients.forEach(client => { + if (client !== skipClient && !client.enabledCaps.has(cap)) { + client.sendMessage(prefix, command, params, tags); + } + }) + } } \ No newline at end of file