Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #72 from noobot/channel-created
Browse files Browse the repository at this point in the history
Add Channel created event
  • Loading branch information
Workshop2 authored Dec 28, 2017
2 parents d257342 + 8e9befc commit 6b3e9c7
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/SlackConnector/Connections/Models/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ internal class Channel : Detail

[JsonProperty("members")]
public string[] Members { get; set; }

public string Creator { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using SlackConnector.Connections.Models;

namespace SlackConnector.Connections.Sockets.Messages.Inbound
{
internal class ChannelCreatedMessage : InboundMessage
{
public ChannelCreatedMessage()
{
MessageType = MessageType.Channel_Created;
}

public Channel Channel { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public InboundMessage InterpretMessage(string json)
case MessageType.Reaction_Added:
message = GetReactionMessage(json);
break;
case MessageType.Channel_Created:
message = JsonConvert.DeserializeObject<ChannelCreatedMessage>(json);
break;
}
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ internal enum MessageType
Im_Created,
Team_Join,
Pong,
Reaction_Added
Reaction_Added,
Channel_Created
}
}
7 changes: 7 additions & 0 deletions src/SlackConnector/EventHandlers/ChannelCreatedHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System.Threading.Tasks;
using SlackConnector.Models;

namespace SlackConnector.EventHandlers
{
public delegate Task ChannelCreatedHandler(SlackChannelCreated chatHub);
}
19 changes: 7 additions & 12 deletions src/SlackConnector/ISlackConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ namespace SlackConnector
{
public interface ISlackConnection
{
#region Properties

/// <summary>
/// All of the ChatHubs that are currently open.
/// </summary>
IReadOnlyDictionary<string, SlackChatHub> ConnectedHubs { get; }

/// <summary>
/// UserId => User object.
/// </summary>
Expand Down Expand Up @@ -46,14 +44,6 @@ public interface ISlackConnection
/// </summary>
ContactDetails Self { get; }

#endregion

/// <summary>
/// Disconnect from Slack.
/// </summary>
[Obsolete("Please use Close async method", true)]
void Disconnect();

/// <summary>
/// Close websocket connection to Slack
/// </summary>
Expand All @@ -63,7 +53,7 @@ public interface ISlackConnection
/// Send message to Slack channel.
/// </summary>
Task Say(BotMessage message);

/// <summary>
/// Uploads a file from to a Slack channel
/// </summary>
Expand Down Expand Up @@ -166,5 +156,10 @@ public interface ISlackConnection
/// Raised when SlackApi sends a pong to our ping
/// </summary>
event PongEventHandler OnPong;

/// <summary>
/// Raised when a new channel is created
/// </summary>
event ChannelCreatedHandler OnChannelCreated;
}
}
9 changes: 9 additions & 0 deletions src/SlackConnector/Models/SlackChannelCreated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SlackConnector.Models
{
public class SlackChannelCreated
{
public string Id { get; internal set; }
public string Name { get; internal set; }
public SlackUser Creator { get; internal set; }
}
}
41 changes: 34 additions & 7 deletions src/SlackConnector/SlackConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class SlackConnection : ISlackConnection
private Dictionary<string, SlackUser> _userCache { get; set; }
public IReadOnlyDictionary<string, SlackUser> UserCache => _userCache;

public bool IsConnected => _webSocketClient.IsAlive;
public bool IsConnected => _webSocketClient?.IsAlive ?? false;
public DateTime? ConnectedSince { get; private set; }
public string SlackKey { get; private set; }

Expand Down Expand Up @@ -99,6 +99,7 @@ private Task ListenTo(InboundMessage inboundMessage)
case MessageType.Team_Join: return HandleUserJoined((UserJoinedMessage)inboundMessage);
case MessageType.Pong: return HandlePong((PongMessage)inboundMessage);
case MessageType.Reaction_Added: return HandleReaction((ReactionMessage)inboundMessage);
case MessageType.Channel_Created: return HandleChannelCreated((ChannelCreatedMessage)inboundMessage);
}

return Task.CompletedTask;
Expand Down Expand Up @@ -238,6 +239,23 @@ private Task HandlePong(PongMessage inboundMessage)
return RaisePong(inboundMessage.Timestamp);
}

private Task HandleChannelCreated(ChannelCreatedMessage inboundMessage)
{
string channelId = inboundMessage?.Channel?.Id;
if (channelId == null) return Task.CompletedTask;

var hub = inboundMessage.Channel.ToChatHub();
_connectedHubs[channelId] = hub;

var slackChannelCreated = new SlackChannelCreated
{
Id = channelId,
Name = inboundMessage.Channel.Name,
Creator = GetMessageUser(inboundMessage.Channel.Creator)
};
return RaiseOnChannelCreated(slackChannelCreated);
}

