mirror of
https://gitlab.com/comunic/comunicterm
synced 2025-06-19 16:45:17 +00:00
Can get user info
This commit is contained in:
64
helpers/userhelper.cpp
Normal file
64
helpers/userhelper.cpp
Normal file
@ -0,0 +1,64 @@
|
||||
#include "userhelper.h"
|
||||
|
||||
#include <api_request.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
std::map<int, User> UserHelper::mCache;
|
||||
|
||||
UserHelper::UserHelper()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
User UserHelper::GetSingle(int id)
|
||||
{
|
||||
auto l = vector<int>();
|
||||
l.push_back(id);
|
||||
return getMultiple(l)[id];
|
||||
}
|
||||
|
||||
map<int, User> UserHelper::getMultiple(std::vector<int> ids)
|
||||
{
|
||||
map<int, User> users;
|
||||
vector<int> toGet;
|
||||
|
||||
// Check for missing users in cache
|
||||
for(auto id : ids) {
|
||||
if(users.find(id) == users.end())
|
||||
toGet.push_back(id);
|
||||
else
|
||||
users[id] = users[id];
|
||||
}
|
||||
|
||||
// Get required users info
|
||||
if(toGet.size() > 0) {
|
||||
|
||||
string ids = "";
|
||||
for(auto i : toGet) ids += to_string(i) + ",";
|
||||
|
||||
auto req = ApiRequest("user/getInfoMultiple", true);
|
||||
req.addArg("usersID", ids);
|
||||
|
||||
auto res = req.exec();
|
||||
if(res.code() != 200)
|
||||
throw runtime_error("Could not get the list of users (status code of response: " + to_string(res.code()));
|
||||
|
||||
|
||||
// Parse results
|
||||
const auto obj = res.object();
|
||||
for(auto id : toGet) {
|
||||
const auto user_obj = obj.at(to_string(id)).as_object();
|
||||
User newUser(user_obj.at("userID").as_number().to_int32(),
|
||||
user_obj.at("firstName").as_string(),
|
||||
user_obj.at("lastName").as_string());
|
||||
|
||||
mCache[id] = newUser;
|
||||
users[id] = newUser;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
return users;
|
||||
}
|
25
helpers/userhelper.h
Normal file
25
helpers/userhelper.h
Normal file
@ -0,0 +1,25 @@
|
||||
/**
|
||||
* User helper
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include <entities/user.h>
|
||||
|
||||
|
||||
class UserHelper
|
||||
{
|
||||
public:
|
||||
UserHelper();
|
||||
|
||||
static User GetSingle(int id);
|
||||
static std::map<int, User> getMultiple(std::vector<int> ids);
|
||||
|
||||
private:
|
||||
static std::map<int, User> mCache;
|
||||
};
|
Reference in New Issue
Block a user