diff --git a/.dockerignore b/.dockerignore index bad90b4e..bb17300e 100644 --- a/.dockerignore +++ b/.dockerignore @@ -3,6 +3,7 @@ !Go/ !Jellyfin/frontend/ !shared/SmartSearch/ +!shared/LiveTv/RecordingOwnerTags.cs !shared/PosterBadges/flags/ !shared/WatchParty/invite.html !shared/WatchParty/preview.png diff --git a/.github/workflows/moonbase-go.yml b/.github/workflows/moonbase-go.yml index 4a640b5a..1d3c65b9 100644 --- a/.github/workflows/moonbase-go.yml +++ b/.github/workflows/moonbase-go.yml @@ -7,10 +7,10 @@ name: Moonbase Go on: push: branches: [master] - paths: ['Go/**', 'shared/SmartSearch/**', 'shared/Seasonal/seasonal-holidays.json', '.github/workflows/moonbase-go.yml'] + paths: ['Go/**', 'shared/SmartSearch/**', 'shared/LiveTv/RecordingOwnerTags.cs', 'shared/Seasonal/seasonal-holidays.json', '.github/workflows/moonbase-go.yml'] pull_request: branches: [master] - paths: ['Go/**', 'shared/SmartSearch/**', 'shared/Seasonal/seasonal-holidays.json', '.github/workflows/moonbase-go.yml'] + paths: ['Go/**', 'shared/SmartSearch/**', 'shared/LiveTv/RecordingOwnerTags.cs', 'shared/Seasonal/seasonal-holidays.json', '.github/workflows/moonbase-go.yml'] permissions: contents: read diff --git a/Emby/Emby.Plugins.Moonfin/Api/LiveTvRecordingsRequests.cs b/Emby/Emby.Plugins.Moonfin/Api/LiveTvRecordingsRequests.cs index c21561ad..cd746867 100644 --- a/Emby/Emby.Plugins.Moonfin/Api/LiveTvRecordingsRequests.cs +++ b/Emby/Emby.Plugins.Moonfin/Api/LiveTvRecordingsRequests.cs @@ -63,4 +63,63 @@ public class SuggestedShowingBody public SuggestedShowing? Showing { get; set; } } + + [Route("/Moonfin/LiveTv/Owners/Record", "POST")] + [Authenticated] + public class RecordOwnerRequest : IReturn, IRequiresRequestStream + { + public System.IO.Stream RequestStream { get; set; } = null!; + } + + [Route("/Moonfin/LiveTv/Owners/RecordSeries", "POST")] + [Authenticated] + public class RecordSeriesOwnerRequest : IReturn, IRequiresRequestStream + { + public System.IO.Stream RequestStream { get; set; } = null!; + } + + [Route("/Moonfin/LiveTv/Owners/Claim", "POST")] + [Authenticated] + public class ClaimOwnerRequest : IReturn, IRequiresRequestStream + { + public System.IO.Stream RequestStream { get; set; } = null!; + } + + [Route("/Moonfin/LiveTv/Owners/Lookup", "POST")] + [Authenticated] + public class LookupOwnersRequest : IReturn, IRequiresRequestStream + { + public System.IO.Stream RequestStream { get; set; } = null!; + } + + [Route("/Moonfin/LiveTv/Owners/Mine", "GET")] + [Authenticated] + public class GetMyOwnedRequest : IReturn { } + + /// Body for recording a program, or its series, as its owner. + public class RecordOwnerBody + { + public string? ProgramId { get; set; } + + public System.Collections.Generic.Dictionary? Settings { get; set; } + } + + /// Body for claiming an existing timer, series rule or recording. + public class ClaimOwnerBody + { + /// timer, series or item. + public string? Kind { get; set; } + + public string? Id { get; set; } + } + + /// Body for looking owners up. + public class LookupOwnersBody + { + public System.Collections.Generic.List? TimerIds { get; set; } + + public System.Collections.Generic.List? SeriesTimerIds { get; set; } + + public System.Collections.Generic.List? ItemIds { get; set; } + } } diff --git a/Emby/Emby.Plugins.Moonfin/Api/LiveTvRecordingsService.cs b/Emby/Emby.Plugins.Moonfin/Api/LiveTvRecordingsService.cs index da2218ce..9c69e16b 100644 --- a/Emby/Emby.Plugins.Moonfin/Api/LiveTvRecordingsService.cs +++ b/Emby/Emby.Plugins.Moonfin/Api/LiveTvRecordingsService.cs @@ -117,6 +117,7 @@ public async Task Post(RequestCompleteShowingRequest request) state = IncompleteRecordingRules.DisplayState(showing), scheduledStart = showing.ScheduledStart, channelName = showing.ScheduledChannelName, + otherOwners = OwnerList(service.OtherOwners(showing, user.Id, OwnersEnabled())), }); } @@ -181,6 +182,160 @@ public async Task Post(SaveIncompleteRecordingSettingsRequest request) return Json(IncompleteRecordingConfig.Read()); } + // ---- Recording owners (My Recordings) ---- + + private static bool OwnersEnabled() => Plugin.Instance?.Configuration?.LiveTvRecordingOwners ?? true; + + private object OwnersNotFound() => Json(404, new { error = "Recording owners are not available" }); + + private static object[] OwnerList(System.Collections.Generic.IEnumerable owners) => + owners.Select(o => (object)new { userId = o.UserId.ToString("N"), name = o.Name, imageTag = o.ImageTag }).ToArray(); + + private static bool ProgramVisible(string programId, Guid userId) + { + var host = PluginServices.RecordingHost; + var program = host?.GetProgram(programId); + return program != null && host!.IsVisibleTo(program.ItemId, userId); + } + + private object OwnershipResult(OwnershipOutcome outcome) => + Json(new { id = outcome.Id, owners = OwnerList(outcome.Owners), joined = outcome.Joined }); + + /// Records a program as the caller, or joins the timer that already records it. + public async Task Post(RecordOwnerRequest request) + { + var service = PluginServices.RecordingOwners; + if (service == null) return NotReady(); + var user = AuthHelpers.GetCurrentUser(Request, _authContext); + if (user == null) return NoUser(); + if (user.Policy?.EnableLiveTvManagement != true) return NotAllowed(); + if (!OwnersEnabled()) return OwnersNotFound(); + + var body = await MoonfinJson.ReadBodyAsync(request.RequestStream).ConfigureAwait(false); + if (string.IsNullOrWhiteSpace(body?.ProgramId)) return Json(400, new { error = "programId is required." }); + if (!ProgramVisible(body!.ProgramId!, user.Id)) return Json(404, new { error = "No guide program with that id" }); + + var attempt = await service.TryRecordProgramAsync(body.ProgramId!, user.Id, CancellationToken.None).ConfigureAwait(false); + return attempt.Outcome == null + ? Json(409, new { error = attempt.ErrorOr("The server could not schedule the recording.") }) + : OwnershipResult(attempt.Outcome); + } + + /// Records a program's series as the caller, or joins the rule that already records it. + public async Task Post(RecordSeriesOwnerRequest request) + { + var service = PluginServices.RecordingOwners; + if (service == null) return NotReady(); + var user = AuthHelpers.GetCurrentUser(Request, _authContext); + if (user == null) return NoUser(); + if (user.Policy?.EnableLiveTvManagement != true) return NotAllowed(); + if (!OwnersEnabled()) return OwnersNotFound(); + + var body = await MoonfinJson.ReadBodyAsync(request.RequestStream).ConfigureAwait(false); + if (string.IsNullOrWhiteSpace(body?.ProgramId)) return Json(400, new { error = "programId is required." }); + if (!ProgramVisible(body!.ProgramId!, user.Id)) return Json(404, new { error = "No guide program with that id" }); + + var attempt = await service.TryRecordSeriesAsync(body.ProgramId!, user.Id, body.Settings, CancellationToken.None).ConfigureAwait(false); + return attempt.Outcome == null + ? Json(409, new { error = attempt.ErrorOr("The server could not create the series recording.") }) + : OwnershipResult(attempt.Outcome); + } + + /// Adds the caller to the owners of an existing timer, series rule or recording. + public async Task Post(ClaimOwnerRequest request) + { + var service = PluginServices.RecordingOwners; + if (service == null) return NotReady(); + var user = AuthHelpers.GetCurrentUser(Request, _authContext); + if (user == null) return NoUser(); + if (user.Policy?.EnableLiveTvManagement != true) return NotAllowed(); + if (!OwnersEnabled()) return OwnersNotFound(); + + var body = await MoonfinJson.ReadBodyAsync(request.RequestStream).ConfigureAwait(false); + if (string.IsNullOrWhiteSpace(body?.Id)) return Json(400, new { error = "id is required." }); + + OwnedKind kind; + switch (body!.Kind?.ToLowerInvariant()) + { + case "timer": kind = OwnedKind.Timer; break; + case "series": kind = OwnedKind.Series; break; + case "item": kind = OwnedKind.Item; break; + default: return Json(400, new { error = "kind must be timer, series or item." }); + } + + var id = body.Id!; + if (kind == OwnedKind.Item) + { + var itemId = ResolveItem(id); + if (itemId == null || PluginServices.RecordingHost?.IsVisibleTo(itemId.Value, user.Id) != true) + { + return Json(404, new { error = "No recording with that id" }); + } + + id = itemId.Value.ToString("N"); + } + + var owners = await service.ClaimAsync(kind, id, user.Id, CancellationToken.None).ConfigureAwait(false); + return owners == null + ? Json(404, new { error = "Nothing to claim with that id" }) + : Json(new { owners = OwnerList(owners) }); + } + + /// The owners of the given timers, series rules and recordings, in one call. + public async Task Post(LookupOwnersRequest request) + { + var service = PluginServices.RecordingOwners; + if (service == null) return NotReady(); + var user = AuthHelpers.GetCurrentUser(Request, _authContext); + if (user == null) return NoUser(); + if (user.Policy?.EnableLiveTvAccess != true) return NotAllowed(); + if (!OwnersEnabled()) return OwnersNotFound(); + + var body = await MoonfinJson.ReadBodyAsync(request.RequestStream).ConfigureAwait(false); + var itemIds = (body?.ItemIds ?? new System.Collections.Generic.List()) + .Select(s => ResolveItem(s?.Trim())) + .Where(id => id != null) + .Select(id => id!.Value) + .Distinct() + .ToList(); + + var lookup = await service.LookupAsync( + body?.TimerIds ?? new System.Collections.Generic.List(), + body?.SeriesTimerIds ?? new System.Collections.Generic.List(), + itemIds, + user.Id, + CancellationToken.None).ConfigureAwait(false); + + var timers = lookup.Timers.ToDictionary(p => p.Key, p => OwnerList(p.Value)); + var seriesTimers = lookup.SeriesTimers.ToDictionary(p => p.Key, p => OwnerList(p.Value)); + var items = lookup.Items.ToDictionary(p => ClientId(p.Key.ToString("N")), p => OwnerList(p.Value)); + + // recordingItemIds is left out, not sent as null, when the server can't tell: clients + // read a missing field as "unknown". + return lookup.RecordingItemIds == null + ? Json(new { timers, seriesTimers, items }) + : Json(new { timers, seriesTimers, items, recordingItemIds = lookup.RecordingItemIds.Select(i => ClientId(i.ToString("N"))).ToArray() }); + } + + /// Everything the caller owns. Timer ids may be stale; clients intersect them with the live timer list. + public async Task Get(GetMyOwnedRequest request) + { + var service = PluginServices.RecordingOwners; + if (service == null) return NotReady(); + var user = AuthHelpers.GetCurrentUser(Request, _authContext); + if (user == null) return NoUser(); + if (user.Policy?.EnableLiveTvAccess != true) return NotAllowed(); + if (!OwnersEnabled()) return OwnersNotFound(); + + var mine = await service.GetMineAsync(user.Id, CancellationToken.None).ConfigureAwait(false); + return Json(new + { + timerIds = mine.TimerIds, + seriesTimerIds = mine.SeriesTimerIds, + itemIds = mine.ItemIds.Select(i => ClientId(i.ToString("N"))).ToArray(), + }); + } + // Emby clients send numeric item ids, and the feature keys items by their Guid. private static Guid? ResolveItem(string? id) { diff --git a/Emby/Emby.Plugins.Moonfin/Api/SettingsService.cs b/Emby/Emby.Plugins.Moonfin/Api/SettingsService.cs index 6025c15b..67b91d96 100644 --- a/Emby/Emby.Plugins.Moonfin/Api/SettingsService.cs +++ b/Emby/Emby.Plugins.Moonfin/Api/SettingsService.cs @@ -63,6 +63,13 @@ private bool TryAuthorizedSync([NotNullWhen(true)] out Guid? userId, [NotNullWhe return true; } + /// + /// Ping's recordingOwnersSupported: the setting, but only once the owners service runs, so a + /// client that pings during startup isn't sent to endpoints that would answer 503. + /// + internal static bool RecordingOwnersSupported(PluginConfiguration? config, bool serviceReady) => + serviceReady && (config?.LiveTvRecordingOwners ?? true); + public object Get(GetPingRequest request) { var config = Plugin.Instance?.Configuration; @@ -101,6 +108,7 @@ public object Get(GetPingRequest request) smartSearchEnabled = config?.SmartSearchEnabled ?? false, recommendationsSupported = config?.RecommendationsProviderEnabled ?? true, incompleteRecordingsSupported = config?.LiveTvDetectIncompleteRecordings ?? true, + recordingOwnersSupported = RecordingOwnersSupported(config, PluginServices.RecordingOwners != null), extendedCompleteShowingsSupported = config?.LiveTvDetectIncompleteRecordings ?? true, clientLogSupported = config?.EnableClientLogUpload ?? false, calendarAvailable = seerrEnabled diff --git a/Emby/Emby.Plugins.Moonfin/Pages/configPage.html b/Emby/Emby.Plugins.Moonfin/Pages/configPage.html index c65a31a4..f5642d10 100644 --- a/Emby/Emby.Plugins.Moonfin/Pages/configPage.html +++ b/Emby/Emby.Plugins.Moonfin/Pages/configPage.html @@ -5280,6 +5280,15 @@

Uploaded Themes

Live TV Recordings

+
+ +
+ Remembers which users scheduled each recording, so Moonfin can show a My Recordings list and who else records a show. +
+