Can get a list of messages from the server.

This commit is contained in:
Pierre HUBERT 2018-12-13 18:55:47 +01:00
parent 58b4afe653
commit 640da86c0d
5 changed files with 179 additions and 2 deletions

View File

@ -28,7 +28,8 @@ SOURCES += \
utils/timeutils.cpp \
widgets/conversationwidget.cpp \
data/newconversationmessage.cpp \
helpers/conversationhelper.cpp
helpers/conversationhelper.cpp \
data/conversationmessage.cpp
HEADERS += \
helpers/accounthelper.h \
@ -58,7 +59,8 @@ HEADERS += \
utils/timeutils.h \
widgets/conversationwidget.h \
data/newconversationmessage.h \
helpers/conversationhelper.h
helpers/conversationhelper.h \
data/conversationmessage.h
FORMS += \
widgets/loginwidget.ui \

View File

@ -0,0 +1,56 @@
#include "conversationmessage.h"
ConversationMessage::ConversationMessage()
{
}
int ConversationMessage::iD() const
{
return mID;
}
void ConversationMessage::setID(int iD)
{
mID = iD;
}
int ConversationMessage::userID() const
{
return mUserID;
}
void ConversationMessage::setUserID(int userID)
{
mUserID = userID;
}
int ConversationMessage::timeInsert() const
{
return mTimeInsert;
}
void ConversationMessage::setTimeInsert(int timeInsert)
{
mTimeInsert = timeInsert;
}
QString ConversationMessage::message() const
{
return mMessage;
}
void ConversationMessage::setMessage(const QString &message)
{
mMessage = message;
}
QString ConversationMessage::imagePath() const
{
return mImagePath;
}
void ConversationMessage::setImagePath(const QString &imagePath)
{
mImagePath = imagePath;
}

View File

@ -0,0 +1,40 @@
/**
* Conversation message object
*
* @author Pierre HUBERT
*/
#ifndef CONVERSATIONMESSAGE_H
#define CONVERSATIONMESSAGE_H
#include <QString>
class ConversationMessage
{
public:
ConversationMessage();
int iD() const;
void setID(int iD);
int userID() const;
void setUserID(int userID);
int timeInsert() const;
void setTimeInsert(int timeInsert);
QString message() const;
void setMessage(const QString &message);
QString imagePath() const;
void setImagePath(const QString &imagePath);
private:
int mID;
int mUserID;
int mTimeInsert;
QString mMessage;
QString mImagePath;
};
#endif // CONVERSATIONMESSAGE_H

View File

@ -1,3 +1,6 @@
#include <QJsonObject>
#include <QJsonArray>
#include "apihelper.h"
#include "conversationhelper.h"
#include "../data/apirequest.h"
@ -18,6 +21,17 @@ void ConversationHelper::sendMessage(const NewConversationMessage &message)
mAPIHelper->execute(request);
}
void ConversationHelper::getMessages(int conversationID, int last_message_id)
{
APIRequest *request = new APIRequest;
request->setURI("conversations/refresh_single");
request->addInt("conversationID", conversationID);
request->addInt("last_message_id", last_message_id > 0 ? last_message_id : 0);
connect(request, &APIRequest::finished, this, &ConversationHelper::getMessagesFinished);
mAPIHelper->execute(request);
}
void ConversationHelper::sendMessageFinished(int code)
{
//Delete sender
@ -31,3 +45,33 @@ void ConversationHelper::sendMessageFinished(int code)
//Success
emit sendMessageCallback(true);
}
void ConversationHelper::getMessagesFinished(int code, const QJsonDocument &document)
{
//Delete sender
qobject_cast<APIRequest *>(sender())->deleteLater();
//Check if the operation is not a success
if(code != 200){
emit getMessagesCallback(false, QList<ConversationMessage>());
return;
}
QJsonArray array = document.array();
QList<ConversationMessage> list;
for(QJsonValue entry : array)
list.append(QJsonObjectToConversationMessage(entry.toObject()));
emit getMessagesCallback(true, list);
}
ConversationMessage ConversationHelper::QJsonObjectToConversationMessage(const QJsonObject &object)
{
ConversationMessage message;
message.setID(object.value("ID").toInt());
message.setUserID(object.value("ID_user").toInt());
message.setTimeInsert(object.value("time_insert").toInt());
message.setMessage(object.value("message").toString());
message.setImagePath(object.value("image_path").toString());
return message;
}

View File

@ -13,6 +13,9 @@
#include "../data/newconversationmessage.h"
#include "../data/apirequest.h"
#include "../data/conversationmessage.h"
class QJsonObject;
class APIHelper;
@ -29,6 +32,14 @@ public:
*/
void sendMessage(const NewConversationMessage &message);
/**
* Get some messages of a conversation
*
* @param conversationID The ID of the target conversation
* @param last_message_id The ID of the last known message (-1 for none)
*/
void getMessages(int conversationID, int last_message_id = -1);
signals:
/**
@ -39,6 +50,14 @@ signals:
*/
void sendMessageCallback(bool success);
/**
* Get the list of messages callback
*
* @param success TRUE for a success / FALSE else
* @param list The list of messages
*/
void getMessagesCallback(bool success, QList<ConversationMessage> list);
public slots:
@ -52,8 +71,24 @@ private slots:
*/
void sendMessageFinished(int code);
/**
* Finished to retrieve the list of messages of the user
*
* @param code HTTP response code
* @param document Server response
*/
void getMessagesFinished(int code, const QJsonDocument &document);
private:
/**
* Turn a json object into a conversation object
*
* @param object The object to convert
* @return Generated object
*/
static ConversationMessage QJsonObjectToConversationMessage(const QJsonObject &object);
//Private fields
APIHelper *mAPIHelper;
};