Skip to content
Snippets Groups Projects
Commit a42fc494 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

server account manager: sync contacts

Change-Id: Ia36712d1f7342bcffd8c47a7bcb3c2490b3762b1
parent a7378c4b
No related branches found
No related tags found
No related merge requests found
...@@ -86,8 +86,12 @@ struct Contact ...@@ -86,8 +86,12 @@ struct Contact
Json::Value toJson() const { Json::Value toJson() const {
Json::Value json; Json::Value json;
json["added"] = Json::Int64(added); json["added"] = Json::Int64(added);
if (removed) {
json["removed"] = Json::Int64(removed); json["removed"] = Json::Int64(removed);
}
if (confirmed)
json["confirmed"] = confirmed; json["confirmed"] = confirmed;
if (banned)
json["banned"] = banned; json["banned"] = banned;
return json; return json;
} }
......
...@@ -27,16 +27,18 @@ ...@@ -27,16 +27,18 @@
#include "manager.h" #include "manager.h"
#include <algorithm> #include <algorithm>
#include <string_view>
namespace jami { namespace jami {
using Request = dht::http::Request; using Request = dht::http::Request;
static const std::string PATH_LOGIN = "/api/login"; #define JAMI_PATH_LOGIN "/api/login"
static const std::string PATH_AUTH = "/api/auth"; #define JAMI_PATH_AUTH "/api/auth"
static const std::string PATH_DEVICE = PATH_AUTH + "/device"; constexpr std::string_view PATH_DEVICE = JAMI_PATH_AUTH "/device";
static const std::string PATH_DEVICES = PATH_AUTH + "/devices"; constexpr std::string_view PATH_DEVICES = JAMI_PATH_AUTH "/devices";
static const std::string PATH_SEARCH = PATH_AUTH + "/directory/search"; constexpr std::string_view PATH_SEARCH = JAMI_PATH_AUTH "/directory/search";
constexpr std::string_view PATH_CONTACTS = JAMI_PATH_AUTH "/contacts";
ServerAccountManager::ServerAccountManager( ServerAccountManager::ServerAccountManager(
const std::string& path, const std::string& path,
...@@ -183,7 +185,10 @@ ServerAccountManager::initAuthentication( ...@@ -183,7 +185,10 @@ ServerAccountManager::initAuthentication(
void void
ServerAccountManager::authenticateDevice() { ServerAccountManager::authenticateDevice() {
const std::string url = managerHostname_ + PATH_LOGIN; if (not info_) {
authFailed(TokenScope::Device, 0);
}
const std::string url = managerHostname_ + JAMI_PATH_LOGIN;
JAMI_WARN("[Auth] getting a device token: %s", url.c_str()); JAMI_WARN("[Auth] getting a device token: %s", url.c_str());
auto request = std::make_shared<Request>(*Manager::instance().ioContext(), url, Json::Value{Json::objectValue}, [onAsync = onAsync_] (Json::Value json, const dht::http::Response& response){ auto request = std::make_shared<Request>(*Manager::instance().ioContext(), url, Json::Value{Json::objectValue}, [onAsync = onAsync_] (Json::Value json, const dht::http::Response& response){
onAsync([=] (AccountManager& accountManager) { onAsync([=] (AccountManager& accountManager) {
...@@ -304,19 +309,54 @@ void ServerAccountManager::sendAccountRequest(const std::shared_ptr<dht::http::R ...@@ -304,19 +309,54 @@ void ServerAccountManager::sendAccountRequest(const std::shared_ptr<dht::http::R
void void
ServerAccountManager::syncDevices() ServerAccountManager::syncDevices()
{ {
const std::string url = managerHostname_ + PATH_DEVICES; const std::string urlDevices = managerHostname_ + PATH_DEVICES;
JAMI_WARN("[Auth] syncDevices %s", url.c_str()); const std::string urlContacts = managerHostname_ + PATH_CONTACTS;
sendDeviceRequest(std::make_shared<Request>(*Manager::instance().ioContext(), url, [onAsync = onAsync_] (Json::Value json, const dht::http::Response& response){
JAMI_WARN("[Auth] syncContacts %s", urlContacts.c_str());
Json::Value jsonContacts(Json::arrayValue);
for (const auto& contact : info_->contacts->getContacts()) {
auto jsonContact = contact.second.toJson();
jsonContact["uri"] = contact.first.toString();
jsonContacts.append(std::move(jsonContact));
}
sendDeviceRequest(std::make_shared<Request>(*Manager::instance().ioContext(), urlContacts, jsonContacts, [onAsync = onAsync_] (Json::Value json, const dht::http::Response& response){
onAsync([=] (AccountManager& accountManager) { onAsync([=] (AccountManager& accountManager) {
JAMI_DBG("[Auth] Got request callback with status code=%u", response.status_code); JAMI_DBG("[Auth] Got contact sync request callback with status code=%u", response.status_code);
auto& this_ = *static_cast<ServerAccountManager*>(&accountManager); auto& this_ = *static_cast<ServerAccountManager*>(&accountManager);
if (response.status_code >= 200 && response.status_code < 300) { if (response.status_code >= 200 && response.status_code < 300) {
try { try {
JAMI_WARN("[Auth] Got server response: %s", response.body.c_str()); JAMI_WARN("[Auth] Got server response: %s", response.body.c_str());
if (not json.isArray()) { if (not json.isArray()) {
JAMI_ERR("[Auth] Can't parse server response: not an array"); JAMI_ERR("[Auth] Can't parse server response: not an array");
return; } else {
for (unsigned i=0, n=json.size(); i<n; i++) {
const auto& e = json[i];
Contact contact(e);
this_.info_->contacts->updateContact(dht::InfoHash{e["uri"].asString()}, contact);
} }
}
}
catch (const std::exception& e) {
JAMI_ERR("Error when iterating contact list: %s", e.what());
}
} else if (response.status_code == 401)
this_.authError(TokenScope::Device);
this_.clearRequest(response.request);
});
}, logger_));
JAMI_WARN("[Auth] syncDevices %s", urlDevices.c_str());
sendDeviceRequest(std::make_shared<Request>(*Manager::instance().ioContext(), urlDevices, [onAsync = onAsync_] (Json::Value json, const dht::http::Response& response){
onAsync([=] (AccountManager& accountManager) {
JAMI_DBG("[Auth] Got request callback with status code=%u", response.status_code);
auto& this_ = *static_cast<ServerAccountManager*>(&accountManager);
if (response.status_code >= 200 && response.status_code < 300) {
try {
JAMI_WARN("[Auth] Got server response: %s", response.body.c_str());
if (not json.isArray()) {
JAMI_ERR("[Auth] Can't parse server response: not an array");
} else {
for (unsigned i=0, n=json.size(); i<n; i++) { for (unsigned i=0, n=json.size(); i<n; i++) {
const auto& e = json[i]; const auto& e = json[i];
dht::InfoHash deviceId(e["deviceId"].asString()); dht::InfoHash deviceId(e["deviceId"].asString());
...@@ -325,8 +365,9 @@ ServerAccountManager::syncDevices() ...@@ -325,8 +365,9 @@ ServerAccountManager::syncDevices()
} }
} }
} }
}
catch (const std::exception& e) { catch (const std::exception& e) {
JAMI_ERR("Error when loading device list: %s", e.what()); JAMI_ERR("Error when iterating device list: %s", e.what());
} }
} else if (response.status_code == 401) } else if (response.status_code == 401)
this_.authError(TokenScope::Device); this_.authError(TokenScope::Device);
......
...@@ -27,6 +27,25 @@ ...@@ -27,6 +27,25 @@
#include <WTypes.h> #include <WTypes.h>
#endif #endif
// Add string operators crucially missing from standard
// see https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/1RcShRhrmRc
namespace std {
inline string operator+(const string& s, const string_view& sv) {
string ret;
ret.reserve(s.size() + sv.size());
ret.append(s);
ret.append(sv);
return ret;
}
inline string operator+(const string_view& sv, const string& s) {
string ret;
ret.reserve(s.size() + sv.size());
ret.append(sv);
ret.append(s);
return ret;
}
}
namespace jami { namespace jami {
constexpr static const char TRUE_STR[] = "true"; constexpr static const char TRUE_STR[] = "true";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment