aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Events/Types
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2024-10-26 18:32:35 -0500
committerGitHub <noreply@github.com>2024-10-26 18:32:35 -0500
commite8238d19b3c7b29d2892caeafd66733b5622d1b2 (patch)
treee8ced369b7631ef46d3043118aac5518dca76d35 /src/slskd/Events/Types
parent54590c78f2102634d982698608784e8d3c9043e0 (diff)
parent334627cd06476df897cf3f3ec02ca70b51b7fec1 (diff)
downloadslskd-e8238d19b3c7b29d2892caeafd66733b5622d1b2.tar.gz
Merge pull request #1232 from slskd/scripts
Add the ability to run user-defined scripts when application events are raised
Diffstat (limited to 'src/slskd/Events/Types')
-rw-r--r--src/slskd/Events/Types/EventRecord.cs9
-rw-r--r--src/slskd/Events/Types/Events.cs11
2 files changed, 13 insertions, 7 deletions
diff --git a/src/slskd/Events/Types/EventRecord.cs b/src/slskd/Events/Types/EventRecord.cs
index 11825c09..4323a42b 100644
--- a/src/slskd/Events/Types/EventRecord.cs
+++ b/src/slskd/Events/Types/EventRecord.cs
@@ -19,7 +19,6 @@ 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;
@@ -31,7 +30,7 @@ using Microsoft.EntityFrameworkCore;
public record EventRecord
{
public DateTime Timestamp { get; init; } = DateTime.UtcNow;
- public string Type { get; private set; }
+ public EventType Type { get; private set; }
public string Data { get; init; }
[Key]
public Guid Id { get; private set; }
@@ -61,10 +60,6 @@ public record EventRecord
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();
@@ -76,7 +71,7 @@ public record EventRecord
return new EventRecord
{
Timestamp = e.Timestamp,
- Type = type,
+ Type = e.Type,
Id = e.Id,
Data = data.ToJsonString(JsonSerializerOptions),
};
diff --git a/src/slskd/Events/Types/Events.cs b/src/slskd/Events/Types/Events.cs
index 20ec3f9f..9a3a71ed 100644
--- a/src/slskd/Events/Types/Events.cs
+++ b/src/slskd/Events/Types/Events.cs
@@ -24,10 +24,20 @@ public abstract record Event
{
public Guid Id { get; } = Guid.NewGuid();
public DateTime Timestamp { get; init; } = DateTime.UtcNow;
+ public abstract EventType Type { get; }
+}
+
+public enum EventType
+{
+ None = 0,
+ Any = 1,
+ DownloadFileComplete = 2,
+ DownloadDirectoryComplete = 3,
}
public sealed record DownloadFileCompleteEvent : Event
{
+ public override EventType Type => EventType.DownloadFileComplete;
public string LocalFilename { get; init; }
public string RemoteFilename { get; init; }
public Transfer Transfer { get; init; }
@@ -35,6 +45,7 @@ public sealed record DownloadFileCompleteEvent : Event
public sealed record DownloadDirectoryCompleteEvent : Event
{
+ public override EventType Type => EventType.DownloadDirectoryComplete;
public string LocalDirectoryName { get; init; }
public string RemoteDirectoryName { get; init; }
public string Username { get; init; }