Commit a42fc494 authored by Adrien Béraud's avatar Adrien Béraud

server account manager: sync contacts

Change-Id: Ia36712d1f7342bcffd8c47a7bcb3c2490b3762b1
parent a7378c4b
......@@ -86,9 +86,13 @@ struct Contact
Json::Value toJson() const {
Json::Value json;
json["added"] = Json::Int64(added);
json["removed"] = Json::Int64(removed);
json["confirmed"] = confirmed;
json["banned"] = banned;
if (removed) {
json["removed"] = Json::Int64(removed);
}
if (confirmed)
json["confirmed"] = confirmed;
if (banned)
json["banned"] = banned;
return json;
}
......
......@@ -27,16 +27,18 @@
#include "manager.h"
#include <algorithm>
#include <string_view>
namespace jami {
using Request = dht::http::Request;
static const std::string PATH_LOGIN = "/api/login";
static const std::string PATH_AUTH = "/api/auth";
static const std::string PATH_DEVICE = PATH_AUTH + "/device";
static const std::string PATH_DEVICES = PATH_AUTH + "/devices";
static const std::string PATH_SEARCH = PATH_AUTH + "/directory/search";
#define JAMI_PATH_LOGIN "/api/login"
#define JAMI_PATH_AUTH "/api/auth"
constexpr std::string_view PATH_DEVICE = JAMI_PATH_AUTH "/device";
constexpr std::string_view PATH_DEVICES = JAMI_PATH_AUTH "/devices";
constexpr std::string_view PATH_SEARCH = JAMI_PATH_AUTH "/directory/search";
constexpr std::string_view PATH_CONTACTS = JAMI_PATH_AUTH "/contacts";
ServerAccountManager::ServerAccountManager(
const std::string& path,
......@@ -183,7 +185,10 @@ ServerAccountManager::initAuthentication(
void
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());
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) {
......@@ -304,29 +309,65 @@ void ServerAccountManager::sendAccountRequest(const std::shared_ptr<dht::http::R
void
ServerAccountManager::syncDevices()
{
const std::string url = managerHostname_ + PATH_DEVICES;
JAMI_WARN("[Auth] syncDevices %s", url.c_str());
sendDeviceRequest(std::make_shared<Request>(*Manager::instance().ioContext(), url, [onAsync = onAsync_] (Json::Value json, const dht::http::Response& response){
const std::string urlDevices = managerHostname_ + PATH_DEVICES;
const std::string urlContacts = managerHostname_ + PATH_CONTACTS;
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) {
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);
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");
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);
}
}
for (unsigned i=0, n=json.size(); i<n; i++) {
const auto& e = json[i];
dht::InfoHash deviceId(e["deviceId"].asString());
if (deviceId) {
this_.info_->contacts->foundAccountDevice(deviceId, e["alias"].asString(), clock::now());
}
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++) {
const auto& e = json[i];
dht::InfoHash deviceId(e["deviceId"].asString());
if (deviceId) {
this_.info_->contacts->foundAccountDevice(deviceId, e["alias"].asString(), clock::now());
}
}
}
}
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)
this_.authError(TokenScope::Device);
......
......@@ -27,6 +27,25 @@
#include <WTypes.h>
#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 {
constexpr static const char TRUE_STR[] = "true";
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment