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

Commit

Permalink
addressed points of the review of the Initial commit made by @cvium
Browse files Browse the repository at this point in the history
  • Loading branch information
fruhnow committed Jun 18, 2019
1 parent 0fb754e commit 26f693c
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 98 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ csx
AppPackages/

# Others
.vs/*
[Bb]in
[Oo]bj
sql
Expand Down
19 changes: 6 additions & 13 deletions Jellyfin.Plugin.Slack/Api/ServerApiEntryPoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public class ServerApiEndpoints : IService
private readonly IHttpClient _httpClient;
private readonly ILogger _logger;
private readonly IJsonSerializer _serializer;



public ServerApiEndpoints(ILogger logger, IHttpClient httpClient, IJsonSerializer serializer)
{
_logger = logger;
Expand All @@ -34,16 +33,10 @@ public ServerApiEndpoints(ILogger logger, IHttpClient httpClient, IJsonSerialize
private SlackConfiguration GetOptions(String userID)
{
return Plugin.Instance.Configuration.Options
.FirstOrDefault(i => string.Equals(i.MediaBrowserUserId, userID, StringComparison.OrdinalIgnoreCase));
}

public void Post(TestNotification request)
{
var task = PostAsync(request);
Task.WaitAll(task);
.FirstOrDefault(i => string.Equals(i.JellyfinUserId, userID, StringComparison.OrdinalIgnoreCase));
}

public async Task PostAsync(TestNotification request)
public async Task Post(TestNotification request)
{
var options = GetOptions(request.UserID);

Expand All @@ -52,13 +45,13 @@ public async Task PostAsync(TestNotification request)
{"text", "This is a test notification from Jellyfin"}
};

var httpRequest = new HttpRequestOptions()
var httpRequest = new HttpRequestOptions
{
Url = options.WebHookUrl,
RequestContent = _serializer.SerializeToString(parameters)
RequestContent = _serializer.SerializeToString(parameters),
RequestHeaders = {["Content-type"] = "application/json"}
};

httpRequest.RequestHeaders["Content-type"] = "application/json";
await _httpClient.Post(httpRequest).ConfigureAwait(false);
}
}
Expand Down
1 change: 0 additions & 1 deletion Jellyfin.Plugin.Slack/Configuration/PluginConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ public PluginConfiguration()
{
Options = new SlackConfiguration[] { };
}

}
}
9 changes: 2 additions & 7 deletions Jellyfin.Plugin.Slack/Configuration/SlackConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Jellyfin.Plugin.Slack.Configuration
namespace Jellyfin.Plugin.Slack.Configuration
{
public class SlackConfiguration
{

public string WebHookUrl { get; set; }
public bool IsEnabled { get; set; }
public string MediaBrowserUserId { get; set; }
public string JellyfinUserId { get; set; }
}
}
62 changes: 10 additions & 52 deletions Jellyfin.Plugin.Slack/Configuration/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
</head>
<body>
<div data-role="page" class="page type-interior pluginConfigurationPage SlackConfigurationPage" data-require="emby-input,emby-button,emby-checkbox,emby-select">

<div data-role="content">
<div class="content-primary">
<p>To use Slack register a new incoming webhook at https://[yourSlackSite]/apps/manage/custom-integrations</p>
Expand Down Expand Up @@ -33,50 +32,33 @@
<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 = "94FB77C3-55AD-4C50-BF4E-4E5497467B79";

function loadUserConfig(page, userId) {

Dashboard.showLoadingMsg();

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

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

return userId === c.MediaBrowserUserId;

return userId === c.JellyfinUserId;
})[0] || {};

$('#chkEnableSlack', page).checked(slackConfig.IsEnabled || false).checkboxradio("refresh");
$('#txtSlackWebhookUrl', page).val(slackConfig.WebHookUrl || '');


});
Dashboard.hideLoadingMsg();
}

$('.SlackConfigurationPage').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 () {
alert("There was an error sending the test notification. Please check your notification settings and try again.");
require(['alert'], function (alert) {
Expand All @@ -85,20 +67,17 @@
};

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

if (config.WebHookUrl === "") {
Dashboard.hideLoadingMsg();
require(['alert'], function (alert) {
alert("Please configure and save at least one notification account.");
});
}

config.Options.map(function (c) {
if (c.WebHookUrl === "") {
Dashboard.hideLoadingMsg();
require(['alert'], function (alert) {
alert("Please configure and save at least one notification account.");
});
}

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

url: ApiClient.getUrl("Notification/Slack/Test/" + c.JellyfinUserId)
}).then(function () {
Dashboard.hideLoadingMsg();
}, onError);
Expand All @@ -107,58 +86,37 @@
});

}).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();
});

$('.SlackConfigurationForm').on('submit', function (e) {

Dashboard.showLoadingMsg();

var form = this;

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

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

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

return userId === c.MediaBrowserUserId;

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

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

slackConfig.MediaBrowserUserId = userId;

slackConfig.JellyfinUserId = userId;
slackConfig.IsEnabled = $('#chkEnableSlack', form).checked();
slackConfig.WebHookUrl = $('#txtSlackWebhookUrl', form).val();

ApiClient.updatePluginConfiguration(pluginId, config).then(Dashboard.processPluginConfigurationUpdateResult);
});

return false;
});

})();

</script>
</div>
</body>
Expand Down
5 changes: 1 addition & 4 deletions Jellyfin.Plugin.Slack/Jellyfin.Plugin.Slack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@

<ItemGroup>
<PackageReference Include="Jellyfin.Controller" Version="10.*" />
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.2.0" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy &quot;$(TargetPath)&quot; &quot;C:\Users\Felix\AppData\Local\jellyfin\plugins\Slack&quot; /y" />
</Target>

</Project>
19 changes: 9 additions & 10 deletions Jellyfin.Plugin.Slack/Notifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using MediaBrowser.Controller.Notifications;
using MediaBrowser.Model.Serialization;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;

namespace Jellyfin.Plugin.Slack
{
Expand All @@ -20,22 +21,21 @@ public class Notifier : INotificationService

public Notifier(ILogger logger, IHttpClient httpClient, IJsonSerializer serializer)
{
_logger = logger;
_httpClient = httpClient;
_serializer = serializer;
_logger = logger;
_httpClient = httpClient;
_serializer = serializer;
}

public bool IsEnabledForUser(User user)
{
var options = GetOptions(user);

return options != null && IsValid(options) && options.IsEnabled;
}

private SlackConfiguration 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.JellyfinUserId, user.Id.ToString("N"), StringComparison.OrdinalIgnoreCase));
}

public string Name => Plugin.Instance.Name;
Expand All @@ -49,14 +49,13 @@ public async Task SendNotification(UserNotification request, CancellationToken c
{"text", $"{request.Name} \n {request.Description}"},
};

_logger.LogDebug("Notification to Slack : {0} - {1}", options.WebHookUrl, request.Description);
var httpRequest = new HttpRequestOptions()
_logger.LogDebug("Notification to Slack : {0} - {1}", options.WebHookUrl, request.Description);
var httpRequest = new HttpRequestOptions
{
Url = options.WebHookUrl,
RequestContent = _serializer.SerializeToString(parameters)
RequestContent = _serializer.SerializeToString(parameters),
RequestHeaders = {[HeaderNames.ContentType] = "application/json"}
};

httpRequest.RequestHeaders["Content-type"] = "application/json";
await _httpClient.Post(httpRequest).ConfigureAwait(false);
}

Expand Down
14 changes: 3 additions & 11 deletions Jellyfin.Plugin.Slack/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,17 @@ public Plugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)

public override string Name => "Slack Notifications";

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

public override string Description
{
get
{
return "Sends notifications to Slack.";
}
}
public override string Description => "Sends notifications to Slack.";

private readonly Guid _id = new Guid("94FB77C3-55AD-4C50-BF4E-4E5497467B79");
public override Guid Id => _id;
Expand Down

0 comments on commit 26f693c

Please sign in to comment.