Skip to content

Commit

Permalink
refactor: improve logs and avoid bad requests
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco committed Feb 5, 2023
1 parent ff24df5 commit 2a2548d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
30 changes: 20 additions & 10 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1194,18 +1194,15 @@ export class DiscordBot {

public async OnMessageReactionAdd(reaction: Discord.MessageReaction, user: Discord.User | Discord.PartialUser) {
const message = reaction.message;
log.info(`Got message reaction add event for ${message.id} with ${reaction.emoji.name}`);
const reactionName = reaction.emoji.name;
log.verbose(`Got message reaction add event for ${message.id} with ${reactionName}`);

let rooms: string[];

try {
rooms = await this.channelSync.GetRoomIdsFromChannel(message.channel);

if (rooms === null) {
throw Error();
}
} catch (err) {
log.verbose("No bridged rooms to send message to. Oh well.");
log.verbose(`No bridged rooms to forward reaction to. Reaction Event: ${reaction}`);
MetricPeg.get.requestOutcome(message.id, true, "dropped");
return;
}
Expand All @@ -1219,17 +1216,25 @@ export class DiscordBot {
});

if (!storeEvent?.Result) {
log.verbose(`Received add reaction event for untracked message. Dropping! Reaction Event: ${reaction}`);
return;
}

while (storeEvent.Next()) {
const matrixIds = storeEvent.MatrixId.split(";");

const [messageEventId, roomId] = storeEvent.MatrixId.split(";");
const redactionEventId = await intent.underlyingClient.unstableApis.addReactionToEvent(
roomId,
messageEventId,
reaction.emoji.id ? `:${reactionName}:` : reactionName
);

for (const room of rooms) {
const reactionEventId = await intent.underlyingClient.unstableApis.addReactionToEvent(
room,
matrixIds[0],
reaction.emoji.id ? `:${reaction.emoji.name}:` : reaction.emoji.name
reaction.emoji.id ? `:${reactionName}:` : reactionName
);

const event = new DbEvent();
Expand All @@ -1247,7 +1252,7 @@ export class DiscordBot {

public async OnMessageReactionRemove(reaction: Discord.MessageReaction, user: Discord.User | Discord.PartialUser) {
const message = reaction.message;
log.info(`Got message reaction remove event for ${message.id} with ${reaction.emoji.name}`);
log.verbose(`Got message reaction remove event for ${message.id} with ${reaction.emoji.name}`);

const intent = this.GetIntentFromDiscordMember(user);
await intent.ensureRegistered();
Expand All @@ -1258,6 +1263,7 @@ export class DiscordBot {
});

if (!storeEvent?.Result) {
log.verbose(`Received remove reaction event for untracked message. Dropping! Reaction Event: ${reaction}`);
return;
}

Expand All @@ -1280,6 +1286,7 @@ export class DiscordBot {
});

if (!event) {
log.verbose(`Received remove reaction event for tracked message where the add reaction event was not bridged. Dropping! Reaction Event: ${reaction}`);
return;
}

Expand All @@ -1299,13 +1306,14 @@ export class DiscordBot {
}

public async OnMessageReactionRemoveAll(message: Discord.Message | Discord.PartialMessage) {
log.info(`Got message reaction remove all event for ${message.id}`);
log.verbose(`Got message reaction remove all event for ${message.id}`);

const storeEvent = await this.store.Get(DbEvent, {
discord_id: message.id,
});

if (!storeEvent?.Result) {
log.verbose(`Received remove all reaction event for untracked message. Dropping! Event: ${message}`);
return;
}

Expand All @@ -1319,7 +1327,9 @@ export class DiscordBot {
"m.annotation"
);

await Promise.all(chunk.map(async (event) => {
const filteredChunk = chunk.filter((event) => event.sender === this.bridge.botUserId);

await Promise.all(filteredChunk.map(async (event) => {
try {
return await underlyingClient.redactEvent(event.room_id, event.event_id);
} catch (ex) {
Expand Down
2 changes: 1 addition & 1 deletion src/db/dbdataevent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { IDbDataMany } from "./dbdatainterface";
import { ISqlCommandParameters } from "./connector";

export class DbEvent implements IDbDataMany {
/** Matrix ID of event. */
/** ID associated with the event in the format "MatrixID:RoomID". */
public MatrixId: string;
/** Discord ID of the relevant message associated with this event. */
public DiscordId: string;
Expand Down

0 comments on commit 2a2548d

Please sign in to comment.