diff --git a/ComunicMessages.pro b/ComunicMessages.pro index a8089e9..9084de5 100644 --- a/ComunicMessages.pro +++ b/ComunicMessages.pro @@ -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 \ diff --git a/data/conversationmessage.cpp b/data/conversationmessage.cpp new file mode 100644 index 0000000..315f2a4 --- /dev/null +++ b/data/conversationmessage.cpp @@ -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; +} diff --git a/data/conversationmessage.h b/data/conversationmessage.h new file mode 100644 index 0000000..ce49750 --- /dev/null +++ b/data/conversationmessage.h @@ -0,0 +1,40 @@ +/** + * Conversation message object + * + * @author Pierre HUBERT + */ + +#ifndef CONVERSATIONMESSAGE_H +#define CONVERSATIONMESSAGE_H + +#include + +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 diff --git a/helpers/conversationhelper.cpp b/helpers/conversationhelper.cpp index 870a354..942c101 100644 --- a/helpers/conversationhelper.cpp +++ b/helpers/conversationhelper.cpp @@ -1,3 +1,6 @@ +#include +#include + #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(sender())->deleteLater(); + + //Check if the operation is not a success + if(code != 200){ + emit getMessagesCallback(false, QList()); + return; + } + + QJsonArray array = document.array(); + QList 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; +} diff --git a/helpers/conversationhelper.h b/helpers/conversationhelper.h index 5536250..dfa9915 100644 --- a/helpers/conversationhelper.h +++ b/helpers/conversationhelper.h @@ -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 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; };