diff --git a/ComunicMessages.pro b/ComunicMessages.pro index 4e3bb21..96374cd 100644 --- a/ComunicMessages.pro +++ b/ComunicMessages.pro @@ -18,7 +18,11 @@ SOURCES += \ controllers/initcontroller.cpp \ data/conversation.cpp \ helpers/conversationslisthelper.cpp \ - widgets/conversationslistwidget.cpp + widgets/conversationslistwidget.cpp \ + widgets/conversationitemwidget.cpp \ + data/user.cpp \ + helpers/usershelper.cpp \ + data/conversationslist.cpp HEADERS += \ helpers/accounthelper.h \ @@ -38,9 +42,14 @@ HEADERS += \ controllers/initcontroller.h \ data/conversation.h \ helpers/conversationslisthelper.h \ - widgets/conversationslistwidget.h + widgets/conversationslistwidget.h \ + widgets/conversationitemwidget.h \ + data/user.h \ + helpers/usershelper.h \ + data/conversationslist.h FORMS += \ widgets/loginwidget.ui \ widgets/mainwindow.ui \ - widgets/aboutthisappdialog.ui + widgets/aboutthisappdialog.ui \ + widgets/conversationitemwidget.ui diff --git a/config.h b/config.h index 63d7ccd..6e80e0d 100644 --- a/config.h +++ b/config.h @@ -15,6 +15,8 @@ /** * API credentials + * + * Note : only HTTPS should be used in production */ #define API_URL "https://api.communiquons.org/" #define API_SERVICE_NAME "ComunicAndroid" diff --git a/data/conversationslist.cpp b/data/conversationslist.cpp new file mode 100644 index 0000000..31e8570 --- /dev/null +++ b/data/conversationslist.cpp @@ -0,0 +1,17 @@ +#include "conversationslist.h" + +ConversationsList::ConversationsList() +{ + +} + +QList ConversationsList::getAllMembersId() const +{ + QList members; + for(Conversation conv : *this){ + for(int memberID : conv.members()) + if(!members.contains(memberID)) + members.append(memberID); + } + return members; +} diff --git a/data/conversationslist.h b/data/conversationslist.h new file mode 100644 index 0000000..b07bef4 --- /dev/null +++ b/data/conversationslist.h @@ -0,0 +1,28 @@ +/** + * Conversations list + * + * @author Pierre HUBERT + */ + +#ifndef CONVERSATIONSLIST_H +#define CONVERSATIONSLIST_H + +#include + +#include "conversation.h" + +class ConversationsList : public QList +{ +public: + ConversationsList(); + + /** + * Get and return the ID of all the members of + * the conversations + * + * @return The IDs of the conversations + */ + QList getAllMembersId() const; +}; + +#endif // CONVERSATIONSLIST_H diff --git a/data/user.cpp b/data/user.cpp new file mode 100644 index 0000000..f27310d --- /dev/null +++ b/data/user.cpp @@ -0,0 +1,46 @@ +#include "user.h" + +User::User() +{ + +} + +int User::iD() const +{ + return mID; +} + +void User::setID(int iD) +{ + mID = iD; +} + +QString User::firstName() const +{ + return mFirstName; +} + +void User::setFirstName(const QString &firstName) +{ + mFirstName = firstName; +} + +QString User::lastName() const +{ + return mLastName; +} + +void User::setLastName(const QString &lastName) +{ + mLastName = lastName; +} + +QString User::accountImage() const +{ + return mAccountImage; +} + +void User::setAccountImage(const QString &accountImage) +{ + mAccountImage = accountImage; +} diff --git a/data/user.h b/data/user.h new file mode 100644 index 0000000..1bb3db9 --- /dev/null +++ b/data/user.h @@ -0,0 +1,36 @@ +/** + * This object contains information about a single user + * + * @author Pierre HUBERT + */ + +#ifndef USER_H +#define USER_H + +#include + +class User +{ +public: + User(); + + int iD() const; + void setID(int iD); + + QString firstName() const; + void setFirstName(const QString &firstName); + + QString lastName() const; + void setLastName(const QString &lastName); + + QString accountImage() const; + void setAccountImage(const QString &accountImage); + +private: + int mID; + QString mFirstName; + QString mLastName; + QString mAccountImage; +}; + +#endif // USER_H diff --git a/helpers/conversationslisthelper.cpp b/helpers/conversationslisthelper.cpp index eb464be..963c265 100644 --- a/helpers/conversationslisthelper.cpp +++ b/helpers/conversationslisthelper.cpp @@ -4,6 +4,7 @@ #include "conversationslisthelper.h" #include "apihelper.h" #include "../data/apirequest.h" +#include "../data/conversationslist.h" ConversationsListHelper::ConversationsListHelper(QObject *parent) : QObject(parent) { @@ -25,7 +26,7 @@ void ConversationsListHelper::getConvListCallback(int code, const QJsonDocument qobject_cast(sender())->deleteLater(); if(code != 200){ - emit onGotList(false, QList()); + emit onGotList(false, ConversationsList()); return; } @@ -33,7 +34,7 @@ void ConversationsListHelper::getConvListCallback(int code, const QJsonDocument QJsonArray arr = document.array(); //Process the list of conversations - QList list; + ConversationsList list; for(int i = 0; i < arr.size(); i++) list.append(GetConversationFromJson(arr.at(i).toObject())); @@ -54,8 +55,9 @@ Conversation ConversationsListHelper::GetConversationFromJson(const QJsonObject //Process the list of members of the conversation QJsonArray members_arr = obj.value("members").toArray(); QList members_list; - for(int i = 0; i < members_arr.size(); i++) - members_list.append(members_arr.at(i).toInt()); + for(int i = 0; i < members_arr.size(); i++){ + members_list.append(members_arr.at(i).toString().toInt()); + } conv.setMembers(members_list); return conv; diff --git a/helpers/conversationslisthelper.h b/helpers/conversationslisthelper.h index c6386d8..b28a52f 100644 --- a/helpers/conversationslisthelper.h +++ b/helpers/conversationslisthelper.h @@ -13,9 +13,11 @@ #include "../data/conversation.h" -class APIHelper; class QJsonObject; +class APIHelper; +class ConversationsList; + class ConversationsListHelper : public QObject { Q_OBJECT @@ -35,7 +37,7 @@ signals: * @param success TRUE for a success / FALSE else * @param list The list of conversations */ - void onGotList(bool success, const QList &list); + void onGotList(bool success, const ConversationsList &list); public slots: diff --git a/helpers/usershelper.cpp b/helpers/usershelper.cpp new file mode 100644 index 0000000..baf302a --- /dev/null +++ b/helpers/usershelper.cpp @@ -0,0 +1,55 @@ +#include + +#include "usershelper.h" +#include "apihelper.h" + +UsersHelper::UsersHelper(QObject *parent) : QObject(parent) +{ + mAPIHelper = new APIHelper(this); +} + +void UsersHelper::getList(QList ids) +{ + APIRequest *request = new APIRequest(this); + request->setURI("user/getInfosMultiple"); + + QString ids_str; + for(int id : ids) + ids_str += QString::number(id) + ","; + + request->addString("usersID", ids_str); + mAPIHelper->execute(request); + + //Make connection + connect(request, &APIRequest::finished, this, &UsersHelper::getUsersInformationFinished); +} + +void UsersHelper::getUsersInformationFinished(int code, const QJsonDocument &document) +{ + //Delete the request + qobject_cast(sender())->deleteLater(); + + //Check for error + if(code != 200){ + emit onGotUsersInfo(false, QList()); + return; + } + + //Parse the list of object + QList list; + QJsonObject obj = document.object(); + for(QString id : obj.keys()) + list.append(ParseJSONToUser(obj.value(id).toObject())); + + emit onGotUsersInfo(true, list); +} + +User UsersHelper::ParseJSONToUser(const QJsonObject &obj) +{ + User user; + user.setID(obj.value("userID").toInt()); + user.setFirstName(obj.value("firstName").toString()); + user.setLastName(obj.value("lastName").toString()); + user.setAccountImage(obj.value("accountImage").toString()); + return user; +} diff --git a/helpers/usershelper.h b/helpers/usershelper.h new file mode 100644 index 0000000..e6511b0 --- /dev/null +++ b/helpers/usershelper.h @@ -0,0 +1,69 @@ +/** + * Users helper + * + * It is used to get information about users + * + * @author Pierre HUBERT + */ + +#ifndef USERSHELPER_H +#define USERSHELPER_H + +#include + +#include "../data/user.h" + +class QJsonObject; + +class APIHelper; + +class UsersHelper : public QObject +{ + Q_OBJECT +public: + explicit UsersHelper(QObject *parent = nullptr); + + /** + * Query information about a list of users + * + * @param ids The ID of the users to get + */ + void getList(QList ids); + +signals: + + /** + * Signal emitted when we have got new users information + * + * @param success TRUE for a success / FALSE else + * @param list Information about the users + */ + void onGotUsersInfo(bool success, const QList &list); + +public slots: + +private slots: + + /** + * Slot called once the API request to get users information has been finished + * + * @param code Result code of the operation (200 for a success) + * @param document Document + */ + void getUsersInformationFinished(int code, const QJsonDocument &document); + +private: + + /** + * Turn a JSON object into a User object + * + * @param obj The object to convert + * @return Generated user object + */ + User ParseJSONToUser(const QJsonObject &obj); + + //Private fields + APIHelper *mAPIHelper; +}; + +#endif // USERSHELPER_H diff --git a/widgets/conversationslistwidget.cpp b/widgets/conversationslistwidget.cpp index ff06ddf..5c60490 100644 --- a/widgets/conversationslistwidget.cpp +++ b/widgets/conversationslistwidget.cpp @@ -1,5 +1,9 @@ +#include + #include "conversationslistwidget.h" #include "../helpers/conversationslisthelper.h" +#include "../helpers/usershelper.h" +#include "../data/conversationslist.h" ConversationsListWidget::ConversationsListWidget(QWidget *parent) : QWidget(parent) @@ -7,6 +11,10 @@ ConversationsListWidget::ConversationsListWidget(QWidget *parent) : //Create conversations helper mConversationsList = new ConversationsListHelper(this); connect(mConversationsList, &ConversationsListHelper::onGotList, this, &ConversationsListWidget::onGotConversationsList); + + //Create users helper + mUsersHelper = new UsersHelper(this); + connect(mUsersHelper, &UsersHelper::onGotUsersInfo, this, &ConversationsListWidget::onGotUsersInfo); } ConversationsListWidget::~ConversationsListWidget() @@ -19,15 +27,26 @@ void ConversationsListWidget::refresh() mConversationsList->getList(); } -void ConversationsListWidget::onGotConversationsList(bool success, const QList &list) +void ConversationsListWidget::onGotConversationsList(bool success, const ConversationsList &list) { - qWarning("Got conversations list (or failure)."); + qWarning("Got conversations list callback."); - if(!success) - qWarning("Failure."); - else { - for(Conversation conv : list){ - qWarning("Conv %d : %s", conv.iD(), conv.name().toStdString().c_str()); - } + if(!success){ + QMessageBox::warning(this, tr("Error"), tr("Could not get the list of conversations!")); + return; } + + //Get the list of users + mUsersHelper->getList(list.getAllMembersId()); +} + +void ConversationsListWidget::onGotUsersInfo(bool success, const QList &users) +{ + if(!success){ + QMessageBox::warning(this, tr("Error"), tr("Could not get information about the members of the conversations!")); + return; + } + + qDebug("Got the list of members of the conversations."); + //TODO : use ConversationItemWidget } diff --git a/widgets/conversationslistwidget.h b/widgets/conversationslistwidget.h index 80b25e3..7f86f2c 100644 --- a/widgets/conversationslistwidget.h +++ b/widgets/conversationslistwidget.h @@ -3,9 +3,11 @@ #include -#include "../data/conversation.h" +#include "../data/conversationslist.h" +#include "../data/user.h" class ConversationsListHelper; +class UsersHelper; class ConversationsListWidget : public QWidget { @@ -28,10 +30,22 @@ private slots: * @param success TRUE for a success / FALSE else * @param list The list of conversation (empty list in case of failure) */ - void onGotConversationsList(bool success, const QList &list); + void onGotConversationsList(bool success, const ConversationsList &list); + + /** + * This slot is triggered once we have information about the users + * + * @param success TRUE in case of success / FALSE else + * @param users The list of suers (empty list in case of failure) + */ + void onGotUsersInfo(bool success, const QList &users); private: ConversationsListHelper *mConversationsList; + UsersHelper *mUsersHelper; + + //Current conversation in cache + ConversationsList mCurrList; }; #endif // CONVERSATIONSLISTWIDGET_H