Fixed the multiple notifications (I think)

pull/3314/head
Jamie Rees 5 years ago
parent 8baa3c1f8c
commit 1069ddf9cf

@ -0,0 +1,10 @@
using Ombi.Store.Entities;
namespace Ombi.Schedule.Jobs.Plex.Models
{
public class AvailabilityModel
{
public int Id { get; set; }
public string RequestedUser { get; set; }
}
}

@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging;
using Ombi.Core.Notifications; using Ombi.Core.Notifications;
using Ombi.Helpers; using Ombi.Helpers;
using Ombi.Notifications.Models; using Ombi.Notifications.Models;
using Ombi.Schedule.Jobs.Plex.Models;
using Ombi.Store.Entities; using Ombi.Store.Entities;
using Ombi.Store.Entities.Requests; using Ombi.Store.Entities.Requests;
using Ombi.Store.Repository; using Ombi.Store.Repository;
@ -99,6 +100,7 @@ namespace Ombi.Schedule.Jobs.Plex
} }
var availableEpisode = new List<AvailabilityModel>();
foreach (var season in child.SeasonRequests) foreach (var season in child.SeasonRequests)
{ {
foreach (var episode in season.Episodes) foreach (var episode in season.Episodes)
@ -113,19 +115,28 @@ namespace Ombi.Schedule.Jobs.Plex
if (foundEp != null) if (foundEp != null)
{ {
availableEpisode.Add(new AvailabilityModel
{
Id = episode.Id
});
episode.Available = true; episode.Available = true;
} }
} }
} }
//TODO Partial avilability notifications here
foreach(var c in availableEpisode)
{
await _tvRepo.MarkEpisodeAsAvailable(c.Id);
}
// Check to see if all of the episodes in all seasons are available for this request // Check to see if all of the episodes in all seasons are available for this request
var allAvailable = child.SeasonRequests.All(x => x.Episodes.All(c => c.Available)); var allAvailable = child.SeasonRequests.All(x => x.Episodes.All(c => c.Available));
if (allAvailable) if (allAvailable)
{ {
_log.LogInformation("[PAC] - Child request {0} is now available, sending notification", $"{child.Title} - {child.Id}"); _log.LogInformation("[PAC] - Child request {0} is now available, sending notification", $"{child.Title} - {child.Id}");
// We have ful-fulled this request! // We have ful-fulled this request!
child.Available = true; await _tvRepo.MarkChildAsAvailable(child.Id);
child.MarkedAsAvailable = DateTime.Now;
await _notificationService.Publish(new NotificationOptions await _notificationService.Publish(new NotificationOptions
{ {
DateTime = DateTime.Now, DateTime = DateTime.Now,
@ -144,9 +155,15 @@ namespace Ombi.Schedule.Jobs.Plex
{ {
// Get all non available // Get all non available
var movies = _movieRepo.GetAll().Include(x => x.RequestedUser).Where(x => !x.Available); var movies = _movieRepo.GetAll().Include(x => x.RequestedUser).Where(x => !x.Available);
var itemsForAvailbility = new List<AvailabilityModel>();
foreach (var movie in movies) foreach (var movie in movies)
{ {
if (movie.Available)
{
return;
}
PlexServerContent item = null; PlexServerContent item = null;
if (movie.ImdbId.HasValue()) if (movie.ImdbId.HasValue())
{ {
@ -164,24 +181,29 @@ namespace Ombi.Schedule.Jobs.Plex
// We don't yet have this // We don't yet have this
continue; continue;
} }
_log.LogInformation("[PAC] - Movie request {0} is now available, sending notification", $"{movie.Title} - {movie.Id}");
itemsForAvailbility.Add(new AvailabilityModel
{
Id = movie.Id,
RequestedUser = movie.RequestedUser != null ? movie.RequestedUser.Email : string.Empty
});
}
movie.Available = true; foreach (var i in itemsForAvailbility)
movie.MarkedAsAvailable = DateTime.Now; {
item.RequestId = movie.Id; await _movieRepo.MarkAsAvailable(i.Id);
_log.LogInformation("[PAC] - Movie request {0} is now available, sending notification", $"{movie.Title} - {movie.Id}");
await _notificationService.Publish(new NotificationOptions await _notificationService.Publish(new NotificationOptions
{ {
DateTime = DateTime.Now, DateTime = DateTime.Now,
NotificationType = NotificationType.RequestAvailable, NotificationType = NotificationType.RequestAvailable,
RequestId = movie.Id, RequestId = i.Id,
RequestType = RequestType.Movie, RequestType = RequestType.Movie,
Recipient = movie.RequestedUser != null ? movie.RequestedUser.Email : string.Empty Recipient = i.RequestedUser
}); });
} }
await _movieRepo.Save();
await _repo.SaveChangesAsync(); await _repo.SaveChangesAsync();
} }

@ -6,6 +6,7 @@ using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using Ombi.Store.Entities; using Ombi.Store.Entities;
using Ombi.Store.Entities.Requests; using Ombi.Store.Entities.Requests;
using Ombi.Store.Repository.Requests;
namespace Ombi.Store.Context namespace Ombi.Store.Context
{ {
@ -27,6 +28,7 @@ namespace Ombi.Store.Context
DbSet<AlbumRequest> AlbumRequests { get; set; } DbSet<AlbumRequest> AlbumRequests { get; set; }
DbSet<TvRequests> TvRequests { get; set; } DbSet<TvRequests> TvRequests { get; set; }
DbSet<ChildRequests> ChildRequests { get; set; } DbSet<ChildRequests> ChildRequests { get; set; }
DbSet<EpisodeRequests> EpisodeRequests { get; set; }
DbSet<Issues> Issues { get; set; } DbSet<Issues> Issues { get; set; }
DbSet<IssueCategory> IssueCategories { get; set; } DbSet<IssueCategory> IssueCategories { get; set; }
DbSet<Tokens> Tokens { get; set; } DbSet<Tokens> Tokens { get; set; }

@ -7,6 +7,7 @@ using Microsoft.EntityFrameworkCore;
using Ombi.Helpers; using Ombi.Helpers;
using Ombi.Store.Entities; using Ombi.Store.Entities;
using Ombi.Store.Entities.Requests; using Ombi.Store.Entities.Requests;
using Ombi.Store.Repository.Requests;
namespace Ombi.Store.Context namespace Ombi.Store.Context
{ {
@ -38,6 +39,7 @@ namespace Ombi.Store.Context
public DbSet<AlbumRequest> AlbumRequests { get; set; } public DbSet<AlbumRequest> AlbumRequests { get; set; }
public DbSet<TvRequests> TvRequests { get; set; } public DbSet<TvRequests> TvRequests { get; set; }
public DbSet<ChildRequests> ChildRequests { get; set; } public DbSet<ChildRequests> ChildRequests { get; set; }
public DbSet<EpisodeRequests> EpisodeRequests { get; set; }
public DbSet<Issues> Issues { get; set; } public DbSet<Issues> Issues { get; set; }
public DbSet<IssueCategory> IssueCategories { get; set; } public DbSet<IssueCategory> IssueCategories { get; set; }

@ -10,6 +10,7 @@ namespace Ombi.Store.Repository.Requests
MovieRequests GetRequest(int theMovieDbId); MovieRequests GetRequest(int theMovieDbId);
Task Update(MovieRequests request); Task Update(MovieRequests request);
Task Save(); Task Save();
Task MarkAsAvailable(int id);
IQueryable<MovieRequests> GetWithUser(); IQueryable<MovieRequests> GetWithUser();
IQueryable<MovieRequests> GetWithUser(string userId); IQueryable<MovieRequests> GetWithUser(string userId);
IQueryable<MovieRequests> GetAll(string userId); IQueryable<MovieRequests> GetAll(string userId);

@ -23,6 +23,8 @@ namespace Ombi.Store.Repository.Requests
Task UpdateChild(ChildRequests request); Task UpdateChild(ChildRequests request);
IQueryable<ChildRequests> GetChild(); IQueryable<ChildRequests> GetChild();
IQueryable<ChildRequests> GetChild(string userId); IQueryable<ChildRequests> GetChild(string userId);
Task MarkEpisodeAsAvailable(int id);
Task MarkChildAsAvailable(int id);
Task Save(); Task Save();
Task DeleteChildRange(IEnumerable<ChildRequests> request); Task DeleteChildRange(IEnumerable<ChildRequests> request);
} }

@ -54,6 +54,14 @@ namespace Ombi.Store.Repository.Requests
.AsQueryable(); .AsQueryable();
} }
public async Task MarkAsAvailable(int id)
{
var movieRequest = new MovieRequests{ Id = id, Available = true, MarkedAsAvailable = DateTime.UtcNow};
var attached = Db.MovieRequests.Attach(movieRequest);
attached.Property(x => x.Available).IsModified = true;
attached.Property(x => x.MarkedAsAvailable).IsModified = true;
await Db.SaveChangesAsync();
}
public IQueryable<MovieRequests> GetWithUser(string userId) public IQueryable<MovieRequests> GetWithUser(string userId)
{ {

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -100,6 +101,23 @@ namespace Ombi.Store.Repository.Requests
.AsQueryable(); .AsQueryable();
} }
public async Task MarkChildAsAvailable(int id)
{
var request = new ChildRequests { Id = id, Available = true, MarkedAsAvailable = DateTime.UtcNow };
var attached = Db.ChildRequests.Attach(request);
attached.Property(x => x.Available).IsModified = true;
attached.Property(x => x.MarkedAsAvailable).IsModified = true;
await Db.SaveChangesAsync();
}
public async Task MarkEpisodeAsAvailable(int id)
{
var request = new EpisodeRequests { Id = id, Available = true };
var attached = Db.EpisodeRequests.Attach(request);
attached.Property(x => x.Available).IsModified = true;
await Db.SaveChangesAsync();
}
public async Task Save() public async Task Save()
{ {
await InternalSaveChanges(); await InternalSaveChanges();
@ -141,10 +159,10 @@ namespace Ombi.Store.Repository.Requests
public async Task Update(TvRequests request) public async Task Update(TvRequests request)
{ {
Db.Update(request); Db.Update(request);
await InternalSaveChanges(); await InternalSaveChanges();
} }
public async Task UpdateChild(ChildRequests request) public async Task UpdateChild(ChildRequests request)
{ {
Db.Update(request); Db.Update(request);

Loading…
Cancel
Save