matrix-nio icon indicating copy to clipboard operation
matrix-nio copied to clipboard

message event callbacks not firing in E2E room

Open redthing1 opened this issue 4 years ago • 17 comments

I am logging into my account using username and password credentials with the AsyncClient.

I have installed libolm and am using the latest matrix-nio[e2e] installed via pip.

Then, I am subscribing for message events like:

client.add_event_callback(on_message, (nio.RoomMessageText,))

This works for messages in non-encrypted rooms, but in encrypted rooms the events never fire. I have tried logging into the account using Riot, and the messages are indeed there and visible to the bot user. However, when accessed through nio, the messages are not read.

redthing1 avatar Jun 07 '20 23:06 redthing1

I'm also experiencing this issue. I started an unencrypted chat with my bot, sent a message, which appeared properly, then enabled encryption, sent a message, and it was not received.

wasv avatar Jun 16 '20 22:06 wasv

Here is the code for my bot (basically the tutorial code with some stuff removed and rearranged). https://github.com/wasv/matrix-bot/tree/82b7c8d016f2bf4406c9f24108b8a991941da81a

Here is the event that did work:

{
  "type": "m.room.message",
  "sender": "@me",
  "content": {
    "msgtype": "m.text",
    "body": "Hello"
  },
  "origin_server_ts": 1592346468678,
  "unsigned": {
    "age": 5090,
    "transaction_id": "m1592346468537.0"
  },
  "event_id": "$uUN17...",
  "room_id": "!gNCZ..."
}

Here is the one that didn't:

{
  "type": "m.room.encrypted",
  "sender": "@me",
  "content": {
    "algorithm": "m.megolm.v1.aes-sha2",
    "sender_key": "Gqhc...",
    "ciphertext": "AwgAEo...",
    "session_id": "PyQQU...",
    "device_id": "ZNU..."
  },
  "origin_server_ts": 1592346603355,
  "unsigned": {
    "age": 62,
    "transaction_id": "m1592346602863.1"
  },
  "event_id": "$zJ7u-...",
  "room_id": "!gNCZ..."
}

Decrypted payload:

{
  "type": "m.room.message",
  "content": {
    "msgtype": "m.text",
    "body": "Ping"
  }
}

wasv avatar Jun 16 '20 22:06 wasv

Hey @wasv, I went to check out your code but it looks like the repo is private. I saw similar behaviour when writing the manual encryption tutorial and it ended up that sync-timing was critical in resolving the issue. If you can share your code that'd be handy to help y'all debug this.

phildenhoff avatar Jun 17 '20 20:06 phildenhoff

Whoops. Forgot I had it private. It's public now.

wasv avatar Jun 17 '20 21:06 wasv

I'm also experiencing this issue. I started an unencrypted chat with my bot, sent a message, which appeared properly, then enabled encryption, sent a message, and it was not received.

You'll need to call self.load_store() in your login method when you restore your login here. This method is called automatically when you login. We should really add a method called restore_login(user_id, device_id, access_token) which does the same.

poljar avatar Jun 18 '20 17:06 poljar

That fixed it, thanks!

Here's the relevant commit for anyone who needs it.

wasv avatar Jun 18 '20 18:06 wasv

We should really add a method called restore_login(user_id, device_id, access_token) which does the same.

I can fork and do a small PR for this.

wasv avatar Jun 18 '20 18:06 wasv

I tried the suggested fix (using load_store), but I am still not seeing events fire in E2E rooms.

redthing1 avatar Jun 18 '20 21:06 redthing1

Update: It does now work in new E2E rooms, but does not work in rooms that were joined earlier. Is there a way to resolve this without recreating the rooms?

redthing1 avatar Jun 18 '20 21:06 redthing1

Update: It does now work in new E2E rooms, but does not work in rooms that were joined earlier. Is there a way to resolve this without recreating the rooms?

Could you also post your project or at least a snippet? Depending on what your device might have been created after you joined so you won't be able to get to the old messages in the previously joined rooms. New messages should arrive no matter when you joined the room.

poljar avatar Jun 19 '20 08:06 poljar

Here is my source: beak.zip

redthing1 avatar Jun 20 '20 00:06 redthing1

@xdrie I took a look at your source but I didn't see anything that stands out as particularly funky.

I tried running it but it never seemed to actually receive messages, whether in a new or old room or encrypted or not. I'm not sure if that's because I didn't configure something or what.

At the same time, another quick bot written in nio was able to see these messages. I think your posted code either isn't pulling events correctly, or I wired it up wrong when I tried it out.

phildenhoff avatar Jun 23 '20 06:06 phildenhoff

can someone fix this?

finzzz avatar Dec 26 '20 00:12 finzzz

Fix what exactly? The problems here were around events not getting decrypted. If the event isn't decrypted the type of the event won't be RoomMessageText. Events might not get decrypted for multiple reasons and they will be emitted as a MegolmEvent.

poljar avatar Dec 26 '20 09:12 poljar

Is there any simple examples on how to handle encrypted messages??

finzzz avatar Dec 26 '20 09:12 finzzz

Depends on what you exactly want/need. Handle how? Retry decryption? Send them? Set up nio to correctly support encryption? Something else?

poljar avatar Jan 03 '21 16:01 poljar

I have a similar issue. When I use login() everything works fine, but when I try to restore login it stops getting new messages from encrypted rooms.

Additionally, when I call load_store() whether manually or using restore_login() I can't even send messages to old rooms (No such room with id .. found)

AdrianKlm avatar Nov 24 '21 20:11 AdrianKlm