From 14d960713c627a3fd6dd6ea5b61d157660544d11 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 12 Jan 2020 21:15:51 +0100 Subject: [PATCH] Can process & return the real name of conversations --- ComunicTerm.pro | 6 +++-- conversations_screen.cpp | 6 ++++- entities/conversation.cpp | 39 +++++++++++++++++++++++++++++++++ entities/conversation.h | 5 +++++ entities/conversationslist.cpp | 22 +++++++++++++++++++ entities/conversationslist.h | 20 +++++++++++++++++ entities/user.h | 4 +++- helpers/conversationshelper.cpp | 5 +++-- helpers/conversationshelper.h | 3 ++- helpers/userhelper.cpp | 9 ++++++-- helpers/userhelper.h | 5 +++-- 11 files changed, 113 insertions(+), 11 deletions(-) create mode 100644 entities/conversationslist.cpp create mode 100644 entities/conversationslist.h diff --git a/ComunicTerm.pro b/ComunicTerm.pro index 91d17d9..4559295 100644 --- a/ComunicTerm.pro +++ b/ComunicTerm.pro @@ -17,7 +17,8 @@ SOURCES += \ mainmenu.cpp \ conversations_screen.cpp \ helpers/conversationshelper.cpp \ - entities/conversation.cpp + entities/conversation.cpp \ + entities/conversationslist.cpp HEADERS += \ config.h \ @@ -31,4 +32,5 @@ HEADERS += \ mainmenu.h \ conversations_screen.h \ helpers/conversationshelper.h \ - entities/conversation.h + entities/conversation.h \ + entities/conversationslist.h diff --git a/conversations_screen.cpp b/conversations_screen.cpp index 1c44b4f..b1dbd38 100644 --- a/conversations_screen.cpp +++ b/conversations_screen.cpp @@ -1,8 +1,10 @@ #include +#include "helpers/userhelper.h" #include "conversations_screen.h" #include "helpers/conversationshelper.h" #include "entities/conversation.h" +#include "entities/conversationslist.h" using namespace std; @@ -10,10 +12,12 @@ using namespace std; void showConversationsScreen() { + auto list = ConversationsHelper::GetList(); + auto users = UserHelper::getMultiple(list.usersList()); for(auto c : list) - cout << "Conv: " << c.name() << endl; + cout << "Conv: " << c.name(users) << endl; cout << "done" << endl; diff --git a/entities/conversation.cpp b/entities/conversation.cpp index 40aa5dc..bb75eb9 100644 --- a/entities/conversation.cpp +++ b/entities/conversation.cpp @@ -1,5 +1,11 @@ +#include + #include "conversation.h" +#include "../helpers/accounthelper.h" + +using namespace std; + Conversation::Conversation() { @@ -35,6 +41,34 @@ void Conversation::setLastActive(int lastActive) mLastActive = lastActive; } +std::string Conversation::name(UsersList users) +{ + if(hasName()) + return mName; + + if(mComputedName.size() == 0) { + size_t i = 0; + for(size_t j = 0; i < 3 && j < mMembers.size(); j++) { + auto userID = mMembers[j]; + + if(userID == AccountHelper::userID()) + continue; + + if(mComputedName.length() > 0) + mComputedName += ", "; + + mComputedName += users[userID].fullName(); + i++; + + } + + if(i < mMembers.size() - 2) + mComputedName += ", ..."; + } + + return mComputedName; +} + std::string Conversation::name() const { return mName; @@ -45,6 +79,11 @@ void Conversation::setName(const std::string &name) mName = name; } +bool Conversation::hasName() const +{ + return mName.size() > 0; +} + bool Conversation::following() const { return mFollowing; diff --git a/entities/conversation.h b/entities/conversation.h index debfe67..bab9ca2 100644 --- a/entities/conversation.h +++ b/entities/conversation.h @@ -10,6 +10,8 @@ #include #include +#include "user.h" + class Conversation { public: @@ -24,8 +26,10 @@ public: int lastActive() const; void setLastActive(int lastActive); + std::string name(UsersList users); std::string name() const; void setName(const std::string &name); + bool hasName() const; bool following() const; void setFollowing(bool following); @@ -41,6 +45,7 @@ private: int mID_Owner; int mLastActive; std::string mName; + std::string mComputedName = ""; bool mFollowing; bool mSawLastMessage; std::vector mMembers; diff --git a/entities/conversationslist.cpp b/entities/conversationslist.cpp new file mode 100644 index 0000000..33a93c0 --- /dev/null +++ b/entities/conversationslist.cpp @@ -0,0 +1,22 @@ +#include "conversationslist.h" + +using namespace std; + +ConversationsList::ConversationsList() +{ + +} + +set ConversationsList::usersList() const +{ + set list; + + for(auto conv : *this) { + list.insert(conv.iD_Owner()); + + for(auto m : conv.members()) + list.insert(m); + } + + return list; +} diff --git a/entities/conversationslist.h b/entities/conversationslist.h new file mode 100644 index 0000000..fe825af --- /dev/null +++ b/entities/conversationslist.h @@ -0,0 +1,20 @@ +/** + * List of conversations + * + * @author Pierre HUBERT + */ + +#pragma once + +#include + +#include "conversation.h" + + +class ConversationsList : public std::vector +{ +public: + ConversationsList(); + + std::set usersList() const; +}; diff --git a/entities/user.h b/entities/user.h index 5fb50e5..b99f6a9 100644 --- a/entities/user.h +++ b/entities/user.h @@ -7,7 +7,7 @@ #pragma once #include - +#include class User { @@ -31,3 +31,5 @@ private: std::string mFirstName; std::string mLastName; }; + +typedef std::map UsersList; diff --git a/helpers/conversationshelper.cpp b/helpers/conversationshelper.cpp index c77d36f..1f06a5f 100644 --- a/helpers/conversationshelper.cpp +++ b/helpers/conversationshelper.cpp @@ -3,6 +3,7 @@ #include "conversationshelper.h" #include "../api_request.h" #include "../entities/conversation.h" +#include "../entities/conversationslist.h" using namespace std; @@ -11,14 +12,14 @@ ConversationsHelper::ConversationsHelper() } -vector ConversationsHelper::GetList() +ConversationsList ConversationsHelper::GetList() { const auto response = ApiRequest("conversations/getList").exec(); if(response.code() != 200) throw runtime_error("Get conversations list failed! (code: " + to_string(response.code())); - vector list; + ConversationsList list; for(auto el : response.array()) { auto obj = el.as_object(); diff --git a/helpers/conversationshelper.h b/helpers/conversationshelper.h index c52d556..a50bed8 100644 --- a/helpers/conversationshelper.h +++ b/helpers/conversationshelper.h @@ -9,6 +9,7 @@ #include class Conversation; +class ConversationsList; class ConversationsHelper { @@ -18,5 +19,5 @@ public: /** * Get the list of conversations of the current user */ - static std::vector GetList(); + static ConversationsList GetList(); }; diff --git a/helpers/userhelper.cpp b/helpers/userhelper.cpp index 7f27922..9b45846 100644 --- a/helpers/userhelper.cpp +++ b/helpers/userhelper.cpp @@ -18,9 +18,9 @@ User UserHelper::GetSingle(int id) return getMultiple(l)[id]; } -map UserHelper::getMultiple(std::vector ids) +UsersList UserHelper::getMultiple(std::vector ids) { - map users; + UsersList users; vector toGet; // Check for missing users in cache @@ -62,3 +62,8 @@ map UserHelper::getMultiple(std::vector ids) return users; } + +UsersList UserHelper::getMultiple(std::set ids) +{ + return getMultiple(vector(ids.begin(), ids.end())); +} diff --git a/helpers/userhelper.h b/helpers/userhelper.h index 14cb8f2..e416c6d 100644 --- a/helpers/userhelper.h +++ b/helpers/userhelper.h @@ -8,17 +8,18 @@ #include #include +#include #include - class UserHelper { public: UserHelper(); static User GetSingle(int id); - static std::map getMultiple(std::vector ids); + static UsersList getMultiple(std::vector ids); + static UsersList getMultiple(std::set ids); private: static std::map mCache;