From 51836f520fe400a33ad9daab932f13c2ed24467d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 2 Sep 2024 18:34:20 -0500 Subject: rename types --- src/slskd/Events/EventBus.cs | 4 +- src/slskd/Events/EventService.cs | 12 ++--- src/slskd/Events/Types/Event.cs | 84 +++++++++++++++++++++++++++++++++++ src/slskd/Events/Types/EventRecord.cs | 84 ----------------------------------- src/slskd/Events/Types/Events.cs | 6 +-- 5 files changed, 95 insertions(+), 95 deletions(-) create mode 100644 src/slskd/Events/Types/Event.cs delete mode 100644 src/slskd/Events/Types/EventRecord.cs (limited to 'src') diff --git a/src/slskd/Events/EventBus.cs b/src/slskd/Events/EventBus.cs index bc05e1b8..c3e418e7 100644 --- a/src/slskd/Events/EventBus.cs +++ b/src/slskd/Events/EventBus.cs @@ -71,12 +71,12 @@ public class EventBus /// The event data. /// The Type of the event. public virtual void Raise(T data) - where T : Event + where T : BaseEvent { Log.Debug("Handling {Type}: {Data}", typeof(T), data); // save the event to the database before broadcasting to consumers - Events.Add(EventRecord.From(data)); + Events.Add(Event.From(data)); // broadcast the event in a fire-and-forget fashion // we don't need to wait for anything, just need to kick off the tasks diff --git a/src/slskd/Events/EventService.cs b/src/slskd/Events/EventService.cs index c10f0d32..759dc8cf 100644 --- a/src/slskd/Events/EventService.cs +++ b/src/slskd/Events/EventService.cs @@ -40,21 +40,21 @@ public class EventService private ILogger Log { get; } = Serilog.Log.ForContext(); /// - /// Adds the specified event . + /// Adds the specified event . /// - /// The record to add. + /// The record to add. /// Thrown when the specified record is null. - public virtual void Add(EventRecord record) + public virtual void Add(Event @event) { - if (record is null) + if (@event is null) { - throw new ArgumentNullException(nameof(record)); + throw new ArgumentNullException(nameof(@event)); } try { using var context = ContextFactory.CreateDbContext(); - context.Add(record); + context.Add(@event); context.SaveChanges(); } catch (Exception ex) diff --git a/src/slskd/Events/Types/Event.cs b/src/slskd/Events/Types/Event.cs new file mode 100644 index 00000000..5b57aa5d --- /dev/null +++ b/src/slskd/Events/Types/Event.cs @@ -0,0 +1,84 @@ +// +// 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.Events; + +using System; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text.Encodings.Web; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; +using Microsoft.EntityFrameworkCore; + +[Index(nameof(Timestamp))] +[Index(nameof(Type))] +public record Event +{ + public DateTime Timestamp { get; init; } = DateTime.UtcNow; + public string Type { get; private set; } + public string Data { get; init; } + [Key] + public Guid Id { get; private set; } + + private static JsonSerializerOptions JsonSerializerOptions { get; } = new JsonSerializerOptions() + { + PropertyNameCaseInsensitive = true, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + WriteIndented = false, + }; + + /// + /// Converts the specified event into an instance of + /// for the purpose of database storage. + /// + /// + /// The property contains the event-specific data serialized as json, applying + /// standard formatting such as camel cased property names and null value omission. This is important so that + /// the values stored in the database will match the values exposed through other means. Maybe not *that* important, + /// but standardizing json is a good practice. + /// + /// The Event to convert. + /// The specific type of the Event. + /// The converted EventRecord. + public static Event From(BaseEvent e) + where T : BaseEvent + { + // this will be 'slskd.NameOfEvent'; we want to chop off 'slskd.' and 'Event' = 'NameOf' + var type = e.GetType().Name.Split('.').TakeLast(1).First(); + type = type.Substring(0, type.Length - "Event".Length); // chop off "Event" suffix + + // construct the data for the record by serializing the event and removing redundant properties + var json = JsonSerializer.Serialize(e as T, JsonSerializerOptions); + var data = JsonNode.Parse(json).AsObject(); + data.Remove(nameof(Timestamp).ToLower()); + data.Remove(nameof(Type).ToLower()); + data.Remove(nameof(Id).ToLower()); + data.Remove(nameof(Data).ToLower()); + + return new Event + { + Timestamp = e.Timestamp, + Type = type, + Id = e.Id, + Data = data.ToJsonString(JsonSerializerOptions), + }; + } +} \ No newline at end of file diff --git a/src/slskd/Events/Types/EventRecord.cs b/src/slskd/Events/Types/EventRecord.cs deleted file mode 100644 index 8ca0ff4d..00000000 --- a/src/slskd/Events/Types/EventRecord.cs +++ /dev/null @@ -1,84 +0,0 @@ -// -// 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.Events; - -using System; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Text.Encodings.Web; -using System.Text.Json; -using System.Text.Json.Nodes; -using System.Text.Json.Serialization; -using Microsoft.EntityFrameworkCore; - -[Index(nameof(Timestamp))] -[Index(nameof(Type))] -public record EventRecord -{ - public DateTime Timestamp { get; init; } = DateTime.UtcNow; - public string Type { get; private set; } - public string Data { get; init; } - [Key] - public Guid Id { get; private set; } - - private static JsonSerializerOptions JsonSerializerOptions { get; } = new JsonSerializerOptions() - { - PropertyNameCaseInsensitive = true, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, - WriteIndented = false, - }; - - /// - /// Converts the specified event into an instance of - /// for the purpose of database storage. - /// - /// - /// The property contains the event-specific data serialized as json, applying - /// standard formatting such as camel cased property names and null value omission. This is important so that - /// the values stored in the database will match the values exposed through other means. Maybe not *that* important, - /// but standardizing json is a good practice. - /// - /// The Event to convert. - /// The specific type of the Event. - /// The converted EventRecord. - public static EventRecord From(Event e) - where T : Event - { - // this will be 'slskd.NameOfEvent'; we want to chop off 'slskd.' and 'Event' = 'NameOf' - var type = e.GetType().Name.Split('.').TakeLast(1).First(); - type = type.Substring(0, type.Length - nameof(Event).Length); - - // construct the data for the record by serializing the event and removing redundant properties - var json = JsonSerializer.Serialize(e as T, JsonSerializerOptions); - var data = JsonNode.Parse(json).AsObject(); - data.Remove(nameof(Timestamp).ToLower()); - data.Remove(nameof(Type).ToLower()); - data.Remove(nameof(Id).ToLower()); - data.Remove(nameof(Data).ToLower()); - - return new EventRecord - { - Timestamp = e.Timestamp, - Type = type, - Id = e.Id, - Data = data.ToJsonString(JsonSerializerOptions), - }; - } -} \ No newline at end of file diff --git a/src/slskd/Events/Types/Events.cs b/src/slskd/Events/Types/Events.cs index bcfdb8d3..416074e6 100644 --- a/src/slskd/Events/Types/Events.cs +++ b/src/slskd/Events/Types/Events.cs @@ -20,20 +20,20 @@ namespace slskd.Events; using System; using slskd.Transfers; -public record Event +public abstract record BaseEvent { public Guid Id { get; } = Guid.NewGuid(); public DateTime Timestamp { get; init; } = DateTime.UtcNow; } -public sealed record DownloadFileCompleteEvent : Event +public sealed record DownloadFileCompleteEvent : BaseEvent { public string LocalFilename { get; init; } public string RemoteFilename { get; init; } public Transfer Transfer { get; init; } } -public sealed record DownloadDirectoryCompleteEvent : Event +public sealed record DownloadDirectoryCompleteEvent : BaseEvent { public string LocalDirectoryName { get; init; } public string RemoteDirectoryName { get; init; } -- cgit v1.2.3