/soc/2015/jgeboski/facebook: b70cc4d02d68: facebook: refactored ...

James Geboski jgeboski at gmail.com
Thu Aug 20 16:20:30 EDT 2015


Changeset: b70cc4d02d689affba1f98b116fa1ed91ae3db28
Author:	 James Geboski <jgeboski at gmail.com>
Date:	 2015-08-20 15:49 -0400
Branch:	 facebook
URL: https://hg.pidgin.im/soc/2015/jgeboski/facebook/rev/b70cc4d02d68

Description:

facebook: refactored data structure usage

diffstat:

 libpurple/protocols/facebook/api.c      |  234 +++++++++++++++++--------------
 libpurple/protocols/facebook/api.h      |   48 ++++--
 libpurple/protocols/facebook/facebook.c |    6 +-
 3 files changed, 163 insertions(+), 125 deletions(-)

diffs (truncated from 717 to 300 lines):

diff --git a/libpurple/protocols/facebook/api.c b/libpurple/protocols/facebook/api.c
--- a/libpurple/protocols/facebook/api.c
+++ b/libpurple/protocols/facebook/api.c
@@ -1038,9 +1038,9 @@ fb_api_event_parse(FbApi *api, FbApiEven
                    JsonNode *root, GError **error)
 {
 	const gchar *str;
+	FbApiEvent *devent;
 	FbJsonValues *values;
 	GError *err = NULL;
-	gpointer mptr;
 	guint i;
 
 	static const struct {
@@ -1057,7 +1057,6 @@ fb_api_event_parse(FbApi *api, FbApiEven
 	};
 
 	for (i = 0; i < G_N_ELEMENTS(evtypes); i++) {
-		event->type = evtypes[i].type;
 		values = fb_json_values_new(root);
 		fb_json_values_add(values, FB_JSON_TYPE_STR, TRUE, "$");
 		fb_json_values_set_array(values, FALSE, evtypes[i].expr);
@@ -1067,9 +1066,10 @@ fb_api_event_parse(FbApi *api, FbApiEven
 			str = strrchr(str, ':');
 
 			if (str != NULL) {
-				event->uid = FB_ID_FROM_STR(str + 1);
-				mptr = fb_api_event_dup(event);
-				events = g_slist_prepend(events, mptr);
+				devent = fb_api_event_dup(event);
+				devent->type = evtypes[i].type;
+				devent->uid = FB_ID_FROM_STR(str + 1);
+				events = g_slist_prepend(events, devent);
 			}
 		}
 
@@ -1166,14 +1166,14 @@ fb_api_cb_publish_typing(FbApi *api, GBy
 	json_node_free(root);
 }
 
-static gboolean
-fb_api_xma_parse(FbApi *api, FbApiMessage *msg, const gchar *body,
-                 JsonNode *root, GError **error)
+static gchar *
+fb_api_xma_parse(FbApi *api, const gchar *body, JsonNode *root, GError **error)
 {
 	const gchar *str;
 	const gchar *url;
 	FbHttpParams *params;
 	FbJsonValues *values;
+	gchar *text;
 	GError *err = NULL;
 
 	values = fb_json_values_new(root);
@@ -1194,20 +1194,20 @@ fb_api_xma_parse(FbApi *api, FbApiMessag
 
 	if (purple_strequal(str, "ExternalUrl")) {
 		params = fb_http_params_new_parse(url, TRUE);
-		msg->text = fb_http_params_dup_str(params, "u", NULL);
+		text = fb_http_params_dup_str(params, "u", NULL);
 		fb_http_params_free(params);
 	} else {
-		msg->text = g_strdup(url);
+		text = g_strdup(url);
 	}
 
-	if (fb_http_urlcmp(body, msg->text, FALSE)) {
-		g_free(msg->text);
+	if (fb_http_urlcmp(body, text, FALSE)) {
+		g_free(text);
 		g_object_unref(values);
-		return FALSE;
+		return NULL;
 	}
 
 	g_object_unref(values);
-	return TRUE;
+	return text;
 }
 
 static GSList *
@@ -1216,9 +1216,10 @@ fb_api_message_parse_attach(FbApi *api, 
 {
 	const gchar *str;
 	const gchar *url;
+	FbApiMessage *dmsg;
 	FbJsonValues *values;
+	gchar *xma;
 	GError *err = NULL;
-	gpointer mptr;
 	JsonNode *node;
 	JsonNode *xode;
 
@@ -1236,12 +1237,10 @@ fb_api_message_parse_attach(FbApi *api, 
 		url = fb_json_values_next_str(values, NULL);
 
 		if ((str != NULL) && g_str_has_prefix(str, "image/")) {
-			msg->flags |= FB_API_MESSAGE_FLAG_IMAGE;
-			msg->text = g_strdup(url);
-			mptr = fb_api_message_dup(msg, FALSE);
-
-			msg->flags &= ~FB_API_MESSAGE_FLAG_IMAGE;
-			msgs = g_slist_prepend(msgs, mptr);
+			dmsg = fb_api_message_dup(msg, FALSE);
+			dmsg->flags |= FB_API_MESSAGE_FLAG_IMAGE;
+			dmsg->text = g_strdup(url);
+			msgs = g_slist_prepend(msgs, dmsg);
 			continue;
 		}
 
@@ -1255,10 +1254,12 @@ fb_api_message_parse_attach(FbApi *api, 
 			}
 
 			xode = fb_json_node_get_nth(node, 0);
-
-			if (fb_api_xma_parse(api, msg, body, xode, &err)) {
-				mptr = fb_api_message_dup(msg, FALSE);
-				msgs = g_slist_prepend(msgs, mptr);
+			xma = fb_api_xma_parse(api, body, xode, &err);
+
+			if (xma != NULL) {
+				dmsg = fb_api_message_dup(msg, FALSE);
+				dmsg->text = xma;
+				msgs = g_slist_prepend(msgs, dmsg);
 			}
 
 			json_node_free(node);
@@ -1276,11 +1277,11 @@ fb_api_message_parse_attach(FbApi *api, 
 			str = _("unknown attachment");
 		}
 
-		msg->text = g_strdup_printf("%s/%" FB_ID_FORMAT " [%s]",
-					    FB_API_URL_MESSAGES,
-					    msg->uid, str);
-		mptr = fb_api_message_dup(msg, FALSE);
-		msgs = g_slist_prepend(msgs, mptr);
+		dmsg = fb_api_message_dup(msg, FALSE);
+		dmsg->text = g_strdup_printf("%s/%" FB_ID_FORMAT " [%s]",
+		                             FB_API_URL_MESSAGES,
+		                             msg->uid, str);
+		msgs = g_slist_prepend(msgs, dmsg);
 	}
 
 	if (G_UNLIKELY(err != NULL)) {
@@ -1296,6 +1297,7 @@ fb_api_cb_publish_ms(FbApi *api, GByteAr
 {
 	const gchar *body;
 	const gchar *data;
+	FbApiMessage *dmsg;
 	FbApiMessage msg;
 	FbApiPrivate *priv = api->priv;
 	FbHttpParams *params;
@@ -1307,7 +1309,6 @@ fb_api_cb_publish_ms(FbApi *api, GByteAr
 	gchar *stoken;
 	GError *err = NULL;
 	gint64 id;
-	gpointer mptr;
 	GRegex *regex;
 	GSList *msgs = NULL;
 	guint size;
@@ -1404,20 +1405,20 @@ fb_api_cb_publish_ms(FbApi *api, GByteAr
 		body = fb_json_values_next_str(values, NULL);
 
 		if (body != NULL) {
-			msg.text = g_strdup(body);
-			mptr = fb_api_message_dup(&msg, FALSE);
-			msgs = g_slist_prepend(msgs, mptr);
+			dmsg = fb_api_message_dup(&msg, FALSE);
+			dmsg->text = g_strdup(body);
+			msgs = g_slist_prepend(msgs, dmsg);
 		}
 
 		id = fb_json_values_next_int(values, 0);
 
 		if (id != 0) {
+			dmsg = fb_api_message_dup(&msg, FALSE);
 			params = fb_http_params_new();
 			fb_http_params_set_int(params, "sticker_id", id);
-			msg.text = fb_http_params_close(params,
-			                                FB_API_URL_STICKER);
-			mptr = fb_api_message_dup(&msg, FALSE);
-			msgs = g_slist_prepend(msgs, mptr);
+			dmsg->text = fb_http_params_close(params,
+			                                  FB_API_URL_STICKER);
+			msgs = g_slist_prepend(msgs, dmsg);
 		}
 
 		node = fb_json_values_get_root(values);
@@ -1444,12 +1445,11 @@ fb_api_cb_publish_ms(FbApi *api, GByteAr
 static void
 fb_api_cb_publish_p(FbApi *api, GByteArray *pload)
 {
-	FbApiPresence pres;
+	FbApiPresence *pres;
 	FbThrift *thft;
 	FbThriftType type;
 	gint32 i32;
 	gint64 i64;
-	gpointer mptr;
 	GSList *press;
 	guint i;
 	guint size;
@@ -1482,11 +1482,11 @@ fb_api_cb_publish_p(FbApi *api, GByteArr
 		g_warn_if_fail(type == FB_THRIFT_TYPE_I32);
 		fb_thrift_read_i32(thft, &i32);
 
-		pres.uid = i64;
-		pres.active = i32 != 0;
-
-		mptr = fb_api_presence_dup(&pres);
-		press = g_slist_prepend(press, mptr);
+		pres = fb_api_presence_dup(NULL);
+		pres->uid = i64;
+		pres->active = i32 != 0;
+		press = g_slist_prepend(press, pres);
+
 		fb_util_debug_info("Presence: %" FB_ID_FORMAT " (%d)",
 		                   i64, i32 != 0);
 
@@ -1840,12 +1840,12 @@ fb_api_cb_contacts(PurpleHttpConnection 
                    gpointer data)
 {
 	FbApi *api = data;
+	FbApiUser *duser;
 	FbApiUser user;
 	FbJsonValues *values;
 	gboolean complete;
 	gchar *writeid = NULL;
 	GError *err = NULL;
-	gpointer mptr;
 	GSList *users = NULL;
 	guint count = 0;
 	JsonNode *node;
@@ -1870,8 +1870,10 @@ fb_api_cb_contacts(PurpleHttpConnection 
 		fb_api_user_reset(&user, FALSE);
 
 		if (fb_api_contact_parse(api, &user, node, &err)) {
-			mptr = fb_api_user_dup(&user, FALSE);
-			users = g_slist_prepend(users, mptr);
+			duser = fb_api_user_dup(&user, FALSE);
+			users = g_slist_prepend(users, duser);
+		} else {
+			fb_api_user_reset(&user, TRUE);
 		}
 
 		if (G_UNLIKELY(err != NULL)) {
@@ -1982,7 +1984,7 @@ fb_api_message(FbApi *api, FbId id, gboo
 	const gchar *tpfx;
 	FbApiPrivate *priv;
 	gchar *json;
-	gpointer mptr;
+	guint64 *dmsgid;
 	guint64 msgid;
 	JsonBuilder *bldr;
 
@@ -1993,8 +1995,8 @@ fb_api_message(FbApi *api, FbId id, gboo
 	msgid = FB_API_MSGID(g_get_real_time() / 1000, g_random_int());
 	tpfx = thread ? "tfbid_" : "";
 
-	mptr = g_memdup(&msgid, sizeof msgid);
-	g_hash_table_replace(priv->msgids, mptr, mptr);
+	dmsgid = g_memdup(&msgid, sizeof msgid);
+	g_hash_table_replace(priv->msgids, dmsgid, dmsgid);
 
 	bldr = fb_json_bldr_new(JSON_NODE_OBJECT);
 	fb_json_bldr_add_int(bldr, "msgid", msgid);
@@ -2067,9 +2069,9 @@ fb_api_cb_unread_parse_attach(FbApi *api
 {
 	const gchar *str;
 	const gchar *url;
+	FbApiMessage *dmsg;
 	FbJsonValues *values;
 	GError *err = NULL;
-	gpointer mptr;
 
 	values = fb_json_values_new(root);
 	fb_json_values_add(values, FB_JSON_TYPE_STR, FALSE, "$.mimetype");
@@ -2083,12 +2085,10 @@ fb_api_cb_unread_parse_attach(FbApi *api
 		url = fb_json_values_next_str(values, NULL);
 
 		if ((str != NULL) && g_str_has_prefix(str, "image/")) {
-			msg->flags |= FB_API_MESSAGE_FLAG_IMAGE;
-			msg->text = g_strdup(url);
-			mptr = fb_api_message_dup(msg, FALSE);
-
-			msg->flags &= ~FB_API_MESSAGE_FLAG_IMAGE;
-			msgs = g_slist_prepend(msgs, mptr);
+			dmsg = fb_api_message_dup(msg, FALSE);
+			dmsg->flags |= FB_API_MESSAGE_FLAG_IMAGE;
+			dmsg->text = g_strdup(url);
+			msgs = g_slist_prepend(msgs, dmsg);
 			continue;
 		}
 
@@ -2098,11 +2098,11 @@ fb_api_cb_unread_parse_attach(FbApi *api
 			str = _("unknown attachment");
 		}
 
-		msg->text = g_strdup_printf("%s/%" FB_ID_FORMAT " [%s]",
-					    FB_API_URL_MESSAGES,



More information about the Commits mailing list