Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Required subscription validation #374

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public interface IPlayableContent
bool IsLive { get; }
string SyncUID { get; }
string SeriesUID { get; }
string RequiredSubscriptionLevel { get; }
}
1 change: 1 addition & 0 deletions RaceControl/RaceControl/PlayableChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public PlayableChannel(Session session, Channel channel)
IsLive = session.IsLive;
SyncUID = session.UID;
SeriesUID = session.SeriesUID;
RequiredSubscriptionLevel = channel.RequiredSubcriptionLevel ?? "Pro";
}

private static string GetDisplayName(Channel channel)
Expand Down
1 change: 1 addition & 0 deletions RaceControl/RaceControl/PlayableContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public abstract class PlayableContent : IPlayableContent
public bool IsLive { get; protected init; }
public string SyncUID { get; protected init; }
public string SeriesUID { get; protected init; }
public string RequiredSubscriptionLevel { get; protected init; }

public override string ToString()
{
Expand Down
1 change: 1 addition & 0 deletions RaceControl/RaceControl/PlayableEpisode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public PlayableEpisode(Episode episode)
ThumbnailUrl = episode.ThumbnailUrl;
SyncUID = episode.UID;
SeriesUID = episode.SeriesUID;
RequiredSubscriptionLevel = episode.RequiredSubscriptionLevel ?? "Pro";
}
}
19 changes: 17 additions & 2 deletions RaceControl/RaceControl/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,7 @@ private async Task LoadEpisodesForGenreAsync(string genre)

private void WatchContent(IPlayableContent playableContent, VideoDialogSettings settings = null)
{
ValidateActiveSubscription(playableContent);
var identifier = _numberGenerator.GetNextNumber();
var parameters = new DialogParameters
{
Expand All @@ -886,10 +887,11 @@ private void WatchContent(IPlayableContent playableContent, VideoDialogSettings
};

_dialogService.Show(nameof(VideoDialog), parameters, _ => _numberGenerator.RemoveNumber(identifier), nameof(VideoDialogWindow));
}
}

private async Task WatchInVlcAsync(IPlayableContent playableContent)
{
ValidateActiveSubscription(playableContent);
var streamUrl = await _apiService.GetTokenisedUrlAsync(Settings.SubscriptionToken, playableContent);
ValidateStreamUrl(streamUrl);
using var process = ProcessUtils.CreateProcess(VlcExeLocation, $"\"{streamUrl}\" --meta-title=\"{playableContent.Title}\"");
Expand All @@ -898,6 +900,7 @@ private async Task WatchInVlcAsync(IPlayableContent playableContent)

private async Task WatchInMpvAsync(IPlayableContent playableContent, VideoDialogSettings settings = null)
{
ValidateActiveSubscription(playableContent);
// Use different stream type because this one doesn't require a playToken cookie (not supported by MPV)
var streamUrl = await _apiService.GetTokenisedUrlAsync(Settings.SubscriptionToken, playableContent, StreamTypeKeys.BigScreenHls);
ValidateStreamUrl(streamUrl);
Expand Down Expand Up @@ -1003,6 +1006,7 @@ void AddVideoQualityArgument(VideoQuality videoQuality)

private async Task WatchInMpcAsync(IPlayableContent playableContent)
{
ValidateActiveSubscription(playableContent);
var streamUrl = await _apiService.GetTokenisedUrlAsync(Settings.SubscriptionToken, playableContent);
ValidateStreamUrl(streamUrl);
using var process = ProcessUtils.CreateProcess(MpcExeLocation, $"\"{streamUrl}\"");
Expand All @@ -1011,6 +1015,7 @@ private async Task WatchInMpcAsync(IPlayableContent playableContent)

private async Task CastContentAsync(IReceiver receiver, IPlayableContent playableContent)
{
ValidateActiveSubscription(playableContent);
var streamUrl = await _apiService.GetTokenisedUrlAsync(Settings.SubscriptionToken, playableContent);
ValidateStreamUrl(streamUrl);
AudioTracks.Clear();
Expand Down Expand Up @@ -1069,13 +1074,15 @@ IDictionary<string, string> GetCustomData()

private async Task CopyUrlAsync(IPlayableContent playableContent)
{
ValidateActiveSubscription(playableContent);
var streamUrl = await _apiService.GetTokenisedUrlAsync(Settings.SubscriptionToken, playableContent);
ValidateStreamUrl(streamUrl);
Clipboard.SetText(streamUrl);
}

private void StartDownload(IPlayableContent playableContent)
{
ValidateActiveSubscription(playableContent);
var defaultFilename = $"{playableContent.Title}.mp4".RemoveInvalidFileNameChars();
var initialDirectory = !string.IsNullOrWhiteSpace(Settings.RecordingLocation) ? Settings.RecordingLocation : FolderUtils.GetSpecialFolderPath(Environment.SpecialFolder.Desktop);
var filename = Path.Join(initialDirectory, defaultFilename);
Expand Down Expand Up @@ -1202,11 +1209,19 @@ private void ClearSessions()
SelectedVodGenre = null;
}

private void ValidateActiveSubscription(IPlayableContent playableContent)
{
if (Settings.SubscriptionStatus.ToLower() != "active")
{
throw new InvalidOperationException($"Failed to execute action: \nAn active F1TV subscription is required of type '{playableContent.RequiredSubscriptionLevel}' or higher");
}
}

private static void ValidateStreamUrl(string streamUrl)
{
if (string.IsNullOrWhiteSpace(streamUrl))
{
throw new Exception("An error occurred while retrieving the stream URL.");
throw new ArgumentException("An error occurred while retrieving the stream URL.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public class Channel
public string Name { get; init; }
public string ChannelType { get; init; }
public string PlaybackUrl { get; init; }
public string RequiredSubcriptionLevel { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Episode
public DateTime? ContractStartDate { get; init; }
public DateTime? ContractEndDate { get; init; }
public long SessionIndex { get; init; }
public string RequiredSubscriptionLevel { get; init; }

public override string ToString()
{
Expand Down
9 changes: 6 additions & 3 deletions RaceControl/Services/RaceControl.Services/F1TV/ApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ public async Task<List<Channel>> GetChannelsForSessionAsync(Session session)
{
Name = s.Type == ChannelTypes.Onboard ? $"{s.DriverFirstName} {s.DriverLastName}" : s.Title,
ChannelType = s.Type,
PlaybackUrl = s.PlaybackUrl
PlaybackUrl = s.PlaybackUrl,
RequiredSubcriptionLevel = metadata.Entitlement
})
.ToList();
}
Expand All @@ -162,7 +163,8 @@ public async Task<List<Channel>> GetChannelsForSessionAsync(Session session)
{
Name = ChannelNames.Wif,
ChannelType = ChannelTypes.Wif,
PlaybackUrl = GetPlaybackUrl(metadata.ContentId)
PlaybackUrl = GetPlaybackUrl(metadata.ContentId),
RequiredSubcriptionLevel = metadata.Entitlement
}
};
}
Expand Down Expand Up @@ -361,7 +363,8 @@ private static Episode CreateEpisode(Container container)
EndDate = container.Metadata.EmfAttributes.SessionEndDate.GetDateTimeFromEpoch(),
ContractStartDate = container.Metadata.ContractStartDate.GetDateTimeFromEpoch(),
ContractEndDate = container.Metadata.ContractEndDate.GetDateTimeFromEpoch(),
SessionIndex = container.Metadata.EmfAttributes.SessionIndex
SessionIndex = container.Metadata.EmfAttributes.SessionIndex,
RequiredSubscriptionLevel = container.Metadata.Entitlement
};
}

Expand Down