[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [ruqola] src/rocketchatrestapi-qt5: Add support for join to channel
From:       Laurent Montel <null () kde ! org>
Date:       2018-09-27 11:50:18
Message-ID: E1g5UoE-0001Ug-AE () code ! kde ! org
[Download RAW message or body]

Git commit 75c15ce346755b4003990e0d8b4820da2e178e4f by Laurent Montel.
Committed on 27/09/2018 at 11:36.
Pushed by mlaurent into branch 'master'.

Add support for join to channel

M  +1    -0    src/rocketchatrestapi-qt5/CMakeLists.txt
M  +1    -0    src/rocketchatrestapi-qt5/autotests/CMakeLists.txt
A  +88   -0    src/rocketchatrestapi-qt5/autotests/channeljoinjobtest.cpp     \
[License: LGPL (v2/3+eV)] A  +39   -0    \
src/rocketchatrestapi-qt5/autotests/channeljoinjobtest.h     [License: LGPL \
(v2/3+eV)] A  +129  -0    src/rocketchatrestapi-qt5/channels/channeljoinjob.cpp     \
[License: LGPL (v2/3+eV)] A  +58   -0    \
src/rocketchatrestapi-qt5/channels/channeljoinjob.h     [License: LGPL (v2/3+eV)] M  \
+13   -0    src/rocketchatrestapi-qt5/restapirequest.cpp M  +2    -0    \
src/rocketchatrestapi-qt5/restapirequest.h

https://commits.kde.org/ruqola/75c15ce346755b4003990e0d8b4820da2e178e4f

diff --git a/src/rocketchatrestapi-qt5/CMakeLists.txt \
b/src/rocketchatrestapi-qt5/CMakeLists.txt index 12db8cc8..3db54741 100644
--- a/src/rocketchatrestapi-qt5/CMakeLists.txt
+++ b/src/rocketchatrestapi-qt5/CMakeLists.txt
@@ -66,6 +66,7 @@ set (RocketChatRestApiQt_SRCS
     channels/channeladdownerjob.cpp
     channels/channeladdmoderatorjob.cpp
     channels/channelkickjob.cpp
+    channels/channeljoinjob.cpp
 
     groups/changegroupstopicjob.cpp
     groups/changegroupsannouncementjob.cpp
diff --git a/src/rocketchatrestapi-qt5/autotests/CMakeLists.txt \
b/src/rocketchatrestapi-qt5/autotests/CMakeLists.txt index 320501d5..528f6694 100644
--- a/src/rocketchatrestapi-qt5/autotests/CMakeLists.txt
+++ b/src/rocketchatrestapi-qt5/autotests/CMakeLists.txt
@@ -82,3 +82,4 @@ add_ruqola_test(groupskickjobtest.cpp)
 add_ruqola_test(fetchkeychainjobtest.cpp)
 add_ruqola_test(fetchmykeysjobtest.cpp)
 add_ruqola_test(addkeytochainjobtest.cpp)
+add_ruqola_test(channeljoinjobtest.cpp)
diff --git a/src/rocketchatrestapi-qt5/autotests/channeljoinjobtest.cpp \
b/src/rocketchatrestapi-qt5/autotests/channeljoinjobtest.cpp new file mode 100644
index 00000000..642fae06
--- /dev/null
+++ b/src/rocketchatrestapi-qt5/autotests/channeljoinjobtest.cpp
@@ -0,0 +1,88 @@
+/*
+   Copyright (c) 2018 Montel Laurent <montel@kde.org>
+
+   This library is free software; you can redistribute it and/or modify
+   it under the terms of the GNU Library General Public License as published
+   by the Free Software Foundation; either version 2 of the License or
+   ( at your option ) version 3 or, at the discretion of KDE e.V.
+   ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public License
+   along with this library; see the file COPYING.LIB.  If not, write to
+   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+   Boston, MA 02110-1301, USA.
+*/
+
+#include "channeljoinjobtest.h"
+#include "channels/channeljoinjob.h"
+#include "ruqola_restapi_helper.h"
+#include <QTest>
+#include <QJsonDocument>
+QTEST_GUILESS_MAIN(ChannelJoinJobTest)
+using namespace RocketChatRestApi;
+ChannelJoinJobTest::ChannelJoinJobTest(QObject *parent)
+    : QObject(parent)
+{
+}
+
+void ChannelJoinJobTest::shouldHaveDefaultValue()
+{
+    ChannelJoinJob job;
+    verifyDefaultValue(&job);
+    QVERIFY(job.requireHttpAuthentication());
+    QVERIFY(job.roomId().isEmpty());
+}
+
+void ChannelJoinJobTest::shouldGenerateRequest()
+{
+    ChannelJoinJob job;
+    QNetworkRequest request = QNetworkRequest(QUrl());
+    verifyAuthentication(&job, request);
+    QCOMPARE(request.url(), \
QUrl(QStringLiteral("http://www.kde.org/api/v1/channels.join"))); +    \
QCOMPARE(request.header(QNetworkRequest::ContentTypeHeader).toString(), \
QStringLiteral("application/json")); +}
+
+void ChannelJoinJobTest::shouldGenerateJson()
+{
+    ChannelJoinJob job;
+    const QString roomId = QStringLiteral("foo1");
+    job.setRoomId(roomId);
+    const QString joinCode = QStringLiteral("bli");
+    job.setJoinCode(joinCode);
+    QCOMPARE(job.json().toJson(QJsonDocument::Compact), \
QStringLiteral("{\"joinCode\":\"%2\",\"roomId\":\"%1\"}").arg(roomId).arg(joinCode).toLatin1());
 +}
+
+void ChannelJoinJobTest::shouldNotStarting()
+{
+    ChannelJoinJob job;
+
+    RestApiMethod *method = new RestApiMethod;
+    method->setServerUrl(QStringLiteral("http://www.kde.org"));
+    job.setRestApiMethod(method);
+
+    QNetworkAccessManager *mNetworkAccessManager = new QNetworkAccessManager;
+    job.setNetworkAccessManager(mNetworkAccessManager);
+    QVERIFY(!job.canStart());
+    const QString auth = QStringLiteral("foo");
+    const QString userId = QStringLiteral("foo");
+    job.setAuthToken(auth);
+    QVERIFY(!job.canStart());
+    job.setUserId(userId);
+    QVERIFY(!job.canStart());
+    const QString roomId = QStringLiteral("foo1");
+    job.setRoomId(roomId);
+    QVERIFY(job.canStart());
+
+    //Join code is optional
+    const QString joinCode = QStringLiteral("fd1");
+    job.setJoinCode(joinCode);
+    QVERIFY(job.canStart());
+
+    delete method;
+    delete mNetworkAccessManager;
+}
diff --git a/src/rocketchatrestapi-qt5/autotests/channeljoinjobtest.h \
b/src/rocketchatrestapi-qt5/autotests/channeljoinjobtest.h new file mode 100644
index 00000000..e743dd79
--- /dev/null
+++ b/src/rocketchatrestapi-qt5/autotests/channeljoinjobtest.h
@@ -0,0 +1,39 @@
+/*
+   Copyright (c) 2018 Montel Laurent <montel@kde.org>
+
+   This library is free software; you can redistribute it and/or modify
+   it under the terms of the GNU Library General Public License as published
+   by the Free Software Foundation; either version 2 of the License or
+   ( at your option ) version 3 or, at the discretion of KDE e.V.
+   ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public License
+   along with this library; see the file COPYING.LIB.  If not, write to
+   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+   Boston, MA 02110-1301, USA.
+*/
+
+#ifndef CHANNELJOINJOBTEST_H
+#define CHANNELJOINJOBTEST_H
+
+#include <QObject>
+
+class ChannelJoinJobTest : public QObject
+{
+    Q_OBJECT
+public:
+    explicit ChannelJoinJobTest(QObject *parent = nullptr);
+    ~ChannelJoinJobTest() = default;
+private Q_SLOTS:
+    void shouldHaveDefaultValue();
+    void shouldGenerateRequest();
+    void shouldGenerateJson();
+    void shouldNotStarting();
+};
+
+#endif // CHANNELJOINJOBTEST_H
diff --git a/src/rocketchatrestapi-qt5/channels/channeljoinjob.cpp \
b/src/rocketchatrestapi-qt5/channels/channeljoinjob.cpp new file mode 100644
index 00000000..e1d21da4
--- /dev/null
+++ b/src/rocketchatrestapi-qt5/channels/channeljoinjob.cpp
@@ -0,0 +1,129 @@
+/*
+   Copyright (c) 2018 Montel Laurent <montel@kde.org>
+
+   This library is free software; you can redistribute it and/or modify
+   it under the terms of the GNU Library General Public License as published
+   by the Free Software Foundation; either version 2 of the License or
+   ( at your option ) version 3 or, at the discretion of KDE e.V.
+   ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public License
+   along with this library; see the file COPYING.LIB.  If not, write to
+   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+   Boston, MA 02110-1301, USA.
+*/
+
+#include "channeljoinjob.h"
+
+#include "rocketchatqtrestapi_debug.h"
+#include "restapimethod.h"
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QNetworkReply>
+using namespace RocketChatRestApi;
+ChannelJoinJob::ChannelJoinJob(QObject *parent)
+    : RestApiAbstractJob(parent)
+{
+}
+
+ChannelJoinJob::~ChannelJoinJob()
+{
+}
+
+bool ChannelJoinJob::start()
+{
+    if (!canStart()) {
+        deleteLater();
+        return false;
+    }
+    const QByteArray baPostData = json().toJson(QJsonDocument::Compact);
+    addLoggerInfo("ChannelJoinJob::start: " + baPostData);
+    QNetworkReply *reply = mNetworkAccessManager->post(request(), baPostData);
+    connect(reply, &QNetworkReply::finished, this, \
&ChannelJoinJob::slotChannelJoinFinished); +    return true;
+}
+
+void ChannelJoinJob::slotChannelJoinFinished()
+{
+    QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
+    if (reply) {
+        const QByteArray data = reply->readAll();
+        const QJsonDocument replyJson = QJsonDocument::fromJson(data);
+        const QJsonObject replyObject = replyJson.object();
+
+        if (replyObject[QStringLiteral("success")].toBool()) {
+            qCDebug(ROCKETCHATQTRESTAPI_LOG) << "channel join success: " << data;
+            Q_EMIT setChannelJoinDone();
+        } else {
+            //Need password ?
+            qCWarning(ROCKETCHATQTRESTAPI_LOG) <<" Problem when we tried to join to \
channel" << data; +        }
+    }
+    deleteLater();
+}
+
+QString ChannelJoinJob::joinCode() const
+{
+    return mJoinCode;
+}
+
+void ChannelJoinJob::setJoinCode(const QString &joinCode)
+{
+    mJoinCode = joinCode;
+}
+
+bool ChannelJoinJob::requireHttpAuthentication() const
+{
+    return true;
+}
+
+bool ChannelJoinJob::canStart() const
+{
+    if (!RestApiAbstractJob::canStart()) {
+        qCWarning(ROCKETCHATQTRESTAPI_LOG) << "Impossible to start ChannelJoinJob \
job"; +        return false;
+    }
+    if (mRoomId.isEmpty()) {
+        qCWarning(ROCKETCHATQTRESTAPI_LOG) << "ChannelJoinJob: RoomId is empty";
+        return false;
+    }
+    return true;
+}
+
+QJsonDocument ChannelJoinJob::json() const
+{
+    QJsonObject jsonObj;
+    jsonObj[QLatin1String("roomId")] = roomId();
+    if (!mJoinCode.isEmpty()) {
+        jsonObj[QLatin1String("joinCode")] = mJoinCode;
+    }
+
+    const QJsonDocument postData = QJsonDocument(jsonObj);
+    return postData;
+}
+
+QString ChannelJoinJob::roomId() const
+{
+    return mRoomId;
+}
+
+void ChannelJoinJob::setRoomId(const QString &roomId)
+{
+    mRoomId = roomId;
+}
+
+QNetworkRequest ChannelJoinJob::request() const
+{
+    const QUrl url = \
mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChannelsJoin); +    \
QNetworkRequest request(url); +    addAuthRawHeader(request);
+    request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
+    request.setAttribute(QNetworkRequest::HTTP2AllowedAttribute, true);
+    request.setHeader(QNetworkRequest::ContentTypeHeader, \
QStringLiteral("application/json")); +    return request;
+}
diff --git a/src/rocketchatrestapi-qt5/channels/channeljoinjob.h \
b/src/rocketchatrestapi-qt5/channels/channeljoinjob.h new file mode 100644
index 00000000..68bac91f
--- /dev/null
+++ b/src/rocketchatrestapi-qt5/channels/channeljoinjob.h
@@ -0,0 +1,58 @@
+/*
+   Copyright (c) 2018 Montel Laurent <montel@kde.org>
+
+   This library is free software; you can redistribute it and/or modify
+   it under the terms of the GNU Library General Public License as published
+   by the Free Software Foundation; either version 2 of the License or
+   ( at your option ) version 3 or, at the discretion of KDE e.V.
+   ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public License
+   along with this library; see the file COPYING.LIB.  If not, write to
+   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+   Boston, MA 02110-1301, USA.
+*/
+
+#ifndef CHANNELJOINJOB_H
+#define CHANNELJOINJOB_H
+
+#include "restapiabstractjob.h"
+#include "librestapi_private_export.h"
+namespace RocketChatRestApi {
+class LIBROCKETCHATRESTAPI_QT5_TESTS_EXPORT ChannelJoinJob : public \
RestApiAbstractJob +{
+    Q_OBJECT
+public:
+    explicit ChannelJoinJob(QObject *parent = nullptr);
+    ~ChannelJoinJob() override;
+
+    Q_REQUIRED_RESULT bool start() override;
+    Q_REQUIRED_RESULT bool requireHttpAuthentication() const override;
+    Q_REQUIRED_RESULT bool canStart() const override;
+
+    Q_REQUIRED_RESULT QNetworkRequest request() const override;
+
+    Q_REQUIRED_RESULT QJsonDocument json() const;
+
+    Q_REQUIRED_RESULT QString roomId() const;
+    void setRoomId(const QString &roomId);
+
+    Q_REQUIRED_RESULT QString joinCode() const;
+    void setJoinCode(const QString &joinCode);
+
+Q_SIGNALS:
+    void setChannelJoinDone();
+
+private:
+    Q_DISABLE_COPY(ChannelJoinJob)
+    void slotChannelJoinFinished();
+    QString mRoomId;
+    QString mJoinCode;
+};
+}
+#endif // CHANNELJOINJOB_H
diff --git a/src/rocketchatrestapi-qt5/restapirequest.cpp \
b/src/rocketchatrestapi-qt5/restapirequest.cpp index 799f5a52..397288a8 100644
--- a/src/rocketchatrestapi-qt5/restapirequest.cpp
+++ b/src/rocketchatrestapi-qt5/restapirequest.cpp
@@ -65,6 +65,7 @@
 #include "channels/setchanneltypejob.h"
 #include "channels/getchannelrolesjob.h"
 #include "channels/setjoincodechanneljob.h"
+#include "channels/channeljoinjob.h"
 
 #include "groups/changegroupsannouncementjob.h"
 #include "groups/changegroupstopicjob.h"
@@ -950,3 +951,15 @@ void RestApiRequest::setJoinCodeChannel(const QString &roomId, \
                const QString &jo
         qCDebug(ROCKETCHATQTRESTAPI_LOG) << "Impossible to start setjoincode";
     }
 }
