From 61253c4ae300492ace0bcdb9cd4abb644bef3118 Mon Sep 17 00:00:00 2001
From: George Hazan <ghazan@miranda.im>
Date: Tue, 13 Aug 2019 11:54:07 +0300
Subject: Twitter:

- fixed update of user names;
- more effective array iterators;
- version bump
---
 protocols/Twitter/src/connection.cpp | 19 +++++++++++++------
 protocols/Twitter/src/contacts.cpp   | 34 +++++++++++-----------------------
 protocols/Twitter/src/oauth.cpp      | 16 ++++++++--------
 protocols/Twitter/src/twitter.cpp    | 17 ++++++-----------
 protocols/Twitter/src/version.h      |  4 ++--
 5 files changed, 40 insertions(+), 50 deletions(-)

diff --git a/protocols/Twitter/src/connection.cpp b/protocols/Twitter/src/connection.cpp
index 8ec1d5bc9a..f2b866d088 100644
--- a/protocols/Twitter/src/connection.cpp
+++ b/protocols/Twitter/src/connection.cpp
@@ -307,7 +307,13 @@ void TwitterProto::MessageLoop(void*)
 
 		if (m_iStatus != ID_STATUS_ONLINE)
 			break;
-	
+
+		if (i % 10 == 0)
+			UpdateFriends();
+
+		if (m_iStatus != ID_STATUS_ONLINE)
+			break;
+
 		UpdateStatuses(new_account, popups, tweetToMsg);
 		if (m_iStatus != ID_STATUS_ONLINE)
 			break;
@@ -424,12 +430,13 @@ void TwitterProto::UpdateFriends()
 			friends = twit_.get_friends();
 		}
 
-		for (std::vector<twitter_user>::iterator i = friends.begin(); i != friends.end(); ++i) {
-			if (i->username == twit_.get_username())
+		for (auto &i : friends) {
+			if (i.username == twit_.get_username())
 				continue;
 
-			MCONTACT hContact = AddToClientList(i->username.c_str(), i->status.text.c_str());
-			UpdateAvatar(hContact, i->profile_image_url);
+			MCONTACT hContact = AddToClientList(i.username.c_str(), i.status.text.c_str());
+			setUString(hContact, "Nick", i.real_name.c_str());
+			UpdateAvatar(hContact, i.profile_image_url);
 		}
 		disconnectionCount = 0;
 		debugLogA("***** Friends list updated");
@@ -528,7 +535,7 @@ void TwitterProto::UpdateStatuses(bool pre_read, bool popups, bool tweetToMsg)
 				continue;
 
 			MCONTACT hContact = AddToClientList(i->username.c_str(), "");
-			UpdateAvatar(hContact, i->profile_image_url); // as UpdateFriends() doesn't work at the moment, i'm going to update the avatars here
+			UpdateAvatar(hContact, i->profile_image_url);
 
 			// i think we maybe should just do that DBEF_READ line instead of stopping ALL this code.  have to test.
 			if (tweetToMsg) {
diff --git a/protocols/Twitter/src/contacts.cpp b/protocols/Twitter/src/contacts.cpp
index caed5ebeb9..24eecd5112 100644
--- a/protocols/Twitter/src/contacts.cpp
+++ b/protocols/Twitter/src/contacts.cpp
@@ -211,15 +211,8 @@ MCONTACT TwitterProto::UsernameToHContact(const char *name)
 		if (getByte(hContact, "ChatRoom"))
 			continue;
 
-		DBVARIANT dbv;
-		if (!getString(hContact, TWITTER_KEY_UN, &dbv)) {
-			if (mir_strcmp(name, dbv.pszVal) == 0) {
-				db_free(&dbv);
-				return hContact;
-			}
-			else
-				db_free(&dbv);
-		}
+		if (getMStringA(hContact, TWITTER_KEY_UN) == name)
+			return hContact;
 	}
 
 	return 0;
@@ -237,19 +230,14 @@ MCONTACT TwitterProto::AddToClientList(const char *name, const char *status)
 
 	// If not, make a new contact!
 	hContact = db_add_contact();
-	if (hContact) {
-		if (Proto_AddToContact(hContact, m_szModuleName) == 0) {
-			setString(hContact, TWITTER_KEY_UN, name);
-			setString(hContact, "Homepage", "https://twitter.com/" + CMStringA(name));
-			setWord(hContact, "Status", ID_STATUS_ONLINE);
-			db_set_utf(hContact, "CList", "StatusMsg", status);
-
-			Skin_PlaySound("TwitterNewContact");
-			Clist_SetGroup(hContact, getMStringW(TWITTER_KEY_GROUP));
-			return hContact;
-		}
-		db_delete_contact(hContact);
-	}
+	Proto_AddToContact(hContact, m_szModuleName);
 
-	return 0;
+	setString(hContact, TWITTER_KEY_UN, name);
+	setString(hContact, "Homepage", "https://twitter.com/" + CMStringA(name));
+	setWord(hContact, "Status", ID_STATUS_ONLINE);
+	db_set_utf(hContact, "CList", "StatusMsg", status);
+
+	Skin_PlaySound("TwitterNewContact");
+	Clist_SetGroup(hContact, getMStringW(TWITTER_KEY_GROUP));
+	return hContact;
 }
