diff --git a/lib/src/connect/meeting/main_websocket/chat/chat.dart b/lib/src/connect/meeting/main_websocket/chat/chat.dart index be518d1..26997fd 100644 --- a/lib/src/connect/meeting/main_websocket/chat/chat.dart +++ b/lib/src/connect/meeting/main_websocket/chat/chat.dart @@ -1,3 +1,6 @@ +import 'dart:async'; + +import 'package:bbb_app/src/connect/meeting/main_websocket/chat/message.dart'; import 'package:bbb_app/src/connect/meeting/main_websocket/module.dart'; import 'package:flutter/widgets.dart'; @@ -6,6 +9,16 @@ class ChatModule extends Module { /// The default chat ID of the main public group chat. static const String defaultChatID = "MAIN-PUBLIC-GROUP-CHAT"; + /// Topic of chat messages to subscribe to. + static const String _groupChatMessageTopic = "group-chat-msg"; + + /// Controller publishing chat messages. + StreamController _chatMessageController = + StreamController.broadcast(); + + /// Messages already received. + List _messages = []; + ChatModule(messageSender) : super(messageSender); /// Send a chat message. @@ -13,20 +26,54 @@ class ChatModule extends Module { String chatID = defaultChatID, @required String internUserID, @required String message, - }) async {} + }) async { + // TODO + } @override void processMessage(Map msg) { - // TODO: implement processMessage + final String method = msg["msg"]; + + if (method == "added") { + String collectionName = msg["collection"]; + + if (collectionName == "group-chat-msg") { + Map fields = msg["fields"]; + + String chatID = fields["chatId"]; + int timestamp = fields["timestamp"]; + String senderID = fields["sender"]; + String content = fields["message"]; + + ChatMessage message = ChatMessage( + chatID: chatID, + senderID: senderID, + content: content, + timestamp: new DateTime.fromMillisecondsSinceEpoch(timestamp), + ); + + print( + "Incoming message: ${message.content} (From ${message.timestamp.toString()}"); + + _messages.add(message); + _chatMessageController.add(message); + } + } } @override void onConnected() { - // TODO: Subscribe to the chat topic + subscribe(_groupChatMessageTopic); } @override Future onDisconnect() async { // Do nothing } + + /// Get a stream of incoming messages. + Stream get messageStream => _chatMessageController.stream; + + /// Get already received messages. + List get messages => _messages; } diff --git a/lib/src/connect/meeting/main_websocket/chat/message.dart b/lib/src/connect/meeting/main_websocket/chat/message.dart new file mode 100644 index 0000000..36c9f0e --- /dev/null +++ b/lib/src/connect/meeting/main_websocket/chat/message.dart @@ -0,0 +1,32 @@ +/// A chat message representation. +class ChatMessage { + /// ID of the chat. + String _chatID; + + /// Intern user ID of the sender. + String _senderID; + + /// Content of the message. + String _content; + + /// Timestamp of the message. + DateTime _timestamp; + + ChatMessage({ + String chatID, + String senderID, + String content, + DateTime timestamp, + }) : this._chatID = chatID, + this._senderID = senderID, + this._content = content, + this._timestamp = timestamp; + + String get content => _content; + + String get senderID => _senderID; + + String get chatID => _chatID; + + DateTime get timestamp => _timestamp; +}