Compare commits

...

4 Commits

8 changed files with 67 additions and 43 deletions

View File

@ -26,8 +26,8 @@ public abstract class GlobalBase
{ {
this.logger = logger; this.logger = logger;
this.settings = settings; this.settings = settings;
this.notificationConnectors = settings.LoadNotificationConnectors(); this.notificationConnectors = settings.LoadNotificationConnectors(this);
this.libraryConnectors = settings.LoadLibraryConnectors(); this.libraryConnectors = settings.LoadLibraryConnectors(this);
this.cachedPublications = new(); this.cachedPublications = new();
} }
@ -58,10 +58,10 @@ public abstract class GlobalBase
File.WriteAllText(settings.notificationConnectorsFilePath, JsonConvert.SerializeObject(notificationConnectors)); File.WriteAllText(settings.notificationConnectorsFilePath, JsonConvert.SerializeObject(notificationConnectors));
} }
protected void DeleteNotificationConnector(NotificationConnector.NotificationManagerType notificationManagerType) protected void DeleteNotificationConnector(NotificationConnector.NotificationConnectorType notificationConnectorType)
{ {
Log($"Removing {notificationManagerType}"); Log($"Removing {notificationConnectorType}");
notificationConnectors.RemoveWhere(nc => nc.notificationManagerType == notificationManagerType); notificationConnectors.RemoveWhere(nc => nc.notificationConnectorType == notificationConnectorType);
} }
protected void UpdateLibraries() protected void UpdateLibraries()

View File

@ -5,6 +5,13 @@ namespace Tranga.LibraryConnectors;
public class LibraryManagerJsonConverter : JsonConverter public class LibraryManagerJsonConverter : JsonConverter
{ {
private GlobalBase _clone;
internal LibraryManagerJsonConverter(GlobalBase clone)
{
this._clone = clone;
}
public override bool CanConvert(Type objectType) public override bool CanConvert(Type objectType)
{ {
return (objectType == typeof(LibraryConnector)); return (objectType == typeof(LibraryConnector));
@ -13,11 +20,15 @@ public class LibraryManagerJsonConverter : JsonConverter
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{ {
JObject jo = JObject.Load(reader); JObject jo = JObject.Load(reader);
if (jo["libraryType"]!.Value<Int64>() == (Int64)LibraryConnector.LibraryType.Komga) if (jo["libraryType"]!.Value<byte>() == (byte)LibraryConnector.LibraryType.Komga)
return jo.ToObject<Komga>(serializer)!; return new Komga(this._clone,
jo.GetValue("baseUrl")!.Value<string>()!,
jo.GetValue("auth")!.Value<string>()!);
if (jo["libraryType"]!.Value<Int64>() == (Int64)LibraryConnector.LibraryType.Kavita) if (jo["libraryType"]!.Value<byte>() == (byte)LibraryConnector.LibraryType.Kavita)
return jo.ToObject<Kavita>(serializer)!; return new Kavita(this._clone,
jo.GetValue("baseUrl")!.Value<string>()!,
jo.GetValue("auth")!.Value<string>()!);
throw new Exception(); throw new Exception();
} }

View File

@ -11,7 +11,7 @@ public class Gotify : NotificationConnector
private readonly HttpClient _client = new(); private readonly HttpClient _client = new();
[JsonConstructor] [JsonConstructor]
public Gotify(GlobalBase clone, string endpoint, string appToken) : base(clone, NotificationManagerType.Gotify) public Gotify(GlobalBase clone, string endpoint, string appToken) : base(clone, NotificationConnectorType.Gotify)
{ {
this.endpoint = endpoint; this.endpoint = endpoint;
this.appToken = appToken; this.appToken = appToken;

View File

@ -10,7 +10,7 @@ public class LunaSea : NotificationConnector
private readonly HttpClient _client = new(); private readonly HttpClient _client = new();
[JsonConstructor] [JsonConstructor]
public LunaSea(GlobalBase clone, string id) : base(clone, NotificationManagerType.LunaSea) public LunaSea(GlobalBase clone, string id) : base(clone, NotificationConnectorType.LunaSea)
{ {
this.id = id; this.id = id;
} }

View File

@ -2,14 +2,14 @@
public abstract class NotificationConnector : GlobalBase public abstract class NotificationConnector : GlobalBase
{ {
public readonly NotificationManagerType notificationManagerType; public readonly NotificationConnectorType notificationConnectorType;
protected NotificationConnector(GlobalBase clone, NotificationManagerType notificationManagerType) : base(clone) protected NotificationConnector(GlobalBase clone, NotificationConnectorType notificationConnectorType) : base(clone)
{ {
this.notificationManagerType = notificationManagerType; this.notificationConnectorType = notificationConnectorType;
} }
public enum NotificationManagerType : byte { Gotify = 0, LunaSea = 1 } public enum NotificationConnectorType : byte { Gotify = 0, LunaSea = 1 }
public abstract void SendNotification(string title, string notificationText); public abstract void SendNotification(string title, string notificationText);
} }

View File

@ -5,6 +5,13 @@ namespace Tranga.NotificationConnectors;
public class NotificationManagerJsonConverter : JsonConverter public class NotificationManagerJsonConverter : JsonConverter
{ {
private GlobalBase _clone;
public NotificationManagerJsonConverter(GlobalBase clone)
{
this._clone = clone;
}
public override bool CanConvert(Type objectType) public override bool CanConvert(Type objectType)
{ {
return (objectType == typeof(NotificationConnector)); return (objectType == typeof(NotificationConnector));
@ -14,10 +21,11 @@ public class NotificationManagerJsonConverter : JsonConverter
JsonSerializer serializer) JsonSerializer serializer)
{ {
JObject jo = JObject.Load(reader); JObject jo = JObject.Load(reader);
if (jo["notificationManagerType"]!.Value<byte>() == (byte)NotificationConnector.NotificationManagerType.Gotify) if (jo["notificationConnectorType"]!.Value<byte>() == (byte)NotificationConnector.NotificationConnectorType.Gotify)
return jo.ToObject<Gotify>(serializer)!; return new Gotify(this._clone, jo.GetValue("endpoint")!.Value<string>()!, jo.GetValue("appToken")!.Value<string>()!);
else if (jo["notificationManagerType"]!.Value<byte>() == (byte)NotificationConnector.NotificationManagerType.LunaSea) else if (jo["notificationConnectorType"]!.Value<byte>() ==
return jo.ToObject<LunaSea>(serializer)!; (byte)NotificationConnector.NotificationConnectorType.LunaSea)
return new LunaSea(this._clone, jo.GetValue("id")!.Value<string>()!);
throw new Exception(); throw new Exception();
} }

View File

@ -165,20 +165,25 @@ public class Server : GlobalBase
case "Jobs/Waiting": case "Jobs/Waiting":
SendResponse(HttpStatusCode.OK, response, _parent._jobBoss.jobs.Where(jjob => jjob.progressToken.state is ProgressToken.State.Standby)); SendResponse(HttpStatusCode.OK, response, _parent._jobBoss.jobs.Where(jjob => jjob.progressToken.state is ProgressToken.State.Standby));
break; break;
case "Jobs/MonitorJobs":
SendResponse(HttpStatusCode.OK, response, _parent._jobBoss.jobs.Where(jjob => jjob is DownloadNewChapters));
break;
case "Settings": case "Settings":
SendResponse(HttpStatusCode.OK, response, settings); SendResponse(HttpStatusCode.OK, response, settings);
break; break;
case "NotificationConnectors": case "NotificationConnectors":
SendResponse(HttpStatusCode.OK, response, notificationConnectors); SendResponse(HttpStatusCode.OK, response, notificationConnectors);
break; break;
case "NotificationsConnectors/Types": case "NotificationConnectors/Types":
SendResponse(HttpStatusCode.OK, response, Enum.GetNames(typeof(NotificationConnector.NotificationManagerType))); SendResponse(HttpStatusCode.OK, response,
Enum.GetValues<NotificationConnector.NotificationConnectorType>().Select(nc => new KeyValuePair<byte, string?>((byte)nc, Enum.GetName(nc))));
break; break;
case "LibraryConnectors": case "LibraryConnectors":
SendResponse(HttpStatusCode.OK, response, libraryConnectors); SendResponse(HttpStatusCode.OK, response, libraryConnectors);
break; break;
case "LibraryConnectors/Types": case "LibraryConnectors/Types":
SendResponse(HttpStatusCode.OK, response, Enum.GetNames(typeof(LibraryConnector.LibraryType))); SendResponse(HttpStatusCode.OK, response,
Enum.GetValues<LibraryConnector.LibraryType>().Select(lc => new KeyValuePair<byte, string?>((byte)lc, Enum.GetName(lc))));
break; break;
default: default:
SendResponse(HttpStatusCode.BadRequest, response); SendResponse(HttpStatusCode.BadRequest, response);
@ -257,13 +262,13 @@ public class Server : GlobalBase
break;*/ break;*/
case "NotificationConnectors/Update": case "NotificationConnectors/Update":
if (!requestVariables.TryGetValue("notificationConnector", out string? notificationConnectorStr) || if (!requestVariables.TryGetValue("notificationConnector", out string? notificationConnectorStr) ||
!Enum.TryParse(notificationConnectorStr, out NotificationConnector.NotificationManagerType notificationManagerType)) !Enum.TryParse(notificationConnectorStr, out NotificationConnector.NotificationConnectorType notificationConnectorType))
{ {
SendResponse(HttpStatusCode.BadRequest, response); SendResponse(HttpStatusCode.BadRequest, response);
break; break;
} }
if (notificationManagerType is NotificationConnector.NotificationManagerType.Gotify) if (notificationConnectorType is NotificationConnector.NotificationConnectorType.Gotify)
{ {
if (!requestVariables.TryGetValue("gotifyUrl", out string? gotifyUrl) || if (!requestVariables.TryGetValue("gotifyUrl", out string? gotifyUrl) ||
!requestVariables.TryGetValue("gotifyAppToken", out string? gotifyAppToken)) !requestVariables.TryGetValue("gotifyAppToken", out string? gotifyAppToken))
@ -276,7 +281,7 @@ public class Server : GlobalBase
break; break;
} }
if (notificationManagerType is NotificationConnector.NotificationManagerType.LunaSea) if (notificationConnectorType is NotificationConnector.NotificationConnectorType.LunaSea)
{ {
if (!requestVariables.TryGetValue("lunaseaWebhook", out string? lunaseaWebhook)) if (!requestVariables.TryGetValue("lunaseaWebhook", out string? lunaseaWebhook))
{ {
@ -288,16 +293,16 @@ public class Server : GlobalBase
break; break;
} }
break; break;
case "LibraryManagers/Update": case "LibraryConnectors/Update":
if (!requestVariables.TryGetValue("libraryManager", out string? libraryManagerStr) || if (!requestVariables.TryGetValue("libraryConnector", out string? libraryConnectorStr) ||
!Enum.TryParse(libraryManagerStr, !Enum.TryParse(libraryConnectorStr,
out LibraryConnector.LibraryType libraryManagerType)) out LibraryConnector.LibraryType libraryConnectorType))
{ {
SendResponse(HttpStatusCode.BadRequest, response); SendResponse(HttpStatusCode.BadRequest, response);
break; break;
} }
if (libraryManagerType is LibraryConnector.LibraryType.Kavita) if (libraryConnectorType is LibraryConnector.LibraryType.Kavita)
{ {
if (!requestVariables.TryGetValue("kavitaUrl", out string? kavitaUrl) || if (!requestVariables.TryGetValue("kavitaUrl", out string? kavitaUrl) ||
!requestVariables.TryGetValue("kavitaUsername", out string? kavitaUsername) || !requestVariables.TryGetValue("kavitaUsername", out string? kavitaUsername) ||
@ -311,7 +316,7 @@ public class Server : GlobalBase
break; break;
} }
if (libraryManagerType is LibraryConnector.LibraryType.Komga) if (libraryConnectorType is LibraryConnector.LibraryType.Komga)
{ {
if (!requestVariables.TryGetValue("komgaUrl", out string? komgaUrl) || if (!requestVariables.TryGetValue("komgaUrl", out string? komgaUrl) ||
!requestVariables.TryGetValue("komgaAuth", out string? komgaAuth)) !requestVariables.TryGetValue("komgaAuth", out string? komgaAuth))
@ -392,23 +397,23 @@ public class Server : GlobalBase
break; break;
case "NotificationConnectors": case "NotificationConnectors":
if (!requestVariables.TryGetValue("notificationConnector", out string? notificationConnectorStr) || if (!requestVariables.TryGetValue("notificationConnector", out string? notificationConnectorStr) ||
!Enum.TryParse(notificationConnectorStr, out NotificationConnector.NotificationManagerType notificationManagerType)) !Enum.TryParse(notificationConnectorStr, out NotificationConnector.NotificationConnectorType notificationConnectorType))
{ {
SendResponse(HttpStatusCode.BadRequest, response); SendResponse(HttpStatusCode.BadRequest, response);
break; break;
} }
DeleteNotificationConnector(notificationManagerType); DeleteNotificationConnector(notificationConnectorType);
SendResponse(HttpStatusCode.Accepted, response); SendResponse(HttpStatusCode.Accepted, response);
break; break;
case "LibraryManagers": case "LibraryConnectors":
if (!requestVariables.TryGetValue("libraryManager", out string? libraryManagerStr) || if (!requestVariables.TryGetValue("libraryConnectors", out string? libraryConnectorStr) ||
!Enum.TryParse(libraryManagerStr, !Enum.TryParse(libraryConnectorStr,
out LibraryConnector.LibraryType libraryManagerType)) out LibraryConnector.LibraryType libraryConnectoryType))
{ {
SendResponse(HttpStatusCode.BadRequest, response); SendResponse(HttpStatusCode.BadRequest, response);
break; break;
} }
DeleteLibraryConnector(libraryManagerType); DeleteLibraryConnector(libraryConnectoryType);
SendResponse(HttpStatusCode.Accepted, response); SendResponse(HttpStatusCode.Accepted, response);
break; break;
default: default:

View File

@ -50,7 +50,7 @@ public class TrangaSettings
UpdateDownloadLocation(this.downloadLocation!, false); UpdateDownloadLocation(this.downloadLocation!, false);
} }
public HashSet<LibraryConnector> LoadLibraryConnectors() public HashSet<LibraryConnector> LoadLibraryConnectors(GlobalBase clone)
{ {
if (!File.Exists(libraryConnectorsFilePath)) if (!File.Exists(libraryConnectorsFilePath))
return new HashSet<LibraryConnector>(); return new HashSet<LibraryConnector>();
@ -59,21 +59,21 @@ public class TrangaSettings
{ {
Converters = Converters =
{ {
new LibraryManagerJsonConverter() new LibraryManagerJsonConverter(clone)
} }
})!; })!;
} }
public HashSet<NotificationConnector> LoadNotificationConnectors() public HashSet<NotificationConnector> LoadNotificationConnectors(GlobalBase clone)
{ {
if (!File.Exists(notificationConnectorsFilePath)) if (!File.Exists(notificationConnectorsFilePath))
return new HashSet<NotificationConnector>(); return new HashSet<NotificationConnector>();
return JsonConvert.DeserializeObject<HashSet<NotificationConnector>>(File.ReadAllText(libraryConnectorsFilePath), return JsonConvert.DeserializeObject<HashSet<NotificationConnector>>(File.ReadAllText(notificationConnectorsFilePath),
new JsonSerializerSettings() new JsonSerializerSettings()
{ {
Converters = Converters =
{ {
new NotificationManagerJsonConverter() new NotificationManagerJsonConverter(clone)
} }
})!; })!;
} }