Moved UpdateSettings to TrangaSettings

Added NotificaitonManager
Added Gotify
Added Notification on MonitorTask download new chapters
This commit is contained in:
2023-06-15 18:25:32 +02:00
parent e789c429cd
commit 25c90782dc
8 changed files with 186 additions and 36 deletions

View File

@ -0,0 +1,49 @@
using Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Tranga.NotificationManagers;
namespace Tranga;
public abstract class NotificationManager
{
protected Logger? logger;
public NotificationManagerType notificationManagerType { get; }
protected NotificationManager(NotificationManagerType notificationManagerType, Logger? logger = null)
{
this.notificationManagerType = notificationManagerType;
this.logger = logger;
}
public enum NotificationManagerType : byte { Gotify = 0 }
public abstract void SendNotification(string title, string notificationText);
public class NotificationManagerJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(NotificationManager));
}
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
if (jo["notificationManagerType"]!.Value<byte>() == (byte)NotificationManagerType.Gotify)
return jo.ToObject<Gotify>(serializer)!;
throw new Exception();
}
public override bool CanWrite => false;
/// <summary>
/// Don't call this
/// </summary>
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
throw new Exception("Dont call this");
}
}
}

View File

@ -0,0 +1,49 @@
using System.Text;
using Logging;
using Newtonsoft.Json;
namespace Tranga.NotificationManagers;
public class Gotify : NotificationManager
{
public string endpoint { get; }
public string appToken { get; }
private readonly HttpClient _client = new();
public Gotify(string endpoint, string appToken, Logger? logger = null) : base(NotificationManagerType.Gotify, logger)
{
this.endpoint = endpoint;
this.appToken = appToken;
}
public override void SendNotification(string title, string notificationText)
{
logger?.WriteLine(this.GetType().ToString(), $"Sending notification: {title} - {notificationText}");
MessageData message = new(title, notificationText);
HttpRequestMessage request = new(HttpMethod.Post, $"{endpoint}/message");
request.Headers.Add("X-Gotify-Key", this.appToken);
request.Content = new StringContent(JsonConvert.SerializeObject(message, Formatting.None), Encoding.UTF8, "application/json");
HttpResponseMessage response = _client.Send(request);
if (!response.IsSuccessStatusCode)
{
StreamReader sr = new (response.Content.ReadAsStream());
logger?.WriteLine(this.GetType().ToString(), $"{response.StatusCode}: {sr.ReadToEnd()}");
}
}
private class MessageData
{
public string message { get; }
public long priority { get; }
public string title { get; }
public Dictionary<string, object> extras { get; }
public MessageData(string title, string message)
{
this.title = title;
this.message = message;
this.extras = new();
this.priority = 2;
}
}
}

View File

@ -1,7 +1,6 @@
using Logging;
using Newtonsoft.Json;
using Tranga.Connectors;
using Tranga.LibraryManagers;
using Tranga.TrangaTasks;
namespace Tranga;
@ -25,8 +24,9 @@ public class TaskManager
/// <param name="workingDirectory">Path to the working directory</param>
/// <param name="imageCachePath">Path to the cover-image cache</param>
/// <param name="libraryManagers"></param>
/// <param name="notificationManagers"></param>
/// <param name="logger"></param>
public TaskManager(string downloadFolderPath, string workingDirectory, string imageCachePath, HashSet<LibraryManager> libraryManagers, Logger? logger = null) : this(new TrangaSettings(downloadFolderPath, workingDirectory, libraryManagers), logger)
public TaskManager(string downloadFolderPath, string workingDirectory, string imageCachePath, HashSet<LibraryManager> libraryManagers, HashSet<NotificationManager> notificationManagers, Logger? logger = null) : this(new TrangaSettings(downloadFolderPath, workingDirectory, libraryManagers, notificationManagers), logger)
{
}
@ -47,23 +47,6 @@ public class TaskManager
Thread taskChecker = new(TaskCheckerThread);
taskChecker.Start();
}
public void UpdateSettings(string? downloadLocation, string? komgaUrl, string? komgaAuth, string? kavitaUrl, string? kavitaUsername, string? kavitaPassword)
{
if (komgaUrl is not null && komgaAuth is not null && komgaUrl.Length > 0 && komgaAuth.Length > 0)
{
settings.libraryManagers.RemoveWhere(lm => lm.GetType() == typeof(Komga));
settings.libraryManagers.Add(new Komga(komgaUrl, komgaAuth, logger));
}
if (kavitaUrl is not null && kavitaUsername is not null && kavitaPassword is not null && kavitaUrl.Length > 0 && kavitaUsername.Length > 0 && kavitaPassword.Length > 0)
{
settings.libraryManagers.RemoveWhere(lm => lm.GetType() == typeof(Kavita));
settings.libraryManagers.Add(new Kavita(kavitaUrl, kavitaUsername, kavitaPassword, logger));
}
if (downloadLocation is not null && downloadLocation.Length > 0)
settings.downloadLocation = downloadLocation;
ExportDataAndSettings();
}
/// <summary>
/// Runs continuously until shutdown.

View File

@ -1,6 +1,7 @@
using Logging;
using Newtonsoft.Json;
using Tranga.LibraryManagers;
using Tranga.NotificationManagers;
namespace Tranga;
@ -8,32 +9,69 @@ public class TrangaSettings
{
public string downloadLocation { get; set; }
public string workingDirectory { get; set; }
[JsonIgnore]public string settingsFilePath => Path.Join(workingDirectory, "settings.json");
[JsonIgnore]public string tasksFilePath => Path.Join(workingDirectory, "tasks.json");
[JsonIgnore]public string knownPublicationsPath => Path.Join(workingDirectory, "knownPublications.json");
[JsonIgnore] public string settingsFilePath => Path.Join(workingDirectory, "settings.json");
[JsonIgnore] public string tasksFilePath => Path.Join(workingDirectory, "tasks.json");
[JsonIgnore] public string knownPublicationsPath => Path.Join(workingDirectory, "knownPublications.json");
[JsonIgnore] public string coverImageCache => Path.Join(workingDirectory, "imageCache");
public HashSet<LibraryManager> libraryManagers { get; }
public HashSet<NotificationManager> notificationManagers { get; }
public TrangaSettings(string downloadLocation, string workingDirectory, HashSet<LibraryManager> libraryManagers)
public TrangaSettings(string downloadLocation, string workingDirectory, HashSet<LibraryManager> libraryManagers,
HashSet<NotificationManager> notificationManagers)
{
if (downloadLocation.Length < 1 || workingDirectory.Length < 1)
throw new ArgumentException("Download-location and working-directory paths can not be empty!");
this.workingDirectory = workingDirectory;
this.downloadLocation = downloadLocation;
this.libraryManagers = libraryManagers;
this.notificationManagers = notificationManagers;
}
public static TrangaSettings LoadSettings(string importFilePath, Logger? logger)
{
if (!File.Exists(importFilePath))
return new TrangaSettings(Path.Join(Directory.GetCurrentDirectory(), "Downloads"), Directory.GetCurrentDirectory(), new HashSet<LibraryManager>());
return new TrangaSettings(Path.Join(Directory.GetCurrentDirectory(), "Downloads"),
Directory.GetCurrentDirectory(), new HashSet<LibraryManager>(), new HashSet<NotificationManager>());
string toRead = File.ReadAllText(importFilePath);
TrangaSettings settings = JsonConvert.DeserializeObject<TrangaSettings>(toRead, new JsonSerializerSettings() { Converters = { new LibraryManager.LibraryManagerJsonConverter()} })!;
if(logger is not null)
foreach(LibraryManager lm in settings.libraryManagers)
TrangaSettings settings = JsonConvert.DeserializeObject<TrangaSettings>(toRead,
new JsonSerializerSettings { Converters = { new NotificationManager.NotificationManagerJsonConverter(), new LibraryManager.LibraryManagerJsonConverter() } })!;
if (logger is not null)
foreach (LibraryManager lm in settings.libraryManagers)
lm.AddLogger(logger);
return settings;
}
public void UpdateSettings(UpdateField field, Logger? logger = null, params string[] values)
{
switch (field)
{
case UpdateField.DownloadLocation:
if (values.Length != 1)
return;
this.downloadLocation = values[0];
break;
case UpdateField.Komga:
if (values.Length != 2)
return;
libraryManagers.RemoveWhere(lm => lm.GetType() == typeof(Komga));
libraryManagers.Add(new Komga(values[0], values[1], logger));
break;
case UpdateField.Kavita:
if (values.Length != 3)
return;
libraryManagers.RemoveWhere(lm => lm.GetType() == typeof(Kavita));
libraryManagers.Add(new Kavita(values[0], values[1], values[2], logger));
break;
case UpdateField.Gotify:
if (values.Length != 2)
return;
notificationManagers.RemoveWhere(nm => nm.GetType() == typeof(Gotify));
notificationManagers.Add(new Gotify(values[0], values[1], logger));
break;
}
}
public enum UpdateField { DownloadLocation, Komga, Kavita, Gotify}
}

View File

@ -36,6 +36,9 @@ public class DownloadNewChaptersTask : TrangaTask
taskManager.AddTask(newTask);
this.childTasks.Add(newTask);
}
if(newChapters.Count > 0)
foreach(NotificationManager nm in taskManager.settings.notificationManagers)
nm.SendNotification(pub.sortName, $"Downloading {newChapters.Count} new Chapters.");
}
public override string ToString()