mirror of https://github.com/Ombi-app/Ombi
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
603 lines
27 KiB
603 lines
27 KiB
9 years ago
|
#region Copyright
|
||
|
// /************************************************************************
|
||
|
// Copyright (c) 2016 Jamie Rees
|
||
|
// File: RequestsModule.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 System;
|
||
8 years ago
|
using System.Collections.Generic;
|
||
9 years ago
|
using System.Linq;
|
||
8 years ago
|
using System.Threading.Tasks;
|
||
9 years ago
|
using Nancy;
|
||
|
using Nancy.Responses.Negotiation;
|
||
|
using NLog;
|
||
8 years ago
|
using Ombi.Api.Interfaces;
|
||
8 years ago
|
using Ombi.Api.Models.Sonarr;
|
||
8 years ago
|
using Ombi.Core;
|
||
|
using Ombi.Core.Models;
|
||
|
using Ombi.Core.SettingModels;
|
||
|
using Ombi.Helpers;
|
||
|
using Ombi.Helpers.Analytics;
|
||
|
using Ombi.Helpers.Permissions;
|
||
|
using Ombi.Services.Interfaces;
|
||
|
using Ombi.Services.Notification;
|
||
|
using Ombi.Store;
|
||
|
using Ombi.UI.Models;
|
||
8 years ago
|
using Ombi.UI.Models.Admin;
|
||
8 years ago
|
using Ombi.UI.Models.Requests;
|
||
8 years ago
|
using Action = Ombi.Helpers.Analytics.Action;
|
||
|
using ISecurityExtensions = Ombi.Core.ISecurityExtensions;
|
||
9 years ago
|
|
||
8 years ago
|
namespace Ombi.UI.Modules
|
||
9 years ago
|
{
|
||
|
public class RequestsModule : BaseAuthModule
|
||
|
{
|
||
|
public RequestsModule(
|
||
|
IRequestService service,
|
||
|
ISettingsService<PlexRequestSettings> prSettings,
|
||
|
ISettingsService<PlexSettings> plex,
|
||
|
INotificationService notify,
|
||
|
ISettingsService<SonarrSettings> sonarrSettings,
|
||
|
ISettingsService<SickRageSettings> sickRageSettings,
|
||
|
ISettingsService<CouchPotatoSettings> cpSettings,
|
||
|
ICouchPotatoApi cpApi,
|
||
|
ISonarrApi sonarrApi,
|
||
|
ISickRageApi sickRageApi,
|
||
9 years ago
|
ICacheProvider cache,
|
||
8 years ago
|
IAnalytics an,
|
||
8 years ago
|
IPlexNotificationEngine engine,
|
||
8 years ago
|
IEmbyNotificationEngine embyEngine,
|
||
8 years ago
|
ISecurityExtensions security,
|
||
8 years ago
|
ISettingsService<CustomizationSettings> customSettings,
|
||
8 years ago
|
ISettingsService<EmbySettings> embyS,
|
||
|
ISettingsService<RadarrSettings> radarr,
|
||
|
IRadarrApi radarrApi) : base("requests", prSettings, security)
|
||
9 years ago
|
{
|
||
|
Service = service;
|
||
|
PrSettings = prSettings;
|
||
|
PlexSettings = plex;
|
||
|
NotificationService = notify;
|
||
|
SonarrSettings = sonarrSettings;
|
||
|
SickRageSettings = sickRageSettings;
|
||
|
CpSettings = cpSettings;
|
||
|
SonarrApi = sonarrApi;
|
||
|
SickRageApi = sickRageApi;
|
||
|
CpApi = cpApi;
|
||
|
Cache = cache;
|
||
9 years ago
|
Analytics = an;
|
||
8 years ago
|
PlexNotificationEngine = engine;
|
||
|
EmbyNotificationEngine = embyEngine;
|
||
8 years ago
|
CustomizationSettings = customSettings;
|
||
8 years ago
|
EmbySettings = embyS;
|
||
8 years ago
|
Radarr = radarr;
|
||
|
RadarrApi = radarrApi;
|
||
9 years ago
|
|
||
|
Get["/", true] = async (x, ct) => await LoadRequests();
|
||
|
Get["/movies", true] = async (x, ct) => await GetMovies();
|
||
|
Get["/tvshows", true] = async (c, ct) => await GetTvShows();
|
||
|
Get["/albums", true] = async (x, ct) => await GetAlbumRequests();
|
||
|
Post["/delete", true] = async (x, ct) => await DeleteRequest((int)Request.Form.id);
|
||
|
Post["/reportissue", true] = async (x, ct) => await ReportIssue((int)Request.Form.requestId, (IssueState)(int)Request.Form.issue, null);
|
||
|
Post["/reportissuecomment", true] = async (x, ct) => await ReportIssue((int)Request.Form.requestId, IssueState.Other, (string)Request.Form.commentArea);
|
||
|
|
||
|
Post["/clearissues", true] = async (x, ct) => await ClearIssue((int)Request.Form.Id);
|
||
|
|
||
|
Post["/changeavailability", true] = async (x, ct) => await ChangeRequestAvailability((int)Request.Form.Id, (bool)Request.Form.Available);
|
||
8 years ago
|
|
||
8 years ago
|
Post["/changeRootFoldertv", true] = async (x, ct) => await ChangeRootFolder(RequestType.TvShow, (int)Request.Form.requestId, (int)Request.Form.rootFolderId);
|
||
|
Post["/changeRootFoldermovie", true] = async (x, ct) => await ChangeRootFolder(RequestType.Movie, (int)Request.Form.requestId, (int)Request.Form.rootFolderId);
|
||
8 years ago
|
|
||
8 years ago
|
Get["/UpdateFilters", true] = async (x, ct) => await GetFilterAndSortSettings();
|
||
9 years ago
|
}
|
||
|
|
||
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||
|
private IRequestService Service { get; }
|
||
9 years ago
|
private IAnalytics Analytics { get; }
|
||
9 years ago
|
private INotificationService NotificationService { get; }
|
||
|
private ISettingsService<PlexRequestSettings> PrSettings { get; }
|
||
|
private ISettingsService<PlexSettings> PlexSettings { get; }
|
||
|
private ISettingsService<SonarrSettings> SonarrSettings { get; }
|
||
|
private ISettingsService<SickRageSettings> SickRageSettings { get; }
|
||
|
private ISettingsService<CouchPotatoSettings> CpSettings { get; }
|
||
8 years ago
|
private ISettingsService<CustomizationSettings> CustomizationSettings { get; }
|
||
8 years ago
|
private ISettingsService<RadarrSettings> Radarr { get; }
|
||
8 years ago
|
private ISettingsService<EmbySettings> EmbySettings { get; }
|
||
9 years ago
|
private ISonarrApi SonarrApi { get; }
|
||
8 years ago
|
private IRadarrApi RadarrApi { get; }
|
||
9 years ago
|
private ISickRageApi SickRageApi { get; }
|
||
|
private ICouchPotatoApi CpApi { get; }
|
||
|
private ICacheProvider Cache { get; }
|
||
8 years ago
|
private INotificationEngine PlexNotificationEngine { get; }
|
||
|
private INotificationEngine EmbyNotificationEngine { get; }
|
||
9 years ago
|
|
||
|
private async Task<Negotiator> LoadRequests()
|
||
|
{
|
||
|
var settings = await PrSettings.GetSettingsAsync();
|
||
8 years ago
|
var custom = await CustomizationSettings.GetSettingsAsync();
|
||
|
|
||
|
return View["Index", new RequestsIndexViewModel { CustomizationSettings = custom, PlexRequestSettings = settings }];
|
||
9 years ago
|
}
|
||
|
|
||
|
private async Task<Response> GetMovies()
|
||
|
{
|
||
|
var allRequests = await Service.GetAllAsync();
|
||
|
allRequests = allRequests.Where(x => x.Type == RequestType.Movie);
|
||
|
|
||
|
var dbMovies = allRequests.ToList();
|
||
|
|
||
8 years ago
|
if (Security.HasPermissions(User, Permissions.UsersCanViewOnlyOwnRequests) && !IsAdmin)
|
||
9 years ago
|
{
|
||
|
dbMovies = dbMovies.Where(x => x.UserHasRequested(Username)).ToList();
|
||
|
}
|
||
|
|
||
|
List<QualityModel> qualities = new List<QualityModel>();
|
||
8 years ago
|
var rootFolders = new List<RootFolderModel>();
|
||
9 years ago
|
|
||
8 years ago
|
var radarr = await Radarr.GetSettingsAsync();
|
||
9 years ago
|
if (IsAdmin)
|
||
|
{
|
||
8 years ago
|
try
|
||
9 years ago
|
{
|
||
8 years ago
|
var cpSettings = await CpSettings.GetSettingsAsync();
|
||
|
if (cpSettings.Enabled)
|
||
9 years ago
|
{
|
||
8 years ago
|
try
|
||
9 years ago
|
{
|
||
8 years ago
|
var result = await Cache.GetOrSetAsync(CacheKeys.CouchPotatoQualityProfiles, async () =>
|
||
|
{
|
||
|
return
|
||
|
await Task.Run(() => CpApi.GetProfiles(cpSettings.FullUri, cpSettings.ApiKey))
|
||
|
.ConfigureAwait(false);
|
||
|
});
|
||
|
if (result != null)
|
||
|
{
|
||
|
qualities =
|
||
8 years ago
|
result.list.Select(x => new QualityModel { Id = x._id, Name = x.label }).ToList();
|
||
8 years ago
|
}
|
||
|
}
|
||
|
catch (Exception e)
|
||
9 years ago
|
{
|
||
8 years ago
|
Log.Info(e);
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
8 years ago
|
if (radarr.Enabled)
|
||
9 years ago
|
{
|
||
8 years ago
|
var rootFoldersResult = await Cache.GetOrSetAsync(CacheKeys.RadarrRootFolders, async () =>
|
||
|
{
|
||
|
return await Task.Run(() => RadarrApi.GetRootFolders(radarr.ApiKey, radarr.FullUri));
|
||
|
});
|
||
|
|
||
|
rootFolders =
|
||
|
rootFoldersResult.Select(
|
||
8 years ago
|
x => new RootFolderModel { Id = x.id.ToString(), Path = x.path, FreeSpace = x.freespace })
|
||
8 years ago
|
.ToList();
|
||
|
|
||
|
var result = await Cache.GetOrSetAsync(CacheKeys.RadarrQualityProfiles, async () =>
|
||
|
{
|
||
|
return await Task.Run(() => RadarrApi.GetProfiles(radarr.ApiKey, radarr.FullUri));
|
||
|
});
|
||
|
qualities = result.Select(x => new QualityModel { Id = x.id.ToString(), Name = x.name }).ToList();
|
||
9 years ago
|
}
|
||
|
}
|
||
8 years ago
|
catch (Exception e)
|
||
|
{
|
||
|
Log.Error(e);
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
|
||
8 years ago
|
var canManageRequest = Security.HasAnyPermissions(User, Permissions.Administrator, Permissions.ManageRequests);
|
||
8 years ago
|
var allowViewUsers = Security.HasAnyPermissions(User, Permissions.Administrator, Permissions.ViewUsers);
|
||
|
|
||
9 years ago
|
var viewModel = dbMovies.Select(movie => new RequestViewModel
|
||
|
{
|
||
|
ProviderId = movie.ProviderId,
|
||
|
Type = movie.Type,
|
||
|
Status = movie.Status,
|
||
|
ImdbId = movie.ImdbId,
|
||
|
Id = movie.Id,
|
||
|
PosterPath = movie.PosterPath,
|
||
|
ReleaseDate = movie.ReleaseDate,
|
||
|
ReleaseDateTicks = movie.ReleaseDate.Ticks,
|
||
|
RequestedDate = movie.RequestedDate,
|
||
|
Released = DateTime.Now > movie.ReleaseDate,
|
||
|
RequestedDateTicks = DateTimeHelper.OffsetUTCDateTime(movie.RequestedDate, DateTimeOffset).Ticks,
|
||
|
Approved = movie.Available || movie.Approved,
|
||
|
Title = movie.Title,
|
||
|
Overview = movie.Overview,
|
||
8 years ago
|
RequestedUsers = canManageRequest || allowViewUsers ? movie.AllUsers.ToArray() : new string[] { },
|
||
9 years ago
|
ReleaseYear = movie.ReleaseDate.Year.ToString(),
|
||
|
Available = movie.Available,
|
||
8 years ago
|
Admin = canManageRequest,
|
||
9 years ago
|
IssueId = movie.IssueId,
|
||
8 years ago
|
Denied = movie.Denied,
|
||
|
DeniedReason = movie.DeniedReason,
|
||
8 years ago
|
Qualities = qualities.ToArray(),
|
||
8 years ago
|
HasRootFolders = rootFolders.Any(),
|
||
|
RootFolders = rootFolders.ToArray(),
|
||
|
CurrentRootPath = radarr.Enabled ? GetRootPath(movie.RootFolderSelected, radarr).Result : null
|
||
9 years ago
|
}).ToList();
|
||
|
|
||
|
return Response.AsJson(viewModel);
|
||
|
}
|
||
|
|
||
|
private async Task<Response> GetTvShows()
|
||
|
{
|
||
|
var requests = await Service.GetAllAsync();
|
||
|
requests = requests.Where(x => x.Type == RequestType.TvShow);
|
||
|
|
||
|
var dbTv = requests;
|
||
8 years ago
|
if (Security.HasPermissions(User, Permissions.UsersCanViewOnlyOwnRequests) && !IsAdmin)
|
||
9 years ago
|
{
|
||
|
dbTv = dbTv.Where(x => x.UserHasRequested(Username)).ToList();
|
||
|
}
|
||
|
|
||
|
IEnumerable<QualityModel> qualities = new List<QualityModel>();
|
||
8 years ago
|
IEnumerable<RootFolderModel> rootFolders = new List<RootFolderModel>();
|
||
|
|
||
|
var sonarrSettings = await SonarrSettings.GetSettingsAsync();
|
||
9 years ago
|
if (IsAdmin)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
if (sonarrSettings.Enabled)
|
||
|
{
|
||
8 years ago
|
var result = await Cache.GetOrSetAsync(CacheKeys.SonarrQualityProfiles, async () =>
|
||
9 years ago
|
{
|
||
|
return await Task.Run(() => SonarrApi.GetProfiles(sonarrSettings.ApiKey, sonarrSettings.FullUri));
|
||
|
});
|
||
8 years ago
|
qualities = result.Select(x => new QualityModel { Id = x.id.ToString(), Name = x.name }).ToList();
|
||
|
|
||
8 years ago
|
|
||
|
var rootFoldersResult = await Cache.GetOrSetAsync(CacheKeys.SonarrRootFolders, async () =>
|
||
|
{
|
||
|
return await Task.Run(() => SonarrApi.GetRootFolders(sonarrSettings.ApiKey, sonarrSettings.FullUri));
|
||
|
});
|
||
|
|
||
|
rootFolders = rootFoldersResult.Select(x => new RootFolderModel { Id = x.id.ToString(), Path = x.path, FreeSpace = x.freespace }).ToList();
|
||
|
}
|
||
9 years ago
|
else
|
||
|
{
|
||
8 years ago
|
var sickRageSettings = await SickRageSettings.GetSettingsAsync();
|
||
9 years ago
|
if (sickRageSettings.Enabled)
|
||
|
{
|
||
8 years ago
|
qualities = sickRageSettings.Qualities.Select(x => new QualityModel { Id = x.Key, Name = x.Value }).ToList();
|
||
9 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
8 years ago
|
Log.Info(e);
|
||
9 years ago
|
}
|
||
|
|
||
|
}
|
||
|
|
||
8 years ago
|
|
||
8 years ago
|
|
||
8 years ago
|
var canManageRequest = Security.HasAnyPermissions(User, Permissions.Administrator, Permissions.ManageRequests);
|
||
8 years ago
|
var allowViewUsers = Security.HasAnyPermissions(User, Permissions.Administrator, Permissions.ViewUsers);
|
||
|
|
||
8 years ago
|
var viewModel = dbTv.Select(tv => new RequestViewModel
|
||
9 years ago
|
{
|
||
8 years ago
|
ProviderId = tv.ProviderId,
|
||
|
Type = tv.Type,
|
||
|
Status = tv.Status,
|
||
|
ImdbId = tv.ImdbId,
|
||
|
Id = tv.Id,
|
||
8 years ago
|
PosterPath = tv.PosterPath?.Contains("http:") ?? false ? tv.PosterPath?.Replace("http:", "https:") : tv.PosterPath ?? string.Empty, // We make the poster path https on request, but this is just incase
|
||
8 years ago
|
ReleaseDate = tv.ReleaseDate,
|
||
|
ReleaseDateTicks = tv.ReleaseDate.Ticks,
|
||
|
RequestedDate = tv.RequestedDate,
|
||
|
RequestedDateTicks = DateTimeHelper.OffsetUTCDateTime(tv.RequestedDate, DateTimeOffset).Ticks,
|
||
|
Released = DateTime.Now > tv.ReleaseDate,
|
||
|
Approved = tv.Available || tv.Approved,
|
||
|
Title = tv.Title,
|
||
|
Overview = tv.Overview,
|
||
8 years ago
|
RequestedUsers = canManageRequest || allowViewUsers ? tv.AllUsers.ToArray() : new string[] { },
|
||
8 years ago
|
ReleaseYear = tv.ReleaseDate.Year.ToString(),
|
||
|
Available = tv.Available,
|
||
8 years ago
|
Admin = canManageRequest,
|
||
8 years ago
|
IssueId = tv.IssueId,
|
||
8 years ago
|
Denied = tv.Denied,
|
||
|
DeniedReason = tv.DeniedReason,
|
||
8 years ago
|
TvSeriesRequestType = tv.SeasonsRequested,
|
||
|
Qualities = qualities.ToArray(),
|
||
8 years ago
|
Episodes = tv.Episodes.ToArray(),
|
||
8 years ago
|
RootFolders = rootFolders.ToArray(),
|
||
8 years ago
|
HasRootFolders = rootFolders.Any(),
|
||
8 years ago
|
CurrentRootPath = sonarrSettings.Enabled ? GetRootPath(tv.RootFolderSelected, sonarrSettings).Result : null
|
||
9 years ago
|
}).ToList();
|
||
|
|
||
|
return Response.AsJson(viewModel);
|
||
|
}
|
||
|
|
||
8 years ago
|
private async Task<string> GetRootPath(int pathId, SonarrSettings sonarrSettings)
|
||
|
{
|
||
|
var rootFoldersResult = await Cache.GetOrSetAsync(CacheKeys.SonarrRootFolders, async () =>
|
||
|
{
|
||
|
return await Task.Run(() => SonarrApi.GetRootFolders(sonarrSettings.ApiKey, sonarrSettings.FullUri));
|
||
|
});
|
||
|
|
||
|
foreach (var r in rootFoldersResult.Where(r => r.id == pathId))
|
||
|
{
|
||
|
return r.path;
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
int outRoot;
|
||
|
var defaultPath = int.TryParse(sonarrSettings.RootPath, out outRoot);
|
||
|
|
||
|
if (defaultPath)
|
||
|
{
|
||
|
// Return default path
|
||
8 years ago
|
return rootFoldersResult.FirstOrDefault(x => x.id.Equals(outRoot))?.path ?? string.Empty;
|
||
8 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
return rootFoldersResult.FirstOrDefault()?.path ?? string.Empty;
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
private async Task<string> GetRootPath(int pathId, RadarrSettings radarrSettings)
|
||
|
{
|
||
|
var rootFoldersResult = await Cache.GetOrSetAsync(CacheKeys.RadarrRootFolders, async () =>
|
||
|
{
|
||
|
return await Task.Run(() => RadarrApi.GetRootFolders(radarrSettings.ApiKey, radarrSettings.FullUri));
|
||
|
});
|
||
|
|
||
|
foreach (var r in rootFoldersResult.Where(r => r.id == pathId))
|
||
|
{
|
||
|
return r.path;
|
||
|
}
|
||
|
|
||
|
int outRoot;
|
||
|
var defaultPath = int.TryParse(radarrSettings.RootPath, out outRoot);
|
||
|
|
||
|
if (defaultPath)
|
||
|
{
|
||
|
// Return default path
|
||
|
return rootFoldersResult.FirstOrDefault(x => x.id.Equals(outRoot))?.path ?? string.Empty;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return rootFoldersResult.FirstOrDefault()?.path ?? string.Empty;
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
private async Task<Response> GetAlbumRequests()
|
||
|
{
|
||
|
var dbAlbum = await Service.GetAllAsync();
|
||
|
dbAlbum = dbAlbum.Where(x => x.Type == RequestType.Album);
|
||
8 years ago
|
if (Security.HasPermissions(User, Permissions.UsersCanViewOnlyOwnRequests) && !IsAdmin)
|
||
9 years ago
|
{
|
||
|
dbAlbum = dbAlbum.Where(x => x.UserHasRequested(Username));
|
||
|
}
|
||
8 years ago
|
var canManageRequest = Security.HasAnyPermissions(User, Permissions.Administrator, Permissions.ManageRequests);
|
||
9 years ago
|
var viewModel = dbAlbum.Select(album =>
|
||
|
{
|
||
|
return new RequestViewModel
|
||
|
{
|
||
|
ProviderId = album.ProviderId,
|
||
|
Type = album.Type,
|
||
|
Status = album.Status,
|
||
|
ImdbId = album.ImdbId,
|
||
|
Id = album.Id,
|
||
|
PosterPath = album.PosterPath,
|
||
|
ReleaseDate = album.ReleaseDate,
|
||
|
ReleaseDateTicks = album.ReleaseDate.Ticks,
|
||
|
RequestedDate = album.RequestedDate,
|
||
|
RequestedDateTicks = DateTimeHelper.OffsetUTCDateTime(album.RequestedDate, DateTimeOffset).Ticks,
|
||
|
Released = DateTime.Now > album.ReleaseDate,
|
||
|
Approved = album.Available || album.Approved,
|
||
|
Title = album.Title,
|
||
|
Overview = album.Overview,
|
||
8 years ago
|
RequestedUsers = canManageRequest ? album.AllUsers.ToArray() : new string[] { },
|
||
9 years ago
|
ReleaseYear = album.ReleaseDate.Year.ToString(),
|
||
|
Available = album.Available,
|
||
8 years ago
|
Admin = canManageRequest,
|
||
9 years ago
|
IssueId = album.IssueId,
|
||
8 years ago
|
Denied = album.Denied,
|
||
|
DeniedReason = album.DeniedReason,
|
||
9 years ago
|
TvSeriesRequestType = album.SeasonsRequested,
|
||
|
MusicBrainzId = album.MusicBrainzId,
|
||
|
ArtistName = album.ArtistName
|
||
|
|
||
|
};
|
||
|
}).ToList();
|
||
|
|
||
|
return Response.AsJson(viewModel);
|
||
|
}
|
||
|
|
||
|
private async Task<Response> DeleteRequest(int requestid)
|
||
|
{
|
||
8 years ago
|
if (!Security.HasAnyPermissions(User, Permissions.Administrator, Permissions.ManageRequests))
|
||
|
{
|
||
|
return Response.AsJson(new JsonResponseModel { Result = true });
|
||
|
}
|
||
|
|
||
8 years ago
|
|
||
9 years ago
|
Analytics.TrackEventAsync(Category.Requests, Action.Delete, "Delete Request", Username, CookieHelper.GetAnalyticClientId(Cookies));
|
||
9 years ago
|
|
||
|
var currentEntity = await Service.GetAsync(requestid);
|
||
|
await Service.DeleteRequestAsync(currentEntity);
|
||
|
return Response.AsJson(new JsonResponseModel { Result = true });
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Reports the issue.
|
||
|
/// Comment can be null if the <c>IssueState != Other</c>
|
||
|
/// </summary>
|
||
|
/// <param name="requestId">The request identifier.</param>
|
||
|
/// <param name="issue">The issue.</param>
|
||
|
/// <param name="comment">The comment.</param>
|
||
|
/// <returns></returns>
|
||
|
private async Task<Response> ReportIssue(int requestId, IssueState issue, string comment)
|
||
|
{
|
||
8 years ago
|
if (!Security.HasPermissions(User, Permissions.ReportIssue))
|
||
|
{
|
||
|
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Sorry, you do not have the correct permissions to report an issue." });
|
||
|
}
|
||
9 years ago
|
var originalRequest = await Service.GetAsync(requestId);
|
||
|
if (originalRequest == null)
|
||
|
{
|
||
|
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Could not add issue, please try again or contact the administrator!" });
|
||
|
}
|
||
|
originalRequest.Issues = issue;
|
||
|
originalRequest.OtherMessage = !string.IsNullOrEmpty(comment)
|
||
|
? $"{Username} - {comment}"
|
||
|
: string.Empty;
|
||
|
|
||
|
|
||
|
var result = await Service.UpdateRequestAsync(originalRequest);
|
||
|
|
||
|
var model = new NotificationModel
|
||
|
{
|
||
|
User = Username,
|
||
|
NotificationType = NotificationType.Issue,
|
||
|
Title = originalRequest.Title,
|
||
|
DateTime = DateTime.Now,
|
||
8 years ago
|
Body = issue == IssueState.Other ? comment : issue.ToString().ToCamelCaseWords(),
|
||
|
ImgSrc = originalRequest.Type == RequestType.Movie ? $"https://image.tmdb.org/t/p/w300/{originalRequest.PosterPath}" : originalRequest.PosterPath
|
||
9 years ago
|
};
|
||
|
await NotificationService.Publish(model);
|
||
|
|
||
|
return Response.AsJson(result
|
||
|
? new JsonResponseModel { Result = true }
|
||
|
: new JsonResponseModel { Result = false, Message = "Could not add issue, please try again or contact the administrator!" });
|
||
|
}
|
||
|
|
||
|
private async Task<Response> ClearIssue(int requestId)
|
||
|
{
|
||
8 years ago
|
if (!Security.HasAnyPermissions(User, Permissions.Administrator, Permissions.ManageRequests))
|
||
|
{
|
||
|
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Sorry, you do not have the correct permissions to clear an issue." });
|
||
|
}
|
||
9 years ago
|
|
||
|
var originalRequest = await Service.GetAsync(requestId);
|
||
|
if (originalRequest == null)
|
||
|
{
|
||
|
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Request does not exist to clear it!" });
|
||
|
}
|
||
|
originalRequest.Issues = IssueState.None;
|
||
|
originalRequest.OtherMessage = string.Empty;
|
||
|
|
||
|
var result = await Service.UpdateRequestAsync(originalRequest);
|
||
|
return Response.AsJson(result
|
||
|
? new JsonResponseModel { Result = true }
|
||
|
: new JsonResponseModel { Result = false, Message = "Could not clear issue, please try again or check the logs" });
|
||
|
}
|
||
|
|
||
|
private async Task<Response> ChangeRequestAvailability(int requestId, bool available)
|
||
|
{
|
||
8 years ago
|
if (!Security.HasAnyPermissions(User, Permissions.Administrator, Permissions.ManageRequests))
|
||
|
{
|
||
|
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Sorry, you do not have the correct permissions to change a request." });
|
||
|
}
|
||
8 years ago
|
|
||
9 years ago
|
Analytics.TrackEventAsync(Category.Requests, Action.Update, available ? "Make request available" : "Make request unavailable", Username, CookieHelper.GetAnalyticClientId(Cookies));
|
||
9 years ago
|
var originalRequest = await Service.GetAsync(requestId);
|
||
|
if (originalRequest == null)
|
||
|
{
|
||
|
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Request does not exist to change the availability!" });
|
||
|
}
|
||
|
|
||
|
originalRequest.Available = available;
|
||
|
|
||
|
var result = await Service.UpdateRequestAsync(originalRequest);
|
||
8 years ago
|
|
||
|
var plexSettings = await PlexSettings.GetSettingsAsync();
|
||
|
if (plexSettings.Enable)
|
||
|
{
|
||
|
await
|
||
|
PlexNotificationEngine.NotifyUsers(originalRequest,
|
||
|
available ? NotificationType.RequestAvailable : NotificationType.RequestDeclined);
|
||
|
}
|
||
|
|
||
|
var embySettings = await EmbySettings.GetSettingsAsync();
|
||
|
if (embySettings.Enable)
|
||
|
{
|
||
|
await EmbyNotificationEngine.NotifyUsers(originalRequest,
|
||
|
available ? NotificationType.RequestAvailable : NotificationType.RequestDeclined);
|
||
|
}
|
||
9 years ago
|
return Response.AsJson(result
|
||
|
? new { Result = true, Available = available, Message = string.Empty }
|
||
|
: new { Result = false, Available = false, Message = "Could not update the availability, please try again or check the logs" });
|
||
|
}
|
||
|
|
||
8 years ago
|
private async Task<Response> GetFilterAndSortSettings()
|
||
8 years ago
|
{
|
||
8 years ago
|
var s = await CustomizationSettings.GetSettingsAsync();
|
||
8 years ago
|
|
||
8 years ago
|
var sortVal = EnumHelper<SortOptions>.GetDisplayValue((SortOptions)s.DefaultSort);
|
||
|
var filterVal = EnumHelper<FilterOptions>.GetDisplayValue((FilterOptions)s.DefaultFilter);
|
||
8 years ago
|
|
||
8 years ago
|
var vm = new
|
||
|
{
|
||
|
DefaultSort = sortVal,
|
||
|
DefaultFilter = filterVal
|
||
|
};
|
||
|
|
||
|
return Response.AsJson(vm);
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
private async Task<Response> ChangeRootFolder(RequestType type, int id, int rootFolderId)
|
||
8 years ago
|
{
|
||
8 years ago
|
var rootFolders = new List<SonarrRootFolder>();
|
||
|
if (type == RequestType.TvShow)
|
||
|
{
|
||
|
// Get all root folders
|
||
|
var settings = await SonarrSettings.GetSettingsAsync();
|
||
|
rootFolders = SonarrApi.GetRootFolders(settings.ApiKey, settings.FullUri);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
|
||
|
var settings = await Radarr.GetSettingsAsync();
|
||
|
rootFolders = RadarrApi.GetRootFolders(settings.ApiKey, settings.FullUri);
|
||
|
}
|
||
8 years ago
|
|
||
|
// Get Request
|
||
|
var allRequests = await Service.GetAllAsync();
|
||
|
var request = allRequests.FirstOrDefault(x => x.Id == id);
|
||
|
|
||
|
if (request == null)
|
||
|
{
|
||
8 years ago
|
return Response.AsJson(new JsonResponseModel { Result = false });
|
||
8 years ago
|
}
|
||
|
|
||
|
foreach (var folder in rootFolders)
|
||
|
{
|
||
|
if (folder.id.Equals(rootFolderId))
|
||
|
{
|
||
|
request.RootFolderSelected = folder.id;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
await Service.UpdateRequestAsync(request);
|
||
|
|
||
8 years ago
|
return Response.AsJson(new JsonResponseModel { Result = true });
|
||
|
}
|
||
|
}
|
||
9 years ago
|
}
|