aboutsummaryrefslogtreecommitdiff
path: root/connectionprivate.cpp
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2016-04-12 15:53:46 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2016-05-02 19:22:33 +0900
commitd5bde8fbf3f8a47fe1140f307be0dcd39d1c0307 (patch)
treebefd7152720c67cb4f3ede3f1974d58df217b71a /connectionprivate.cpp
parent107b6fc4fc91d2cde3fc3d6ac4c532f1ef986e89 (diff)
downloadlibquotient-d5bde8fbf3f8a47fe1140f307be0dcd39d1c0307.tar.gz
libquotient-d5bde8fbf3f8a47fe1140f307be0dcd39d1c0307.zip
Deduplicated room check-and-creation code.
Diffstat (limited to 'connectionprivate.cpp')
-rw-r--r--connectionprivate.cpp63
1 files changed, 28 insertions, 35 deletions
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<QMatrixClient::RoomMemberEvent*>(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<SyncRoomData>& 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<PasswordLogin*>(job);
@@ -166,17 +168,8 @@ void ConnectionPrivate::gotJoinRoom(KJob* job)
JoinRoomJob* joinJob = static_cast<JoinRoomJob*>(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
{