Skip to content

Commit

Permalink
Optional event when new message from MAM
Browse files Browse the repository at this point in the history
  • Loading branch information
singpolyma committed Dec 10, 2024
1 parent 8cf0d49 commit c97bb32
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions snikket/Chat.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,7 @@ class Channel extends Chat {
}
thenshim.PromiseTools.all(promises).then((stored) -> {
for (message in stored) {
client.notifySyncMessageHandlers(message);
if (message != null && message.chatId() == chatId) chatMessages.push(message);
if (chatMessages.length > 1000) chatMessages.shift(); // Save some RAM
}
Expand Down
33 changes: 30 additions & 3 deletions snikket/Client.hx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Client extends EventEmitter {
public var sendAvailable(null, default): Bool = true;
private var stream:GenericStream;
private var chatMessageHandlers: Array<(ChatMessage, ChatMessageEvent)->Void> = [];
private var syncMessageHandlers: Array<(ChatMessage)->Void> = [];
private var chatStateHandlers: Array<(String,String,Null<String>,UserState)->Void> = [];
@:allow(snikket)
private var jid(default,null):JID;
Expand Down Expand Up @@ -1017,10 +1018,20 @@ class Client extends EventEmitter {
chatMessageHandlers.push((m, e) -> handler(m, cast e));
#else
public function addChatMessageListener(handler:(ChatMessage, ChatMessageEvent)->Void):Void {
chatMessageHandlers.push(handler);
chatMessageHandlers.push(handler);
#end
}

/**
Event fired when syncing a new ChatMessage that was send when offline.
Normally you don't want this, but it may be useful if you want to notify on app start.
@param handler takes one argument, the ChatMessage
**/
public function addSyncMessageListener(handler:(ChatMessage)->Void):Void {
syncMessageHandlers.push(handler);
}

/**
Event fired when a Chat's metadata is updated, or when a new Chat is added
Expand Down Expand Up @@ -1246,6 +1257,16 @@ class Client extends EventEmitter {
}
}

@:allow(snikket)
private function notifySyncMessageHandlers(message: ChatMessage) {
if (message == null || message.versions.length > 1) return;
final chat = getChat(message.chatId());
if (chat != null && chat.isBlocked) return; // Don't notify blocked chats
for (handler in syncMessageHandlers) {
handler(message);
}
}

private function rosterGet() {
var rosterGet = new RosterGet();
rosterGet.onFinished(() -> {
Expand Down Expand Up @@ -1394,14 +1415,20 @@ class Client extends EventEmitter {
}));
case ReactionUpdateStanza(update):
promises.push(new thenshim.Promise((resolve, reject) -> {
persistence.storeReaction(accountId(), update, resolve);
persistence.storeReaction(accountId(), update, (_) -> resolve(null));
}));
default:
// ignore
}
}
trace("SYNC: MAM page wait for writes");
thenshim.PromiseTools.all(promises).then((_) -> {
thenshim.PromiseTools.all(promises).then((storedMessages) -> {
if (syncMessageHandlers.length > 0) {
for (message in storedMessages) {
notifySyncMessageHandlers(message);
}
}

if (sync.hasMore()) {
sync.fetchNext();
} else {
Expand Down

0 comments on commit c97bb32

Please sign in to comment.