Browse Source

feat(lobby): lobby chat messaging

Messaging methods in lobby room
getLocalId to get the local lobby room id from jid
setMessageListener to listen for messages in lobby room

Reviewed-by: Fecri Kaan Ulubey <f.kaan93@gmail.com>

ref(lobby chat) refactor lobby chat api to conform to other existing lib-jitsi-meet apis
dev1
Kusi Musah Hussein 3 years ago
parent
commit
a94c65a12f
2 changed files with 125 additions and 0 deletions
  1. 57
    0
      JitsiConference.js
  2. 68
    0
      modules/xmpp/Lobby.js

+ 57
- 0
JitsiConference.js View File

@@ -3941,6 +3941,63 @@ JitsiConference.prototype.joinLobby = function(displayName, email) {
3941 3941
     return Promise.reject(new Error('The conference not started'));
3942 3942
 };
3943 3943
 
3944
+/**
3945
+ * Gets the local id for a participant in a lobby room.
3946
+ * Returns undefined when current participant is not in the lobby room.
3947
+ * This is used for lobby room private chat messages.
3948
+ *
3949
+ * @returns {string}
3950
+ */
3951
+JitsiConference.prototype.myLobbyUserId = function() {
3952
+    if (this.room) {
3953
+        return this.room.getLobby().getLocalId();
3954
+    }
3955
+};
3956
+
3957
+/**
3958
+ * Sends a message to a lobby room.
3959
+ * When id is specified it sends a private message.
3960
+ * Otherwise it sends the message to all moderators.
3961
+ * @param {message} Object The message to send
3962
+ * @param {string} id The participant id.
3963
+ *
3964
+ * @returns {void}
3965
+ */
3966
+JitsiConference.prototype.sendLobbyMessage = function(message, id) {
3967
+    if (this.room) {
3968
+        if (id) {
3969
+            return this.room.getLobby().sendPrivateMessage(id, message);
3970
+        }
3971
+
3972
+        return this.room.getLobby().sendMessage(message);
3973
+    }
3974
+};
3975
+
3976
+/**
3977
+ * Adds a message listener to the lobby room
3978
+ * @param {Function} listener The listener function,
3979
+ * called when a new message is received in the lobby room.
3980
+ *
3981
+ * @returns {Function} Handler returned to be able to remove it later.
3982
+ */
3983
+JitsiConference.prototype.addLobbyMessageListener = function(listener) {
3984
+    if (this.room) {
3985
+        return this.room.getLobby().addMessageListener(listener);
3986
+    }
3987
+};
3988
+
3989
+/**
3990
+ * Removes a message handler from the lobby room
3991
+ * @param {Function} handler The handler function  to remove.
3992
+ *
3993
+ * @returns {void}
3994
+ */
3995
+JitsiConference.prototype.removeLobbyMessageHandler = function(handler) {
3996
+    if (this.room) {
3997
+        return this.room.getLobby().removeMessageHandler(handler);
3998
+    }
3999
+};
4000
+
3944 4001
 /**
3945 4002
  * Denies an occupant in the lobby room access to the conference.
3946 4003
  * @param {string} id The participant id.

+ 68
- 0
modules/xmpp/Lobby.js View File

@@ -82,6 +82,74 @@ export default class Lobby {
82 82
         this.mainRoom.setMembersOnly(false);
83 83
     }
84 84
 
85
+    /**
86
+     * Broadcast a message to all participants in the lobby room
87
+     * @param {Object} message The message to send
88
+     *
89
+     * @returns {void}
90
+     */
91
+    sendMessage(message) {
92
+        if (this.lobbyRoom) {
93
+            this.lobbyRoom.sendMessage(JSON.stringify(message), 'json-message');
94
+        }
95
+    }
96
+
97
+    /**
98
+     * Sends a private message to a participant in a lobby room.
99
+     * @param {string} id The message to send
100
+     * @param {Object} message The message to send
101
+     *
102
+     * @returns {void}
103
+     */
104
+    sendPrivateMessage(id, message) {
105
+        if (this.lobbyRoom) {
106
+            this.lobbyRoom.sendPrivateMessage(id, JSON.stringify(message), 'json-message');
107
+        }
108
+    }
109
+
110
+    /**
111
+     * Gets the local id for a participant in a lobby room.
112
+     * This is used for lobby room private chat messages.
113
+     *
114
+     * @returns {string}
115
+     */
116
+    getLocalId() {
117
+        if (this.lobbyRoom) {
118
+            return Strophe.getResourceFromJid(this.lobbyRoom.myroomjid);
119
+        }
120
+    }
121
+
122
+    /**
123
+     * Adds a message listener to the lobby room.
124
+     * @param {Function} listener The listener function,
125
+     * called when a new message is received in the lobby room.
126
+     *
127
+     * @returns {Function} Handler returned to be able to remove it later.
128
+     */
129
+    addMessageListener(listener) {
130
+        if (this.lobbyRoom) {
131
+            const handler = (participantId, message) => {
132
+                listener(message, Strophe.getResourceFromJid(participantId));
133
+            };
134
+
135
+            this.lobbyRoom.on(XMPPEvents.JSON_MESSAGE_RECEIVED, handler);
136
+
137
+            return handler;
138
+        }
139
+    }
140
+
141
+    /**
142
+     * Remove a message handler from the lobby room.
143
+     * @param {Function} handler The handler function to remove.
144
+     *
145
+     * @returns {void}
146
+     */
147
+    removeMessageHandler(handler) {
148
+        if (this.lobbyRoom) {
149
+            this.lobbyRoom.off(XMPPEvents.JSON_MESSAGE_RECEIVED, handler);
150
+        }
151
+    }
152
+
85 153
     /**
86 154
      * Leaves the lobby room.
87 155
      *

Loading…
Cancel
Save