/soc/2015/jgeboski/facebook: 860ee76f5f58: facebook: fixed all J...

James Geboski jgeboski at gmail.com
Mon Aug 10 01:13:42 EDT 2015


Changeset: 860ee76f5f5840d3b804c940e976229fb4f707ed
Author:	 James Geboski <jgeboski at gmail.com>
Date:	 2015-08-10 01:03 -0400
Branch:	 facebook
URL: https://hg.pidgin.im/soc/2015/jgeboski/facebook/rev/860ee76f5f58

Description:

facebook: fixed all JSON arrays being required

diffstat:

 libpurple/protocols/facebook/api.c  |  44 ++++++++++++++++++++++--------------
 libpurple/protocols/facebook/json.c |  27 ++++++++++++++++-----
 libpurple/protocols/facebook/json.h |   9 ++++++-
 3 files changed, 55 insertions(+), 25 deletions(-)

diffs (294 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
@@ -347,7 +347,7 @@ fb_api_json_chk(FbApi *api, gconstpointe
 	root = fb_json_node_new(data, size, &err);
 	FB_API_ERROR_EMIT(api, err, return FALSE);
 
-	values = fb_json_values_new(root, NULL);
+	values = fb_json_values_new(root);
 	fb_json_values_add(values, FB_JSON_TYPE_INT, FALSE, "$.error_code");
 	fb_json_values_add(values, FB_JSON_TYPE_STR, FALSE, "$.error.type");
 	fb_json_values_add(values, FB_JSON_TYPE_STR, FALSE, "$.errorCode");
@@ -768,7 +768,7 @@ fb_api_cb_seqid(PurpleHttpConnection *co
 		return;
 	}
 
-	values = fb_json_values_new(root, NULL);
+	values = fb_json_values_new(root);
 	fb_json_values_add(values, FB_JSON_TYPE_STR, TRUE,
 	                   "$.viewer.message_threads.sync_sequence_id");
 	fb_json_values_add(values, FB_JSON_TYPE_INT, TRUE,
@@ -846,7 +846,7 @@ fb_api_cb_publish_mark(FbApi *api, GByte
 		return;
 	}
 
-	values = fb_json_values_new(root, NULL);
+	values = fb_json_values_new(root);
 	fb_json_values_add(values, FB_JSON_TYPE_BOOL, FALSE, "$.succeeded");
 	fb_json_values_update(values, &err);
 
@@ -879,7 +879,7 @@ fb_api_cb_publish_typing(FbApi *api, GBy
 		return;
 	}
 
-	values = fb_json_values_new(root, NULL);
+	values = fb_json_values_new(root);
 	fb_json_values_add(values, FB_JSON_TYPE_STR, TRUE, "$.type");
 	fb_json_values_add(values, FB_JSON_TYPE_INT, TRUE, "$.sender_fbid");
 	fb_json_values_add(values, FB_JSON_TYPE_INT, TRUE, "$.state");
@@ -915,7 +915,7 @@ fb_api_message_parse_xma(FbApi *api, Jso
 	gchar *ret;
 	GError *err = NULL;
 
-	values = fb_json_values_new(root, NULL);
+	values = fb_json_values_new(root);
 	fb_json_values_add(values, FB_JSON_TYPE_STR, TRUE,
 	                   "$.story_attachment.target.__type__.name");
 	fb_json_values_add(values, FB_JSON_TYPE_STR, TRUE,
@@ -956,11 +956,13 @@ fb_api_message_parse_attach(FbApi *api, 
 	JsonNode *node;
 	JsonNode *xode;
 
-	values = fb_json_values_new(root, "$.deltaNewMessage.attachments");
+	values = fb_json_values_new(root);
 	fb_json_values_add(values, FB_JSON_TYPE_STR, FALSE,
 	                   "$.imageMetadata.imageURIMap.0");
 	fb_json_values_add(values, FB_JSON_TYPE_STR, FALSE, "$.xmaGraphQL");
 	fb_json_values_add(values, FB_JSON_TYPE_STR, FALSE, "$.filename");
+	fb_json_values_set_array(values, FALSE, "$.deltaNewMessage"
+	                                         ".attachments");
 
 	while (fb_json_values_update(values, &err)) {
 		msg->text = fb_json_values_next_str_dup(values, NULL);
@@ -1066,7 +1068,7 @@ fb_api_cb_publish_ms(FbApi *api, GByteAr
 	}
 
 	g_free(json);
-	values = fb_json_values_new(root, NULL);
+	values = fb_json_values_new(root);
 	fb_json_values_add(values, FB_JSON_TYPE_INT, FALSE,
 	                   "$.lastIssuedSeqId");
 	fb_json_values_add(values, FB_JSON_TYPE_STR, FALSE, "$.syncToken");
@@ -1090,7 +1092,7 @@ fb_api_cb_publish_ms(FbApi *api, GByteAr
 		return;
 	}
 
-	values = fb_json_values_new(root, "$.deltas");
+	values = fb_json_values_new(root);
 	fb_json_values_add(values, FB_JSON_TYPE_INT, FALSE,
 	                   "$.deltaNewMessage.messageMetadata"
 			    ".offlineThreadingId");
@@ -1106,6 +1108,7 @@ fb_api_cb_publish_ms(FbApi *api, GByteAr
 	                   "$.deltaNewMessage.body");
 	fb_json_values_add(values, FB_JSON_TYPE_INT, FALSE,
 	                   "$.deltaNewMessage.stickerId");
+	fb_json_values_set_array(values, TRUE, "$.deltas");
 
 	while (fb_json_values_update(values, &err)) {
 		id = fb_json_values_next_int(values, 0);
@@ -1414,7 +1417,7 @@ fb_api_cb_auth(PurpleHttpConnection *con
 		return;
 	}
 
-	values = fb_json_values_new(root, NULL);
+	values = fb_json_values_new(root);
 	fb_json_values_add(values, FB_JSON_TYPE_STR, TRUE, "$.access_token");
 	fb_json_values_add(values, FB_JSON_TYPE_INT, TRUE, "$.uid");
 	fb_json_values_update(values, &err);
@@ -1468,7 +1471,7 @@ fb_api_cb_contacts(PurpleHttpConnection 
 		return;
 	}
 
-	values = fb_json_values_new(root, "$.viewer.messenger_contacts.nodes");
+	values = fb_json_values_new(root);
 	fb_json_values_add(values, FB_JSON_TYPE_STR, TRUE,
 	                   "$.represented_profile.id");
 	fb_json_values_add(values, FB_JSON_TYPE_STR, TRUE,
@@ -1479,6 +1482,8 @@ fb_api_cb_contacts(PurpleHttpConnection 
 	                   "$.structured_name.text");
 	fb_json_values_add(values, FB_JSON_TYPE_STR, TRUE,
 	                   "$.hugePictureUrl.uri");
+	fb_json_values_set_array(values, FALSE, "$.viewer.messenger_contacts"
+	                                         ".nodes");
 
 	while (fb_json_values_update(values, &err)) {
 		fb_api_user_reset(&user, FALSE);
@@ -1699,10 +1704,11 @@ fb_api_cb_unread_parse_attach(FbApi *api
 	GError *err = NULL;
 	gpointer mptr;
 
-	values = fb_json_values_new(root, "$.blob_attachments");
+	values = fb_json_values_new(root);
 	fb_json_values_add(values, FB_JSON_TYPE_STR, FALSE,
 	                   "$.image_full_screen.uri");
 	fb_json_values_add(values, FB_JSON_TYPE_STR, FALSE, "$.filename");
+	fb_json_values_set_array(values, FALSE, "$.blob_attachments");
 
 	while (fb_json_values_update(values, &err)) {
 		msg->text = fb_json_values_next_str_dup(values, NULL);
@@ -1764,7 +1770,7 @@ fb_api_cb_unread_msgs(PurpleHttpConnecti
 		return;
 	}
 
-	values = fb_json_values_new(node, NULL);
+	values = fb_json_values_new(node);
 	fb_json_values_add(values, FB_JSON_TYPE_STR, FALSE,
 	                   "$.thread_key.thread_fbid");
 	fb_json_values_update(values, &err);
@@ -1779,12 +1785,13 @@ fb_api_cb_unread_msgs(PurpleHttpConnecti
 	msg.tid = FB_ID_FROM_STR(str);
 
 	fb_json_values_free(values);
-	values = fb_json_values_new(node, "$.messages.nodes");
+	values = fb_json_values_new(node);
 	fb_json_values_add(values, FB_JSON_TYPE_BOOL, TRUE, "$.unread");
 	fb_json_values_add(values, FB_JSON_TYPE_STR, TRUE,
 	                   "$.message_sender.messaging_actor.id");
 	fb_json_values_add(values, FB_JSON_TYPE_STR, FALSE, "$.message.text");
 	fb_json_values_add(values, FB_JSON_TYPE_STR, FALSE, "$.sticker.id");
+	fb_json_values_set_array(values, FALSE, "$.messages.nodes");
 
 	while (fb_json_values_update(values, &err)) {
 		if (!fb_json_values_next_bool(values, FALSE)) {
@@ -1871,12 +1878,14 @@ fb_api_cb_unread(PurpleHttpConnection *c
 		return;
 	}
 
-	values = fb_json_values_new(root, "$.viewer.message_threads.nodes");
+	values = fb_json_values_new(root);
 	fb_json_values_add(values, FB_JSON_TYPE_INT, TRUE, "$.unread_count");
 	fb_json_values_add(values, FB_JSON_TYPE_STR, FALSE,
 	                   "$.thread_key.other_user_id");
 	fb_json_values_add(values, FB_JSON_TYPE_STR, FALSE,
 	                   "$.thread_key.thread_fbid");
+	fb_json_values_set_array(values, FALSE, "$.viewer.message_threads"
+	                                         ".nodes");
 
 	while (fb_json_values_update(values, &err)) {
 		count = fb_json_values_next_int(values, -5);
@@ -1948,7 +1957,7 @@ fb_api_thread_parse(FbApi *api, FbApiThr
 	GError *err = NULL;
 	gpointer mptr;
 
-	values = fb_json_values_new(root, NULL);
+	values = fb_json_values_new(root);
 	fb_json_values_add(values, FB_JSON_TYPE_STR, FALSE,
 	                   "$.thread_key.thread_fbid");
 	fb_json_values_add(values, FB_JSON_TYPE_STR, FALSE, "$.name");
@@ -1971,11 +1980,12 @@ fb_api_thread_parse(FbApi *api, FbApiThr
 	thrd->topic = fb_json_values_next_str_dup(values, NULL);
 	fb_json_values_free(values);
 
-	values = fb_json_values_new(root, "$.all_participants.nodes");
+	values = fb_json_values_new(root);
 	fb_json_values_add(values, FB_JSON_TYPE_STR, TRUE,
 	                   "$.messaging_actor.id");
 	fb_json_values_add(values, FB_JSON_TYPE_STR, TRUE,
 	                   "$.messaging_actor.name");
+	fb_json_values_set_array(values, TRUE, "$.all_participants.nodes");
 
 	while (fb_json_values_update(values, &err)) {
 		fb_api_user_reset(&user, FALSE);
@@ -2099,7 +2109,7 @@ fb_api_cb_thread_create(PurpleHttpConnec
 		return;
 	}
 
-	values = fb_json_values_new(root, NULL);
+	values = fb_json_values_new(root);
 	fb_json_values_add(values, FB_JSON_TYPE_STR, TRUE, "$.thread_fbid");
 	fb_json_values_update(values, &err);
 
diff --git a/libpurple/protocols/facebook/json.c b/libpurple/protocols/facebook/json.c
--- a/libpurple/protocols/facebook/json.c
+++ b/libpurple/protocols/facebook/json.c
@@ -349,7 +349,7 @@ fb_json_node_get_str(JsonNode *root, con
 }
 
 FbJsonValues *
-fb_json_values_new(JsonNode *root, const gchar *arrexpr)
+fb_json_values_new(JsonNode *root)
 {
 	FbJsonValues *ret;
 
@@ -359,10 +359,6 @@ fb_json_values_new(JsonNode *root, const
 	ret->root = root;
 	ret->queue = g_queue_new();
 
-	if (arrexpr != NULL) {
-		ret->array = fb_json_node_get_arr(root, arrexpr, &ret->error);
-	}
-
 	return ret;
 }
 
@@ -436,6 +432,21 @@ fb_json_values_get_root(FbJsonValues *va
 	return json_array_get_element(values->array, index);
 }
 
+void
+fb_json_values_set_array(FbJsonValues *values, gboolean required,
+                         const gchar *expr)
+{
+	g_return_if_fail(values != NULL);
+
+	values->array = fb_json_node_get_arr(values->root, expr,
+	                                     &values->error);
+	values->isarray = TRUE;
+
+	if ((values->error != NULL) && !required) {
+		g_clear_error(&values->error);
+	}
+}
+
 gboolean
 fb_json_values_update(FbJsonValues *values, GError **error)
 {
@@ -454,8 +465,10 @@ fb_json_values_update(FbJsonValues *valu
 		return FALSE;
 	}
 
-	if (values->array != NULL) {
-		if (json_array_get_length(values->array) <= values->index) {
+	if (values->isarray) {
+		if ((values->array == NULL) ||
+		    (json_array_get_length(values->array) <= values->index))
+		{
 			return FALSE;
 		}
 
diff --git a/libpurple/protocols/facebook/json.h b/libpurple/protocols/facebook/json.h
--- a/libpurple/protocols/facebook/json.h
+++ b/libpurple/protocols/facebook/json.h
@@ -64,8 +64,11 @@ struct _FbJsonValues
 	JsonNode *root;
 	GQueue *queue;
 	GList *next;
+
+	gboolean isarray;
 	JsonArray *array;
 	guint index;
+
 	GError *error;
 };
 
@@ -132,7 +135,7 @@ gchar *
 fb_json_node_get_str(JsonNode *root, const gchar *expr, GError **error);
 
 FbJsonValues *
-fb_json_values_new(JsonNode *root, const gchar *arrexpr);
+fb_json_values_new(JsonNode *root);
 
 void
 fb_json_values_free(FbJsonValues *values);
@@ -144,6 +147,10 @@ fb_json_values_add(FbJsonValues *values,
 JsonNode *
 fb_json_values_get_root(FbJsonValues *values);
 
+void
+fb_json_values_set_array(FbJsonValues *values, gboolean required,
+                         const gchar *expr);
+
 gboolean
 fb_json_values_update(FbJsonValues *values, GError **error);
 



More information about the Commits mailing list