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 #66 from noobot/loredous-ReactionHandling
Browse files Browse the repository at this point in the history
Loredous reaction handling
  • Loading branch information
Workshop2 authored Oct 25, 2017
2 parents 29ece92 + e0212ec commit 8ee9b04
Show file tree
Hide file tree
Showing 22 changed files with 709 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SlackConnector.Logging;
using SlackConnector.Connections.Sockets.Messages.Inbound.ReactionItem;

namespace SlackConnector.Connections.Sockets.Messages.Inbound
{
Expand Down Expand Up @@ -42,6 +43,9 @@ public InboundMessage InterpretMessage(string json)
case MessageType.Pong:
message = JsonConvert.DeserializeObject<PongMessage>(json);
break;
case MessageType.Reaction_Added:
message = GetReactionMessage(json);
break;
}
}
catch (Exception ex)
Expand All @@ -59,11 +63,10 @@ public InboundMessage InterpretMessage(string json)

private static MessageType ParseMessageType(string json)
{
MessageType messageType = MessageType.Unknown;

var messageType = MessageType.Unknown;
if (!string.IsNullOrWhiteSpace(json))
{
JObject messageJobject = JObject.Parse(json);
var messageJobject = JObject.Parse(json);
Enum.TryParse(messageJobject["type"].Value<string>(), true, out messageType);
}

Expand All @@ -73,7 +76,6 @@ private static MessageType ParseMessageType(string json)
private static ChatMessage GetChatMessage(string json)
{
var message = JsonConvert.DeserializeObject<ChatMessage>(json);

if (message != null)
{
message.Channel = WebUtility.HtmlDecode(message.Channel);
Expand All @@ -84,5 +86,47 @@ private static ChatMessage GetChatMessage(string json)

return message;
}

private static ReactionMessage GetReactionMessage(string json)
{
var message = JsonConvert.DeserializeObject<ReactionMessage>(json);

var reactionItemType = ParseReactionItemType(json);
switch (reactionItemType)
{
case ReactionItemType.file:
message.ReactingTo = GenerateReactionItem<FileReaction>(json);
break;
case ReactionItemType.file_comment:
message.ReactingTo = GenerateReactionItem<FileCommentReaction>(json);
break;
case ReactionItemType.message:
message.ReactingTo = GenerateReactionItem<MessageReaction>(json);
break;
default:
message.ReactingTo = GenerateReactionItem<UnknownReaction>(json);
break;
}

return message;
}

private static IReactionItem GenerateReactionItem<T>(string json) where T : IReactionItem
{
var messageJobject = JObject.Parse(json);
return JsonConvert.DeserializeObject<T>(messageJobject["item"].ToString());
}

private static ReactionItemType ParseReactionItemType(string json)
{
var messageType = ReactionItemType.unknown;
if (!string.IsNullOrWhiteSpace(json))
{
var messageJobject = JObject.Parse(json);
Enum.TryParse(messageJobject["item"]["type"].Value<string>(), true, out messageType);
}

return messageType;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ internal enum MessageType
Channel_Joined,
Im_Created,
Team_Join,
Pong
Pong,
Reaction_Added
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ namespace SlackConnector.Connections.Sockets.Messages.Inbound
{
internal class PongMessage : InboundMessage
{
[JsonProperty("time")]
public DateTime Timestamp { get; set; }
[JsonProperty("reply_to")]
public int ReplyTo { get; set; }

public PongMessage()
{
MessageType = MessageType.Pong;
}

[JsonProperty("time")]
public DateTime Timestamp { get; set; }
[JsonProperty("reply_to")]
public int ReplyTo { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Newtonsoft.Json;

namespace SlackConnector.Connections.Sockets.Messages.Inbound.ReactionItem
{
internal class FileCommentReaction : IReactionItem
{
[JsonProperty("file")]
public string File { get; set; }

[JsonProperty("file_comment")]
public string FileComment { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SlackConnector.Connections.Sockets.Messages.Inbound.ReactionItem
{
internal class FileReaction : IReactionItem
{
public string File { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace SlackConnector.Connections.Sockets.Messages.Inbound.ReactionItem
{
interface IReactionItem
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace SlackConnector.Connections.Sockets.Messages.Inbound.ReactionItem
{
internal class MessageReaction : IReactionItem
{
[JsonProperty("channel")]
public string Channel { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace SlackConnector.Connections.Sockets.Messages.Inbound.ReactionItem
{
internal enum ReactionItemType
{
unknown = 0,
message,
file,
file_comment
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace SlackConnector.Connections.Sockets.Messages.Inbound.ReactionItem
{
internal class UnknownReaction : IReactionItem
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Newtonsoft.Json;
using SlackConnector.Connections.Sockets.Messages.Inbound.ReactionItem;

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

[JsonProperty("user")]
public string User { get; set; }

[JsonProperty("reaction")]
public string Reaction { get; set; }

[JsonProperty("event_ts")]
public double Timestamp { get; set; }

public IReactionItem ReactingTo { get; set; }

[JsonProperty("item_user")]
public string ReactingToUser { get; set; }
}
}
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 ReactionReceivedEventHandler(ISlackReaction message);
}
5 changes: 5 additions & 0 deletions src/SlackConnector/ISlackConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ public interface ISlackConnection
/// </summary>
event MessageReceivedEventHandler OnMessageReceived;

/// <summary>
/// Raised when reaction messages are received.
/// </summary>
event ReactionReceivedEventHandler OnReaction;

/// <summary>
/// Raised when bot joins a channel or group
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions src/SlackConnector/Models/ISlackReaction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace SlackConnector.Models
{
public interface ISlackReaction
{
string RawData { get; }
SlackUser User { get; }
double Timestamp { get; }
string Reaction { get; }
SlackUser ReactingToUser { get; }
}
}
13 changes: 13 additions & 0 deletions src/SlackConnector/Models/Reactions/SlackFileCommentReaction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace SlackConnector.Models.Reactions
{
public class SlackFileCommentReaction : ISlackReaction
{
public string RawData { get; internal set; }
public SlackUser User { get; internal set; }
public double Timestamp { get; internal set; }
public string Reaction { get; internal set; }
public string File { get; internal set; }
public string FileComment { get; internal set; }
public SlackUser ReactingToUser { get; internal set; }
}
}
12 changes: 12 additions & 0 deletions src/SlackConnector/Models/Reactions/SlackFileReaction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace SlackConnector.Models.Reactions
{
public class SlackFileReaction : ISlackReaction
{
public string RawData { get; internal set; }
public SlackUser User { get; internal set; }
public double Timestamp { get; internal set; }
public string Reaction { get; internal set; }
public string File { get; internal set; }
public SlackUser ReactingToUser { get; internal set; }
}
}
12 changes: 12 additions & 0 deletions src/SlackConnector/Models/Reactions/SlackMessageReaction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace SlackConnector.Models.Reactions
{
public class SlackMessageReaction : ISlackReaction
{
public SlackChatHub ChatHub { get; internal set; }
public string RawData { get; internal set; }
public SlackUser User { get; internal set; }
public double Timestamp { get; internal set; }
public string Reaction { get; internal set; }
public SlackUser ReactingToUser { get; internal set; }
}
}
11 changes: 11 additions & 0 deletions src/SlackConnector/Models/Reactions/SlackUnknownReaction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace SlackConnector.Models.Reactions
{
public class SlackUnknownReaction : ISlackReaction
{
public string RawData { get; internal set; }
public SlackUser User { get; internal set; }
public double Timestamp { get; internal set; }
public string Reaction { get; internal set; }
public SlackUser ReactingToUser { get; internal set; }
}
}
Loading

0 comments on commit 8ee9b04

Please sign in to comment.