Skip to content

Commit

Permalink
Include what happened to a message when notifying UI
Browse files Browse the repository at this point in the history
So they can tell if it was eg a corretion or reaction and not notify
about it as if new, or similar.
  • Loading branch information
singpolyma committed Nov 13, 2024
1 parent 54ea84f commit 18967ab
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ npm/snikket-browser.js:
sed -i 's/snikket\.MessageDirection/enums.MessageDirection/g' npm/snikket-browser.d.ts
sed -i 's/snikket\.MessageType/enums.MessageType/g' npm/snikket-browser.d.ts
sed -i 's/snikket\.UserState/enums.UserState/g' npm/snikket-browser.d.ts
sed -i 's/snikket\.ChatMessageEvent/enums.ChatMessageEvent/g' npm/snikket-browser.d.ts
sed -i 's/_Push.Push_Fields_/Push/g' npm/snikket-browser.d.ts
sed -i '1ivar exports = {};' npm/snikket-browser.js
echo "export const snikket = exports.snikket;" >> npm/snikket-browser.js
Expand All @@ -27,6 +28,7 @@ npm/snikket.js:
sed -i 's/snikket\.MessageDirection/enums.MessageDirection/g' npm/snikket.d.ts
sed -i 's/snikket\.MessageType/enums.MessageType/g' npm/snikket.d.ts
sed -i 's/snikket\.UserState/enums.UserState/g' npm/snikket.d.ts
sed -i 's/snikket\.ChatMessageEvent/enums.ChatMessageEvent/g' npm/snikket.d.ts
sed -i 's/_Push.Push_Fields_/Push/g' npm/snikket.d.ts
sed -i '1iimport { createRequire } from "module";' npm/snikket.js
sed -i '1iglobal.require = createRequire(import.meta.url);' npm/snikket.js
Expand Down
5 changes: 3 additions & 2 deletions npm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ export import SerializedChat = snikket.SerializedChat;
export import jingle = snikket.jingle;
export const VERSION = snikket.Version.HUMAN;

export import UiState = enums.UiState;
export import MessageStatus = enums.MessageStatus;
export import ChatMessageEvent = enums.ChatMessageEvent;
export import MessageDirection = enums.MessageDirection;
export import MessageStatus = enums.MessageStatus;
export import MessageType = enums.MessageType;
export import UiState = enums.UiState;
export import UserState = enums.UserState;

export namespace persistence {
Expand Down
16 changes: 8 additions & 8 deletions snikket/Chat.hx
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ class DirectChat extends Chat {
setLastMessage(corrected);
client.trigger("chats/update", [this]);
}
client.notifyMessageHandlers(corrected);
client.notifyMessageHandlers(corrected, CorrectionEvent);
});
}

Expand All @@ -748,15 +748,15 @@ class DirectChat extends Chat {
}
setLastMessage(message);
client.trigger("chats/update", [this]);
client.notifyMessageHandlers(stored);
client.notifyMessageHandlers(stored, stored.versions.length > 1 ? CorrectionEvent : DeliveryEvent);
});
case ReactionUpdateStanza(update):
persistence.storeReaction(client.accountId(), update, (stored) -> {
for (recipient in message.recipients) {
message.to = recipient;
client.sendStanza(message.asStanza());
}
if (stored != null) client.notifyMessageHandlers(stored);
if (stored != null) client.notifyMessageHandlers(stored, ReactionEvent);
});
default:
trace("Invalid message", fromStanza);
Expand All @@ -778,7 +778,7 @@ class DirectChat extends Chat {
stanza.attr.set("to", recipient);
client.sendStanza(stanza);
}
if (stored != null) client.notifyMessageHandlers(stored);
if (stored != null) client.notifyMessageHandlers(stored, ReactionEvent);
});
}

Expand Down Expand Up @@ -1152,7 +1152,7 @@ class Channel extends Chat {
setLastMessage(corrected);
client.trigger("chats/update", [this]);
}
client.notifyMessageHandlers(corrected);
client.notifyMessageHandlers(corrected, CorrectionEvent);
});
}

Expand All @@ -1177,12 +1177,12 @@ class Channel extends Chat {
client.sendStanza(stanza);
setLastMessage(stored);
client.trigger("chats/update", [this]);
client.notifyMessageHandlers(stored);
client.notifyMessageHandlers(stored, stored.versions.length > 1 ? CorrectionEvent : DeliveryEvent);
});
case ReactionUpdateStanza(update):
persistence.storeReaction(client.accountId(), update, (stored) -> {
client.sendStanza(stanza);
if (stored != null) client.notifyMessageHandlers(stored);
if (stored != null) client.notifyMessageHandlers(stored, ReactionEvent);
});
default:
trace("Invalid message", fromStanza);
Expand All @@ -1202,7 +1202,7 @@ class Channel extends Chat {
final stanza = update.asStanza();
stanza.attr.set("to", chatId);
client.sendStanza(stanza);
if (stored != null) client.notifyMessageHandlers(stored);
if (stored != null) client.notifyMessageHandlers(stored, ReactionEvent);
});
}

Expand Down
31 changes: 22 additions & 9 deletions snikket/Client.hx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ using Lambda;
import HaxeCBridge;
#end

enum abstract ChatMessageEvent(Int) {
var DeliveryEvent;
var CorrectionEvent;
var ReactionEvent;
var StatusEvent;
}

