matrix-org / matrix-ios-kit

Reusable UI interfaces to ease building of Matrix client apps
http://www.matrix.org
Apache License 2.0
128 stars 71 forks source link

duplicate messages when sending - local echo not removed #705

Open xinsight opened 4 years ago

xinsight commented 4 years ago

I'm seeing duplicate messages when sending from MXKRoomViewController. It seems these are "local echo" messages, which are supposed to be removed after the message sent to the server.

In -[MXKRoomDataSource refreshEventListeners:] - the event listener is created. It checks if there are outgoing messages, and then attempts to find the local echo.

MXEvent *localEcho;
if (self.room.outgoingMessages.count && [event.sender isEqualToString:self.mxSession.myUser.userId])
{
    localEcho = [self.room pendingLocalEchoRelatedToEvent:event];

Two problems: room.outgoingMessages.count is zero. But even if that check is removed, pendingLocalEchoRelatedToEvent: returns nil.

The two messages in the room are almost identical - so i'm not sure how the matrix kit or matrix sdk is supposed to reliably detect the local echo. For example:

{
  "origin_server_ts" : 1597651389299,
  "sender" : "@user1:xxx.org",
  "content" : {
    "msgtype" : "m.text",
    "body" : "Echo?"
  },
  "type" : "m.room.message",
  "event_id" : "$pdATtqY6beNczJlxfpAnk1DbW8k8l9ycTHeeNdij-_0",
  "room_id" : "!rytTykSOlVDqLRpOkK:xxx.org"
}
{
  "sender" : "@user1:xxx.org",
  "content" : {
    "msgtype" : "m.text",
    "body" : "Echo?"
  },
  "origin_server_ts" : 1597651385012,
  "room_id" : "!rytTykSOlVDqLRpOkK:xxx.org",
  "event_id" : "$pdATtqY6beNczJlxfpAnk1DbW8k8l9ycTHeeNdij-_0",
  "unsigned" : {
    "age" : 56854,
    "transaction_id" : "kMXEventLocalId_3AEC3166-F36C-495C-A048-A5FB5D436B56-96982-00019D04955033FC"
  },
  "type" : "m.room.message"
}

Anyone have any thoughts on why the local echo detection might be failing? Any workarounds? This issue happens consistently for me (and I'm not doing anything weird).

xinsight commented 4 years ago

Note: Only one message is sent to the server. If I restart the MXKRoomViewController, I only see each outgoing message a single time (as expected).

xinsight commented 4 years ago

Figured it out. Apparently you need to attach a store to your session. You have MXNoStore, MXMemoryStore or MXFileStore. For example:

let session = MXSession(matrixRestClient: authenticatedRestClient)!
let store = MXMemoryStore()

session.setStore(store) { response in

    guard case .success = response else {
        fatalError("unable to set store")
    }

    session.start { response in
        // ...
    }
}

Would be nice for Matrix SDX to warn that a store was missing! Hopefully this issue can help someone else from some head-scratching.