Commit c01c062a authored by Robert Rudman's avatar Robert Rudman

Tidy up code a bit. Add some useful filters for events.

parent a3e2a745
......@@ -52,17 +52,19 @@ namespace StackExchangeChat.Console
// var result = apiThing.TotalQuestionsByTag("design").GetAwaiter().GetResult();
var chatClient = serviceProvider.GetService<ChatClient>();
chatClient
.SubscribeToEvents(ChatSite.StackExchange, 86421)
var events = chatClient.SubscribeToEvents(ChatSite.StackExchange, 86421);
events.Subscribe(System.Console.WriteLine);
events
.OnlyMessages()
.SameRoomOnly()
.Subscribe(async chatEvent =>
{
try
{
if (chatEvent.EventDetails.UserId != chatEvent.RoomDetails.MyUserId)
if (chatEvent.ChatEventDetails.UserId != chatEvent.RoomDetails.MyUserId)
{
await chatClient.SendMessage(chatEvent.RoomDetails.ChatSite, chatEvent.RoomDetails.RoomId,
$":{chatEvent.EventDetails.MessageId} Replying to message..");
$":{chatEvent.ChatEventDetails.MessageId} Replying to message..");
}
} catch (Exception ex) { }
}, exception =>
......
......@@ -71,13 +71,13 @@ namespace StackExchangeChat
if (eventsObject == null)
return;
var events = eventsObject.ToObject<List<EventDetails>>();
var events = eventsObject.ToObject<List<ChatEventDetails>>();
foreach (var @event in events)
{
var chatEvent = new ChatEvent
{
RoomDetails = roomDetails,
EventDetails = @event
ChatEventDetails = @event
};
observer.OnNext(chatEvent);
}
......
......@@ -4,12 +4,12 @@ namespace StackExchangeChat
{
public class ChatEvent
{
public EventDetails EventDetails { get; set; }
public ChatEventDetails ChatEventDetails { get; set; }
public RoomDetails RoomDetails { get; set; }
public override string ToString()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
return JsonConvert.SerializeObject(this);
}
}
}
......@@ -2,10 +2,10 @@
namespace StackExchangeChat
{
public class EventDetails
public class ChatEventDetails
{
[JsonProperty(PropertyName = "event_type")]
public EventType EventType { get; set; }
public ChatEventType ChatEventType { get; set; }
[JsonProperty(PropertyName = "time_stamp")]
public int TimeStamp { get; set; }
......
namespace StackExchangeChat
{
// Copy & pasted from https://github.com/SOBotics/SharpExchange/blob/master/SharpExchange/Chat/Events/EventType.cs
public enum EventType
public enum ChatEventType
{
/// <summary>
/// A new message has been posted.
......@@ -34,12 +34,12 @@
MessageStarToggled = 6,
/// <summary>
/// No idea.
/// Browser simply logs the content of this event.
/// </summary>
DebugMessage = 7,
/// <summary>
/// The current account has been mentioned (@Username) in a message.
/// The current account has been mentioned (@Username) in a message. May originate from another room.
/// </summary>
UserMentioned = 8,
......@@ -66,12 +66,13 @@
ModeratorFlag = 12,
/// <summary>
/// No idea.
/// User is ignored or un-ignored.
/// </summary>
UserSettingsChanged = 13,
/// <summary>
/// No idea.
/// Notifications displayed as a banner in a room. Does not include room invitations.
/// For example, room events starting.
/// </summary>
GlobalNotification = 14,
......@@ -81,7 +82,7 @@
UserAccessLevelChanged = 15,
/// <summary>
/// No idea.
/// Not sure which events trigger this. Behaves the same as GlobalNotification in the web browser.
/// </summary>
UserNotification = 16,
......@@ -91,7 +92,7 @@
RoomInvitation = 17,
/// <summary>
/// Someone has posted a direct reply to a message posted by this account.
/// Someone has posted a direct reply to a message posted by this account. May originate from another room.
/// </summary>
MessageReply = 18,
......@@ -111,7 +112,7 @@
TimeBreak = 21,
/// <summary>
/// No idea.
/// New item in the room's feed ticker received.
/// </summary>
FeedTicker = 22,
......
......@@ -3,6 +3,11 @@
public class RoomDetails
{
public ChatSite ChatSite { get; set; }
/// <summary>
/// Note that this is the room id of the room being subscribed to.
/// However, pings from other rooms will still trigger events for this room. This means this might not match the event's room id.
/// </summary>
public int RoomId { get; set; }
public int MyUserId { get; set; }
......
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
......@@ -17,12 +16,7 @@ namespace StackExchangeChat
private readonly IServiceProvider _serviceProvider;
private readonly IChatCredentials _chatCredentials;
private struct SiteRoomIdPair
{
public ChatSite ChatSite;
public int RoomId;
}
private struct SiteRoomIdPair { public ChatSite ChatSite; public int RoomId; }
private readonly Dictionary<ChatSite, DateTime> _cookieExpires = new Dictionary<ChatSite, DateTime>();
private readonly Dictionary<ChatSite, Task<Cookie>> _authenticateTasks = new Dictionary<ChatSite, Task<Cookie>>();
private readonly object _locker = new object();
......@@ -137,8 +131,7 @@ namespace StackExchangeChat
});
await httpClient.PostAsync($"https://{chatSite.LoginDomain}/users/login", loginPayload);
var cookies = cookieContainer.GetCookies(new Uri($"https://{chatSite.LoginDomain}")).Cast<Cookie>()
.ToList();
var cookies = cookieContainer.GetCookies(new Uri($"https://{chatSite.LoginDomain}")).Cast<Cookie>().ToList();
var acctCookie = cookies.FirstOrDefault(c => c.Name == "acct");
if (acctCookie == null || acctCookie.Expired)
throw new Exception();
......
using System;
using System.Linq;
using System.Reactive.Linq;
namespace StackExchangeChat.Utilities
......@@ -7,9 +8,26 @@ namespace StackExchangeChat.Utilities
{
public static IObservable<ChatEvent> OnlyMessages(this IObservable<ChatEvent> observable)
{
return observable.Where(c =>
c.EventDetails.EventType == EventType.MessagePosted ||
c.EventDetails.EventType == EventType.MessageEdited);
return observable.OnlyEventTypes(ChatEventType.MessagePosted, ChatEventType.MessageEdited);
}
public static IObservable<ChatEvent> Pinged(this IObservable<ChatEvent> observable)
{
return observable.OnlyEventTypes(ChatEventType.MessageReply, ChatEventType.UserMentioned);
}
/// <summary>
/// If a user pings the bot from another room, an event will still be emitted in the subscribed room.
/// This filters messages to only those posted in the room subscribed to.
/// </summary>
public static IObservable<ChatEvent> SameRoomOnly(this IObservable<ChatEvent> observable)
{
return observable.Where(c => c.ChatEventDetails.RoomId == c.RoomDetails.RoomId);
}
public static IObservable<ChatEvent> OnlyEventTypes(this IObservable<ChatEvent> observable, params ChatEventType[] chatEventTypes)
{
return observable.Where(c => chatEventTypes.Contains(c.ChatEventDetails.ChatEventType));
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment