Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce reactions #428

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
},
"devDependencies": {
"@eng-automation/js-style": "^3.1.0",
"@eng-automation/testing": "^1.4.0",
"@eng-automation/testing": "^1.5.1",
"@types/body-parser": "^1.19.2",
"@types/express": "^4.17.13",
"@types/jest": "^29.5.12",
Expand Down
18 changes: 15 additions & 3 deletions src/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ const sendMessage = (roomId: string, msg: string, formattedMsg?: string) => {
bot.sendEvent(roomId, null, "m.room.message", msgObject, "").catch((e) => logger.error(e));
};

const sendReaction = (roomId: string, eventId: string, emoji: string) => {
const msgObject: mSDK.IContent = { "m.relates_to": { event_id: eventId, key: emoji, rel_type: "m.annotation" } };

bot.sendEvent(roomId, null, "m.reaction", msgObject, "").catch((e) => logger.error(e));
};

const printHelpMessage = (roomId: string, message = "") =>
sendMessage(
roomId,
Expand All @@ -73,11 +79,12 @@ bot.on(mSDK.RoomEvent.MyMembership, (room: mSDK.Room, membership: string) => {
bot.on(mSDK.RoomEvent.Timeline, (event: mSDK.MatrixEvent) => {
const sender = event.getSender();
const roomId = event.getRoomId();
const eventId = event.getId();
const { body } = event.getContent<{ body: string }>();

if (roomId === undefined) {
if (roomId === undefined || eventId == undefined) {
// Should never happen for a "Room.timeline" event
throw new Error("roomId is not defined");
throw new Error("roomId or eventId is not defined");
}

// only act on messages
Expand Down Expand Up @@ -114,8 +121,10 @@ bot.on(mSDK.RoomEvent.Timeline, (event: mSDK.MatrixEvent) => {
logger.error("⭕ An error occurred when checking the balance", e);
});
} else if (action === "!drip") {
sendReaction(roomId, eventId, "👀");
if (!arg0) {
logger.warn("Address not provided, skipping");
sendReaction(roomId, eventId, "😕");
sendMessage(roomId, `${sender} Please provide an address to be funded.`);
return;
Comment on lines +124 to 128
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like there's no need to send 👀 reaction, if we're sending a 😕 right after.
Why not move sendReaction(roomId, eventId, "👀"); after synchronous checks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean, also thought about it.

In my pedantic mind it's more consistent to always have the same pattern:

  • seen 👀 -> 👍 OK
  • seen 👀 -> 😕 FAIL

Changing this would introduce a 😕 FAIL for a message that "has not been seen" in contrast with others.

Those were just my thoughts, don't know if it makes sense :D

}

Expand All @@ -127,6 +136,7 @@ bot.on(mSDK.RoomEvent.Timeline, (event: mSDK.MatrixEvent) => {
try {
AccountId().enc(address);
} catch (e) {
sendReaction(roomId, eventId, "😕");
sendMessage(roomId, `${sender} provided an incompatible address.`);
return;
}
Expand Down Expand Up @@ -163,9 +173,11 @@ bot.on(mSDK.RoomEvent.Timeline, (event: mSDK.MatrixEvent) => {

const message = formattedSuccessfulDripResponse(dripAmount, sender, res.hash);

sendReaction(roomId, eventId, "👍");
sendMessage(roomId, message.plain, message.formatted);
})
.catch((e) => {
sendReaction(roomId, eventId, "😕");
sendMessage(roomId, "An unexpected error occurred, please check the server logs");
logger.error("⭕ An error occurred when dripping", e);
});
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -515,15 +515,15 @@ __metadata:
languageName: node
linkType: hard

"@eng-automation/testing@npm:^1.4.0":
version: 1.4.0
resolution: "@eng-automation/testing@npm:1.4.0"
"@eng-automation/testing@npm:^1.5.1":
version: 1.5.1
resolution: "@eng-automation/testing@npm:1.5.1"
dependencies:
"@eng-automation/js": "npm:^2.1.0"
"@octokit/rest": "npm:^19.0.7"
mockttp: "npm:^3.9.4"
selfsigned: "npm:^2.1.0"
checksum: 10c0/b71a5adf52feb6e19aa851db1c478febb9fb23220b29f16e06b05bea79a9b1601a58016065d10fe97ec37101fd74a0addf28f84f1709f6131e70a43bf2b73c22
checksum: 10c0/7582e24b39f339febc864b8fcfe8f32c943c662843aec614d236a6b3ab51afe6ec1b349a5bf45fd9d7c3de8a19c8bb01656491a367034b5dc88b4ac9d37befca
languageName: node
linkType: hard

Expand Down Expand Up @@ -4985,7 +4985,7 @@ __metadata:
dependencies:
"@eng-automation/js": "npm:^2.1.0"
"@eng-automation/js-style": "npm:^3.1.0"
"@eng-automation/testing": "npm:^1.4.0"
"@eng-automation/testing": "npm:^1.5.1"
"@polkadot-labs/hdkd": "npm:^0.0.6"
"@polkadot-labs/hdkd-helpers": "npm:^0.0.6"
"@types/body-parser": "npm:^1.19.2"
Expand Down
Loading