From 016b038ae695d4b8e55ee4e6cd97136a23a344c4 Mon Sep 17 00:00:00 2001 From: Antonio Zhu Date: Wed, 30 Nov 2022 14:55:55 +0100 Subject: [PATCH] Added room retrieval at subscription --- src/Rocket.Chat.Net.Example/Program.cs | 10 +++-- .../Driver/RocketChatDriver.cs | 40 ++++++++++++++++++- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/Rocket.Chat.Net.Example/Program.cs b/src/Rocket.Chat.Net.Example/Program.cs index b671ff0..abe9e0c 100644 --- a/src/Rocket.Chat.Net.Example/Program.cs +++ b/src/Rocket.Chat.Net.Example/Program.cs @@ -32,17 +32,19 @@ private static async Task MainAsync() await bot.ConnectAsync(); // Login - ILoginOption loginOption = new EmailLoginOption + ILoginOption loginOption = new UsernameLoginOption() { - Email = username, + Username = username, Password = password }; try { await bot.LoginAsync(loginOption); + Console.WriteLine("Logged in!"); } - catch (Exception e) { - Console.Write(e); + catch (Exception e) + { + Console.WriteLine(e); } // Start listening for messages diff --git a/src/Rocket.Chat.Net/Driver/RocketChatDriver.cs b/src/Rocket.Chat.Net/Driver/RocketChatDriver.cs index 6ab7b67..1db476c 100644 --- a/src/Rocket.Chat.Net/Driver/RocketChatDriver.cs +++ b/src/Rocket.Chat.Net/Driver/RocketChatDriver.cs @@ -2,6 +2,7 @@ { using System; using System.Collections.Generic; + using System.ComponentModel.Design; using System.IO; using System.Linq; using System.Threading; @@ -180,8 +181,30 @@ public async Task SubscribeToRoomListAsync() public async Task SubscribeToRoomAsync(string roomId = null) { - _logger.Info($"Subscribing to Room: #{roomId ?? "ALLROOMS"}"); - await _client.SubscribeAsync(MessageTopic, TimeoutToken, roomId, MessageSubscriptionLimit.ToString()).ConfigureAwait(false); + List ids = new List(); + + // Subscribe to all rooms when roomId is null + if (roomId == null) + { + var methodResult = await GetAvailableRoomInfoCollection().ConfigureAwait(false); + ids.AddRange(methodResult.Result.Select(roomInfo => roomInfo.Id)); + } else + // Subsribe to given roomId + { + ids.Add(roomId); + } + + List tasks = new List(); + foreach (string id in ids) + { + _logger.Info($"Subscribing to Room: #{id}"); + Task task = new Task(() => _client.SubscribeAsync(MessageTopic, TimeoutToken, id, MessageSubscriptionLimit.ToString())); + tasks.Add(task); + task.Start(); + } + + await Task.WhenAll(tasks).ConfigureAwait(false); + } public async Task SubscribeToRoomInformationAsync(string roomName, RoomType type) @@ -642,6 +665,19 @@ public TypedStreamCollection GetRoomInfoCollection() return typedCollection; } + public async Task>> GetAvailableRoomInfoCollection() + { + JObject result = await _client.CallAsync("rooms/get", CancellationToken.None, new object[] { 0 }).ConfigureAwait(false); + return result.ToObject>>(JsonSerializer); + } + + public async Task> GetAvailableRoomsCollection() + { + JObject result = await _client.CallAsync("rooms/get", CancellationToken.None, new object[] { 0 }).ConfigureAwait(false); + MethodResult> roomMethodResult = result.ToObject>>(JsonSerializer); + throw new NotImplementedException(); + } + public void Dispose() { _client.Dispose();