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 #6 from Bond-009/fixbuild
Browse files Browse the repository at this point in the history
Fix build errors
  • Loading branch information
joshuaboniface authored May 29, 2019
2 parents 92ccd81 + cb18699 commit 862bc78
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 109 deletions.
19 changes: 8 additions & 11 deletions MediaBrowser.Plugins.SmtpNotifications/Api/ServerApiEntryPoints.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using MediaBrowser.Controller.Security;
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Notifications;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Services;
using Microsoft.Extensions.Logging;

namespace MediaBrowser.Plugins.SmtpNotifications.Api
{
Expand All @@ -16,7 +15,7 @@ public class TestNotification : IReturnVoid
public string UserID { get; set; }
}

class ServerApiEndpoints : IService
public class ServerApiEndpoints : IService
{
private readonly IUserManager _userManager;
private readonly ILogger _logger;
Expand All @@ -27,18 +26,16 @@ public ServerApiEndpoints(IUserManager userManager, ILogger logger)
_logger = logger;
}

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

Task.WaitAll(task);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MediaBrowser.Model.Plugins;
using System;
using MediaBrowser.Model.Plugins;

namespace MediaBrowser.Plugins.SmtpNotifications.Configuration
{
Expand All @@ -11,7 +12,7 @@ public class PluginConfiguration : BasePluginConfiguration

public PluginConfiguration()
{
Options = new SMTPOptions[] { };
Options = Array.Empty<SMTPOptions>();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyVersion>3.0.0</AssemblyVersion>
<FileVersion>3.0.0</FileVersion>
<AssemblyVersion>4.0.0</AssemblyVersion>
<FileVersion>4.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
102 changes: 40 additions & 62 deletions MediaBrowser.Plugins.SmtpNotifications/Notifier.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Notifications;
using MediaBrowser.Controller.Security;
using Microsoft.Extensions.Logging;
using MediaBrowser.Plugins.SmtpNotifications.Configuration;
using System;
using System;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Notifications;
using MediaBrowser.Plugins.SmtpNotifications.Configuration;
using Microsoft.Extensions.Logging;

namespace MediaBrowser.Plugins.SmtpNotifications
{
public class Notifier : INotificationService
{
private readonly ILogger _logger;
public static Notifier Instance { get; private set; }

public Notifier(ILogger logger)
{
_logger = logger;

Instance = this;
}

public bool IsEnabledForUser(User user)
Expand All @@ -32,35 +28,12 @@ public bool IsEnabledForUser(User user)
}

private SMTPOptions GetOptions(User user)
{
return Plugin.Instance.Configuration.Options
=> Plugin.Instance.Configuration.Options
.FirstOrDefault(i => string.Equals(i.UserId, user.Id.ToString("N"), StringComparison.OrdinalIgnoreCase));
}

public string Name
{
get { return Plugin.Instance.Name; }
}


public Task SendNotification(UserNotification request, CancellationToken cancellationToken)
{
return Task.Run(() => TrySendNotification(request, cancellationToken));
}
public string Name => Plugin.Instance.Name;

private void TrySendNotification(UserNotification request, CancellationToken cancellationToken)
{
try
{
SendNotificationCore(request, cancellationToken);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error sending email");
}
}

private void SendNotificationCore(UserNotification request, CancellationToken cancellationToken)
public async Task SendNotification(UserNotification request, CancellationToken cancellationToken)
{
var options = GetOptions(request.User);

Expand All @@ -69,38 +42,43 @@ private void SendNotificationCore(UserNotification request, CancellationToken ca
Subject = "Emby: " + request.Name,
Body = string.Format("{0}\n\n{1}", request.Name, request.Description)
})
using (var client = new SmtpClient
{
using (var client = new SmtpClient
{
Host = options.Server,
Port = options.Port,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Timeout = 20000
})
Host = options.Server,
Port = options.Port,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Timeout = 20000
})
{
if (options.SSL)
{
if (options.SSL) client.EnableSsl = true;
client.EnableSsl = true;
}

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

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

try
{
client.Send(mail);
_logger.LogInformation("Completed sending email {to} with subject {subject}", options.EmailTo, mail.Subject);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error sending email");
}
try
{
await client.SendMailAsync(mail).ConfigureAwait(false);
_logger.LogInformation("Completed sending email {to} with subject {subject}", options.EmailTo, mail.Subject);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error sending email");
}
}
}
Expand Down
48 changes: 17 additions & 31 deletions MediaBrowser.Plugins.SmtpNotifications/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
using System.Collections.Generic;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Controller.Security;
using MediaBrowser.Model.Plugins;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Plugins.SmtpNotifications.Configuration;
using MediaBrowser.Model.Drawing;
using System.IO;

namespace MediaBrowser.Plugins.SmtpNotifications
{
Expand All @@ -16,55 +13,44 @@ namespace MediaBrowser.Plugins.SmtpNotifications
/// </summary>
public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
{
private Guid _id = new Guid("cfa0f7f4-4155-4d71-849b-d6598dc4c5bb");

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

public IEnumerable<PluginPageInfo> GetPages()
{
return new[]
{
new PluginPageInfo
{
Name = Name,
EmbeddedResourcePath = GetType().Namespace + ".Configuration.config.html"
}
};
}

private Guid _id = new Guid("cfa0f7f4-4155-4d71-849b-d6598dc4c5bb");
public override Guid Id
{
get { return _id; }
}
public override Guid Id => _id;

/// <summary>
/// Gets the name of the plugin
/// </summary>
/// <value>The name.</value>
public override string Name
{
get { return "Email Notifications"; }
}
public override string Name => "Email Notifications";

/// <summary>
/// Gets the description.
/// </summary>
/// <value>The description.</value>
public override string Description
{
get
{
return "Send SMTP email notifications";
}
}
public override string Description => "Send SMTP email notifications.";

/// <summary>
/// Gets the instance.
/// </summary>
/// <value>The instance.</value>
public static Plugin Instance { get; private set; }

public IEnumerable<PluginPageInfo> GetPages()
{
return new[]
{
new PluginPageInfo
{
Name = Name,
EmbeddedResourcePath = GetType().Namespace + ".Configuration.config.html"
}
};
}
}
}
2 changes: 1 addition & 1 deletion build.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: "jellyfin-plugin-emailnotifications"
guid: "cfa0f7f4-4155-4d71-849b-d6598dc4c5bb"
version: "3" # Please increment with each pull request
version: "4" # Please increment with each pull request
jellyfin_version: "10.3.0" # The earliest binary-compatible version
owner: "jellyfin"
nicename: "Email"
Expand Down

0 comments on commit 862bc78

Please sign in to comment.