parent
31e02bdead
commit
094df71301
@ -0,0 +1,218 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Core.Datastore.Migration;
|
||||
using NzbDrone.Core.Notifications.Telegram;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
|
||||
namespace NzbDrone.Core.Test.Datastore.Migration
|
||||
{
|
||||
[TestFixture]
|
||||
public class telegram_link_previewFixture : MigrationTest<telegram_link_preview>
|
||||
{
|
||||
[Test]
|
||||
public void should_set_link_preview_to_none_when_no_metadata_links()
|
||||
{
|
||||
var db = WithMigrationTestDb(c =>
|
||||
{
|
||||
c.Insert.IntoTable("Notifications").Row(new
|
||||
{
|
||||
OnGrab = true,
|
||||
OnDownload = true,
|
||||
OnUpgrade = true,
|
||||
OnHealthIssue = true,
|
||||
IncludeHealthWarnings = true,
|
||||
OnRename = true,
|
||||
Name = "Telegram Sonarr",
|
||||
Implementation = "Telegram",
|
||||
Tags = "[]",
|
||||
Settings = new TelegramSettings217
|
||||
{
|
||||
BotToken = "secret",
|
||||
ChatId = "12345",
|
||||
SendSilently = false,
|
||||
IncludeAppNameInTitle = false,
|
||||
IncludeInstanceNameInTitle = false,
|
||||
MetadataLinks = new List<int>()
|
||||
}.ToJson(),
|
||||
ConfigContract = "TelegramSettings"
|
||||
});
|
||||
});
|
||||
|
||||
var items = db.Query<NotificationDefinition218>("SELECT * FROM \"Notifications\"");
|
||||
|
||||
items.Should().HaveCount(1);
|
||||
items.First().Implementation.Should().Be("Telegram");
|
||||
items.First().ConfigContract.Should().Be("TelegramSettings");
|
||||
items.First().Settings.LinkPreview.Should().Be((int)MetadataLinkPreviewType.None);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_set_link_preview_to_first_metadata_link_when_not_tvdb()
|
||||
{
|
||||
var db = WithMigrationTestDb(c =>
|
||||
{
|
||||
c.Insert.IntoTable("Notifications").Row(new
|
||||
{
|
||||
OnGrab = true,
|
||||
OnDownload = true,
|
||||
OnUpgrade = true,
|
||||
OnHealthIssue = true,
|
||||
IncludeHealthWarnings = true,
|
||||
OnRename = true,
|
||||
Name = "Telegram Sonarr",
|
||||
Implementation = "Telegram",
|
||||
Tags = "[]",
|
||||
Settings = new TelegramSettings217
|
||||
{
|
||||
BotToken = "secret",
|
||||
ChatId = "12345",
|
||||
SendSilently = false,
|
||||
IncludeAppNameInTitle = false,
|
||||
IncludeInstanceNameInTitle = false,
|
||||
MetadataLinks = new List<int> { 0, 1 }
|
||||
}.ToJson(),
|
||||
ConfigContract = "TelegramSettings"
|
||||
});
|
||||
});
|
||||
|
||||
var items = db.Query<NotificationDefinition218>("SELECT * FROM \"Notifications\"");
|
||||
|
||||
items.Should().HaveCount(1);
|
||||
items.First().Implementation.Should().Be("Telegram");
|
||||
items.First().ConfigContract.Should().Be("TelegramSettings");
|
||||
items.First().Settings.LinkPreview.Should().Be((int)MetadataLinkPreviewType.Imdb);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_set_link_preview_to_first_metadata_link_that_is_not_tvdb()
|
||||
{
|
||||
var db = WithMigrationTestDb(c =>
|
||||
{
|
||||
c.Insert.IntoTable("Notifications").Row(new
|
||||
{
|
||||
OnGrab = true,
|
||||
OnDownload = true,
|
||||
OnUpgrade = true,
|
||||
OnHealthIssue = true,
|
||||
IncludeHealthWarnings = true,
|
||||
OnRename = true,
|
||||
Name = "Telegram Sonarr",
|
||||
Implementation = "Telegram",
|
||||
Tags = "[]",
|
||||
Settings = new TelegramSettings217
|
||||
{
|
||||
BotToken = "secret",
|
||||
ChatId = "12345",
|
||||
SendSilently = false,
|
||||
IncludeAppNameInTitle = false,
|
||||
IncludeInstanceNameInTitle = false,
|
||||
MetadataLinks = new List<int> { 1, 2 }
|
||||
}.ToJson(),
|
||||
ConfigContract = "TelegramSettings"
|
||||
});
|
||||
});
|
||||
|
||||
var items = db.Query<NotificationDefinition218>("SELECT * FROM \"Notifications\"");
|
||||
|
||||
items.Should().HaveCount(1);
|
||||
items.First().Implementation.Should().Be("Telegram");
|
||||
items.First().ConfigContract.Should().Be("TelegramSettings");
|
||||
items.First().Settings.LinkPreview.Should().Be((int)MetadataLinkPreviewType.Tvmaze);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_set_link_preview_to_none_when_only_metadata_link_is_tvdb()
|
||||
{
|
||||
var db = WithMigrationTestDb(c =>
|
||||
{
|
||||
c.Insert.IntoTable("Notifications").Row(new
|
||||
{
|
||||
OnGrab = true,
|
||||
OnDownload = true,
|
||||
OnUpgrade = true,
|
||||
OnHealthIssue = true,
|
||||
IncludeHealthWarnings = true,
|
||||
OnRename = true,
|
||||
Name = "Telegram Sonarr",
|
||||
Implementation = "Telegram",
|
||||
Tags = "[]",
|
||||
Settings = new TelegramSettings217
|
||||
{
|
||||
BotToken = "secret",
|
||||
ChatId = "12345",
|
||||
SendSilently = false,
|
||||
IncludeAppNameInTitle = false,
|
||||
IncludeInstanceNameInTitle = false,
|
||||
MetadataLinks = new List<int> { 1 }
|
||||
}.ToJson(),
|
||||
ConfigContract = "TelegramSettings"
|
||||
});
|
||||
});
|
||||
|
||||
var items = db.Query<NotificationDefinition218>("SELECT * FROM \"Notifications\"");
|
||||
|
||||
items.Should().HaveCount(1);
|
||||
items.First().Implementation.Should().Be("Telegram");
|
||||
items.First().ConfigContract.Should().Be("TelegramSettings");
|
||||
items.First().Settings.LinkPreview.Should().Be((int)MetadataLinkPreviewType.None);
|
||||
}
|
||||
}
|
||||
|
||||
public class NotificationDefinition218
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Implementation { get; set; }
|
||||
public string ConfigContract { get; set; }
|
||||
public TelegramSettings218 Settings { get; set; }
|
||||
public string Name { get; set; }
|
||||
public bool OnGrab { get; set; }
|
||||
public bool OnDownload { get; set; }
|
||||
public bool OnUpgrade { get; set; }
|
||||
public bool OnRename { get; set; }
|
||||
public bool OnSeriesDelete { get; set; }
|
||||
public bool OnEpisodeFileDelete { get; set; }
|
||||
public bool OnEpisodeFileDeleteForUpgrade { get; set; }
|
||||
public bool OnHealthIssue { get; set; }
|
||||
public bool OnApplicationUpdate { get; set; }
|
||||
public bool OnManualInteractionRequired { get; set; }
|
||||
public bool OnSeriesAdd { get; set; }
|
||||
public bool OnHealthRestored { get; set; }
|
||||
public bool OnImportComplete { get; set; }
|
||||
public bool SupportsOnGrab { get; set; }
|
||||
public bool SupportsOnDownload { get; set; }
|
||||
public bool SupportsOnUpgrade { get; set; }
|
||||
public bool SupportsOnRename { get; set; }
|
||||
public bool SupportsOnSeriesDelete { get; set; }
|
||||
public bool SupportsOnEpisodeFileDelete { get; set; }
|
||||
public bool SupportsOnEpisodeFileDeleteForUpgrade { get; set; }
|
||||
public bool SupportsOnHealthIssue { get; set; }
|
||||
public bool IncludeHealthWarnings { get; set; }
|
||||
public List<int> Tags { get; set; }
|
||||
}
|
||||
|
||||
public class TelegramSettings217
|
||||
{
|
||||
public string BotToken { get; set; }
|
||||
public string ChatId { get; set; }
|
||||
public int? TopicId { get; set; }
|
||||
public bool SendSilently { get; set; }
|
||||
public bool IncludeAppNameInTitle { get; set; }
|
||||
public bool IncludeInstanceNameInTitle { get; set; }
|
||||
public IEnumerable<int> MetadataLinks { get; set; }
|
||||
}
|
||||
|
||||
public class TelegramSettings218
|
||||
{
|
||||
public string BotToken { get; set; }
|
||||
public string ChatId { get; set; }
|
||||
public int? TopicId { get; set; }
|
||||
public bool SendSilently { get; set; }
|
||||
public bool IncludeAppNameInTitle { get; set; }
|
||||
public bool IncludeInstanceNameInTitle { get; set; }
|
||||
public IEnumerable<int> MetadataLinks { get; set; }
|
||||
public int LinkPreview { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using Dapper;
|
||||
using FluentMigrator;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Core.Datastore.Migration.Framework;
|
||||
|
||||
namespace NzbDrone.Core.Datastore.Migration
|
||||
{
|
||||
[Migration(218)]
|
||||
public class telegram_link_preview : NzbDroneMigrationBase
|
||||
{
|
||||
protected override void MainDbUpgrade()
|
||||
{
|
||||
Execute.WithConnection(ChangeEncryption);
|
||||
}
|
||||
|
||||
private void ChangeEncryption(IDbConnection conn, IDbTransaction tran)
|
||||
{
|
||||
var updated = new List<object>();
|
||||
using (var cmd = conn.CreateCommand())
|
||||
{
|
||||
cmd.Transaction = tran;
|
||||
cmd.CommandText = "SELECT \"Id\", \"Settings\" FROM \"Notifications\" WHERE \"Implementation\" = 'Telegram'";
|
||||
|
||||
using (var reader = cmd.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
var id = reader.GetInt32(0);
|
||||
var settings = Json.Deserialize<JObject>(reader.GetString(1));
|
||||
|
||||
var metadataLinks = settings["metadataLinks"] as JArray;
|
||||
|
||||
if (metadataLinks == null)
|
||||
{
|
||||
settings["linkPreview"] = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
settings["linkPreview"] = metadataLinks.FirstOrDefault(l => l.Value<int?>() != 1) ?? -1;
|
||||
}
|
||||
|
||||
updated.Add(new
|
||||
{
|
||||
Settings = settings.ToJson(),
|
||||
Id = id
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var updateSql = "UPDATE \"Notifications\" SET \"Settings\" = @Settings WHERE \"Id\" = @Id";
|
||||
conn.Execute(updateSql, updated, transaction: tran);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
using NzbDrone.Core.Annotations;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Telegram
|
||||
{
|
||||
// Maintain the same values as MetadataLinkType
|
||||
|
||||
public enum MetadataLinkPreviewType
|
||||
{
|
||||
[FieldOption(Label = "None")]
|
||||
None = -1,
|
||||
|
||||
[FieldOption(Label = "IMDb")]
|
||||
Imdb = 0,
|
||||
|
||||
// No preview data is supported for TheTVDB at this time
|
||||
// [FieldOption(Label = "TVDb")]
|
||||
// Tvdb = 1,
|
||||
|
||||
[FieldOption(Label = "TVMaze")]
|
||||
Tvmaze = 2,
|
||||
|
||||
[FieldOption(Label = "Trakt")]
|
||||
Trakt = 3
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Telegram;
|
||||
|
||||
public class TelegramLinkPreviewOptions
|
||||
{
|
||||
[JsonProperty("is_disabled")]
|
||||
public bool IsDisabled { get; set; }
|
||||
|
||||
[JsonProperty("url")]
|
||||
public string Url { get; set; }
|
||||
|
||||
public TelegramLinkPreviewOptions(List<TelegramLink> links, TelegramSettings settings)
|
||||
{
|
||||
IsDisabled = (MetadataLinkPreviewType)settings.LinkPreview == MetadataLinkPreviewType.None;
|
||||
Url = links.FirstOrDefault(l => l.Type.HasValue && (int)l.Type.Value == settings.LinkPreview)?.Link;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Telegram;
|
||||
|
||||
public class TelegramPayload
|
||||
{
|
||||
[JsonProperty("chat_id")]
|
||||
public string ChatId { get; set; }
|
||||
|
||||
[JsonProperty("parse_mode")]
|
||||
public string ParseMode = "HTML";
|
||||
public string Text { get; set; }
|
||||
|
||||
[JsonProperty("disable_notification")]
|
||||
public bool DisableNotification { get; set; }
|
||||
|
||||
[JsonProperty("message_thread_id")]
|
||||
public int? MessageThreadId { get; set; }
|
||||
|
||||
[JsonProperty("link_preview_options")]
|
||||
public TelegramLinkPreviewOptions LinkPreviewOptions { get; set; }
|
||||
}
|
Loading…
Reference in new issue