libpurple/protocols/facebook/api.c

branch
facebook
changeset 37337
aecff282de76
parent 37336
fb0b92c3533e
child 37338
e4d77bc25235
equal deleted inserted replaced
37336:fb0b92c3533e 37337:aecff282de76
238 0, 238 0,
239 NULL, NULL, 239 NULL, NULL,
240 fb_marshal_VOID__OBJECT, 240 fb_marshal_VOID__OBJECT,
241 G_TYPE_NONE, 241 G_TYPE_NONE,
242 1, G_TYPE_ERROR); 242 1, G_TYPE_ERROR);
243 g_signal_new("events",
244 G_TYPE_FROM_CLASS(klass),
245 G_SIGNAL_ACTION,
246 0,
247 NULL, NULL,
248 fb_marshal_VOID__POINTER,
249 G_TYPE_NONE,
250 1, G_TYPE_POINTER);
243 g_signal_new("messages", 251 g_signal_new("messages",
244 G_TYPE_FROM_CLASS(klass), 252 G_TYPE_FROM_CLASS(klass),
245 G_SIGNAL_ACTION, 253 G_SIGNAL_ACTION,
246 0, 254 0,
247 NULL, NULL, 255 NULL, NULL,
863 871
864 fb_json_values_free(values); 872 fb_json_values_free(values);
865 json_node_free(root); 873 json_node_free(root);
866 } 874 }
867 875
876 static GSList *
877 fb_api_event_parse(FbApi *api, FbApiEvent *event, GSList *events,
878 JsonNode *root, GError **error)
879 {
880 const gchar *str;
881 FbJsonValues *values;
882 GError *err = NULL;
883 gpointer mptr;
884 guint i;
885
886 static const struct {
887 FbApiEventType type;
888 const gchar *expr;
889 } evtypes[] = {
890 {
891 FB_API_EVENT_TYPE_THREAD_USER_ADDED,
892 "$.log_message_data.added_participants"
893 }, {
894 FB_API_EVENT_TYPE_THREAD_USER_REMOVED,
895 "$.log_message_data.removed_participants"
896 }
897 };
898
899 for (i = 0; i < G_N_ELEMENTS(evtypes); i++) {
900 event->type = evtypes[i].type;
901 values = fb_json_values_new(root);
902 fb_json_values_add(values, FB_JSON_TYPE_STR, TRUE, "$");
903 fb_json_values_set_array(values, FALSE, evtypes[i].expr);
904
905 while (fb_json_values_update(values, &err)) {
906 str = fb_json_values_next_str(values, "");
907 str = strrchr(str, ':');
908
909 if (str != NULL) {
910 event->uid = FB_ID_FROM_STR(str + 1);
911 mptr = fb_api_event_dup(event);
912 events = g_slist_prepend(events, mptr);
913 }
914 }
915
916 fb_json_values_free(values);
917
918 if (G_UNLIKELY(err != NULL)) {
919 g_propagate_error(error, err);
920 break;
921 }
922 }
923
924 return events;
925 }
926
927 static void
928 fb_api_cb_mercury(FbApi *api, GByteArray *pload)
929 {
930 const gchar *str;
931 FbApiEvent event;
932 FbJsonValues *values;
933 GError *err = NULL;
934 GSList *events = NULL;
935 JsonNode *root;
936 JsonNode *node;
937
938 if (!fb_api_json_chk(api, pload->data, pload->len, &root)) {
939 return;
940 }
941
942 values = fb_json_values_new(root);
943 fb_json_values_add(values, FB_JSON_TYPE_STR, TRUE, "$.thread_fbid");
944 fb_json_values_set_array(values, FALSE, "$.actions");
945
946 while (fb_json_values_update(values, &err)) {
947 fb_api_event_reset(&event);
948 str = fb_json_values_next_str(values, "0");
949 event.tid = FB_ID_FROM_STR(str);
950
951 node = fb_json_values_get_root(values);
952 events = fb_api_event_parse(api, &event, events, node, &err);
953 }
954
955 if (G_LIKELY(err == NULL)) {
956 events = g_slist_reverse(events);
957 g_signal_emit_by_name(api, "events", events);
958 } else {
959 fb_api_error_emit(api, err);
960 }
961
962 g_slist_free_full(events, (GDestroyNotify) fb_api_event_free);
963 fb_json_values_free(values);
964 json_node_free(root);
965
966 }
967
868 static void 968 static void
869 fb_api_cb_publish_typing(FbApi *api, GByteArray *pload) 969 fb_api_cb_publish_typing(FbApi *api, GByteArray *pload)
870 { 970 {
871 const gchar *str; 971 const gchar *str;
872 FbApiPrivate *priv = api->priv; 972 FbApiPrivate *priv = api->priv;
1271 static const struct { 1371 static const struct {
1272 const gchar *topic; 1372 const gchar *topic;
1273 void (*func) (FbApi *api, GByteArray *pload); 1373 void (*func) (FbApi *api, GByteArray *pload);
1274 } parsers[] = { 1374 } parsers[] = {
1275 {"/mark_thread_response", fb_api_cb_publish_mark}, 1375 {"/mark_thread_response", fb_api_cb_publish_mark},
1376 {"/mercury", fb_api_cb_mercury},
1276 {"/orca_typing_notifications", fb_api_cb_publish_typing}, 1377 {"/orca_typing_notifications", fb_api_cb_publish_typing},
1277 {"/t_ms", fb_api_cb_publish_ms}, 1378 {"/t_ms", fb_api_cb_publish_ms},
1278 {"/t_p", fb_api_cb_publish_p} 1379 {"/t_p", fb_api_cb_publish_p}
1279 }; 1380 };
1280 1381
2331 json = fb_json_bldr_close(bldr, JSON_NODE_OBJECT, NULL); 2432 json = fb_json_bldr_close(bldr, JSON_NODE_OBJECT, NULL);
2332 fb_api_publish(api, "/typing", "%s", json); 2433 fb_api_publish(api, "/typing", "%s", json);
2333 g_free(json); 2434 g_free(json);
2334 } 2435 }
2335 2436
2437 FbApiEvent *
2438 fb_api_event_dup(FbApiEvent *event)
2439 {
2440 g_return_val_if_fail(event != NULL, NULL);
2441 return g_memdup(event, sizeof *event);
2442 }
2443
2444 void
2445 fb_api_event_reset(FbApiEvent *event)
2446 {
2447 g_return_if_fail(event != NULL);
2448 memset(event, 0, sizeof *event);
2449 }
2450
2451 void
2452 fb_api_event_free(FbApiEvent *event)
2453 {
2454 if (G_LIKELY(event != NULL)) {
2455 g_free(event);
2456 }
2457 }
2458
2336 FbApiMessage * 2459 FbApiMessage *
2337 fb_api_message_dup(FbApiMessage *msg, gboolean deep) 2460 fb_api_message_dup(FbApiMessage *msg, gboolean deep)
2338 { 2461 {
2339 FbApiMessage *ret; 2462 FbApiMessage *ret;
2340 2463

mercurial