mirror of
https://gitlab.com/comunic/comunicmessages
synced 2025-06-20 08:55:17 +00:00
Can get the list of conversations
This commit is contained in:
@ -59,7 +59,9 @@ void APIHelper::finished()
|
||||
APIRequest *request = mRequestsList.findForReply(qobject_cast<QNetworkReply *>(sender()), true);
|
||||
|
||||
//Process and return response
|
||||
emit request->success(QJsonDocument::fromJson(request->networkReply()->readAll()));
|
||||
QJsonDocument document = QJsonDocument::fromJson(request->networkReply()->readAll());
|
||||
emit request->success(document);
|
||||
emit request->finished(200, document);
|
||||
}
|
||||
|
||||
void APIHelper::error()
|
||||
@ -72,6 +74,7 @@ void APIHelper::error()
|
||||
qWarning("An error occurred in an API request! (code: %d)", response_code);
|
||||
|
||||
emit request->error(response_code);
|
||||
emit request->finished(response_code, QJsonDocument());
|
||||
}
|
||||
|
||||
void APIHelper::sslErrors()
|
||||
|
62
helpers/conversationslisthelper.cpp
Normal file
62
helpers/conversationslisthelper.cpp
Normal file
@ -0,0 +1,62 @@
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include "conversationslisthelper.h"
|
||||
#include "apihelper.h"
|
||||
#include "../data/apirequest.h"
|
||||
|
||||
ConversationsListHelper::ConversationsListHelper(QObject *parent) : QObject(parent)
|
||||
{
|
||||
mAPIHelper = new APIHelper(this);
|
||||
}
|
||||
|
||||
void ConversationsListHelper::getList()
|
||||
{
|
||||
//We need to perform a request on the server
|
||||
APIRequest *request = new APIRequest(this);
|
||||
request->setURI("conversations/getList");
|
||||
connect(request, &APIRequest::finished, this, &ConversationsListHelper::getConvListCallback);
|
||||
mAPIHelper->execute(request);
|
||||
}
|
||||
|
||||
void ConversationsListHelper::getConvListCallback(int code, const QJsonDocument &document)
|
||||
{
|
||||
//Delete the request
|
||||
qobject_cast<APIRequest *>(sender())->deleteLater();
|
||||
|
||||
if(code != 200){
|
||||
emit onGotList(false, QList<Conversation>());
|
||||
return;
|
||||
}
|
||||
|
||||
//The request successfully completed
|
||||
QJsonArray arr = document.array();
|
||||
|
||||
//Process the list of conversations
|
||||
QList<Conversation> list;
|
||||
for(int i = 0; i < arr.size(); i++)
|
||||
list.append(GetConversationFromJson(arr.at(i).toObject()));
|
||||
|
||||
//Finished
|
||||
emit onGotList(true, list);
|
||||
}
|
||||
|
||||
Conversation ConversationsListHelper::GetConversationFromJson(const QJsonObject &obj)
|
||||
{
|
||||
Conversation conv;
|
||||
conv.setID(obj.value("ID").toInt());
|
||||
conv.setIDowner(obj.value("ID_owner").toInt());
|
||||
conv.setLastActive(obj.value("last_active").toInt());
|
||||
conv.setName(obj.value("name").toString());
|
||||
conv.setFollowing(obj.value("following").toBool());
|
||||
conv.setSawLastMessage(obj.value("saw_last_message").toBool());
|
||||
|
||||
//Process the list of members of the conversation
|
||||
QJsonArray members_arr = obj.value("members").toArray();
|
||||
QList<int> members_list;
|
||||
for(int i = 0; i < members_arr.size(); i++)
|
||||
members_list.append(members_arr.at(i).toInt());
|
||||
conv.setMembers(members_list);
|
||||
|
||||
return conv;
|
||||
}
|
67
helpers/conversationslisthelper.h
Normal file
67
helpers/conversationslisthelper.h
Normal file
@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Conversations list helper
|
||||
*
|
||||
* Does all the tasks related with conversations managment
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
|
||||
#ifndef CONVERSATIONSLISTHELPER_H
|
||||
#define CONVERSATIONSLISTHELPER_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include "../data/conversation.h"
|
||||
|
||||
class APIHelper;
|
||||
class QJsonObject;
|
||||
|
||||
class ConversationsListHelper : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ConversationsListHelper(QObject *parent = nullptr);
|
||||
|
||||
/**
|
||||
* Get the list of conversations
|
||||
*/
|
||||
void getList();
|
||||
|
||||
signals:
|
||||
|
||||
/**
|
||||
* This signal is emitted once we have a got a new list of conversation
|
||||
*
|
||||
* @param success TRUE for a success / FALSE else
|
||||
* @param list The list of conversations
|
||||
*/
|
||||
void onGotList(bool success, const QList<Conversation> &list);
|
||||
|
||||
public slots:
|
||||
|
||||
private slots:
|
||||
|
||||
/**
|
||||
* This slot is triggered once the request to the server was
|
||||
* completed
|
||||
*
|
||||
* @param code Response code from the server
|
||||
* @param document Response document
|
||||
*/
|
||||
void getConvListCallback(int code, const QJsonDocument &document);
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* Turn a QJsonObject into a Conversation one
|
||||
*
|
||||
* @param obj The object to convert
|
||||
* @return Generated conversation object
|
||||
*/
|
||||
static Conversation GetConversationFromJson(const QJsonObject &obj);
|
||||
|
||||
//Private fields
|
||||
APIHelper *mAPIHelper;
|
||||
};
|
||||
|
||||
#endif // CONVERSATIONSLISTHELPER_H
|
Reference in New Issue
Block a user