From ddc8524f2427c9f64ecd9c9850a629fe38702918 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Thu, 19 Oct 2017 15:36:38 -0400
Subject: [PATCH] newaccountmodel: load avatar for account from database

Change-Id: Icff9e2532a931ed1190915147ea21d86aaf1f40d
Reviewed-by: Olivier Soldano <olivier.soldano@savoirfairelinux.com>
---
 src/authority/databasehelper.cpp | 14 ++++++++++++++
 src/authority/databasehelper.h   |  7 +++++++
 src/newaccountmodel.cpp          | 10 ++++++----
 3 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/src/authority/databasehelper.cpp b/src/authority/databasehelper.cpp
index 09a0347b..8be410d6 100644
--- a/src/authority/databasehelper.cpp
+++ b/src/authority/databasehelper.cpp
@@ -96,6 +96,20 @@ getPeerParticipantsForConversation(Database& db, const std::string& profileId, c
                      {{":id", conversationId}, {":participant_id", profileId}}).payloads;
 }
 
+std::string
+getAvatarForProfileId(Database& db, const std::string& profileId)
+{
+    auto returnFromDb = db.select("photo",
+                                  "profiles",
+                                  "id=:id",
+                                  {{":id", profileId}});
+    if (returnFromDb.nbrOfCols == 1 && returnFromDb.payloads.size() >= 1) {
+      auto payloads = returnFromDb.payloads;
+      return payloads[0];
+    }
+    return "";
+}
+
 api::contact::Info
 buildContactFromProfileId(Database& db, const std::string& profileId)
 {
diff --git a/src/authority/databasehelper.h b/src/authority/databasehelper.h
index 59cc183a..11999fa3 100644
--- a/src/authority/databasehelper.h
+++ b/src/authority/databasehelper.h
@@ -77,6 +77,13 @@ std::vector<std::string> getPeerParticipantsForConversation(Database& db,
                                                             const std::string& profileId,
                                                             const std::string& conversationId);
 
+/**
+ * @param  db
+ * @param  profileId
+ * @return the avatar in the database for a profile
+ */
+std::string getAvatarForProfileId(Database& db, const std::string& profileId);
+
 api::contact::Info buildContactFromProfileId(Database& db, const std::string& profileId);
 
 /**
diff --git a/src/newaccountmodel.cpp b/src/newaccountmodel.cpp
index f64d7528..b713f245 100644
--- a/src/newaccountmodel.cpp
+++ b/src/newaccountmodel.cpp
@@ -167,7 +167,6 @@ NewAccountModelPimpl::addToAccounts(const std::string& accountId)
     auto& owner = item.second;
     owner.id = accountId;
     owner.enabled = details["Account.enable"] == QString("true");
-    // TODO get avatar;
     owner.profileInfo.type = details["Account.type"] == "RING" ? profile::Type::RING : profile::Type::SIP;
     owner.profileInfo.alias = details["Account.alias"].toStdString();
     owner.registeredName = owner.profileInfo.type == profile::Type::RING ?
@@ -176,9 +175,12 @@ NewAccountModelPimpl::addToAccounts(const std::string& accountId)
                         details["Account.username"].toStdString().substr(std::string("ring:").size())
                         : details["Account.username"].toStdString();
     // Add profile into database
-    authority::database::getOrInsertProfile(database, owner.profileInfo.uri,
-                                            owner.profileInfo.alias, "",
-                                            details["Account.type"].toStdString());
+    auto accountProfileId = authority::database::getOrInsertProfile(database, owner.profileInfo.uri,
+                                                                    owner.profileInfo.alias, "",
+                                                                    details["Account.type"].toStdString());
+    // Retrieve avatar from database
+    auto avatar = authority::database::getAvatarForProfileId(database, accountProfileId);
+    owner.profileInfo.avatar = avatar;
     // Init models for this account
     owner.callModel = std::make_unique<NewCallModel>(owner, callbacksHandler);
     owner.contactModel = std::make_unique<ContactModel>(owner, database, callbacksHandler);
-- 
GitLab