From 86996a227834f68a8225113f93b80df6aed895e5 Mon Sep 17 00:00:00 2001
From: Armin Felder <armin.felder@osalliance.com>
Date: Tue, 11 Dec 2018 17:36:08 +0100
Subject: [PATCH] fixes inserted channel message handling

---
 rocketchat.cpp       |  2 +-
 rocketchatserver.cpp | 27 +++++++++++++++++++++++----
 2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/rocketchat.cpp b/rocketchat.cpp
index 359ad36..1c11283 100755
--- a/rocketchat.cpp
+++ b/rocketchat.cpp
@@ -87,7 +87,7 @@ RocketChat::RocketChat( QGuiApplication *app )
 #endif
     qRegisterMetaType<ConnectionState>( "ConnectionState" );
     qRegisterMetaType<Qt::ApplicationState>( "Qt::ApplicationState" );
-    qInstallMessageHandler( customOutput );
+    // qInstallMessageHandler( customOutput );
 }
 
 RocketChat::~RocketChat()
diff --git a/rocketchatserver.cpp b/rocketchatserver.cpp
index fbd050e..539f09f 100755
--- a/rocketchatserver.cpp
+++ b/rocketchatserver.cpp
@@ -981,11 +981,20 @@ void RocketChatServerData::handleChannelMessage( const QJsonObject &pMessage )
             if ( firstArg == QStringLiteral( "insert" ) || firstArg == QStringLiteral( "inserted" ) ) {
                 QJsonObject newChannel = args[1].toObject();
 
-                if ( newChannel.contains( QStringLiteral( "name" ) ) && newChannel.contains( QStringLiteral( "rid" ) ) && newChannel.contains( "t" ) ) {
+                if ( newChannel.contains( QStringLiteral( "name" ) ) && ( newChannel.contains( QStringLiteral( "rid" ) ) || newChannel.contains( QStringLiteral( "_id" ) ) ) && newChannel.contains( "t" ) ) {
                     QString name = newChannel[QStringLiteral( "name" )].toString();
-                    QString rid = newChannel[QStringLiteral( "rid" )].toString();
+                    QString rid;
 
-                    if ( !mChannels->contains( QStringLiteral( "id" ) ) ) {
+                    if ( newChannel.contains( "rid" ) ) {
+                        rid = newChannel[QStringLiteral( "rid" )].toString();
+                    } else if ( newChannel.contains( QStringLiteral( "_id" ) ) ) {
+                        rid = newChannel[QStringLiteral( "_id" )].toString();
+                    } else {
+                        qWarning() << "invalid channel inserted message";
+                        return ;
+                    }
+
+                    if ( !mChannels->contains( rid ) ) {
                         auto channel  = mChannelService->createChannelObject( rid, name, newChannel["t"].toString() );
 
                         if ( !channel.isNull() ) {
@@ -1001,7 +1010,17 @@ void RocketChatServerData::handleChannelMessage( const QJsonObject &pMessage )
             } else if ( firstArg == QStringLiteral( "updated" ) ) {
                 QJsonObject data = args[1].toObject();
                 int unread = data[QStringLiteral( "unread" )].toInt();
-                QString rid = data[QStringLiteral( "rid" )].toString();
+                QString rid;
+
+                if ( data.contains( "rid" ) ) {
+                    rid = data[QStringLiteral( "rid" )].toString();
+                } else if ( data.contains( QStringLiteral( "_id" ) ) ) {
+                    rid = data[QStringLiteral( "_id" )].toString();
+                } else {
+                    qWarning() << "invalid channel update message";
+                    return ;
+                }
+
                 QString type = data[QStringLiteral( "t" )].toString();
                 bool blocked = false;
                 QString name = data[QStringLiteral( "name" )].toString();
-- 
GitLab