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.
This commit is contained in:
Adrian Perez de Castro
2016-07-05 01:03:23 +03:00
parent bad6e6ee16
commit cadda45130

View File

@@ -369,15 +369,16 @@ function Client:join_room(room)
else else
error("argument #1 must be a string or a room object") error("argument #1 must be a string or a room object")
end end
room = self:_make_room(response.room_id) return self:_make_room(response.room_id)
self:fire("joined", room)
return room
end end
function Client:_make_room(room_id) function Client:_make_room(room_id)
assert(not self.rooms[room_id], "Room already exists") local room = self.rooms[room_id]
local room = Room(self, room_id) if not room then
self.rooms[room_id] = room room = Room(self, room_id)
self.rooms[room_id] = room
self:fire("joined", room)
end
return room return room
end end
@@ -440,7 +441,6 @@ end
function Client:_sync_handle_room__join(room_id, data) function Client:_sync_handle_room__join(room_id, data)
local room = self:_make_room(room_id) local room = self:_make_room(room_id)
room:_push_events(data.timeline.events) room:_push_events(data.timeline.events)
self:fire("joined", room)
end end
function Client:_sync_handle_room__invite(room_id, data) function Client:_sync_handle_room__invite(room_id, data)