From b614f649e385bc3927034f89bd10f48776e4b123 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 9 Mar 2021 07:47:36 +0100 Subject: [PATCH] Show in live who is writing --- .../conversations/writingNotifier.js | 61 ++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/assets/js/components/conversations/writingNotifier.js b/assets/js/components/conversations/writingNotifier.js index e100b7d7..c552b3d9 100644 --- a/assets/js/components/conversations/writingNotifier.js +++ b/assets/js/components/conversations/writingNotifier.js @@ -13,7 +13,66 @@ class ConversationWritingNotifier { class: "user-writing-message" }) - this.setText("hello world for conv " + convID) + this.setText("") + + this.usersFifo = [] + + // Listen to events + this.listener = (e) => { + if(!this.messageArea.isConnected) { + document.removeEventListener("WritingMessageInConv", this.listener) + return; + } + + if (e.detail.conv_id == convID) + this.newWritingEvent(e.detail.user_id) + } + document.addEventListener("WritingMessageInConv", e => this.listener(e)) + } + + /** + * Handle new writing event + * + * @param {number} user_id Target user ID + */ + async newWritingEvent(user_id) { + this.usersFifo.push(user_id) + await this.refreshText() + + setTimeout(() => { + this.usersFifo.shift(); + this.refreshText() + }, ServerConfig.conf.conversation_writing_event_lifetime * 1000) + } + + /** + * Apply new text + */ + async refreshText() { + try { + if (this.usersFifo.length == 0) + return this.setText(""); + + const users = [...new Set([...this.usersFifo])]; + const info = await getUsers(users); + + if (users.length == 1) + this.setText(tr("%1% is writing...", {"1": info.get(users[0]).fullName})) + + else + { + let last = users.pop(); + this.setText(tr("%1% and %2% are writing...", { + "1": users.map(id => info.get(id).fullName).join(", "), + "2": info.get(last).fullName + })); + } + + + } catch(e) { + console.error(e); + this.setText("") + } } /**