From 8f7b1c17d0dd0832cd38c782a4c9804ac0838145 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 23 May 2021 11:31:10 -0500 Subject: move app stuff into Core --- src/slskd/Core/Messaging/ConversationTracker.cs | 62 ++++++++++++ src/slskd/Core/Messaging/Domain/PrivateMessage.cs | 71 ++++++++++++++ src/slskd/Core/Messaging/Domain/Room.cs | 75 +++++++++++++++ src/slskd/Core/Messaging/Domain/RoomMessage.cs | 59 ++++++++++++ src/slskd/Core/Messaging/IConversationTracker.cs | 55 +++++++++++ src/slskd/Core/Messaging/IRoomTracker.cs | 75 +++++++++++++++ src/slskd/Core/Messaging/RoomTracker.cs | 112 ++++++++++++++++++++++ 7 files changed, 509 insertions(+) create mode 100644 src/slskd/Core/Messaging/ConversationTracker.cs create mode 100644 src/slskd/Core/Messaging/Domain/PrivateMessage.cs create mode 100644 src/slskd/Core/Messaging/Domain/Room.cs create mode 100644 src/slskd/Core/Messaging/Domain/RoomMessage.cs create mode 100644 src/slskd/Core/Messaging/IConversationTracker.cs create mode 100644 src/slskd/Core/Messaging/IRoomTracker.cs create mode 100644 src/slskd/Core/Messaging/RoomTracker.cs (limited to 'src/slskd/Core/Messaging') 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 (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/. +// + +namespace slskd.Messaging +{ + using System.Collections.Concurrent; + using System.Collections.Generic; + + /// + /// Tracks private message conversations. + /// + public class ConversationTracker : IConversationTracker + { + /// + /// Tracked private message conversations. + /// + public ConcurrentDictionary> Conversations { get; } = new ConcurrentDictionary>(); + + /// + /// Adds a private message conversation and appends the specified , or just appends the + /// message if the conversation exists. + /// + /// + /// + public void AddOrUpdate(string username, PrivateMessage message) + { + Conversations.AddOrUpdate(username, new List() { message }, (_, messageList) => + { + messageList.Add(message); + return messageList; + }); + } + + /// + /// Returns the list of private messages for the specified , if any exist. + /// + /// + /// + /// + public bool TryGet(string username, out IList messages) => Conversations.TryGetValue(username, out messages); + + /// + /// Removes a tracked private message conversation for the specified user. + /// + /// + 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 (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/. +// + +namespace slskd.Messaging +{ + using System; + using Soulseek; + + /// + /// A private message. + /// + public class PrivateMessage + { + /// + /// A value indicating whether the message has been acknowledged. + /// + public bool Acknowledged { get; set; } = false; + + /// + /// The unique message id, used to acknowledge receipt. + /// + public int Id { get; set; } + + /// + /// The message. + /// + public string Message { get; set; } + + /// + /// A value indicating whether the message was replayed. + /// + public bool Replayed { get; set; } + + /// + /// The UTC timestamp of the message. + /// + public DateTime Timestamp { get; set; } = DateTime.UtcNow; + + /// + /// The username of the user who sent the message. + /// + 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 (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/. +// + +namespace slskd.Messaging +{ + using System.Collections.Generic; + using System.Linq; + using Soulseek; + + public class Room + { + /// + /// The room name. + /// + public string Name { get; set; } + + /// + /// A value indicating whether the room is private. + /// + public bool IsPrivate { get; set; } + + /// + /// The number of operators in the room, if private. + /// + public int? OperatorCount { get; set; } + + /// + /// The operators in the room, if private. + /// + public IList Operators { get; set; } + + /// + /// The owner of the room, if private. + /// + public string Owner { get; set; } + + /// + /// The list of users in the room. + /// + public IList Users { get; set; } = new List(); + + /// + /// The list of messages. + /// + public IList Messages { get; set; } = new List(); + + 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(), + }; + } + } +} 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 (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/. +// + +namespace slskd.Messaging +{ + using System; + using Soulseek; + + /// + /// A message sent to a room. + /// + public class RoomMessage + { + /// + /// The timestamp of the message. + /// + public DateTime Timestamp { get; set; } = DateTime.UtcNow; + + /// + /// The username of the user who sent the message. + /// + public string Username { get; set; } + + /// + /// The message. + /// + public string Message { get; set; } + + /// + /// The room to which the message pertains. + /// + 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 (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/. +// + +namespace slskd.Messaging +{ + using System.Collections.Concurrent; + using System.Collections.Generic; + + /// + /// Tracks private message conversations. + /// + public interface IConversationTracker + { + /// + /// Tracked private message conversations. + /// + ConcurrentDictionary> Conversations { get; } + + /// + /// Adds a private message conversation and appends the specified , or just appends the + /// message if the conversation exists. + /// + /// + /// + void AddOrUpdate(string username, PrivateMessage message); + + /// + /// Returns the list of private messages for the specified , if any exist. + /// + /// + /// + /// + bool TryGet(string username, out IList messages); + + /// + /// Removes a tracked private message conversation for the specified user. + /// + /// + 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 (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/. +// + +namespace slskd.Messaging +{ + using System.Collections.Concurrent; + using Soulseek; + + /// + /// Tracks rooms. + /// + public interface IRoomTracker + { + /// + /// Gets tracked rooms. + /// + ConcurrentDictionary Rooms { get; } + + /// + /// Adds a room and appends the specified , or just appends the message if the room exists. + /// + /// + /// + void AddOrUpdateMessage(string roomName, RoomMessage message); + + /// + /// Adds the specified room to the tracker. + /// + /// + /// + void TryAdd(string roomName, Room room); + + /// + /// Adds the specified to the specified room. + /// + /// + /// + void TryAddUser(string roomName, UserData userData); + + /// + /// Returns the list of messages for the specified , if it is tracked. + /// + /// + /// + /// + bool TryGet(string roomName, out Room room); + + /// + /// Removes a tracked room. + /// + /// + void TryRemove(string roomName); + + /// + /// Removes the specified from the specified room. + /// + /// + /// + 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 (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/. +// + +namespace slskd.Messaging +{ + using System.Collections.Concurrent; + using System.Collections.Generic; + using System.Linq; + using Soulseek; + + /// + /// Tracks rooms. + /// + public class RoomTracker : IRoomTracker + { + /// + /// Initializes a new instance of the class. + /// + /// + public RoomTracker(int messageLimit = 25) + { + MessageLimit = messageLimit; + } + + /// + /// Tracked rooms. + /// + public ConcurrentDictionary Rooms { get; } = new ConcurrentDictionary(); + + private int MessageLimit { get; } + + /// + /// Adds a room and appends the specified , or just appends the message if the room exists. + /// + /// + /// + public void AddOrUpdateMessage(string roomName, RoomMessage message) + { + Rooms.AddOrUpdate(roomName, new Room() { Messages = new List() { message } }, (_, room) => + { + if (room.Messages.Count >= MessageLimit) + { + room.Messages = room.Messages.TakeLast(MessageLimit - 1).ToList(); + } + + room.Messages.Add(message); + return room; + }); + } + + /// + /// Adds the specified room to the tracker. + /// + /// + /// + public void TryAdd(string roomName, Room room) => Rooms.TryAdd(roomName, room); + + /// + /// Adds the specified to the specified room. + /// + /// + /// + public void TryAddUser(string roomName, UserData userData) + { + if (Rooms.TryGetValue(roomName, out var room)) + { + room.Users.Add(userData); + } + } + + /// + /// Removes a tracked room. + /// + /// + /// + /// + public bool TryGet(string roomName, out Room room) => Rooms.TryGetValue(roomName, out room); + + /// + /// Returns the list of messages for the specified , if it is tracked. + /// + /// + public void TryRemove(string roomName) => Rooms.TryRemove(roomName, out _); + + /// + /// Removes the specified from the specified room. + /// + /// + /// + 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 -- cgit v1.2.3