git » sdk » commit 12e495f

Do not overwrite selfChat avatar with null

author Stephen Paul Weber
2026-10-21 18:20:33 UTC
committer Stephen Paul Weber
2026-10-21 18:20:33 UTC
parent f8153cfe1a3eaad019ec71567f2b4edd7d6aaf14

Do not overwrite selfChat avatar with null

During startOffline we hydrate all the chats before assigning any of
them into the chats array. However, this means that when a
SerializedChat of a Channel has toChat called on it, it will call new
Channel, which will call client.getDirectChat(client.accountId()). This
sees no such chat, and makes a new one and stores it, overwriting the
one we already had with one that, among other things, now has no avatar.

The fix is to get the selfChat into chats/chatsIndex first before we
call toChat on anyone else.

borogove/Client.hx +23 -11
test/TestClient.hx +94 -0

diff --git a/borogove/Client.hx b/borogove/Client.hx
index dfe1460..949ee9e 100644
--- a/borogove/Client.hx
+++ b/borogove/Client.hx
@@ -875,20 +875,32 @@ class Client extends EventEmitter {
 
 			return persistence.getChats(accountId());
 		}).then((protoChats) -> {
-			protoChats.sort((a, b) -> a.chatId == accountId() ? 1 : 0);
 			var oneProtoChat = null;
 			final chatPromises = [];
-			while ((oneProtoChat = protoChats.pop()) != null) {
-				chatPromises.push(oneProtoChat.toChat(this, stream, persistence));
-			}
-			return thenshim.PromiseTools.all(chatPromises).then(theChats -> {
-				chats = theChats;
-				for (chat in chats) {
-					chatsIndex[chat.chatId] = chat;
+
+			final selfChatIndex = protoChats.findIndex(a -> a.chatId == accountId());
+			return (if (selfChatIndex == -1) {
+				Promise.resolve(null);
+			} else {
+				protoChats.splice(selfChatIndex, 1)[0].toChat(this, stream, persistence).then(selfChat -> {
+					chats = [selfChat];
+					chatsIndex[selfChat.chatId] = selfChat;
+					chatPromises.push(Promise.resolve(selfChat));
+					return null;
+				});
+			}).then(_ -> {
+				while ((oneProtoChat = protoChats.pop()) != null) {
+					chatPromises.push(oneProtoChat.toChat(this, stream, persistence));
 				}
-				getDirectChat(accountId()); // Ensure self chat exists
-				getDirectChat(JID.parse(accountId()).domain); // Ensure server chat exists
-				return persistence.getChatsUnreadDetails(accountId(), chats);
+				return thenshim.PromiseTools.all(chatPromises).then(theChats -> {
+					chats = theChats;
+					for (chat in chats) {
+						chatsIndex[chat.chatId] = chat;
+					}
+					getDirectChat(accountId()); // Ensure self chat exists
+					getDirectChat(JID.parse(accountId()).domain); // Ensure server chat exists
+					return persistence.getChatsUnreadDetails(accountId(), chats);
+				});
 			});
 		}).then((details) -> {
 			for (detail in details) {
diff --git a/test/TestClient.hx b/test/TestClient.hx
index 0bb6ea9..4a9a21f 100644
--- a/test/TestClient.hx
+++ b/test/TestClient.hx
@@ -638,6 +638,79 @@ class TestClient extends utest.Test {
 		client.start();
 	}
 
+	public function testRestoreChannelDoesNotOverrideSelfChat(async: Async) {
+		final accountId = "me@example.com";
+		final channelId = "room@example.com";
+		final avatarBytes = haxe.io.Bytes.ofHex("0102030405060708090a0b0c0d0e0f1011121314").getData();
+
+		final selfProtoChat = new SerializedChat(
+			accountId,
+			true,
+			true,
+			avatarBytes,
+			new Map(),
+			null,
+			"Self Name",
+			Open,
+			false,
+			new Status("", ""),
+			null,
+			null,
+			null,
+			null,
+			false,
+			false,
+			new borogove.Chat.StringMapNullableKey(),
+			null,
+			null,
+			[],
+			"DirectChat"
+		);
+
+		final channelProtoChat = new SerializedChat(
+			channelId,
+			false,
+			false,
+			null,
+			new Map(),
+			null,
+			"Channel Name",
+			Open,
+			false,
+			new Status("", ""),
+			null,
+			null,
+			null,
+			null,
+			false,
+			false,
+			new borogove.Chat.StringMapNullableKey(),
+			null,
+			null,
+			[],
+			"Channel"
+		);
+
+		final persistence = new RestoreChatsMockPersistence([channelProtoChat, selfProtoChat]);
+		final client = new Client(accountId, persistence);
+
+		client.startOffline().then(_ -> {
+			final storedSelfChats = persistence.storedChats.filter(c -> c.chatId == accountId);
+			Assert.equals(0, storedSelfChats.length);
+
+			final selfChat = client.getDirectChat(accountId, false);
+			Assert.notNull(selfChat);
+			Assert.equals("Self Name", selfChat.displayName);
+			Assert.equals(selfProtoChat.avatarSha1, selfChat.avatarSha1);
+
+			final channel = Std.downcast(client.getChat(channelId), borogove.Chat.Channel);
+			Assert.notNull(channel);
+			Assert.equals(selfChat.getPhoto(), channel.self.photoUri);
+
+			async.done();
+		});
+	}
+
 	public function testGetChannelsReturnsOnlyChannels() {
 		final persistence = new Dummy();
 		final client = new Client("test@example.com", persistence);
@@ -1326,3 +1399,24 @@ class VoiceRequestMockPersistence extends Dummy {
 		return Promise.resolve(true);
 	}
 }
+
+@:access(borogove)
+class RestoreChatsMockPersistence extends Dummy {
+	public var storedChats: Array<Chat> = [];
+	public var chats: Array<SerializedChat>;
+
+	public function new(chats: Array<SerializedChat>) {
+		super();
+		this.chats = chats;
+	}
+
+	override public function getChats(accountId: String): Promise<Array<SerializedChat>> {
+		return Promise.resolve(chats.copy());
+	}
+
+	override public function storeChats(accountId: String, chats: Array<Chat>) {
+		for (c in chats) {
+			storedChats.push(c);
+		}
+	}
+}