+
+void RestApiRequest::channelJoin(const QString &roomId, const QString &joinCode)
+{
+    ChannelJoinJob *job = new ChannelJoinJob(this);
+    initializeRestApiJob(job);
+    job->setJoinCode(joinCode);
+    job->setRoomId(roomId);
+    connect(job, &ChannelJoinJob::setChannelJoinDone, this, \
&RestApiRequest::setChannelJoinDone); +    if (!job->start()) {
+        qCDebug(ROCKETCHATQTRESTAPI_LOG) << "Impossible to start setChannelJoin";
+    }
+}
diff --git a/src/rocketchatrestapi-qt5/restapirequest.h \
b/src/rocketchatrestapi-qt5/restapirequest.h index 8903bde3..b35679cd 100644
--- a/src/rocketchatrestapi-qt5/restapirequest.h
+++ b/src/rocketchatrestapi-qt5/restapirequest.h
@@ -120,6 +120,7 @@ public:
     void fetchMyKeys();
     void fetchKeyChain();
     void setJoinCodeChannel(const QString &roomId, const QString &joinCode);
+    void channelJoin(const QString &roomId, const QString &joinCode);
 Q_SIGNALS:
     void avatar(const QString &userId, const QString &url);
     void logoutDone();
@@ -144,6 +145,7 @@ Q_SIGNALS:
     void fetchMyKeysDone();
     void fetchKeyChainDone();
     void setJoinCodeDone();
+    void setChannelJoinDone();
 
 private:
     Q_DISABLE_COPY(RestApiRequest)


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic