Skip to content

Commit

Permalink
fix: join room on reaction events
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco committed Jul 6, 2023
1 parent 8725ea1 commit 2ac843f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1197,10 +1197,6 @@ export class DiscordBot {
const reactionName = reaction.emoji.name;
log.verbose(`Got message reaction add event for ${message.id} with ${reactionName}`);

const intent = this.GetIntentFromDiscordMember(user);
await intent.ensureRegistered();
this.userActivity.updateUserActivity(intent.userId);

const storeEvent = await this.store.Get(DbEvent, {
discord_id: message.id
});
Expand All @@ -1210,8 +1206,21 @@ export class DiscordBot {
return;
}

const intent = this.GetIntentFromDiscordMember(user);
await intent.ensureRegistered();
this.userActivity.updateUserActivity(intent.userId);

while (storeEvent.Next()) {
const [ eventId, roomId ] = storeEvent.MatrixId.split(";");

// If the user is partial, only forward the event for rooms they're already in.
if (user.partial) {
log.warn(`Skipping reaction add for user with Discord ID ${user.id} in ${roomId}. User was partial.`);
continue;
}

await this.userSync.JoinRoom(user, roomId);

const reactionEventId = await intent.underlyingClient.unstableApis.addReactionToEvent(
roomId,
eventId,
Expand All @@ -1234,10 +1243,6 @@ export class DiscordBot {
const message = reaction.message;
log.verbose(`Got message reaction remove event for ${message.id} with ${reaction.emoji.name}`);

const intent = this.GetIntentFromDiscordMember(user);
await intent.ensureRegistered();
this.userActivity.updateUserActivity(intent.userId);

const storeEvent = await this.store.Get(DbEvent, {
discord_id: message.id,
});
Expand All @@ -1247,8 +1252,21 @@ export class DiscordBot {
return;
}

const intent = this.GetIntentFromDiscordMember(user);
await intent.ensureRegistered();
this.userActivity.updateUserActivity(intent.userId);

while (storeEvent.Next()) {
const [ eventId, roomId ] = storeEvent.MatrixId.split(";");

// If the user is partial, only forward the event for rooms they're already in.
if (user.partial) {
log.warn(`Skipping reaction remove for user with Discord ID ${user.id} in ${roomId}. User was partial.`);
continue;
}

await this.userSync.JoinRoom(user, roomId);

const underlyingClient = intent.underlyingClient;

const { chunk } = await underlyingClient.unstableApis.getRelationsForEvent(
Expand Down
3 changes: 3 additions & 0 deletions test/test_discordbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,9 @@ describe("DiscordBot", () => {
discord.userActivity = {
updateUserActivity: () => { }
};
discord.userSync = {
JoinRoom: async () => { },
};
discord.GetIntentFromDiscordMember = () => {
return mockBridge.getIntent(author.id);
}
Expand Down

0 comments on commit 2ac843f

Please sign in to comment.