4 Commits 7fb5b08994 ... b6a3fe1d88

Author SHA1 Message Date
  Alyssa Rosenzweig b6a3fe1d88 Do the right thing for serializing chat users 5 years ago
  Alyssa Rosenzweig 025985cab3 Fix slight bug with room subscriptions 5 years ago
  Alyssa Rosenzweig 44a33f6168 Don't double free 5 years ago
  Alyssa Rosenzweig 8ba8432a48 Self user well for Discord 5 years ago
1 changed files with 20 additions and 15 deletions
  1. 20 15
      core.c

+ 20 - 15
core.c

@@ -349,24 +349,31 @@ sapphire_process_message(Connection *conn, JsonObject *data)
 		}
 
 		gboolean is_joined = (uintptr_t) g_hash_table_lookup(id_to_joined, id);
+		gboolean is_subscribed = g_hash_table_contains(conn->subscribed_ids, id);
 
 		if (!is_joined) {
 			purple_roomlist_room_join(chat->roomlist, chat->room);
 			g_hash_table_insert(id_to_joined, g_strdup(id), (void *) TRUE);
-		} else if (!g_hash_table_contains(conn->subscribed_ids, id)) {
+		} else if (!is_subscribed) {
 			/* If we already joined but not in this connection, just send back details */
 
+			const gchar *topic = purple_conv_chat_get_topic(PURPLE_CONV_CHAT(chat->conv));
 			JsonArray *users = sapphire_serialize_chat_users(chat);
 
 			JsonObject *data = json_object_new();
 			json_object_set_string_member(data, "op", "joined");
 			json_object_set_string_member(data, "chat", id);
+			json_object_set_string_member(data, "topic", topic);
 			json_object_set_array_member(data, "members", users);
 			sapphire_send(conn, data);
 			json_object_unref(data);
+		}
 
+		if (!is_subscribed) {
+			/* We want to know about this room */
 			g_hash_table_add(conn->subscribed_ids, g_strdup(id));
 		}
+
 	} else if (purple_strequal(op, "topic")) {
 		const gchar *chat = json_object_get_string_member(data, "chat");
 		const gchar *topic = json_object_get_string_member(data, "topic");
@@ -663,21 +670,25 @@ sapphire_serialize_chat_user_id(PurpleConversation *conv, const gchar *who)
 		int id = purple_conv_chat_get_id(conv_chat);
 		gchar *real_name = prpl_info->get_cb_real_name(connection, id, who);
 		gchar *normalized = g_strdup(purple_normalize(account, real_name));
-		g_free(real_name);
 
 		/* Check if it's, uh, us */
 		const char *username = purple_normalize(account, purple_account_get_username(account));
+		const char *display_name = purple_connection_get_display_name(connection);
 
-		if (purple_strequal(username, normalized)) {
+		if (purple_strequal(username, normalized) || purple_strequal(display_name, normalized)) {
 			g_free(normalized);
 			return sapphire_serialize_account_id(account);
 		}
 
+		printf("From %s to %s to %s\n", who, real_name, normalized);
+
 		/* Serialize it formally for protocol independence */
 		gchar *out = sapphire_serialize_user_id(account, normalized);
 		g_free(normalized);
+		g_free(real_name);
 		return out;
 	} else {
+		printf("Bailing on %s\n", who);
 		return g_strdup(who);
 	}
 }
@@ -737,6 +748,7 @@ sapphire_serialize_chat_users(SapphireChat *chat)
 			PurpleConvChatBuddy *cb = (PurpleConvChatBuddy *) l->data;
 			const gchar *who = purple_conv_chat_cb_get_name(cb);
 			PurpleConvChatBuddyFlags flags = purple_conv_chat_user_get_flags(conv_chat, who);
+			printf("For %s %s\n", cb->name, cb->alias);
 			sapphire_serialize_chat_buddy(juser, chat->conv, who, flags);
 
 			json_array_add_object_element(jusers, juser);
@@ -746,6 +758,9 @@ sapphire_serialize_chat_users(SapphireChat *chat)
 	return jusers;
 }
 
+/* Serializes the unopened chat pieces, not the conversation bits which have a
+ * rather more complex path */
+
 static JsonObject *
 sapphire_serialize_chat(SapphireChat *chat)
 {
@@ -756,17 +771,6 @@ sapphire_serialize_chat(SapphireChat *chat)
 	json_object_set_string_member(obj, "group", chat->group);
 	json_object_set_string_member(obj, "account", chat->account_id);
 
-	/* If there's a conversation attached, we can do a lot better than that! */
-	if (chat->conv) {
-		PurpleConvChat *conv_chat = purple_conversation_get_chat_data(chat->conv);
-		const gchar *topic = purple_conv_chat_get_topic(conv_chat);
-
-		JsonArray *jusers = sapphire_serialize_chat_users(chat);
-
-		json_object_set_string_member(obj, "topic", topic);
-		json_object_set_array_member(obj, "users", jusers);
-	}
-
 	sapphire_serialize_unacked_messages(obj, chat->id);
 
 	return obj;
@@ -964,7 +968,7 @@ void
 sapphire_send_world(Connection *conn)
 {
 	/* Initialize connected state */
-	conn->subscribed_ids = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
+	conn->subscribed_ids = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
 
 	JsonObject *data = json_object_new();
 
@@ -1206,6 +1210,7 @@ sapphire_buddy_joined(PurpleConversation *conv, const char *who, PurpleConvChatB
 
 	JsonArray *lst = json_array_new();
 	JsonObject *buddy = json_object_new();
+	printf("Got %s\n", who);
 	sapphire_serialize_chat_buddy(buddy, conv, who, flags);
 	json_array_add_object_element(lst, buddy);
 	json_object_set_array_member(data, "members", lst);