private SlackUser GetMessageUser(string userId)
{
if (string.IsNullOrEmpty(userId))
Expand Down Expand Up @@ -265,12 +283,6 @@ public async Task Close()
}
}

[Obsolete("Please use Close async method", true)]
public void Disconnect()
{
throw new NotImplementedException();
}

public async Task Say(BotMessage message)
{
if (string.IsNullOrEmpty(message.ChatHub?.Id))
Expand Down Expand Up @@ -563,6 +575,21 @@ private async Task RaisePong(DateTime timestamp)
}
}

public event ChannelCreatedHandler OnChannelCreated;
private async Task RaiseOnChannelCreated(SlackChannelCreated chatHub)
{
var e = OnChannelCreated;
if (e != null)
{
try
{
await e(chatHub);
}
catch
{
}
}
}
//TODO: USER JOINED EVENT HANDLING
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -460,5 +460,40 @@ private void should_return_unknown_reaction_message(MessageInterpreter interpret

result.ShouldLookLike(expected);
}

[Theory, AutoMoqData]
private void should_return_channel_created_message(MessageInterpreter interpreter)
{
// given
string json = @"
{
'type': 'channel_created',
'channel': {
'id': 'C024BE91L',
'name': 'fun',
'created': 1360782804,
'creator': 'U024BE7LH'
}
}
";

// when
var result = interpreter.InterpretMessage(json);

// then
var expected = new ChannelCreatedMessage
{
MessageType = MessageType.Channel_Created,
Channel = new Channel
{
Id = "C024BE91L",
Name = "fun",
Creator = "U024BE7LH"
},
RawData = json,
};

result.ShouldLookLike(expected);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Moq;
using Ploeh.AutoFixture;
using SlackConnector.Connections.Models;
using SlackConnector.Connections.Sockets;
using SlackConnector.Connections.Sockets.Messages.Inbound;
using SlackConnector.Models;
using Xunit;
using Shouldly;

namespace SlackConnector.Tests.Unit.SlackConnectionTests.InboundMessageTests
{
public class ChannelCreatedTests
{
[Theory, AutoMoqData]
private async Task should_raise_event(
Mock<IWebSocketClient> webSocket,
SlackConnection slackConnection,
SlackUser slackUser,
Fixture fixture)
{
// given
var connectionInfo = new ConnectionInformation
{
WebSocket = webSocket.Object,
Users = new Dictionary<string, SlackUser>
{
{slackUser.Id , slackUser}
}
};
await slackConnection.Initialise(connectionInfo);

SlackChannelCreated channelCreated = null;
slackConnection.OnChannelCreated += channel =>
{
channelCreated = channel;
return Task.CompletedTask;
};

var inboundMessage = new ChannelCreatedMessage
{
Channel = new Channel
{
Creator = slackUser.Id,
Id = fixture.Create<string>(),
Name = fixture.Create<string>()
}
};

// when
webSocket.Raise(x => x.OnMessage += null, null, inboundMessage);

// then
channelCreated.Id.ShouldBe(inboundMessage.Channel.Id);
channelCreated.Name.ShouldBe(inboundMessage.Channel.Name);
channelCreated.Creator.ShouldBe(slackUser);
slackConnection.ConnectedHubs.ContainsKey(inboundMessage.Channel.Id).ShouldBeTrue();
}

[Theory, AutoMoqData]
private async Task should_not_raise_event_given_missing_data(
Mock<IWebSocketClient> webSocket,
SlackConnection slackConnection)
{
// given
var connectionInfo = new ConnectionInformation { WebSocket = webSocket.Object };
await slackConnection.Initialise(connectionInfo);

SlackChannelCreated channelCreated = null;
slackConnection.OnChannelCreated += channel =>
{
channelCreated = channel;
return Task.CompletedTask;
};

var inboundMessage = new ChannelCreatedMessage { Channel = null };

// when
webSocket.Raise(x => x.OnMessage += null, null, inboundMessage);

// then
channelCreated.ShouldBeNull();
slackConnection.ConnectedHubs.ShouldBeEmpty();
}
}
}
2 changes: 2 additions & 0 deletions tests/SlackConnector.Tests.Unit/Stubs/SlackConnectionStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public void RaiseOnUserJoined()
}

public event PongEventHandler OnPong;
public event ChannelCreatedHandler OnChannelCreated;

public void RaiseOnPong()
{
OnPong?.Invoke(DateTime.MinValue);
Expand Down

0 comments on commit 6b3e9c7

Please sign in to comment.