aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Core/Messaging
diff options
context:
space:
mode:
Diffstat (limited to 'src/slskd/Core/Messaging')
-rw-r--r--src/slskd/Core/Messaging/ConversationTracker.cs62
-rw-r--r--src/slskd/Core/Messaging/Domain/PrivateMessage.cs71
-rw-r--r--src/slskd/Core/Messaging/Domain/Room.cs75
-rw-r--r--src/slskd/Core/Messaging/Domain/RoomMessage.cs59
-rw-r--r--src/slskd/Core/Messaging/IConversationTracker.cs55
-rw-r--r--src/slskd/Core/Messaging/IRoomTracker.cs75
-rw-r--r--src/slskd/Core/Messaging/RoomTracker.cs112
7 files changed, 509 insertions, 0 deletions
diff --git a/src/slskd/Core/Messaging/ConversationTracker.cs b/src/slskd/Core/Messaging/ConversationTracker.cs
new file mode 100644
index 00000000..27d678ca
--- /dev/null
+++ b/src/slskd/Core/Messaging/ConversationTracker.cs
@@ -0,0 +1,62 @@
+// <copyright file="ConversationTracker.cs" company="slskd Team">
+// Copyright (c) slskd Team. All rights reserved.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see https://www.gnu.org/licenses/.
+// </copyright>
+
+namespace slskd.Messaging
+{
+ using System.Collections.Concurrent;
+ using System.Collections.Generic;
+
+ /// <summary>
+ /// Tracks private message conversations.
+ /// </summary>
+ public class ConversationTracker : IConversationTracker
+ {
+ /// <summary>
+ /// Tracked private message conversations.
+ /// </summary>
+ public ConcurrentDictionary<string, IList<PrivateMessage>> Conversations { get; } = new ConcurrentDictionary<string, IList<PrivateMessage>>();
+
+ /// <summary>
+ /// Adds a private message conversation and appends the specified <paramref name="message"/>, or just appends the
+ /// message if the conversation exists.
+ /// </summary>
+ /// <param name="username"></param>
+ /// <param name="message"></param>
+ public void AddOrUpdate(string username, PrivateMessage message)
+ {
+ Conversations.AddOrUpdate(username, new List<PrivateMessage>() { message }, (_, messageList) =>
+ {
+ messageList.Add(message);
+ return messageList;
+ });
+ }
+
+ /// <summary>
+ /// Returns the list of private messages for the specified <paramref name="username"/>, if any exist.
+ /// </summary>
+ /// <param name="username"></param>
+ /// <param name="messages"></param>
+ /// <returns></returns>
+ public bool TryGet(string username, out IList<PrivateMessage> messages) => Conversations.TryGetValue(username, out messages);
+
+ /// <summary>
+ /// Removes a tracked private message conversation for the specified user.
+ /// </summary>
+ /// <param name="username"></param>
+ public void TryRemove(string username) => Conversations.TryRemove(username, out _);
+ }
+} \ No newline at end of file
diff --git a/src/slskd/Core/Messaging/Domain/PrivateMessage.cs b/src/slskd/Core/Messaging/Domain/PrivateMessage.cs
new file mode 100644
index 00000000..236632aa
--- /dev/null
+++ b/src/slskd/Core/Messaging/Domain/PrivateMessage.cs
@@ -0,0 +1,71 @@
+// <copyright file="PrivateMessage.cs" company="slskd Team">
+// Copyright (c) slskd Team. All rights reserved.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see https://www.gnu.org/licenses/.
+// </copyright>
+
+namespace slskd.Messaging
+{
+ using System;
+ using Soulseek;
+
+ /// <summary>
+ /// A private message.
+ /// </summary>
+ public class PrivateMessage
+ {
+ /// <summary>
+ /// A value indicating whether the message has been acknowledged.
+ /// </summary>
+ public bool Acknowledged { get; set; } = false;
+
+ /// <summary>
+ /// The unique message id, used to acknowledge receipt.
+ /// </summary>
+ public int Id { get; set; }
+
+ /// <summary>
+ /// The message.
+ /// </summary>
+ public string Message { get; set; }
+
+ /// <summary>
+ /// A value indicating whether the message was replayed.
+ /// </summary>
+ public bool Replayed { get; set; }
+
+ /// <summary>
+ /// The UTC timestamp of the message.
+ /// </summary>
+ public DateTime Timestamp { get; set; } = DateTime.UtcNow;
+
+ /// <summary>
+ /// The username of the user who sent the message.
+ /// </summary>
+ public string Username { get; set; }
+
+ public static PrivateMessage FromEventArgs(PrivateMessageReceivedEventArgs eventArgs)
+ {
+ return new PrivateMessage()
+ {
+ Id = eventArgs.Id,
+ Timestamp = eventArgs.Timestamp,
+ Username = eventArgs.Username,
+ Message = eventArgs.Message,
+ Acknowledged = false,
+ Replayed = eventArgs.Replayed,
+ };
+ }
+ }
+} \ No newline at end of file
diff --git a/src/slskd/Core/Messaging/Domain/Room.cs b/src/slskd/Core/Messaging/Domain/Room.cs
new file mode 100644
index 00000000..459dea2c
--- /dev/null
+++ b/src/slskd/Core/Messaging/Domain/Room.cs
@@ -0,0 +1,75 @@
+// <copyright file="Room.cs" company="slskd Team">
+// Copyright (c) slskd Team. All rights reserved.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see https://www.gnu.org/licenses/.
+// </copyright>
+
+namespace slskd.Messaging
+{
+ using System.Collections.Generic;
+ using System.Linq;
+ using Soulseek;
+
+ public class Room
+ {
+ /// <summary>
+ /// The room name.
+ /// </summary>
+ public string Name { get; set; }
+
+ /// <summary>
+ /// A value indicating whether the room is private.
+ /// </summary>
+ public bool IsPrivate { get; set; }
+
+ /// <summary>
+ /// The number of operators in the room, if private.
+ /// </summary>
+ public int? OperatorCount { get; set; }
+
+ /// <summary>
+ /// The operators in the room, if private.
+ /// </summary>
+ public IList<string> Operators { get; set; }
+
+ /// <summary>
+ /// The owner of the room, if private.
+ /// </summary>
+ public string Owner { get; set; }
+
+ /// <summary>
+ /// The list of users in the room.
+ /// </summary>
+ public IList<UserData> Users { get; set; } = new List<UserData>();
+
+ /// <summary>
+ /// The list of messages.
+ /// </summary>
+ public IList<RoomMessage> Messages { get; set; } = new List<RoomMessage>();
+
+ public static Room FromRoomData(RoomData roomData)
+ {
+ return new Room()
+ {
+ Name = roomData.Name,
+ IsPrivate = roomData.IsPrivate,
+ OperatorCount = roomData.OperatorCount,
+ Operators = roomData.Operators?.ToList(),
+ Owner = roomData.Owner,
+ Users = roomData.Users?.ToList(),
+ Messages = new List<RoomMessage>(),
+ };
+ }
+ }
+}
diff --git a/src/slskd/Core/Messaging/Domain/RoomMessage.cs b/src/slskd/Core/Messaging/Domain/RoomMessage.cs
new file mode 100644
index 00000000..7a06a192
--- /dev/null
+++ b/src/slskd/Core/Messaging/Domain/RoomMessage.cs
@@ -0,0 +1,59 @@
+// <copyright file="RoomMessage.cs" company="slskd Team">
+// Copyright (c) slskd Team. All rights reserved.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see https://www.gnu.org/licenses/.
+// </copyright>
+
+namespace slskd.Messaging
+{
+ using System;
+ using Soulseek;
+
+ /// <summary>
+ /// A message sent to a room.
+ /// </summary>
+ public class RoomMessage
+ {
+ /// <summary>
+ /// The timestamp of the message.
+ /// </summary>
+ public DateTime Timestamp { get; set; } = DateTime.UtcNow;
+
+ /// <summary>
+ /// The username of the user who sent the message.
+ /// </summary>
+ public string Username { get; set; }
+
+ /// <summary>
+ /// The message.
+ /// </summary>
+ public string Message { get; set; }
+
+ /// <summary>
+ /// The room to which the message pertains.
+ /// </summary>
+ public string RoomName { get; set; }
+
+ public static RoomMessage FromEventArgs(RoomMessageReceivedEventArgs eventArgs, DateTime? timestamp = null)
+ {
+ return new RoomMessage()
+ {
+ Timestamp = timestamp ?? DateTime.UtcNow,
+ Username = eventArgs.Username,
+ Message = eventArgs.Message,
+ RoomName = eventArgs.RoomName,
+ };
+ }
+ }
+}
diff --git a/src/slskd/Core/Messaging/IConversationTracker.cs b/src/slskd/Core/Messaging/IConversationTracker.cs
new file mode 100644
index 00000000..8e7a90f4
--- /dev/null
+++ b/src/slskd/Core/Messaging/IConversationTracker.cs
@@ -0,0 +1,55 @@
+// <copyright file="IConversationTracker.cs" company="slskd Team">
+// Copyright (c) slskd Team. All rights reserved.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see https://www.gnu.org/licenses/.
+// </copyright>
+
+namespace slskd.Messaging
+{
+ using System.Collections.Concurrent;
+ using System.Collections.Generic;
+
+ /// <summary>
+ /// Tracks private message conversations.
+ /// </summary>
+ public interface IConversationTracker
+ {
+ /// <summary>
+ /// Tracked private message conversations.
+ /// </summary>
+ ConcurrentDictionary<string, IList<PrivateMessage>> Conversations { get; }
+
+ /// <summary>
+ /// Adds a private message conversation and appends the specified <paramref name="message"/>, or just appends the
+ /// message if the conversation exists.
+ /// </summary>
+ /// <param name="username"></param>
+ /// <param name="message"></param>
+ void AddOrUpdate(string username, PrivateMessage message);
+
+ /// <summary>
+ /// Returns the list of private messages for the specified <paramref name="username"/>, if any exist.
+ /// </summary>
+ /// <param name="username"></param>
+ /// <param name="messages"></param>
+ /// <returns></returns>
+ bool TryGet(string username, out IList<PrivateMessage> messages);
+
+ /// <summary>
+ /// Removes a tracked private message conversation for the specified user.
+ /// </summary>
+ /// <param name="username"></param>
+ void TryRemove(string username);
+ }
+} \ No newline at end of file
diff --git a/src/slskd/Core/Messaging/IRoomTracker.cs b/src/slskd/Core/Messaging/IRoomTracker.cs
new file mode 100644
index 00000000..225e5f69
--- /dev/null
+++ b/src/slskd/Core/Messaging/IRoomTracker.cs
@@ -0,0 +1,75 @@
+// <copyright file="IRoomTracker.cs" company="slskd Team">
+// Copyright (c) slskd Team. All rights reserved.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see https://www.gnu.org/licenses/.
+// </copyright>
+
+namespace slskd.Messaging
+{
+ using System.Collections.Concurrent;
+ using Soulseek;
+
+ /// <summary>
+ /// Tracks rooms.
+ /// </summary>
+ public interface IRoomTracker
+ {
+ /// <summary>
+ /// Gets tracked rooms.
+ /// </summary>
+ ConcurrentDictionary<string, Room> Rooms { get; }
+
+ /// <summary>
+ /// Adds a room and appends the specified <paramref name="message"/>, or just appends the message if the room exists.
+ /// </summary>
+ /// <param name="roomName"></param>
+ /// <param name="message"></param>
+ void AddOrUpdateMessage(string roomName, RoomMessage message);
+
+ /// <summary>
+ /// Adds the specified room to the tracker.
+ /// </summary>
+ /// <param name="roomName"></param>
+ /// <param name="room"></param>
+ void TryAdd(string roomName, Room room);
+
+ /// <summary>
+ /// Adds the specified <paramref name="userData"/> to the specified room.
+ /// </summary>
+ /// <param name="roomName"></param>
+ /// <param name="userData"></param>
+ void TryAddUser(string roomName, UserData userData);
+
+ /// <summary>
+ /// Returns the list of messages for the specified <paramref name="roomName"/>, if it is tracked.
+ /// </summary>
+ /// <param name="roomName"></param>
+ /// <param name="room"></param>
+ /// <returns></returns>
+ bool TryGet(string roomName, out Room room);
+
+ /// <summary>
+ /// Removes a tracked room.
+ /// </summary>
+ /// <param name="roomName"></param>
+ void TryRemove(string roomName);
+
+ /// <summary>
+ /// Removes the specified <paramref name="username"/> from the specified room.
+ /// </summary>
+ /// <param name="roomName"></param>
+ /// <param name="username"></param>
+ void TryRemoveUser(string roomName, string username);
+ }
+} \ No newline at end of file
diff --git a/src/slskd/Core/Messaging/RoomTracker.cs b/src/slskd/Core/Messaging/RoomTracker.cs
new file mode 100644
index 00000000..c586f401
--- /dev/null
+++ b/src/slskd/Core/Messaging/RoomTracker.cs
@@ -0,0 +1,112 @@
+// <copyright file="RoomTracker.cs" company="slskd Team">
+// Copyright (c) slskd Team. All rights reserved.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see https://www.gnu.org/licenses/.
+// </copyright>
+
+namespace slskd.Messaging
+{
+ using System.Collections.Concurrent;
+ using System.Collections.Generic;
+ using System.Linq;
+ using Soulseek;
+
+ /// <summary>
+ /// Tracks rooms.
+ /// </summary>
+ public class RoomTracker : IRoomTracker
+ {
+ /// <summary>
+ /// Initializes a new instance of the <see cref="RoomTracker"/> class.
+ /// </summary>
+ /// <param name="messageLimit"></param>
+ public RoomTracker(int messageLimit = 25)
+ {
+ MessageLimit = messageLimit;
+ }
+
+ /// <summary>
+ /// Tracked rooms.
+ /// </summary>
+ public ConcurrentDictionary<string, Room> Rooms { get; } = new ConcurrentDictionary<string, Room>();
+
+ private int MessageLimit { get; }
+
+ /// <summary>
+ /// Adds a room and appends the specified <paramref name="message"/>, or just appends the message if the room exists.
+ /// </summary>
+ /// <param name="roomName"></param>
+ /// <param name="message"></param>
+ public void AddOrUpdateMessage(string roomName, RoomMessage message)
+ {
+ Rooms.AddOrUpdate(roomName, new Room() { Messages = new List<RoomMessage>() { message } }, (_, room) =>
+ {
+ if (room.Messages.Count >= MessageLimit)
+ {
+ room.Messages = room.Messages.TakeLast(MessageLimit - 1).ToList();
+ }
+
+ room.Messages.Add(message);
+ return room;
+ });
+ }
+
+ /// <summary>
+ /// Adds the specified room to the tracker.
+ /// </summary>
+ /// <param name="roomName"></param>
+ /// <param name="room"></param>
+ public void TryAdd(string roomName, Room room) => Rooms.TryAdd(roomName, room);
+
+ /// <summary>
+ /// Adds the specified <paramref name="userData"/> to the specified room.
+ /// </summary>
+ /// <param name="roomName"></param>
+ /// <param name="userData"></param>
+ public void TryAddUser(string roomName, UserData userData)
+ {
+ if (Rooms.TryGetValue(roomName, out var room))
+ {
+ room.Users.Add(userData);
+ }
+ }
+
+ /// <summary>
+ /// Removes a tracked room.
+ /// </summary>
+ /// <param name="roomName"></param>
+ /// <param name="room"></param>
+ /// <returns></returns>
+ public bool TryGet(string roomName, out Room room) => Rooms.TryGetValue(roomName, out room);
+
+ /// <summary>
+ /// Returns the list of messages for the specified <paramref name="roomName"/>, if it is tracked.
+ /// </summary>
+ /// <param name="roomName"></param>
+ public void TryRemove(string roomName) => Rooms.TryRemove(roomName, out _);
+
+ /// <summary>
+ /// Removes the specified <paramref name="username"/> from the specified room.
+ /// </summary>
+ /// <param name="roomName"></param>
+ /// <param name="username"></param>
+ public void TryRemoveUser(string roomName, string username)
+ {
+ if (Rooms.TryGetValue(roomName, out var room))
+ {
+ room.Users = room.Users.Where(u => u.Username != username).ToList();
+ }
+ }
+ }
+} \ No newline at end of file