@:expose
#if cpp
@:build(HaxeCBridge.expose())
Expand All @@ -45,7 +52,7 @@ class Client extends EventEmitter {
**/
public var sendAvailable(null, default): Bool = true;
private var stream:GenericStream;
private var chatMessageHandlers: Array<(ChatMessage)->Void> = [];
private var chatMessageHandlers: Array<(ChatMessage, ChatMessageEvent)->Void> = [];
private var chatStateHandlers: Array<(String,String,Null<String>,UserState)->Void> = [];
@:allow(snikket)
private var jid(default,null):JID;
Expand Down Expand Up @@ -111,7 +118,7 @@ class Client extends EventEmitter {
accountId(),
data.id,
MessageDeliveredToServer,
notifyMessageHandlers
(m) -> notifyMessageHandlers(m, StatusEvent)
);
return EventHandled;
});
Expand All @@ -121,7 +128,7 @@ class Client extends EventEmitter {
accountId(),
data.id,
MessageFailedToSend,
notifyMessageHandlers
(m) -> notifyMessageHandlers(m, StatusEvent)
);
return EventHandled;
});
Expand Down Expand Up @@ -155,7 +162,7 @@ class Client extends EventEmitter {
if (chat == null && stanza.attr.get("type") != "groupchat") chat = getDirectChat(chatMessage.chatId());
if (chat != null) {
final updateChat = (chatMessage) -> {
notifyMessageHandlers(chatMessage);
notifyMessageHandlers(chatMessage, chatMessage.versions.length > 1 ? CorrectionEvent : DeliveryEvent);
if (chatMessage.versions.length < 1 || chat.lastMessageId() == chatMessage.serverId || chat.lastMessageId() == chatMessage.localId) {
chat.setLastMessage(chatMessage);
if (chatMessage.versions.length < 1) chat.setUnreadCount(chatMessage.isIncoming() ? chat.unreadCount() + 1 : 0);
Expand All @@ -173,7 +180,7 @@ class Client extends EventEmitter {
for (hash in update.inlineHashReferences()) {
fetchMediaByHash([hash], [from]);
}
persistence.storeReaction(accountId(), update, (stored) -> if (stored != null) notifyMessageHandlers(stored));
persistence.storeReaction(accountId(), update, (stored) -> if (stored != null) notifyMessageHandlers(stored, ReactionEvent));
default:
// ignore
}
Expand Down Expand Up @@ -975,10 +982,16 @@ class Client extends EventEmitter {
Also fires when status of a ChatMessage changes,
when a ChatMessage is edited, or when a reaction is added
@param handler takes one argument, the ChatMessage
@param handler takes two arguments, the ChatMessage and ChatMessageEvent enum describing what happened
**/
public function addChatMessageListener(handler:ChatMessage->Void):Void {
#if cpp
// HaxeCBridge doesn't support "secondary" enums yet
public function addChatMessageListener(handler:(ChatMessage, Int)->Void):Void {
chatMessageHandlers.push((m, e) -> handler(m, cast e));
#else
public function addChatMessageListener(handler:(ChatMessage, ChatMessageEvent)->Void):Void {
chatMessageHandlers.push(handler);
#end
}

/**
Expand Down Expand Up @@ -1198,11 +1211,11 @@ class Client extends EventEmitter {
}

@:allow(snikket)
private function notifyMessageHandlers(message: ChatMessage) {
private function notifyMessageHandlers(message: ChatMessage, event: ChatMessageEvent) {
final chat = getChat(message.chatId());
if (chat != null && chat.isBlocked) return; // Don't notify blocked chats
for (handler in chatMessageHandlers) {
handler(message);
handler(message, event);
}
}

Expand Down
12 changes: 6 additions & 6 deletions snikket/jingle/Session.hx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class IncomingProposedSession implements Session {
final event = new Stanza("ringing", { xmlns: "urn:xmpp:jingle-message:0", id: sid });
final msg = mkCallMessage(from, client.jid, event);
client.storeMessage(msg, (stored) -> {
client.notifyMessageHandlers(stored);
client.notifyMessageHandlers(stored, CorrectionEvent);
});
client.trigger("call/ring", { chatId: from.asBare().asString(), session: this });
}
Expand All @@ -88,7 +88,7 @@ class IncomingProposedSession implements Session {
final event = new Stanza("reject", { xmlns: "urn:xmpp:jingle-message:0", id: sid });
final msg = mkCallMessage(from, client.jid, event);
client.storeMessage(msg, (stored) -> {
client.notifyMessageHandlers(stored);
client.notifyMessageHandlers(stored, CorrectionEvent);
});
client.getDirectChat(from.asBare().asString(), false).jingleSessions.remove(sid);
}
Expand Down Expand Up @@ -122,7 +122,7 @@ class IncomingProposedSession implements Session {
final event = new Stanza("proceed", { xmlns: "urn:xmpp:jingle-message:0", id: sid });
final msg = mkCallMessage(from, client.jid, event);
client.storeMessage(msg, (stored) -> {
client.notifyMessageHandlers(stored);
client.notifyMessageHandlers(stored, CorrectionEvent);
client.sendStanza(
new Stanza("message", { to: from.asString(), type: "chat", id: msg.versions[0].localId })
.addChild(event)
Expand Down Expand Up @@ -191,7 +191,7 @@ class OutgoingProposedSession implements Session {
.addChild(event)
.tag("store", { xmlns: "urn:xmpp:hints" });
client.sendStanza(stanza);
client.notifyMessageHandlers(stored);
client.notifyMessageHandlers(stored, DeliveryEvent);
client.trigger("call/ringing", { chatId: to.asBare().asString() });
});
}
Expand All @@ -209,7 +209,7 @@ class OutgoingProposedSession implements Session {
.addChild(event)
.tag("store", { xmlns: "urn:xmpp:hints" })
);
client.notifyMessageHandlers(stored);
client.notifyMessageHandlers(stored, CorrectionEvent);
});
client.getDirectChat(to.asBare().asString(), false).jingleSessions.remove(sid);
}
Expand Down Expand Up @@ -368,7 +368,7 @@ class InitiatedSession implements Session {
final event = new Stanza("finish", { xmlns: "urn:xmpp:jingle-message:0", id: sid });
final msg = mkCallMessage(counterpart, client.jid, event);
client.storeMessage(msg, (stored) -> {
client.notifyMessageHandlers(stored);
client.notifyMessageHandlers(stored, CorrectionEvent);
client.sendStanza(
new Stanza("message", { to: counterpart.asString(), type: "chat", id: msg.versions[0].localId })
.addChild(event)
Expand Down

0 comments on commit 18967ab

Please sign in to comment.