Skip to content
This repository has been archived by the owner on Jun 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #7 from dkanada/master
Browse files Browse the repository at this point in the history
Fix all build issues and clean up some code
  • Loading branch information
joshuaboniface authored May 15, 2019
2 parents 6315898 + 0a86544 commit 92ccd81
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,23 @@ public class TestNotification : IReturnVoid
class ServerApiEndpoints : IService
{
private readonly IUserManager _userManager;
private readonly IEncryptionManager _encryption;
private readonly ILogger _logger;

public ServerApiEndpoints(IUserManager userManager, ILogger logger, IEncryptionManager encryption)
public ServerApiEndpoints(IUserManager userManager, ILogger logger)
{
_userManager = userManager;
_logger = logger;
_encryption = encryption;
}

public void Post(TestNotification request)
{
var task = new Notifier(_logger, _encryption).SendNotification(new UserNotification
var task = new Notifier(_logger).SendNotification(new UserNotification
{
Date = DateTime.UtcNow,
Description = "This is a test notification from Emby Server",
Level = Model.Notifications.NotificationLevel.Normal,
Name = "Emby: Test Notification",
User = _userManager.GetUserById(request.UserID)

}, CancellationToken.None);

Task.WaitAll(task);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public class SMTPOptions
public bool UseCredentials { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string PwData { get; set; }
public string MediaBrowserUserId { get; set; }
public string UserId { get; set; }
public bool SSL { get; set; }

public SMTPOptions()
Expand Down
41 changes: 4 additions & 37 deletions MediaBrowser.Plugins.SmtpNotifications/Configuration/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
</head>
<body>
<div data-role="page" class="page type-interior pluginConfigurationPage smtpConfigurationPage" data-require="emby-select,emby-checkbox,emby-input,emby-button">

<div data-role="content">
<div class="content-primary">

<form class="smtpConfigurationForm">

<div style="height:0; overflow: hidden;"><input type="text" name="fakeusernameremembered" tabindex="-1" /><input type="password" name="fakepasswordremembered" tabindex="-1" /></div>

<div class="selectContainer">
Expand Down Expand Up @@ -73,27 +70,19 @@
<div>
<button is="emby-button" type="submit" class="raised button-submit block"><span>Save</span></button>
</div>

</form>
</div>
</div>

<script type="text/javascript">

(function () {

var pluginId = "cfa0f7f4-4155-4d71-849b-d6598dc4c5bb";

function loadUserConfig(page, userId) {

Dashboard.showLoadingMsg();

ApiClient.getPluginConfiguration(pluginId).then(function (config) {

var smtpConfig = config.Options.filter(function (c) {

return userId == c.MediaBrowserUserId;

return userId == c.UserId;
})[0] || { Enabled: false };

$('#chkEnableSMTP', page).checked(smtpConfig.Enabled).checkboxradio("refresh");
Expand All @@ -111,26 +100,21 @@
}

$('.smtpConfigurationPage').on('pageinit', function (event) {

var page = this;

$('#selectUser', page).on('change', function () {

loadUserConfig(page, this.value);
});

$('#testNotification', page).on('click', function (event) {

Dashboard.showLoadingMsg();

var onError = function () {
require(['alert'], function (alert) {
alert("There was an error sending the test email. Please check your email settings and try again.");
});
};

ApiClient.getPluginConfiguration(pluginId).then(function (config) {

if (!config.Options.length) {
Dashboard.hideLoadingMsg();
require(['alert'], function (alert) {
Expand All @@ -139,41 +123,32 @@
}

config.Options.map(function (c) {

ApiClient.ajax({
type: "POST",
url: ApiClient.getUrl("Notification/SMTP/Test/" + c.MediaBrowserUserId)

url: ApiClient.getUrl("Notification/SMTP/Test/" + c.UserId)
}).catch(onError);
});

Dashboard.hideLoadingMsg();

}, onError);
});

$('.smtpConfigurationForm', page).on('submit', function (e) {

Dashboard.showLoadingMsg();

var form = this;

ApiClient.getPluginConfiguration(pluginId).then(function (config) {

var userId = $('#selectUser', form).val();

var smtpConfig = config.Options.filter(function (c) {

return userId == c.MediaBrowserUserId;

return userId == c.UserId;
})[0];

if (!smtpConfig) {
smtpConfig = {};
config.Options.push(smtpConfig);
}

smtpConfig.MediaBrowserUserId = userId;
smtpConfig.UserId = userId;
smtpConfig.Enabled = $('#chkEnableSMTP', form).checked();
smtpConfig.EmailFrom = $('#txtEmailFrom', form).val();
smtpConfig.EmailTo = $('#txtEmailTo', form).val();
Expand All @@ -191,26 +166,18 @@
});

}).on('pageshow', function (event) {

Dashboard.showLoadingMsg();

var page = this;

ApiClient.getUsers().then(function (users) {

$('#selectUser', page).html(users.map(function (user) {

return '<option value="' + user.Id + '">' + user.Name + '</option>';

})).selectmenu('refresh').trigger('change');

});

Dashboard.hideLoadingMsg();
});

})();

</script>
</div>
</body>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;</TargetFrameworks>
<AssemblyVersion>2.0.0</AssemblyVersion>
<FileVersion>2.0.0</FileVersion>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyVersion>3.0.0</AssemblyVersion>
<FileVersion>3.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
19 changes: 8 additions & 11 deletions MediaBrowser.Plugins.SmtpNotifications/Notifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ namespace MediaBrowser.Plugins.SmtpNotifications
{
public class Notifier : INotificationService
{
private readonly IEncryptionManager _encryption;
private readonly ILogger _logger;
public static Notifier Instance { get; private set; }

public Notifier(ILogger logger, IEncryptionManager encryption)
public Notifier(ILogger logger)
{
_encryption = encryption;
_logger = logger;

Instance = this;
Expand All @@ -36,7 +34,7 @@ public bool IsEnabledForUser(User user)
private SMTPOptions GetOptions(User user)
{
return Plugin.Instance.Configuration.Options
.FirstOrDefault(i => string.Equals(i.MediaBrowserUserId, user.Id.ToString("N"), StringComparison.OrdinalIgnoreCase));
.FirstOrDefault(i => string.Equals(i.UserId, user.Id.ToString("N"), StringComparison.OrdinalIgnoreCase));
}

public string Name
Expand All @@ -55,10 +53,6 @@ private void TrySendNotification(UserNotification request, CancellationToken can
try
{
SendNotificationCore(request, cancellationToken);
}
catch (OperationCanceledException)
{

}
catch (Exception ex)
{
Expand Down Expand Up @@ -89,10 +83,13 @@ private void SendNotificationCore(UserNotification request, CancellationToken ca

_logger.LogInformation("Sending email {to} with subject {subject}", options.EmailTo, mail.Subject);

if (options.UseCredentials)
if (options.UseCredentials && !string.IsNullOrEmpty(options.Username) && !string.IsNullOrEmpty(options.Password))
{
client.Credentials = new NetworkCredential(options.Username, options.Password);
}
else
{
var pw = string.IsNullOrWhiteSpace(options.Password) ? _encryption.DecryptString(options.PwData) : options.Password;
client.Credentials = new NetworkCredential(options.Username, pw);
_logger.LogError("Cannot use credentials for email to {user} because the username or password is missing", options.EmailTo);
}

try
Expand Down
7 changes: 2 additions & 5 deletions MediaBrowser.Plugins.SmtpNotifications/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ namespace MediaBrowser.Plugins.SmtpNotifications
/// </summary>
public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
{
private readonly IEncryptionManager _encryption;

public Plugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer, IEncryptionManager encryption)
public Plugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)
: base(applicationPaths, xmlSerializer)
{
_encryption = encryption;
Instance = this;
}

Expand Down Expand Up @@ -60,7 +57,7 @@ public override string Description
{
get
{
return "Sends notifications via email.";
return "Send SMTP email notifications";
}
}

Expand Down
6 changes: 3 additions & 3 deletions build.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
name: "jellyfin-plugin-emailnotifications"
guid: "cfa0f7f4-4155-4d71-849b-d6598dc4c5bb"
version: "2" # Please increment with each pull request
version: "3" # Please increment with each pull request
jellyfin_version: "10.3.0" # The earliest binary-compatible version
owner: "jellyfin"
nicename: "Email Notifications"
nicename: "Email"
description: "Send SMTP email notifications"
overview: "Send SMTP email notifications"
category: "General"
category: "Notifications"
artifacts:
- "MediaBrowser.Plugins.SmtpNotifications.dll"
build_type: "dotnet"
Expand Down

0 comments on commit 92ccd81

Please sign in to comment.