Skip to content

Commit bf4731d

Browse files
Merge pull request #63 from nullinside-development-group/feat/history
feat: keeping a history of chat messages
2 parents 528505b + a2e3c33 commit bf4731d

File tree

8 files changed

+255
-9
lines changed

8 files changed

+255
-9
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
3+
namespace TwitchStreamingTools.Models;
4+
5+
/// <summary>
6+
/// A message sent in twitch chat.
7+
/// </summary>
8+
public class TwitchChatMessage {
9+
/// <summary>
10+
/// Initializes a new instance of the <see cref="TwitchChatMessage" />.
11+
/// </summary>
12+
/// <param name="channel">The channel to the message was sent in.</param>
13+
/// <param name="username">The username of the sender.</param>
14+
/// <param name="message">The chat message.</param>
15+
/// <param name="timestamp">The timestamp of when the message was sent.</param>
16+
public TwitchChatMessage(string channel, string username, string message, DateTime timestamp) {
17+
Channel = channel;
18+
Username = username;
19+
Message = message;
20+
Timestamp = timestamp;
21+
}
22+
23+
/// <summary>
24+
/// The channel to the message was sent in.
25+
/// </summary>
26+
public string Channel { get; set; }
27+
28+
/// <summary>
29+
/// The username of the sender.
30+
/// </summary>
31+
public string Username { get; set; }
32+
33+
/// <summary>
34+
/// The chat message.
35+
/// </summary>
36+
public string Message { get; set; }
37+
38+
/// <summary>
39+
/// The timestamp of when the message was sent.
40+
/// </summary>
41+
public DateTime Timestamp { get; set; }
42+
}

src/TwitchStreamingTools/ServiceCollectionExtensions.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Nullinside.Api.Common.Twitch;
66

77
using TwitchStreamingTools.Services;
8+
using TwitchStreamingTools.Utilities;
89
using TwitchStreamingTools.ViewModels;
910
using TwitchStreamingTools.ViewModels.Pages;
1011

