2021-10-31 19:55:00 +00:00
|
|
|
using System;
|
2021-11-10 23:54:28 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Text.Json;
|
2021-10-31 19:55:00 +00:00
|
|
|
using System.Threading.Tasks;
|
2021-11-10 23:54:28 +00:00
|
|
|
using System.Data.SqlTypes;
|
2021-10-31 19:55:00 +00:00
|
|
|
using Microsoft.AspNetCore.SignalR;
|
2021-11-10 23:54:28 +00:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-10-31 19:55:00 +00:00
|
|
|
|
2021-11-10 01:46:30 +00:00
|
|
|
using SpotifyAPI.Web;
|
2021-11-09 20:58:02 +00:00
|
|
|
using StackExchange.Redis;
|
|
|
|
|
2021-10-31 19:55:00 +00:00
|
|
|
using Selector.Cache;
|
2021-11-10 23:54:28 +00:00
|
|
|
using Selector.Model;
|
2021-10-31 19:55:00 +00:00
|
|
|
|
|
|
|
namespace Selector.Web.Hubs
|
|
|
|
{
|
|
|
|
public interface INowPlayingHubClient
|
|
|
|
{
|
|
|
|
public Task OnNewPlaying(CurrentlyPlayingDTO context);
|
2021-11-10 23:54:28 +00:00
|
|
|
public Task OnNewAudioFeature(TrackAudioFeatures features);
|
2021-11-30 20:38:26 +00:00
|
|
|
public Task OnNewPlayCount(PlayCount playCount);
|
2021-10-31 19:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class NowPlayingHub: Hub<INowPlayingHubClient>
|
|
|
|
{
|
2021-11-09 20:58:02 +00:00
|
|
|
private readonly IDatabaseAsync Cache;
|
2021-11-10 23:54:28 +00:00
|
|
|
private readonly AudioFeaturePuller AudioFeaturePuller;
|
2021-11-30 20:38:26 +00:00
|
|
|
private readonly PlayCountPuller PlayCountPuller;
|
2021-11-10 23:54:28 +00:00
|
|
|
private readonly ApplicationDbContext Db;
|
2021-11-09 20:58:02 +00:00
|
|
|
|
2021-11-30 20:38:26 +00:00
|
|
|
public NowPlayingHub(
|
|
|
|
IDatabaseAsync cache,
|
|
|
|
AudioFeaturePuller featurePuller,
|
|
|
|
ApplicationDbContext db,
|
|
|
|
PlayCountPuller playCountPuller = null
|
|
|
|
)
|
2021-11-09 20:58:02 +00:00
|
|
|
{
|
|
|
|
Cache = cache;
|
2021-11-30 20:38:26 +00:00
|
|
|
AudioFeaturePuller = featurePuller;
|
|
|
|
PlayCountPuller = playCountPuller;
|
2021-11-10 23:54:28 +00:00
|
|
|
Db = db;
|
2021-11-09 20:58:02 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 22:47:49 +00:00
|
|
|
public async Task OnConnected()
|
|
|
|
{
|
|
|
|
await SendNewPlaying();
|
|
|
|
}
|
|
|
|
|
2021-11-09 20:58:02 +00:00
|
|
|
public async Task SendNewPlaying()
|
2021-10-31 19:55:00 +00:00
|
|
|
{
|
2021-11-09 20:58:02 +00:00
|
|
|
var nowPlaying = await Cache.StringGetAsync(Key.CurrentlyPlaying(Context.UserIdentifier));
|
2021-11-10 23:54:28 +00:00
|
|
|
if (nowPlaying != RedisValue.Null)
|
|
|
|
{
|
2021-12-21 22:22:52 +00:00
|
|
|
var deserialised = JsonSerializer.Deserialize(nowPlaying, JsonContext.Default.CurrentlyPlayingDTO);
|
2021-11-10 23:54:28 +00:00
|
|
|
await Clients.Caller.OnNewPlaying(deserialised);
|
|
|
|
}
|
2021-10-31 19:55:00 +00:00
|
|
|
}
|
2021-11-10 01:46:30 +00:00
|
|
|
|
2021-11-10 23:54:28 +00:00
|
|
|
public async Task SendAudioFeatures(string trackId)
|
|
|
|
{
|
|
|
|
var user = Db.Users
|
|
|
|
.AsNoTracking()
|
|
|
|
.Where(u => u.Id == Context.UserIdentifier)
|
|
|
|
.SingleOrDefault()
|
|
|
|
?? throw new SqlNullValueException("No user returned");
|
|
|
|
var watcher = Db.Watcher
|
|
|
|
.AsNoTracking()
|
|
|
|
.Where(w => w.UserId == Context.UserIdentifier
|
|
|
|
&& w.Type == WatcherType.Player)
|
|
|
|
.SingleOrDefault()
|
|
|
|
?? throw new SqlNullValueException($"No player watcher found for [{user.UserName}]");
|
|
|
|
|
|
|
|
var feature = await AudioFeaturePuller.Get(user.SpotifyRefreshToken, trackId);
|
|
|
|
|
|
|
|
if (feature is not null)
|
|
|
|
{
|
|
|
|
await Clients.Caller.OnNewAudioFeature(feature);
|
|
|
|
}
|
|
|
|
}
|
2021-11-30 20:38:26 +00:00
|
|
|
|
|
|
|
public async Task SendPlayCount(string track, string artist, string album, string albumArtist)
|
|
|
|
{
|
|
|
|
if(PlayCountPuller is not null)
|
|
|
|
{
|
|
|
|
var user = Db.Users
|
|
|
|
.AsNoTracking()
|
|
|
|
.Where(u => u.Id == Context.UserIdentifier)
|
|
|
|
.SingleOrDefault()
|
|
|
|
?? throw new SqlNullValueException("No user returned");
|
|
|
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(user.LastFmUsername))
|
|
|
|
{
|
|
|
|
var playCount = await PlayCountPuller.Get(user.LastFmUsername, track, artist, album, albumArtist);
|
|
|
|
|
|
|
|
if (playCount is not null)
|
|
|
|
{
|
|
|
|
await Clients.Caller.OnNewPlayCount(playCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-31 19:55:00 +00:00
|
|
|
}
|
|
|
|
}
|