/soc/2015/jgeboski/facebook: b14336308988: facebook: migrated FQ...

James Geboski jgeboski at gmail.com
Sat Jul 18 20:24:21 EDT 2015


Changeset: b14336308988846cc7c049108d213eff0b510e30
Author:	 James Geboski <jgeboski at gmail.com>
Date:	 2015-07-18 20:19 -0400
Branch:	 facebook
URL: https://hg.pidgin.im/soc/2015/jgeboski/facebook/rev/b14336308988

Description:

facebook: migrated FQL usage to GraphQL

diffstat:

 libpurple/protocols/facebook/api.c  |  373 +++++++++++++++++++----------------
 libpurple/protocols/facebook/api.h  |    3 +-
 libpurple/protocols/facebook/json.c |    7 +
 libpurple/protocols/facebook/json.h |    3 +-
 4 files changed, 215 insertions(+), 171 deletions(-)

diffs (truncated from 582 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
@@ -464,6 +464,23 @@ fb_api_http_req(FbApi *api, const FbApiH
 }
 
 static void
+fb_api_http_graph(FbApi *api, const FbApiHttpInfo *info, JsonBuilder *builder,
+                  const gchar *qid)
+{
+	FbHttpParams *prms;
+	gchar *json;
+
+	prms = fb_http_params_new();
+	json = fb_json_bldr_close(builder, JSON_NODE_OBJECT, NULL);
+
+	fb_http_params_set_str(prms, "query_id", qid);
+	fb_http_params_set_str(prms, "query_params", json);
+	fb_api_http_req(api, info, prms, FB_API_URL_GQL);
+
+	g_free(json);
+}
+
+static void
 fb_api_cb_http_bool(PurpleHttpConnection *con, PurpleHttpResponse *res,
                     gpointer data)
 {
@@ -538,7 +555,7 @@ fb_api_cb_seqid(PurpleHttpConnection *co
 	JsonNode *root;
 
 	static const gchar *expr =
-		"$.data[0].fql_result_set[0].sync_sequence_id";
+		"$.viewer.message_threads.sync_sequence_id";
 
 	if (!fb_api_http_chk(api, con, res, &root)) {
 		return;
@@ -582,15 +599,14 @@ static void
 fb_api_cb_mqtt_connect(FbMqtt *mqtt, gpointer data)
 {
 	FbApi *api = data;
-	FbHttpParams *prms;
 	gchar *json;
 	JsonBuilder *bldr;
 
 	static const FbApiHttpInfo info = {
 		fb_api_cb_seqid,
-		"com.facebook.orca.protocol.methods.u",
-		"fetchThreadList",
-		"GET"
+		"com.facebook.orca.e.y",
+		"ThreadListQuery",
+		"get"
 	};
 
 	bldr = fb_json_bldr_new(JSON_NODE_OBJECT);
@@ -619,19 +635,10 @@ fb_api_cb_mqtt_connect(FbMqtt *mqtt, gpo
 	/* Notifications seem to lead to some sort of sending rate limit */
 	fb_mqtt_unsubscribe(mqtt, "/orca_message_notifications", NULL);
 
+	/* See fb_api_thread_list() for key mapping */
 	bldr = fb_json_bldr_new(JSON_NODE_OBJECT);
-	fb_json_bldr_add_str(bldr, "thread_list_ids",
-		"SELECT sync_sequence_id "
-			"FROM unified_thread "
-			"WHERE folder='inbox' "
-			"ORDER BY sync_sequence_id "
-			"DESC LIMIT 1");
-	json = fb_json_bldr_close(bldr, JSON_NODE_OBJECT, NULL);
-
-	prms = fb_http_params_new();
-	fb_http_params_set_str(prms, "q", json);
-	fb_api_http_req(api, &info, prms, FB_API_URL_FQL);
-	g_free(json);
+	fb_json_bldr_add_str(bldr, "1", "0");
+	fb_api_http_graph(api, &info, bldr, FB_API_QRYID_THREAD_LIST);
 }
 
 static void
@@ -1088,8 +1095,6 @@ finish:
 void
 fb_api_contacts(FbApi *api)
 {
-	FbHttpParams *prms;
-	gchar *json;
 	JsonBuilder *bldr;
 
 	static const FbApiHttpInfo info = {
@@ -1100,14 +1105,14 @@ fb_api_contacts(FbApi *api)
 	};
 
 	/* Object key mapping:
-	 *   0 = profile_types
-	 *   1 = limit
-	 *   2 = small_img_size
-	 *   3 = big_img_size
-	 *   4 = huge_img_size
-	 *   5 = low_res_cover_size
-	 *   6 = media_type
-	 *   7 = high_res_cover_size
+	 *   0: profile_types
+	 *   1: limit
+	 *   2: small_img_size
+	 *   3: big_img_size
+	 *   4: huge_img_size
+	 *   5: low_res_cover_size
+	 *   6: media_type
+	 *   7: high_res_cover_size
 	 */
 
 	bldr = fb_json_bldr_new(JSON_NODE_OBJECT);
@@ -1116,20 +1121,12 @@ fb_api_contacts(FbApi *api)
 	fb_json_bldr_arr_end(bldr);
 
 	fb_json_bldr_add_str(bldr, "1", FB_API_CONTACTS_COUNT);
-
-	json = fb_json_bldr_close(bldr, JSON_NODE_OBJECT, NULL);
-	prms = fb_http_params_new();
-	fb_http_params_set_str(prms, "query_id", FB_API_QRYID_CONTACTS);
-	fb_http_params_set_str(prms, "query_params", json);
-	fb_api_http_req(api, &info, prms, FB_API_URL_GQL);
-	g_free(json);
+	fb_api_http_graph(api, &info, bldr, FB_API_QRYID_CONTACTS);
 }
 
 static void
 fb_api_contacts_after(FbApi *api, const gchar *writeid)
 {
-	FbHttpParams *prms;
-	gchar *json;
 	JsonBuilder *bldr;
 
 	static const FbApiHttpInfo info = {
@@ -1140,15 +1137,15 @@ fb_api_contacts_after(FbApi *api, const 
 	};
 
 	/* Object key mapping:
-	 *   0 = profile_types
-	 *   1 = after
-	 *   2 = limit
-	 *   3 = small_img_size
-	 *   4 = big_img_size
-	 *   5 = huge_img_size
-	 *   6 = low_res_cover_size
-	 *   7 = media_type
-	 *   8 = high_res_cover_size
+	 *   0: profile_types
+	 *   1: after
+	 *   2: limit
+	 *   3: small_img_size
+	 *   4: big_img_size
+	 *   5: huge_img_size
+	 *   6: low_res_cover_size
+	 *   7: media_type
+	 *   8: high_res_cover_size
 	 */
 
 	if (g_str_has_prefix(writeid, "contact_")) {
@@ -1162,13 +1159,7 @@ fb_api_contacts_after(FbApi *api, const 
 
 	fb_json_bldr_add_str(bldr, "1", writeid);
 	fb_json_bldr_add_str(bldr, "2", FB_API_CONTACTS_COUNT);
-
-	json = fb_json_bldr_close(bldr, JSON_NODE_OBJECT, NULL);
-	prms = fb_http_params_new();
-	fb_http_params_set_str(prms, "query_id", FB_API_QRYID_CONTACTS_AFTER);
-	fb_http_params_set_str(prms, "query_params", json);
-	fb_api_http_req(api, &info, prms, FB_API_URL_GQL);
-	g_free(json);
+	fb_api_http_graph(api, &info, bldr, FB_API_QRYID_CONTACTS_AFTER);
 }
 
 void
@@ -1317,110 +1308,69 @@ fb_api_thread_create(FbApi *api, GSList 
 	g_free(json);
 }
 
-static GSList *
-fb_api_cb_threads(PurpleHttpConnection *con, PurpleHttpResponse *res,
-                  gpointer data)
+static gboolean
+fb_api_thread_parse(FbApi *api, FbApiThread *thrd, JsonNode *root)
 {
-	FbApi *api = data;
 	FbApiPrivate *priv = api->priv;
-	FbApiThread thrd;
 	FbApiUser user;
 	gboolean haself;
 	gchar *str;
 	GError *err = NULL;
-	GList *elms = NULL;
-	GList *elms2 = NULL;
+	GList *elms;
 	GList *l;
-	GList *m;
 	gpointer mptr;
-	GSList *ret = NULL;
-	GSList *thrds = NULL;
-	JsonArray *arr = NULL;
-	JsonArray *arr2 = NULL;
+	JsonArray *arr;
 	JsonNode *node;
-	JsonNode *node2;
-	JsonNode *root;
 
-	static const gchar *expr = "$.data[0].fql_result_set";
-
-	if (!fb_api_http_chk(api, con, res, &root)) {
-		return NULL;
+	if (!fb_json_node_chk_str(root, "$.thread_key.thread_fbid", &str)) {
+		return FALSE;
 	}
 
-	arr = fb_json_node_get_arr(root, expr, &err);
-	FB_API_ERROR_CHK(api, err, goto finish);
+	thrd->tid = FB_ID_FROM_STR(str);
+	g_free(str);
+
+	thrd->topic = fb_json_node_get_str(root, "$.name", NULL);
+	arr = fb_json_node_get_arr(root, "$.all_participants.nodes", &err);
+	FB_API_ERROR_CHK(api, err, return FALSE);
 	elms = json_array_get_elements(arr);
 
-	for (l = elms; l != NULL; l = l->next) {
+	for (haself = FALSE, l = elms; l != NULL; l = l->next) {
 		node = l->data;
-		fb_api_thread_reset(&thrd, FALSE);
+		fb_api_user_reset(&user, FALSE);
 
-		str = fb_json_node_get_str(node, "$.thread_fbid", &err);
+		node = fb_json_node_get(node, "$.messaging_actor", &err);
 		FB_API_ERROR_CHK(api, err, goto finish);
-		thrd.tid = FB_ID_FROM_STR(str);
+
+		str = fb_json_node_get_str(node, "$.id", NULL);
+		FB_API_ERROR_CHK(api, err, goto finish);
+		user.uid = FB_ID_FROM_STR(str);
 		g_free(str);
 
-		thrd.topic = fb_json_node_get_str(node, "$.name", NULL);
-		arr2 = fb_json_node_get_arr(node, "$.participants", &err);
-		FB_API_ERROR_CHK(api, err, goto finish);
-		elms2 = json_array_get_elements(arr2);
-
-		for (haself = FALSE, m = elms2; m != NULL; m = m->next) {
-			node2 = m->data;
-			fb_api_user_reset(&user, FALSE);
-
-			str = fb_json_node_get_str(node2, "$.user_id", &err);
-			FB_API_ERROR_CHK(api, err, goto finish);
-			user.uid = FB_ID_FROM_STR(str);
-			g_free(str);
-
-			if (user.uid == priv->uid) {
-				haself = TRUE;
-				continue;
-			}
-
-			user.name = fb_json_node_get_str(node2, "$.name", NULL);
+		if (user.uid != priv->uid) {
+			user.name = fb_json_node_get_str(node, "$.name", NULL);
 			mptr = fb_api_user_dup(&user, FALSE);
-			thrd.users = g_slist_prepend(thrd.users, mptr);
+			thrd->users = g_slist_prepend(thrd->users, mptr);
+		} else {
+			haself = TRUE;
 		}
 
-		if (G_LIKELY(arr2 != NULL)) {
-			json_array_unref(arr2);
-			arr2 = NULL;
-		}
-
-		g_list_free(elms2);
-		elms2 = NULL;
-
-		if (g_slist_length(thrd.users) < 2) {
-			fb_api_thread_reset(&thrd, TRUE);
-			continue;
-		}
-
-		if (haself) {
-			mptr = fb_api_thread_dup(&thrd, FALSE);
-			thrds = g_slist_prepend(thrds, mptr);
-		} else {
-			fb_api_thread_reset(&thrd, TRUE);
-		}
+		json_node_free(node);
+		node = NULL;
 	}
 
-	ret = g_slist_reverse(thrds);
+	if ((g_slist_length(thrd->users) < 2) || !haself) {
+		fb_api_thread_reset(thrd, FALSE);
+		return FALSE;
+	}
 
 finish:
-	if (G_LIKELY(arr != NULL)) {
-		json_array_unref(arr);
+	if (node != NULL) {



More information about the Commits mailing list