From d5bde8fbf3f8a47fe1140f307be0dcd39d1c0307 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Tue, 12 Apr 2016 15:53:46 +0900 Subject: Deduplicated room check-and-creation code. --- connectionprivate.cpp | 63 +++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 35 deletions(-) (limited to 'connectionprivate.cpp') diff --git a/connectionprivate.cpp b/connectionprivate.cpp index 50fd1b0c..851e3d58 100644 --- a/connectionprivate.cpp +++ b/connectionprivate.cpp @@ -75,45 +75,47 @@ void ConnectionPrivate::resolveServer(QString domain) void ConnectionPrivate::processState(State* state) { - QString roomId = state->event()->roomId(); if( state->event()->type() == QMatrixClient::EventType::RoomMember ) { QMatrixClient::RoomMemberEvent* e = static_cast(state->event()); User* user = q->user(e->userId()); user->processEvent(e); } - if( !roomId.isEmpty() ) - { - Room* room; - if( !roomMap.contains(roomId) ) - { - room = q->createRoom(roomId); - roomMap.insert( roomId, room ); - emit q->newRoom(room); - } else { - room = roomMap.value(roomId); - } - room->addInitialState(state); - } + + if ( Room* r = provideRoom(state->event()->roomId()) ) + r->addInitialState(state); } void ConnectionPrivate::processRooms(const QList& data) { for( const SyncRoomData& roomData: data ) { - Room* room; - if( !roomMap.contains(roomData.roomId) ) - { - room = q->createRoom(roomData.roomId); - roomMap.insert( roomData.roomId, room ); - emit q->newRoom(room); - } else { - room = roomMap.value(roomData.roomId); - } - room->updateData(roomData); + if ( Room* r = provideRoom(roomData.roomId) ) + r->updateData(roomData); } } +Room* ConnectionPrivate::provideRoom(QString id) +{ + if (id.isEmpty()) + { + qDebug() << "ConnectionPrivate::provideRoom() with empty id, doing nothing"; + return nullptr; + } + + if (roomMap.contains(id)) + return roomMap.value(id); + + // Not yet in the map, create a new one. + Room* room = q->createRoom(id); + if (!room) + qCritical() << "Failed to create a room!!!" << id; + + roomMap.insert( id, room ); + emit q->newRoom(room); + return room; +} + void ConnectionPrivate::connectDone(KJob* job) { PasswordLogin* realJob = static_cast(job); @@ -166,17 +168,8 @@ void ConnectionPrivate::gotJoinRoom(KJob* job) JoinRoomJob* joinJob = static_cast(job); if( !joinJob->error() ) { - QString roomId = joinJob->roomId(); - Room* room; - if( roomMap.contains(roomId) ) - { - room = roomMap.value(roomId); - } else { - room = q->createRoom(roomId); - roomMap.insert( roomId, room ); - emit q->newRoom(room); - } - emit q->joinedRoom(room); + if ( Room* r = provideRoom(joinJob->roomId()) ) + emit q->joinedRoom(r); } else { -- cgit v1.2.3