git » sdk » commit bccccd8

Support for pinning chats

author Stephen Paul Weber
2024-11-07 05:10:35 UTC
committer Stephen Paul Weber
2024-11-07 05:10:35 UTC
parent 42671203d20a14cee342c5f954d31b0ba86dbb67

Support for pinning chats

An app may want all sorts of meanings or custom UI around this. But if
they do nothing we at least use it to sort to the top of our internal list.

snikket/Chat.hx +10 -0
snikket/Client.hx +5 -1

diff --git a/snikket/Chat.hx b/snikket/Chat.hx
index 9033345..cb1e46b 100644
--- a/snikket/Chat.hx
+++ b/snikket/Chat.hx
@@ -274,6 +274,16 @@ abstract class Chat {
 	**/
 	abstract public function close():Void;
 
+	/**
+		Pin or unpin this chat
+	**/
+	public function togglePinned(): Void {
+		uiState = uiState == Pinned ? Open : Pinned;
+		persistence.storeChat(client.accountId(), this);
+		client.sortChats();
+		client.trigger("chats/update", [this]);
+	}
+
 	/**
 		Block this chat so it will not re-open
 	**/
diff --git a/snikket/Client.hx b/snikket/Client.hx
index c7d0a54..2ecae2e 100644
--- a/snikket/Client.hx
+++ b/snikket/Client.hx
@@ -1120,7 +1120,11 @@ class Client extends EventEmitter {
 
 	@:allow(snikket)
 	private function sortChats() {
-		chats.sort((a, b) -> -Reflect.compare(a.lastMessageTimestamp() ?? "0", b.lastMessageTimestamp() ?? "0"));
+		chats.sort((a, b) -> {
+			if (a.uiState == Pinned && b.uiState != Pinned) return -1;
+			if (b.uiState == Pinned && a.uiState != Pinned) return 1;
+			return -Reflect.compare(a.lastMessageTimestamp() ?? "0", b.lastMessageTimestamp() ?? "0");
+		});
 	}
 
 	@:allow(snikket)