From cadda451300b35e53a294c6ed026280a8c06778f Mon Sep 17 00:00:00 2001 From: Adrian Perez de Castro Date: Tue, 5 Jul 2016 01:03:23 +0300 Subject: [PATCH] Handle "join" events properly This handles the fact that when using the /sync endpoint, events for already joined rooms are also in the "join" dictionary in the response. The firing of the "joined" event is moved to client:_make_room() and done only the first time that the room creation is requested. This also fixes bogus behavior: previosly, when a message was sent to a room there would be multiople places where the "joined" event would be fired, and message events pushed multile times onto the room object, which would call the event handlers more than once for each message. Ugh. --- matrix/client.lua | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/matrix/client.lua b/matrix/client.lua index 320859a..30ac5e3 100644 --- a/matrix/client.lua +++ b/matrix/client.lua @@ -369,15 +369,16 @@ function Client:join_room(room) else error("argument #1 must be a string or a room object") end - room = self:_make_room(response.room_id) - self:fire("joined", room) - return room + return self:_make_room(response.room_id) end function Client:_make_room(room_id) - assert(not self.rooms[room_id], "Room already exists") - local room = Room(self, room_id) - self.rooms[room_id] = room + local room = self.rooms[room_id] + if not room then + room = Room(self, room_id) + self.rooms[room_id] = room + self:fire("joined", room) + end return room end @@ -440,7 +441,6 @@ end function Client:_sync_handle_room__join(room_id, data) local room = self:_make_room(room_id) room:_push_events(data.timeline.events) - self:fire("joined", room) end function Client:_sync_handle_room__invite(room_id, data)