Skip to content
Permalink
Browse files

Add typed API to fetch channel subscribers

  • Loading branch information...
mythz committed Apr 20, 2016
1 parent bd62ed8 commit 1476e232502f690ba1832600c221ad76c15cfda7
@@ -50,6 +50,16 @@ public class ServerEventMessage : IMeta
public Dictionary<string, string> Meta { get; set; }
}

public class ServerEventUser : IMeta
{
public string UserId { get; set; }
public string DisplayName { get; set; }
public string ProfileUrl { get; set; }
public string[] Channels { get; set; }

public Dictionary<string, string> Meta { get; set; }
}

public partial class ServerEventsClient : IDisposable
{
private static ILog log = LogManager.GetLogger(typeof(ServerEventsClient));
@@ -727,6 +737,44 @@ public static Task UnsubscribeFromChannelsAsync(this ServerEventsClient client,
});
}

public static List<ServerEventUser> GetChannelSubscribers(this ServerEventsClient client)
{
var response = client.ServiceClient.Get(new GetEventSubscribers { Channels = client.Channels });
return response.ConvertAll(x => x.ToServerEventUser());
}

public static Task<List<ServerEventUser>> GetChannelSubscribersAsync(this ServerEventsClient client)
{
var responseTask = client.ServiceClient.GetAsync(new GetEventSubscribers { Channels = client.Channels });
return responseTask.ContinueWith(task => task.Result.ConvertAll(x => x.ToServerEventUser()));
}

internal static ServerEventUser ToServerEventUser(this Dictionary<string, string> map)
{
var channels = map.Get("channels");
var to = new ServerEventUser
{
UserId = map.Get("userId"),
DisplayName = map.Get("displayName"),
ProfileUrl = map.Get("profileUrl"),
Channels = !string.IsNullOrEmpty(channels) ? channels.Split(',') : null,
};

foreach (var entry in map)
{
if (entry.Key == "userId" || entry.Key == "displayName" ||
entry.Key == "profileUrl" || entry.Key == "channels")
continue;

if (to.Meta == null)
to.Meta = new Dictionary<string, string>();

to.Meta[entry.Key] = entry.Value;
}

return to;
}

public static T Populate<T>(this T dst, ServerEventMessage src, Dictionary<string, string> msg) where T : ServerEventMessage
{
dst.EventId = src.EventId;
@@ -824,6 +824,12 @@ public async Task Does_receive_messages_on_to_clients_subscribed_on_multiple_cha
clientABCD.Connect()
);

var channelAsubscribers = clientA.GetChannelSubscribers();
Assert.That(channelAsubscribers.Count, Is.EqualTo(4));

var channelABsubscribers = clientA.GetChannelSubscribers();
Assert.That(channelABsubscribers.Count, Is.EqualTo(4));

"Publishing Msg Batch #1 ...".Print();
clientA.PostChat("#1 hello to A", channel: "A");
clientA.PostChat("#2 hello to B", channel: "B");
@@ -924,6 +930,16 @@ public async Task Does_receive_all_join_and_leave_messages()
Assert.That(joinB.Count, Is.EqualTo(2)); //B + [A (B)]
Assert.That(joinAB.Count, Is.EqualTo(2)); //[(A) B] + [A (B)]

var channelAsubscribers = clientA.GetChannelSubscribers();
Assert.That(channelAsubscribers.Count, Is.EqualTo(2));

var channelBsubscribers = clientB.GetChannelSubscribers();
Assert.That(channelBsubscribers.Count, Is.EqualTo(2));

var channelABsubscribers = clientAB.GetChannelSubscribers();
Assert.That(channelABsubscribers.Count, Is.EqualTo(3));


var usersA = clientA.ServiceClient.Get(new GetEventSubscribers { Channels = new[] { "A" } });
var usersB = clientA.ServiceClient.Get(new GetEventSubscribers { Channels = new[] { "B" } });
var usersAB = clientA.ServiceClient.Get(new GetEventSubscribers { Channels = new[] { "A", "B" } });

0 comments on commit 1476e23

Please sign in to comment.
You can’t perform that action at this time.