Skip to content

Commit

Permalink
Fully finished #27 just need to test it!
Browse files Browse the repository at this point in the history
  • Loading branch information
tidusjar committed May 20, 2016
1 parent db00326 commit e49b160
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 139 deletions.
12 changes: 8 additions & 4 deletions PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
using PlexRequests.Services.Models;
using PlexRequests.Services.Notification;
using PlexRequests.Store;
using PlexRequests.Store.Models;
using PlexRequests.Store.Repository;

using Quartz;

Expand All @@ -47,7 +49,7 @@ namespace PlexRequests.Services.Jobs
public class PlexAvailabilityChecker : IJob, IAvailabilityChecker
{
public PlexAvailabilityChecker(ISettingsService<PlexSettings> plexSettings, ISettingsService<AuthenticationSettings> auth, IRequestService request, IPlexApi plex, ICacheProvider cache,
INotificationService notify, IJobRecord rec)
INotificationService notify, IJobRecord rec, IRepository<UsersToNotify> users)
{
Plex = plexSettings;
Auth = auth;
Expand All @@ -56,6 +58,7 @@ public PlexAvailabilityChecker(ISettingsService<PlexSettings> plexSettings, ISet
Cache = cache;
Notification = notify;
Job = rec;
UserNotifyRepo = users;
}

private ISettingsService<PlexSettings> Plex { get; }
Expand All @@ -66,7 +69,7 @@ public PlexAvailabilityChecker(ISettingsService<PlexSettings> plexSettings, ISet
private ICacheProvider Cache { get; }
private INotificationService Notification { get; }
private IJobRecord Job { get; }

private IRepository<UsersToNotify> UserNotifyRepo { get; }
public void CheckAndUpdateAll()
{
Log.Trace("Getting the settings");
Expand Down Expand Up @@ -328,10 +331,11 @@ private void NotifyUsers(IEnumerable<RequestedModel> modelChanged, string apiKey
return;
}

var users = UserNotifyRepo.GetAll().ToList();
foreach (var model in modelChanged)
{
var usersToNotify = model.UsersToNotify; // Users that selected the notification button when requesting a movie/tv show
foreach (var user in usersToNotify)
var selectedUsers = users.Select(x => x.Username).Intersect(model.RequestedUsers);
foreach (var user in selectedUsers)
{
var email = plexUser.User.FirstOrDefault(x => x.Username == user);
if (email == null)
Expand Down
36 changes: 36 additions & 0 deletions PlexRequests.Store/Models/UsersToNotify.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: UsersToNotify.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using Dapper.Contrib.Extensions;

namespace PlexRequests.Store.Models
{
[Table("UsersToNotify")]
public class UsersToNotify : Entity
{
public string Username { get; set; }
}
}
1 change: 1 addition & 0 deletions PlexRequests.Store/PlexRequests.Store.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<Compile Include="DbConfiguration.cs" />
<Compile Include="Entity.cs" />
<Compile Include="Models\ScheduledJobs.cs" />
<Compile Include="Models\UsersToNotify.cs" />
<Compile Include="Repository\IRequestRepository.cs" />
<Compile Include="Repository\ISettingsRepository.cs" />
<Compile Include="ISqliteConfiguration.cs" />
Expand Down
16 changes: 0 additions & 16 deletions PlexRequests.Store/RequestedModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public RequestedModel()
public List<string> RequestedUsers { get; set; }
public string ArtistName { get; set; }
public string ArtistId { get; set; }
public List<string> UsersToNotify { get; private set; }

[JsonIgnore]
public List<string> AllUsers
Expand Down Expand Up @@ -68,21 +67,6 @@ public bool UserHasRequested(string username)
{
return AllUsers.Any(x => x.Equals(username, StringComparison.OrdinalIgnoreCase));
}

public void AddUserToNotification(string username)
{
if (UsersToNotify == null)
{
UsersToNotify = new List<string>();
}
if (UsersToNotify.FirstOrDefault(x => x == username) != null)
{
// User already exists in the notification list
return;
}

UsersToNotify.Add(username);
}
}

public enum RequestType
Expand Down
9 changes: 8 additions & 1 deletion PlexRequests.Store/SqlTables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,11 @@ CREATE TABLE IF NOT EXISTS ScheduledJobs
Name varchar(100) NOT NULL,
LastRun varchar(100) NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS ScheduledJobs_Id ON ScheduledJobs (Id);
CREATE UNIQUE INDEX IF NOT EXISTS ScheduledJobs_Id ON ScheduledJobs (Id);

CREATE TABLE IF NOT EXISTS UsersToNotify
(
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Username varchar(100) NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS UsersToNotify_Id ON UsersToNotify (Id);
1 change: 1 addition & 0 deletions PlexRequests.UI/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ protected override void ConfigureRequestContainer(TinyIoCContainer container, Na

// Repo's
container.Register<IRepository<LogEntity>, GenericRepository<LogEntity>>();
container.Register<IRepository<UsersToNotify>, GenericRepository<UsersToNotify>>();
container.Register<IRepository<ScheduledJobs>, GenericRepository<ScheduledJobs>>();
container.Register<IRequestService, JsonRequestService>();
container.Register<ISettingsRepository, SettingsJsonRepository>();
Expand Down
52 changes: 41 additions & 11 deletions PlexRequests.UI/Content/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ $(function () {
});
focusSearch($('li.active a', '#nav-tabs').first().attr('href'));

// Get the user notification setting
var url = createBaseUrl(base, '/search/notifyuser/');
$.ajax({
type: "get",
url: url,
dataType: "json",
success: function (response) {
$('#notifyUser').prop("checked", response);
},
error: function (e) {
console.log(e);
generateNotify("Something went wrong!", "danger");
}
});

// Type in movie search
$("#movieSearchContent").on("input", function () {
if (searchTimer) {
Expand Down Expand Up @@ -80,10 +95,6 @@ $(function () {
data = data + "&seasons=first";
}

var $notify = $('#notifyUser').is(':checked');

data = data + "&notify=" + $notify;

var type = $form.prop('method');
var url = $form.prop('action');

Expand Down Expand Up @@ -117,10 +128,6 @@ $(function () {
var url = $form.prop('action');
var data = $form.serialize();

var $notify = $('#notifyUser').is(':checked');

data = data + "&notify=" + $notify;

sendRequestAjax(data, type, url, buttonId);

});
Expand All @@ -142,13 +149,36 @@ $(function () {
var type = $form.prop('method');
var url = $form.prop('action');
var data = $form.serialize();
var $notify = $('#notifyUser').is(':checked');

data = data + "&notify=" + $notify;

sendRequestAjax(data, type, url, buttonId);
});

// Enable/Disable user notifications
$('#saveNotificationSettings')
.click(function (e) {
e.preventDefault();
var url = createBaseUrl(base, '/search/notifyuser/');
var checked = $('#notifyUser').prop('checked');
$.ajax({
type: "post",
url: url,
data: {notify: checked},
dataType: "json",
success: function (response) {
console.log(response);
if (response.result === true) {
generateNotify(response.message || "Success!", "success");
} else {
generateNotify(response.message, "warning");
}
},
error: function (e) {
console.log(e);
generateNotify("Something went wrong!", "danger");
}
});
});

function focusSearch($content) {
if ($content.length > 0) {
$('input[type=text].form-control', $content).first().focus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public SwaggerModelData GetModelData()
with.Property(x => x.RequestedDate).Description("The date if the request, if this is not set, the request date will be set at the time of the Api call");
with.Property(x => x.RequestedUsers).Description("A collection of the requested users").Required(true);
with.Property(x => x.Type).Description("The type of request: Movie = 0, TvShow = 1, Album = 2").Required(true);
with.Property(x => x.UsersToNotify).Description("A list of Plex users to notify");
});
}
}
Expand Down
6 changes: 5 additions & 1 deletion PlexRequests.UI/Modules/AdminModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,11 @@ private Negotiator Status()
private Response AutoUpdate()
{
var url = Request.Form["url"];
var startInfo = new ProcessStartInfo("PlexRequests.Updater.exe") { Arguments = url};

var startInfo = Type.GetType("Mono.Runtime") != null
? new ProcessStartInfo("mono PlexRequests.Updater.exe") { Arguments = url }
: new ProcessStartInfo("PlexRequests.Updater.exe") { Arguments = url };

Process.Start(startInfo);

Environment.Exit(0);
Expand Down
Loading

0 comments on commit e49b160

Please sign in to comment.