From 9e64b26c646913d5669723269c07e6fe8277f10c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20P=C3=B6sel?= Date: Sun, 17 Apr 2016 17:06:12 +0000 Subject: Facebook: Loading names of all users in multi chat from server (not only existing Miranda contacts) It primary iterates over Miranda's contacts list to find existing contacts (e.g. to preserve existing custom names of these contacts). Then for missing ones it does request to server to get their names. git-svn-id: http://svn.miranda-ng.org/main/trunk@16701 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/FacebookRM/src/json.cpp | 49 +++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 18 deletions(-) (limited to 'protocols/FacebookRM/src/json.cpp') diff --git a/protocols/FacebookRM/src/json.cpp b/protocols/FacebookRM/src/json.cpp index ecd69d393b..d4e4c84d87 100644 --- a/protocols/FacebookRM/src/json.cpp +++ b/protocols/FacebookRM/src/json.cpp @@ -158,6 +158,33 @@ void parseUser(const JSONNode &it, facebook_user *fbu) } } +int facebook_json_parser::parse_chat_participant_names(std::string *data, std::map* participants) +{ + std::string jsonData = data->substr(9); + + JSONNode root = JSONNode::parse(jsonData.c_str()); + if (!root) + return EXIT_FAILURE; + + const JSONNode &profiles = root["payload"].at("profiles"); + if (!profiles) + return EXIT_FAILURE; + + for (auto it = profiles.begin(); it != profiles.end(); ++it) { + std::string userId = (*it).name(); + std::string userName = (*it)["name"].as_string(); + + if (userId.empty() || userName.empty()) + continue; + + auto participant = participants->find(userId); + if (participant != participants->end()) + participant->second = userName; + } + + return EXIT_SUCCESS; +} + int facebook_json_parser::parse_friends(std::string *data, std::map< std::string, facebook_user* >* friends) { std::string jsonData = data->substr(9); @@ -1195,22 +1222,6 @@ int facebook_json_parser::parse_chat_info(std::string *data, facebook_chatroom* if (!threads) return EXIT_FAILURE; - /*const JSONNode &roger = payload, "roger"); - if (roger) { - for (unsigned int i = 0; i < json_size(roger); i++) { - const JSONNode &it = json_at(roger, i); - std::tstring id = _A2T(json_name(it)); - - // Ignore "wrong" (duplicit) identifiers - these that doesn't begin with "id." - if (id.substr(0, 3) == _T("id.")) { - facebook_chatroom *room = new facebook_chatroom(); - room->thread_id = id; - - chatrooms->insert(std::make_pair((char*)_T2A(room->thread_id.c_str()), room)); - } - } - }*/ - for (auto it = threads.begin(); it != threads.end(); ++it) { const JSONNode &is_canonical_user_ = (*it)["is_canonical_user"]; const JSONNode &thread_id_ = (*it)["thread_id"]; @@ -1223,14 +1234,16 @@ int facebook_json_parser::parse_chat_info(std::string *data, facebook_chatroom* std::string tid = thread_id_.as_string(); - // TODO: allow more users to parse at once + // TODO: allow more chats to parse at once if (fbc->thread_id != tid) continue; + // const JSONNode &former_participants = (*it)["former_participants"]; // TODO: Do we want to list also former participants? We can show them with different role or something like that... + const JSONNode &participants = (*it)["participants"]; for (auto jt = participants.begin(); jt != participants.end(); ++jt) { std::string user_id = (*jt).as_string(); - fbc->participants.insert(std::make_pair(user_id.substr(5), "")); + fbc->participants.insert(std::make_pair(user_id.substr(5), "")); // strip "fbid:" prefix } fbc->chat_name = std::tstring(ptrT(mir_utf8decodeT(name_.as_string().c_str()))); -- cgit v1.2.3