diff --git a/protocols/Twitter/src/oauth.cpp b/protocols/Twitter/src/oauth.cpp
index e7ff001450..3de06a8121 100644
--- a/protocols/Twitter/src/oauth.cpp
+++ b/protocols/Twitter/src/oauth.cpp
@@ -122,10 +122,12 @@ wstring mir_twitter::OAuthWebRequestSubmit(
 
 wstring mir_twitter::OAuthWebRequestSubmit(const OAuthParameters &parameters, const wstring&)
 {
-	wstring oauthHeader = L"OAuth ";
+	wstring oauthHeader;
 
 	for (auto &it : parameters) {
-		if (it != *parameters.begin())
+		if (oauthHeader.empty())
+			oauthHeader += L"OAuth ";
+		else
 			oauthHeader += L",";
 
 		wstring pair;
@@ -268,20 +270,18 @@ wstring mir_twitter::OAuthNormalizeUrl(const wstring& url)
 wstring mir_twitter::OAuthNormalizeRequestParameters(const OAuthParameters& requestParameters)
 {
 	list<wstring> sorted;
-	for (OAuthParameters::const_iterator it = requestParameters.begin();
-		it != requestParameters.end();
-		++it) {
-		wstring param = it->first + L"=" + it->second;
+	for (auto &it : requestParameters) {
+		wstring param = it.first + L"=" + it.second;
 		sorted.push_back(param);
 	}
 	sorted.sort();
 
 	wstring params;
-	for (list<wstring>::iterator it = sorted.begin(); it != sorted.end(); ++it) {
+	for (auto &it : sorted) {
 		if (params.size() > 0)
 			params += L"&";
 
-		params += *it;
+		params += it;
 	}
 
 	return params;
diff --git a/protocols/Twitter/src/twitter.cpp b/protocols/Twitter/src/twitter.cpp
index 1b2b98a2e3..e5cb815fd7 100644
--- a/protocols/Twitter/src/twitter.cpp
+++ b/protocols/Twitter/src/twitter.cpp
@@ -69,7 +69,7 @@ const std::string & twitter::get_base_url() const
 std::vector<twitter_user> twitter::get_friends()
 {
 	std::vector<twitter_user> friends;
-	http::response resp = slurp(base_url_ + "1.1/statuses/friends.json", http::get);
+	http::response resp = slurp(base_url_ + "1.1/friends/list.json", http::get);
 
 	if (resp.code != 200)
 		throw bad_response();
@@ -78,8 +78,7 @@ std::vector<twitter_user> twitter::get_friends()
 	if (!root)
 		throw std::exception("unable to parse response");
 
-	for (auto it = root.begin(); it != root.end(); ++it) {
-		const JSONNode &one = *it;
+	for (auto &one : root["users"]) {
 		twitter_user user;
 		user.username = one["screen_name"].as_string();
 		user.real_name = one["name"].as_string();
@@ -218,13 +217,12 @@ std::vector<twitter_user> twitter::get_statuses(int count, twitter_id id)
 	if (!root)
 		throw std::exception("unable to parse response");
 
-	const JSONNode &pNodes = root.as_array();
-	for (auto it = pNodes.begin(); it != pNodes.end(); ++it) {
-		const JSONNode &one = *it,
-			&pUser = one["user"];
+	for (auto &one : root) {
+		const JSONNode &pUser = one["user"];
 
 		twitter_user u;
 		u.username = pUser["screen_name"].as_string();
+		u.real_name = pUser["name"].as_string();
 		u.profile_image_url = pUser["profile_image_url"].as_string();
 
 		// the tweet will be truncated unless we take action.  i hate you twitter API
@@ -282,12 +280,9 @@ std::vector<twitter_user> twitter::get_direct(twitter_id id)
 	if (!root)
 		throw std::exception("unable to parse response");
 
-	const JSONNode &pNodes = root.as_array();
-	for (auto it = pNodes.begin(); it != pNodes.end(); ++it) {
+	for (auto &one : root) {
 		twitter_user u;
-		const JSONNode &one = *it;
 		u.username = one["sender_screen_name"].as_string();
-
 		u.status.text = one["text"].as_string();
 		u.status.id = str2int(one["id"].as_string());
 		std::string timestr = one["created_at"].as_string();
diff --git a/protocols/Twitter/src/version.h b/protocols/Twitter/src/version.h
index 6023a8ef8f..38e02d79bc 100644
--- a/protocols/Twitter/src/version.h
+++ b/protocols/Twitter/src/version.h
@@ -1,7 +1,7 @@
 #define __MAJOR_VERSION            1
 #define __MINOR_VERSION            3
-#define __RELEASE_NUM              0
-#define __BUILD_NUM                3
+#define __RELEASE_NUM              1
+#define __BUILD_NUM                1
 
 #include <stdver.h>
 
-- 
cgit v1.2.3