diff --git a/telegram-purple.c b/telegram-purple.c index db5ee12..5e762a4 100644 --- a/telegram-purple.c +++ b/telegram-purple.c @@ -251,6 +251,11 @@ static void on_userpic_loaded (struct tgl_state *TLS, void *extra, int success, static void on_get_dialog_list_done (struct tgl_state *TLS, void *callback_extra, int success, int size, tgl_peer_id_t peers[], tgl_message_id_t *last_msg_id[], int unread_count[]) { info ("Fetched dialogue list of size: %d", size); + if (tgp_error_if_false (TLS, success, "Fetching dialogue list failed", TLS->error)) { + return; + } + + // add all peers in the dialogue list to the buddy list int i; for (i = size - 1; i >= 0; i--) { tgl_peer_t *UC = tgl_peer_get (TLS, peers[i]); @@ -288,6 +293,10 @@ static void on_get_dialog_list_done (struct tgl_state *TLS, void *callback_extra } } } + + // now that the dialogue list is loaded, handle all pending chat joins + tls_get_data (TLS)->dialogues_ready = TRUE; + tgp_chat_join_all_pending (TLS); } void on_user_get_info (struct tgl_state *TLS, void *info_data, int success, struct tgl_user *U) { @@ -318,7 +327,6 @@ static const char *tgprpl_list_icon (PurpleAccount *acct, PurpleBuddy *buddy) { } static void tgprpl_tooltip_text (PurpleBuddy *buddy, PurpleNotifyUserInfo *info, gboolean full) { - // buddy in old format that didn't migrate if (! tgp_blist_buddy_has_id (buddy)) { return; @@ -497,7 +505,13 @@ static void update_on_logged_in (struct tgl_state *TLS) { static void update_on_ready (struct tgl_state *TLS) { info ("update_on_ready(): The account is done fetching new history"); - purple_connection_set_display_name (tls_get_conn (TLS), purple_account_get_username (tls_get_pa (TLS))); + + tgl_peer_t *P = tgl_peer_get (TLS, TLS->our_id); + g_warn_if_fail(P); + if (P) { + purple_connection_set_display_name (tls_get_conn (TLS), P->print_name); + } + tgl_do_get_dialog_list (TLS, 200, 0, on_get_dialog_list_done, NULL); tgl_do_update_contact_list (TLS, 0, 0); } diff --git a/tgp-chat.c b/tgp-chat.c index 49361c0..fa14103 100644 --- a/tgp-chat.c +++ b/tgp-chat.c @@ -156,6 +156,19 @@ GHashTable *tgprpl_chat_info_defaults (PurpleConnection *gc, const char *chat_na void tgprpl_chat_join (PurpleConnection *gc, GHashTable *data) { debug ("tgprpl_chat_join()"); + g_return_if_fail(data); + + // auto joins will cause chat joins at a time when the dialogue list is not ready, remember + // those chats and join them once the dialogue list is fetched + if (! gc_get_data (gc)->dialogues_ready) { + g_return_if_fail (data); + const char *id = g_hash_table_lookup (data, "id"); + if (id) { + debug ("attempting to join chat %s while not ready, queue up for later", id); + gc_get_data (gc)->pending_joins = g_list_append (gc_get_data (gc)->pending_joins, g_strdup (id)); + } + return; + } // join existing chat by id when the user clicks on a chat in the buddy list void *value = g_hash_table_lookup (data, "id"); @@ -253,3 +266,17 @@ void tgprpl_roomlist_cancel (PurpleRoomlist *list) { purple_roomlist_unref (list); } } + +void tgp_chat_join_all_pending (struct tgl_state *TLS) { + GList *pending; + for (pending = tls_get_data (TLS)->pending_joins; pending != NULL; pending = g_list_next(pending)) { + GHashTable *data = g_hash_table_new (g_str_hash, g_str_equal); + g_hash_table_insert (data, "id", pending->data); + tgprpl_chat_join (tls_get_conn (TLS), data); + g_hash_table_destroy (data); + } + if (tls_get_data (TLS)->pending_joins) { + g_list_free (tls_get_data (TLS)->pending_joins); + tls_get_data (TLS)->pending_joins = NULL; + } +} diff --git a/tgp-chat.h b/tgp-chat.h index 75593a9..4171189 100644 --- a/tgp-chat.h +++ b/tgp-chat.h @@ -38,4 +38,5 @@ GList *tgprpl_chat_join_info (PurpleConnection *gc); PurpleRoomlist *tgprpl_roomlist_get_list (PurpleConnection *gc); void tgprpl_roomlist_cancel (PurpleRoomlist *list); GHashTable *tgprpl_chat_info_defaults (PurpleConnection *gc, const char *chat_name); +void tgp_chat_join_all_pending (struct tgl_state *TLS); #endif diff --git a/tgp-structs.c b/tgp-structs.c index 15c8eeb..9a69b87 100644 --- a/tgp-structs.c +++ b/tgp-structs.c @@ -114,6 +114,7 @@ void *connection_data_free (connection_data *conn) { tgp_g_queue_free_full (conn->new_messages, tgp_msg_loading_free); tgp_g_queue_free_full (conn->out_messages, tgp_msg_sending_free); tgp_g_list_free_full (conn->used_images, used_image_free); + tgp_g_list_free_full (conn->pending_joins, g_free); g_hash_table_destroy (conn->pending_reads); g_hash_table_destroy (conn->pending_chat_info); g_hash_table_destroy (conn->id_to_purple_name); diff --git a/tgp-structs.h b/tgp-structs.h index 7ce0a7c..c90d991 100644 --- a/tgp-structs.h +++ b/tgp-structs.h @@ -45,6 +45,8 @@ typedef struct { PurpleRoomlist *roomlist; GHashTable *pending_chat_info; GHashTable *id_to_purple_name; + GList *pending_joins; + int dialogues_ready; } connection_data; typedef struct { diff --git a/tgp-utils.c b/tgp-utils.c index 8d332a1..6f4dcfe 100644 --- a/tgp-utils.c +++ b/tgp-utils.c @@ -93,8 +93,10 @@ void tgp_g_queue_free_full (GQueue *queue, GDestroyNotify free_func) { } void tgp_g_list_free_full (GList *list, GDestroyNotify free_func) { - g_list_foreach (list, (GFunc)free_func, NULL); - g_list_free (list); + if (list) { + g_list_foreach (list, (GFunc)free_func, NULL); + g_list_free (list); + } } const char *tgp_mime_to_filetype (const char *mime) {