generalize lazy join logic into one function

This commit is contained in:
emerson 2022-05-02 18:27:35 -04:00
parent 8214668f14
commit da1c006e69

View file

@ -179,6 +179,16 @@ export class Server {
client.sendTOPIC(targetChannel); 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) { addClient(client: Client) {
this.clients.add(client); this.clients.add(client);
if (this.initialSync) { if (this.initialSync) {
@ -260,13 +270,7 @@ export class Server {
handleMatrixReaction(event: any, targetChannel: Channel) { handleMatrixReaction(event: any, targetChannel: Channel) {
const sourceUser = this.getOrCreateMatrixUser(event["sender"]); const sourceUser = this.getOrCreateMatrixUser(event["sender"]);
if (!targetChannel.matrixUsers.has(sourceUser.nick)) { this.checkForLazyJoin(event, sourceUser, targetChannel);
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);
}
const tags: Map<string, string> = new Map(); const tags: Map<string, string> = new Map();
tags.set('msgid', event["event_id"]); tags.set('msgid', event["event_id"]);
tags.set('account', sourceUser.accountName); tags.set('account', sourceUser.accountName);
@ -308,13 +312,7 @@ export class Server {
handleEncryptedMessage(event: any, targetChannel: Channel) { handleEncryptedMessage(event: any, targetChannel: Channel) {
const sourceUser = this.getOrCreateMatrixUser(event["sender"]); const sourceUser = this.getOrCreateMatrixUser(event["sender"]);
if (!targetChannel.matrixUsers.has(sourceUser.nick)) { this.checkForLazyJoin(event, sourceUser, targetChannel);
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);
}
const messageTags = new Map(); const messageTags = new Map();
messageTags.set('msgid', event["event_id"]); messageTags.set('msgid', event["event_id"]);
messageTags.set('time', new Date(event["origin_server_ts"]).toISOString()); messageTags.set('time', new Date(event["origin_server_ts"]).toISOString());
@ -375,13 +373,7 @@ export class Server {
}); });
} }
else if (membershipStatus === "join") { else if (membershipStatus === "join") {
if (!targetChannel.matrixUsers.has(targetUser.nick)) { this.checkForLazyJoin(event, sourceUser, targetChannel);
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);
}
} }
else if (membershipStatus === "leave") { else if (membershipStatus === "leave") {
if (!targetChannel.matrixUsers.has(targetUser.nick)) if (!targetChannel.matrixUsers.has(targetUser.nick))
@ -416,13 +408,7 @@ export class Server {
handleMatrixMessage(event: any, targetChannel: Channel) { handleMatrixMessage(event: any, targetChannel: Channel) {
const sourceUser = this.getOrCreateMatrixUser(event["sender"]); const sourceUser = this.getOrCreateMatrixUser(event["sender"]);
if (!targetChannel.matrixUsers.has(sourceUser.nick)) { this.checkForLazyJoin(event, sourceUser, targetChannel);
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);
}
const content = event["content"]; const content = event["content"];
const msgtype = content["msgtype"]; const msgtype = content["msgtype"];
let messageContent = content["body"]; let messageContent = content["body"];
@ -526,13 +512,7 @@ export class Server {
handleMatrixRedaction(event: any, targetChannel: Channel) { handleMatrixRedaction(event: any, targetChannel: Channel) {
const sourceUser = this.getOrCreateMatrixUser(event["sender"]); const sourceUser = this.getOrCreateMatrixUser(event["sender"]);
if (!targetChannel.matrixUsers.has(sourceUser.nick)) { this.checkForLazyJoin(event, sourceUser, targetChannel);
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);
}
const reason = event["content"]?.["reason"] || ""; const reason = event["content"]?.["reason"] || "";
const tags: Map<string, string> = new Map(); const tags: Map<string, string> = new Map();
tags.set('reflectionircd.chat/delete-message', event["redacts"]); tags.set('reflectionircd.chat/delete-message', event["redacts"]);