diff options
| author | JP Dillingham <jp@dillingham.me> | 2024-10-06 18:06:22 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-06 18:06:22 -0500 |
| commit | bac18981efba4daa4e4668dd382b7fbed8765636 (patch) | |
| tree | 86307843bd193b5eb5750e254e231dc3751d1f40 /src/slskd/Events/Types | |
| parent | 3fb1176c9788d2de5527df282702aefb0497a0ec (diff) | |
| parent | 5dea82dbd647cdc28c89a1d37e168d3a47abeea7 (diff) | |
| download | slskd-bac18981efba4daa4e4668dd382b7fbed8765636.tar.gz | |
Merge pull request #1215 from slskd/tasks
Add 'Events'
Diffstat (limited to 'src/slskd/Events/Types')
| -rw-r--r-- | src/slskd/Events/Types/EventRecord.cs | 84 | ||||
| -rw-r--r-- | src/slskd/Events/Types/Events.cs | 41 |
2 files changed, 125 insertions, 0 deletions
diff --git a/src/slskd/Events/Types/EventRecord.cs b/src/slskd/Events/Types/EventRecord.cs new file mode 100644 index 00000000..11825c09 --- /dev/null +++ b/src/slskd/Events/Types/EventRecord.cs @@ -0,0 +1,84 @@ +// <copyright file="EventRecord.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.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, + }; + + /// <summary> + /// Converts the specified event <paramref name="e"/> into an instance of <see cref="EventRecord"/> + /// for the purpose of database storage. + /// </summary> + /// <remarks> + /// The <see cref="Data"/> 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. + /// </remarks> + /// <param name="e">The Event to convert.</param> + /// <typeparam name="T">The specific type of the Event.</typeparam> + /// <returns>The converted EventRecord.</returns> + public static EventRecord From<T>(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 - "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 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 new file mode 100644 index 00000000..20ec3f9f --- /dev/null +++ b/src/slskd/Events/Types/Events.cs @@ -0,0 +1,41 @@ +// <copyright file="Events.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.Events; + +using System; +using slskd.Transfers; + +public abstract record Event +{ + public Guid Id { get; } = Guid.NewGuid(); + public DateTime Timestamp { get; init; } = DateTime.UtcNow; +} + +public sealed record DownloadFileCompleteEvent : Event +{ + public string LocalFilename { get; init; } + public string RemoteFilename { get; init; } + public Transfer Transfer { get; init; } +} + +public sealed record DownloadDirectoryCompleteEvent : Event +{ + public string LocalDirectoryName { get; init; } + public string RemoteDirectoryName { get; init; } + public string Username { get; init; } +}
\ No newline at end of file |
