Skip to content

Commit

Permalink
Added room retrieval at subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Zhu committed Nov 30, 2022
1 parent 43bf3ff commit 016b038
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/Rocket.Chat.Net.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 38 additions & 2 deletions src/Rocket.Chat.Net/Driver/RocketChatDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.IO;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -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<string> ids = new List<string>();

// 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<Task> tasks = new List<Task>();
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)
Expand Down Expand Up @@ -642,6 +665,19 @@ public TypedStreamCollection<RoomInfo> GetRoomInfoCollection()
return typedCollection;
}

public async Task<MethodResult<IEnumerable<RoomInfo>>> GetAvailableRoomInfoCollection()
{
JObject result = await _client.CallAsync("rooms/get", CancellationToken.None, new object[] { 0 }).ConfigureAwait(false);
return result.ToObject<MethodResult<IEnumerable<RoomInfo>>>(JsonSerializer);
}

public async Task<TypedStreamCollection<Room>> GetAvailableRoomsCollection()
{
JObject result = await _client.CallAsync("rooms/get", CancellationToken.None, new object[] { 0 }).ConfigureAwait(false);
MethodResult<IEnumerable<RoomInfo>> roomMethodResult = result.ToObject<MethodResult<IEnumerable<RoomInfo>>>(JsonSerializer);
throw new NotImplementedException();
}

public void Dispose()
{
_client.Dispose();
Expand Down

0 comments on commit 016b038

Please sign in to comment.