@@ -19,11 +20,16 @@ public static class ServiceCollectionExtensions {
1920
/// </summary>
2021
/// <param name="collection">The services collection to initialize.</param>
2122
public static void AddCommonServices(this IServiceCollection collection) {
22-
collection.AddSingleton<ITwitchAccountService, TwitchAccountService>();
23+
// Regular stuff
24+
collection.AddSingleton<IConfiguration, Configuration>(_ => Configuration.Instance);
2325
collection.AddSingleton<ITwitchClientProxy, TwitchClientProxy>(_ => TwitchClientProxy.Instance);
26+
collection.AddSingleton<ITwitchChatLog, TwitchChatLog>();
27+
28+
// Services
29+
collection.AddSingleton<ITwitchAccountService, TwitchAccountService>();
2430
collection.AddSingleton<ITwitchTtsService, TwitchTtsService>();
25-
collection.AddSingleton<IConfiguration, Configuration>(_ => Configuration.Instance);
2631

32+
// View models
2733
collection.AddTransient<MainWindowViewModel>();
2834
collection.AddTransient<AccountViewModel>();
2935
collection.AddTransient<ChatViewModel>();

src/TwitchStreamingTools/Services/TwitchTtsService.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Nullinside.Api.Common.Twitch;
99

1010
using TwitchStreamingTools.Tts;
11+
using TwitchStreamingTools.Utilities;
1112

1213
namespace TwitchStreamingTools.Services;
1314

@@ -40,6 +41,11 @@ public class TwitchTtsService : ITwitchTtsService {
4041
/// </summary>
4142
private readonly Thread _thread;
4243

44+
/// <summary>
45+
/// The twitch chat log.
46+
/// </summary>
47+
private readonly ITwitchChatLog _twitchChatLog;
48+
4349
/// <summary>
4450
/// The twitch chat client to forward chat messages from.
4551
/// </summary>
@@ -50,9 +56,11 @@ public class TwitchTtsService : ITwitchTtsService {
5056
/// </summary>
5157
/// <param name="twitchClientProxy">The twitch chat client.</param>
5258
/// <param name="configuration">The application configuration.</param>
53-
public TwitchTtsService(ITwitchClientProxy twitchClientProxy, IConfiguration configuration) {
59+
/// <param name="twitchChatLog">The twitch chat log.</param>
60+
public TwitchTtsService(ITwitchClientProxy twitchClientProxy, IConfiguration configuration, ITwitchChatLog twitchChatLog) {
5461
_configuration = configuration;
5562
_twitchClientProxy = twitchClientProxy;
63+
_twitchChatLog = twitchChatLog;
5664
_thread = new Thread(Main) {
5765
IsBackground = true
5866
};
@@ -96,7 +104,7 @@ private void ConnectChatsInConfig() {
96104
}
97105

98106
foreach (string? newChat in missing) {
99-
var tts = new TwitchChatTts(_configuration, _twitchClientProxy, _configuration.TwitchChats?.FirstOrDefault(t => t.TwitchChannel == newChat));
107+
var tts = new TwitchChatTts(_configuration, _twitchClientProxy, _configuration.TwitchChats?.FirstOrDefault(t => t.TwitchChannel == newChat), _twitchChatLog);
100108
tts.Connect();
101109
_chats?.Add(tts);
102110
}

src/TwitchStreamingTools/Tts/TwitchChatTts.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,16 @@ public class TwitchChatTts : IDisposable, ITwitchChatTts {
7575
/// </summary>
7676
private ManualResetEvent? _ttsSoundOutputSignal;
7777

78+
/// <summary>
79+
/// The twitch chat log.
80+
/// </summary>
81+
public ITwitchChatLog _twitchChatLog;
82+
7883
/// <summary>
7984
/// Initializes a new instance of the <see cref="TwitchChatTts" /> class.
8085
/// </summary>
81-
public TwitchChatTts(IConfiguration configuration, ITwitchClientProxy twitchClient, TwitchChatConfiguration? config) {
86+
public TwitchChatTts(IConfiguration configuration, ITwitchClientProxy twitchClient, TwitchChatConfiguration? config, ITwitchChatLog twitchChatLog) {
87+
_twitchChatLog = twitchChatLog;
8288
_configuration = configuration;
8389
_twitchClient = twitchClient;
8490
ChatConfig = config;
@@ -321,6 +327,7 @@ private void Client_OnMessageReceived(OnMessageReceivedArgs e) {
321327
Console.WriteLine($"Adding: {e.ChatMessage.Username} says {e.ChatMessage.Message}");
322328
try {
323329
_soundsToPlay.Add(e);
330+
_twitchChatLog.AddMessage(new TwitchChatMessage(e.ChatMessage.Channel, e.ChatMessage.Username, e.ChatMessage.Message, e.GetTimestamp() ?? DateTime.UtcNow));
324331
}
325332
catch (Exception ex) {
326333
Console.WriteLine($"Failed to add: {e.ChatMessage.Username} says {e.ChatMessage.Message}\r\n{ex}");
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
using TwitchStreamingTools.Models;
5+
6+
namespace TwitchStreamingTools.Utilities;
7+
8+
/// <summary>
9+
/// The contract for a logger that keeps old twitch messages.
10+
/// </summary>
11+
public interface ITwitchChatLog {
12+
/// <summary>
13+
/// The maximum number of messages to keep in the log, anything larger than this number will be deleted starting with
14+
/// the oldest messages.
15+
/// </summary>
16+
public int MaximumMessageCount { get; set; }
17+
18+
/// <summary>
19+
/// The maximum message age before the message should be deleted, anything older than this will be deleted starting
20+
/// with the oldest messages.
21+
/// </summary>
22+
public TimeSpan MaximumMessageAge { get; set; }
23+
24+
/// <summary>
25+
/// Retrieves all messages.
26+
/// </summary>
27+
/// <param name="channel">If set, filter messages to those sent in a channel, otherwise all channels.</param>
28+
/// <returns>All known twitch messages.</returns>
29+
public IEnumerable<TwitchChatMessage> GetMessages(string? channel = null);
30+
31+
/// <summary>
32+
/// Adds a twitch chat log.
33+
/// </summary>
34+
/// <param name="message">The chat messages.</param>
35+
public void AddMessage(TwitchChatMessage message);
36+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
6+
using Newtonsoft.Json;
7+
8+
using TwitchStreamingTools.Models;
9+
10+
namespace TwitchStreamingTools.Utilities;
11+
12+
/// <summary>
13+
/// A logger that keeps old twitch messages.
14+
/// </summary>
15+
public class TwitchChatLog : ITwitchChatLog {
16+
/// <summary>
17+
/// The location of the log file.
18+
/// </summary>
19+
private static readonly string FILE_LOCATION =
20+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "nullinside",
21+
"twitch-streaming-tools", "twitch-log.json");
22+
23+
/// <summary>
24+
/// The twitch chat messages collection.
25+
/// </summary>
26+
private readonly List<TwitchChatMessage> _messages = new();
27+
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="TwitchChatLog" /> class.
30+
/// </summary>
31+
public TwitchChatLog() {
32+
_messages.AddRange(ReadLog() ?? []);
33+
}
34+
35+
/// <inheritdoc />
36+
public int MaximumMessageCount { get; set; } = 100000;
37+
38+
/// <inheritdoc />
39+
public TimeSpan MaximumMessageAge { get; set; } = TimeSpan.FromDays(7);
40+
41+
/// <inheritdoc />
42+
public IEnumerable<TwitchChatMessage> GetMessages(string? channel = null) {
43+
lock (_messages) {
44+
return channel == null
45+
? _messages.ToList()
46+
: _messages.Where(m => m.Channel == channel).ToList();
47+
}
48+
}
49+
50+
/// <inheritdoc />
51+
public void AddMessage(TwitchChatMessage message) {
52+
lock (_messages) {
53+
_messages.Add(message);
54+
55+
DateTime currentTime = DateTime.Now;
56+
_messages.RemoveAll(m => currentTime - m.Timestamp > MaximumMessageAge);
57+
58+
if (_messages.Count > MaximumMessageCount) {
59+
_messages.RemoveRange(0, _messages.Count - MaximumMessageCount);
60+
}
61+
}
62+
63+
WriteLog();
64+
}
65+
66+
/// <summary>
67+
/// Reads the log from disk.
68+
/// </summary>
69+
/// <returns>The log if successful, null otherwise.</returns>
70+
private static IEnumerable<TwitchChatMessage>? ReadLog() {
71+
try {
72+
string json = File.ReadAllText(FILE_LOCATION);
73+
return JsonConvert.DeserializeObject<IEnumerable<TwitchChatMessage>>(json);
74+
}
75+
catch { return null; }
76+
}
77+
78+
/// <summary>
79+
/// Writes the log file to disk.
80+
/// </summary>
81+
/// <returns>True if successful, false otherwise.</returns>
82+
private bool WriteLog() {
83+
try {
84+
Directory.CreateDirectory(Path.GetDirectoryName(FILE_LOCATION)!);
85+
86+
string json;
87+
lock (_messages) {
88+
json = JsonConvert.SerializeObject(_messages);
89+
}
90+
91+
File.WriteAllText(FILE_LOCATION, json);
92+
return true;
93+
}
94+
catch {
95+
return false;
96+
}
97+
}
98+
}

src/TwitchStreamingTools/ViewModels/Pages/ChatViewModel.cs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Collections.ObjectModel;
34
using System.Linq;
45
using System.Reactive;
6+
using System.Text;
57
using System.Threading.Tasks;
68

79
using Avalonia;
@@ -15,6 +17,7 @@
1517
using TwitchLib.Client.Events;
1618

1719
using TwitchStreamingTools.Models;
20+
using TwitchStreamingTools.Utilities;
1821

1922
namespace TwitchStreamingTools.ViewModels.Pages;
2023

@@ -27,6 +30,11 @@ public class ChatViewModel : PageViewModelBase, IDisposable {
2730
/// </summary>
2831
private readonly IConfiguration _configuration;
2932

33+
/// <summary>
34+
/// The twitch chat log.
35+
/// </summary>
36+
private readonly ITwitchChatLog _twitchChatLog;
37+
3038
/// <summary>
3139
/// The twitch chat client.
3240
/// </summary>
@@ -57,7 +65,9 @@ public class ChatViewModel : PageViewModelBase, IDisposable {
5765
/// </summary>
5866
/// <param name="twitchClient">The twitch chat client.</param>
5967
/// <param name="configuration">The application configuration.</param>
60-
public ChatViewModel(ITwitchClientProxy twitchClient, IConfiguration configuration) {
68+
/// <param name="twitchChatLog">The twitch chat log.</param>
69+
public ChatViewModel(ITwitchClientProxy twitchClient, IConfiguration configuration, ITwitchChatLog twitchChatLog) {
70+
_twitchChatLog = twitchChatLog;
6171
_twitchClient = twitchClient;
6272
_configuration = configuration;
6373

@@ -196,22 +206,61 @@ from user in _selectedTwitchChatNames
196206
/// <param name="msg">The message received.</param>
197207
private void OnChatMessage(OnMessageReceivedArgs msg) {
198208
if (_selectedTwitchChatNames.Count > 1) {
199-
TwitchChat = (TwitchChat + $"({msg.ChatMessage.Channel}) {msg.ChatMessage.Username}: {msg.ChatMessage.Message}").Trim();
209+
TwitchChat = (TwitchChat + FormatChatMessage(msg.ChatMessage.Username, msg.ChatMessage.Message, msg.GetTimestamp() ?? DateTime.UtcNow, msg.ChatMessage.Channel)).Trim();
200210
}
201211
else {
202-
TwitchChat = (TwitchChat + $"{msg.ChatMessage.Username}: {msg.ChatMessage.Message}").Trim();
212+
TwitchChat = (TwitchChat + FormatChatMessage(msg.ChatMessage.Username, msg.ChatMessage.Message, msg.GetTimestamp() ?? DateTime.UtcNow)).Trim();
203213
}
204214

205215
TwitchChat += "\n";
206216
TextBoxCursorPosition = int.MaxValue;
207217
}
208218

219+
/// <summary>
220+
/// Formats a chat message for display.
221+
/// </summary>
222+
/// <param name="username">The username of the user that sent the message.</param>
223+
/// <param name="message">The chat message sent by the user.</param>
224+
/// <param name="timestamp">The timestamp of the message, in UTC.</param>
225+
/// <param name="channel">The channel the message was sent in, if you want it included.</param>
226+
/// <returns>The formatted message.</returns>
227+
private string FormatChatMessage(string username, string message, DateTime timestamp, string? channel = null) {
228+
return null == channel ? $"[{timestamp:MM/dd/yy H:mm:ss}] {username}: {message}" : $"[{timestamp:MM/dd/yy H:mm:ss}] ({channel}) {username}: {message}";
229+
}
230+
209231
/// <summary>
210232
/// Handles registering for twitch chat messages while the UI is open.
211233
/// </summary>
212234
public override void OnLoaded() {
213235
base.OnLoaded();
214236

237+
// Connect to the twitch chats from the configuration.
238+
InitializeTwitchChatConnections();
239+
240+
// Loads the
241+
PopulateChatHistory();
242+
}
243+
244+
/// <summary>
245+
/// Populates the UI with the historic list of chat messages.
246+
/// </summary>
247+
private void PopulateChatHistory() {
248+
// Get the history of messages
249+
IEnumerable<TwitchChatMessage> messages = _twitchChatLog.GetMessages();
250+
251+
// Convert them into a string
252+
var sb = new StringBuilder();
253+
sb.AppendJoin('\n', messages.Select(l => FormatChatMessage(l.Username, l.Message, l.Timestamp, l.Channel)));
254+
sb.Append('\n');
255+
256+
// Update the UI
257+
TwitchChat = sb.ToString();
258+
}
259+
260+
/// <summary>
261+
/// Connects to the twitch chats in the configuration file.
262+
/// </summary>
263+
private void InitializeTwitchChatConnections() {
215264
foreach (TwitchChatConfiguration channel in _configuration.TwitchChats ?? []) {
216265
if (string.IsNullOrWhiteSpace(channel.TwitchChannel)) {
217266
continue;

0 commit comments

Comments
 (0)