From 62291d6a8d4cd4f6aea86d73749ea7f59ad99bb8 Mon Sep 17 00:00:00 2001 From: dequis Date: Fri, 6 Jan 2017 16:14:00 -0300 Subject: Don't fail on groupchats with only two members including self Those are created by the "Marketplace" feature: http://newsroom.fb.com/news/2016/10/introducing-marketplace --- facebook/facebook-api.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/facebook/facebook-api.c b/facebook/facebook-api.c index 4a54e41..0ec8e07 100644 --- a/facebook/facebook-api.c +++ b/facebook/facebook-api.c @@ -2535,6 +2535,7 @@ fb_api_thread_parse(FbApi *api, FbApiThread *thrd, JsonNode *root, FbId uid; FbJsonValues *values; gboolean haself = FALSE; + guint num_users = 0; GError *err = NULL; values = fb_json_values_new(root); @@ -2570,6 +2571,7 @@ fb_api_thread_parse(FbApi *api, FbApiThread *thrd, JsonNode *root, while (fb_json_values_update(values, &err)) { str = fb_json_values_next_str(values, "0"); uid = FB_ID_FROM_STR(str); + num_users++; if (uid != priv->uid) { user = fb_api_user_dup(NULL, FALSE); @@ -2588,7 +2590,7 @@ fb_api_thread_parse(FbApi *api, FbApiThread *thrd, JsonNode *root, return FALSE; } - if ((g_slist_length(thrd->users) < 2) || !haself) { + if (num_users < 2 || !haself) { fb_api_thread_reset(thrd, TRUE); g_object_unref(values); return FALSE; -- cgit v1.2.3 From 878c1dc70a3a5838e2f2ed18be0643696f5c8b92 Mon Sep 17 00:00:00 2001 From: dequis Date: Sat, 7 Jan 2017 12:52:14 -0300 Subject: Refactor, split chat topic string generation to a separate function --- facebook/facebook.c | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/facebook/facebook.c b/facebook/facebook.c index cf3e8c1..55f9c37 100644 --- a/facebook/facebook.c +++ b/facebook/facebook.c @@ -483,6 +483,32 @@ fb_cb_api_presences(FbApi *api, GSList *press, gpointer data) } } +static gchar * +fb_thread_topic_gen(FbApiThread *thrd) +{ + GSList *l; + GString *gstr; + FbApiUser *user; + + if (thrd->topic != NULL) { + return g_strdup(thrd->topic); + } + + gstr = g_string_new(NULL); + + for (l = thrd->users; l != NULL; l = l->next) { + user = l->data; + + if (gstr->len > 0) { + g_string_append(gstr, ", "); + } + + g_string_append(gstr, user->name); + } + + return g_string_free(gstr, FALSE); +} + static void fb_cb_api_thread(FbApi *api, FbApiThread *thrd, gpointer data) { @@ -490,9 +516,9 @@ fb_cb_api_thread(FbApi *api, FbApiThread *thrd, gpointer data) FbApiUser *user; FbData *fata = data; gchar id[FB_ID_STRMAX]; + gchar *topic; GList *h; GSList *l; - GString *gstr; struct groupchat *gc; struct im_connection *ic; @@ -504,24 +530,9 @@ fb_cb_api_thread(FbApi *api, FbApiThread *thrd, gpointer data) return; } - if (thrd->topic == NULL) { - gstr = g_string_new(NULL); - - for (l = thrd->users; l != NULL; l = l->next) { - user = l->data; - - if (gstr->len > 0) { - g_string_append(gstr, ", "); - } - - g_string_append(gstr, user->name); - } - - imcb_chat_topic(gc, NULL, gstr->str, 0); - g_string_free(gstr, TRUE); - } else { - imcb_chat_topic(gc, NULL, (gchar *) thrd->topic, 0); - } + topic = fb_thread_topic_gen(thrd); + imcb_chat_topic(gc, NULL, topic, 0); + g_free(topic); for (l = thrd->users; l != NULL; l = l->next) { user = l->data; -- cgit v1.2.3 From 2707fa7895f33f3886616169073fc8b5fabfdd02 Mon Sep 17 00:00:00 2001 From: dequis Date: Sat, 7 Jan 2017 12:55:26 -0300 Subject: Don't disconnect when trying to join a chat after being kicked This replaces a subset of "Failed to parse thread information" (the ones that aren't really a problem parsing) with "You have been removed from this chat" appearing in the relevant chat window. --- facebook/facebook-api.c | 26 +++++++++++++++++++++++--- facebook/facebook.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/facebook/facebook-api.c b/facebook/facebook-api.c index 0ec8e07..184cb89 100644 --- a/facebook/facebook-api.c +++ b/facebook/facebook-api.c @@ -435,6 +435,23 @@ fb_api_class_init(FbApiClass *klass) G_TYPE_NONE, 1, FB_TYPE_ID); + /** + * FbApi::thread-kicked: + * @api: The #FbApi. + * @thrd: The #FbApiThread. + * + * Emitted upon the reply of a thread request when the user is no longer + * part of that thread. This is emitted as a result of #fb_api_thread(). + */ + g_signal_new("thread-kicked", + G_TYPE_FROM_CLASS(klass), + G_SIGNAL_ACTION, + 0, + NULL, NULL, + fb_marshal_VOID__POINTER, + G_TYPE_NONE, + 1, G_TYPE_POINTER); + /** * FbApi::threads: * @api: The #FbApi. @@ -2591,7 +2608,6 @@ fb_api_thread_parse(FbApi *api, FbApiThread *thrd, JsonNode *root, } if (num_users < 2 || !haself) { - fb_api_thread_reset(thrd, TRUE); g_object_unref(values); return FALSE; } @@ -2626,8 +2642,12 @@ fb_api_cb_thread(FbHttpRequest *req, gpointer data) if (!fb_api_thread_parse(api, &thrd, node, &err)) { if (G_LIKELY(err == NULL)) { - fb_api_error(api, FB_API_ERROR_GENERAL, - "Failed to parse thread information"); + if (thrd.tid) { + g_signal_emit_by_name(api, "thread-kicked", &thrd); + } else { + fb_api_error(api, FB_API_ERROR_GENERAL, + "Failed to parse thread information"); + } } else { fb_api_error_emit(api, err); } diff --git a/facebook/facebook.c b/facebook/facebook.c index 55f9c37..4ef9fc9 100644 --- a/facebook/facebook.c +++ b/facebook/facebook.c @@ -570,6 +570,33 @@ fb_cb_api_thread_create(FbApi *api, FbId tid, gpointer data) imcb_log(ic, "Join: fbjoin %s %d ", acct->tag, 1); } +static void +fb_cb_api_thread_kicked(FbApi *api, FbApiThread *thrd, gpointer data) +{ + FbData *fata = data; + gchar id[FB_ID_STRMAX]; + gchar *topic; + struct groupchat *gc; + struct im_connection *ic; + + FB_ID_TO_STR(thrd->tid, id); + ic = fb_data_get_connection(fata); + gc = bee_chat_by_title(ic->bee, ic, id); + + if (G_UNLIKELY(gc == NULL)) { + return; + } + + topic = fb_thread_topic_gen(thrd); + imcb_chat_topic(gc, NULL, topic, 0); + g_free(topic); + + imcb_chat_log(gc, "You have been removed from this chat"); + + fb_data_remove_groupchat(fata, gc); + imcb_chat_free(gc); +} + static void fb_cb_api_threads(FbApi *api, GSList *thrds, gpointer data) { @@ -743,6 +770,10 @@ fb_login(account_t *acc) "thread-create", G_CALLBACK(fb_cb_api_thread_create), fata); + g_signal_connect(api, + "thread-kicked", + G_CALLBACK(fb_cb_api_thread_kicked), + fata); g_signal_connect(api, "threads", G_CALLBACK(fb_cb_api_threads), -- cgit v1.2.3