diff --git a/src/Server.ts b/src/Server.ts index f7f53fd..b4ec5f7 100644 --- a/src/Server.ts +++ b/src/Server.ts @@ -179,6 +179,16 @@ export class Server { client.sendTOPIC(targetChannel); } + checkForLazyJoin(event: any, sourceUser: MatrixUser, targetChannel: Channel) { + if (!targetChannel.matrixUsers.has(sourceUser.nick)) { + targetChannel.matrixUsers.set(sourceUser.nick, sourceUser); + const prefix = sourceUser.getMask(); + const joinTags = new Map([["account", sourceUser.accountName], ['time', new Date(event["origin_server_ts"]).toISOString()]]) + this.sendToAllWithCap('extended-join', prefix, "JOIN", [targetChannel.name, sourceUser.accountName, sourceUser.realname], joinTags); + this.sendToAllWithoutCap('extended-join', prefix, "JOIN", [targetChannel.name], joinTags); + } + } + addClient(client: Client) { this.clients.add(client); if (this.initialSync) { @@ -260,13 +270,7 @@ export class Server { handleMatrixReaction(event: any, targetChannel: Channel) { const sourceUser = this.getOrCreateMatrixUser(event["sender"]); - if (!targetChannel.matrixUsers.has(sourceUser.nick)) { - targetChannel.matrixUsers.set(sourceUser.nick, sourceUser); - const prefix = sourceUser.getMask(); - const joinTags = new Map([["account", sourceUser.accountName], ['time', new Date(event["origin_server_ts"]).toISOString()]]) - this.sendToAllWithCap('extended-join', prefix, "JOIN", [targetChannel.name, sourceUser.accountName, sourceUser.realname], joinTags); - this.sendToAllWithoutCap('extended-join', prefix, "JOIN", [targetChannel.name], joinTags); - } + this.checkForLazyJoin(event, sourceUser, targetChannel); const tags: Map = new Map(); tags.set('msgid', event["event_id"]); tags.set('account', sourceUser.accountName); @@ -308,13 +312,7 @@ export class Server { handleEncryptedMessage(event: any, targetChannel: Channel) { const sourceUser = this.getOrCreateMatrixUser(event["sender"]); - if (!targetChannel.matrixUsers.has(sourceUser.nick)) { - targetChannel.matrixUsers.set(sourceUser.nick, sourceUser); - const prefix = sourceUser.getMask(); - const joinTags = new Map([["account", sourceUser.accountName], ['time', new Date(event["origin_server_ts"]).toISOString()]]) - this.sendToAllWithCap('extended-join', prefix, "JOIN", [targetChannel.name, sourceUser.accountName, sourceUser.realname], joinTags); - this.sendToAllWithoutCap('extended-join', prefix, "JOIN", [targetChannel.name], joinTags); - } + this.checkForLazyJoin(event, sourceUser, targetChannel); const messageTags = new Map(); messageTags.set('msgid', event["event_id"]); messageTags.set('time', new Date(event["origin_server_ts"]).toISOString()); @@ -375,13 +373,7 @@ export class Server { }); } else if (membershipStatus === "join") { - if (!targetChannel.matrixUsers.has(targetUser.nick)) { - targetChannel.matrixUsers.set(targetUser.nick, targetUser); - const prefix = targetUser.getMask(); - const joinTags = new Map([["account", targetUser.accountName], ['time', new Date(event["origin_server_ts"]).toISOString()]]) - this.sendToAllWithCap('extended-join', prefix, "JOIN", [targetChannel.name, targetUser.accountName, targetUser.realname], joinTags); - this.sendToAllWithoutCap('extended-join', prefix, "JOIN", [targetChannel.name], joinTags); - } + this.checkForLazyJoin(event, sourceUser, targetChannel); } else if (membershipStatus === "leave") { if (!targetChannel.matrixUsers.has(targetUser.nick)) @@ -416,13 +408,7 @@ export class Server { handleMatrixMessage(event: any, targetChannel: Channel) { const sourceUser = this.getOrCreateMatrixUser(event["sender"]); - if (!targetChannel.matrixUsers.has(sourceUser.nick)) { - targetChannel.matrixUsers.set(sourceUser.nick, sourceUser); - const prefix = sourceUser.getMask(); - const joinTags = new Map([["account", sourceUser.accountName], ['time', new Date(event["origin_server_ts"]).toISOString()]]) - this.sendToAllWithCap('extended-join', prefix, "JOIN", [targetChannel.name, sourceUser.accountName, sourceUser.realname], joinTags); - this.sendToAllWithoutCap('extended-join', prefix, "JOIN", [targetChannel.name], joinTags); - } + this.checkForLazyJoin(event, sourceUser, targetChannel); const content = event["content"]; const msgtype = content["msgtype"]; let messageContent = content["body"]; @@ -526,13 +512,7 @@ export class Server { handleMatrixRedaction(event: any, targetChannel: Channel) { const sourceUser = this.getOrCreateMatrixUser(event["sender"]); - if (!targetChannel.matrixUsers.has(sourceUser.nick)) { - targetChannel.matrixUsers.set(sourceUser.nick, sourceUser); - const prefix = sourceUser.getMask(); - const joinTags = new Map([["account", sourceUser.accountName], ['time', new Date(event["origin_server_ts"]).toISOString()]]) - this.sendToAllWithCap('extended-join', prefix, "JOIN", [targetChannel.name, sourceUser.accountName, sourceUser.realname], joinTags); - this.sendToAllWithoutCap('extended-join', prefix, "JOIN", [targetChannel.name], joinTags); - } + this.checkForLazyJoin(event, sourceUser, targetChannel); const reason = event["content"]?.["reason"] || ""; const tags: Map = new Map(); tags.set('reflectionircd.chat/delete-message', event["redacts"]);