From ca80897a3c56eec9ea367bd6c2b7262e306d3685 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 17 Sep 2021 09:50:37 -0500 Subject: move auto join and room tracking to RoomService --- src/slskd/Application/Messaging/RoomService.cs | 165 +++++++++++++++++++++---- 1 file changed, 143 insertions(+), 22 deletions(-) (limited to 'src/slskd/Application/Messaging') diff --git a/src/slskd/Application/Messaging/RoomService.cs b/src/slskd/Application/Messaging/RoomService.cs index 89fa4446..fde62c83 100644 --- a/src/slskd/Application/Messaging/RoomService.cs +++ b/src/slskd/Application/Messaging/RoomService.cs @@ -1,81 +1,202 @@ -namespace slskd.Messaging +// +// 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/. +// + +using Microsoft.Extensions.Options; + +namespace slskd.Messaging { using System; + using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Serilog; using Soulseek; + /// + /// Chat room management and event handling. + /// public interface IRoomService { - Task JoinAsync(string name); - Task LeaveAsync(string name); + /// + /// Joins the specfied . + /// + /// The name of the room to join. + /// The operation context, including information about the room. + Task JoinAsync(string roomName); + + /// + /// Leaves the specified . + /// + /// The name of the room to leave. + /// The operation context. + Task LeaveAsync(string roomName); + + /// + /// Attempts to join the specified . + /// + /// + /// Failures are logged but not thrown. Use JoinAsync() to trap Exceptions. + /// + /// The list of room names to join. + /// The operation context. + Task TryJoinAsync(params string[] roomNames); } + /// + /// Chat room management and event handling. + /// public class RoomService : IRoomService { public RoomService( ISoulseekClient soulseekClient, - IStateMonitor stateMonitor, - IStateMutator stateMutator) + IOptionsMonitor optionsMonitor, + IStateMutator stateMutator, + IRoomTracker roomTracker) { Client = soulseekClient; - Monitor = stateMonitor; - Mutator = stateMutator; + StateMutator = stateMutator; + OptionsMonitor = optionsMonitor; + + RoomTracker = roomTracker; Client.RoomJoined += Client_RoomJoined; Client.RoomLeft += Client_RoomLeft; + Client.RoomMessageReceived += Client_RoomMessageReceived; + Client.LoggedIn += Client_LoggedIn; } private ISoulseekClient Client { get; } - private IStateMonitor Monitor { get; } - private IStateMutator Mutator { get; } private ILogger Logger { get; set; } = Log.ForContext(); + private IStateMutator StateMutator { get; } + private IOptionsMonitor OptionsMonitor { get; } + private IRoomTracker RoomTracker { get; set; } - public async Task JoinAsync(string name) + /// + /// Joins the specfied . + /// + /// The name of the room to join. + /// The operation context, including information about the room. + public async Task JoinAsync(string roomName) { - Logger.Debug("Joining room {Room}", name); + Logger.Debug("Joining room {Room}", roomName); try { - var data = await Client.JoinRoomAsync(name); + var data = await Client.JoinRoomAsync(roomName); - Mutator.SetValue(state => state with { Rooms = state.Rooms.Concat(new[] { name }).ToArray() }); + StateMutator.SetValue(state => state with { Rooms = state.Rooms.Concat(new[] { roomName }).Distinct().ToArray() }); - Logger.Information("Joined room {Room}", name); - Logger.Debug("Room data for {Room}: {Info}", name, data.ToJson()); + Logger.Information("Joined room {Room}", roomName); + Logger.Debug("Room data for {Room}: {Info}", roomName, data.ToJson()); return data; } catch (Exception ex) { - Logger.Warning("Failed to join room {Room}: {Message}", name, ex.Message); + Logger.Warning("Failed to join room {Room}: {Message}", roomName, ex.Message); throw; } } - public async Task LeaveAsync(string name) + /// + /// Leaves the specified . + /// + /// The name of the room to leave. + /// The operation context. + public async Task LeaveAsync(string roomName) { - Logger.Debug("Leaving room {Room}", name); + Logger.Debug("Leaving room {Room}", roomName); + try { - await Client.LeaveRoomAsync(name); + await Client.LeaveRoomAsync(roomName); - Mutator.SetValue(state => state with { Rooms = state.Rooms.Where(room => room != name).ToArray() }); + StateMutator.SetValue(state => state with { Rooms = state.Rooms.Where(room => room != roomName).ToArray() }); } catch (Exception ex) { - Logger.Warning("Failed to leave room {Room}: {Message}", name, ex.Message); + Logger.Warning("Failed to leave room {Room}: {Message}", roomName, ex.Message); throw; } } + /// + /// Attempts to join the specified . + /// + /// + /// Failures are logged but not thrown. Use JoinAsync() to trap Exceptions. + /// + /// The list of room names to join. + /// The operation context. + public async Task TryJoinAsync(params string[] roomNames) + { + var tasks = new List(); + + foreach (var room in roomNames) + { + tasks.Add(JoinAsync(room)); + } + + try + { + await Task.WhenAll(tasks); + } + catch (Exception ex) + { + Logger.Debug(ex, "Caught Exception in JoinAsync"); + } + } + + private async void Client_LoggedIn(object sender, EventArgs e) + { + var autoJoinRooms = OptionsMonitor.CurrentValue.Rooms; + + if (autoJoinRooms.Any()) + { + Logger.Information("Auto-joining room(s) {Rooms}", string.Join(", ", autoJoinRooms)); + await TryJoinAsync(autoJoinRooms); + } + + var previouslyJoinedRooms = RoomTracker.Rooms.Keys.Except(autoJoinRooms); + + if (previouslyJoinedRooms.Any()) + { + Logger.Information("Attempting to rejoin room(s) {Rooms}", string.Join(", ", previouslyJoinedRooms)); + await TryJoinAsync(previouslyJoinedRooms.ToArray()); + } + } + private void Client_RoomJoined(object sender, RoomJoinedEventArgs args) { + if (args.Username != Client.Username) + { + RoomTracker.TryAddUser(args.RoomName, args.UserData); + } } private void Client_RoomLeft(object sender, RoomLeftEventArgs args) { + RoomTracker.TryRemoveUser(args.RoomName, args.Username); + } + + private void Client_RoomMessageReceived(object sender, RoomMessageReceivedEventArgs args) + { + var message = RoomMessage.FromEventArgs(args, DateTime.UtcNow); + RoomTracker.AddOrUpdateMessage(args.RoomName, message); } } -} +} \ No newline at end of file -- cgit v1.2.3