roadie/Roadie.Api.Services/PlayActivityService.cs

240 lines
13 KiB
C#
Raw Normal View History

2019-01-08 22:40:26 +00:00
using Microsoft.AspNetCore.SignalR;
2019-01-05 22:40:33 +00:00
using Microsoft.EntityFrameworkCore;
2018-11-17 02:14:32 +00:00
using Microsoft.Extensions.Logging;
2018-11-17 02:44:08 +00:00
using Roadie.Api.Hubs;
2018-11-17 02:14:32 +00:00
using Roadie.Library;
2018-11-15 00:16:25 +00:00
using Roadie.Library.Caching;
using Roadie.Library.Configuration;
2019-11-17 14:10:17 +00:00
using Roadie.Library.Data.Context;
2018-11-15 00:16:25 +00:00
using Roadie.Library.Encoding;
using Roadie.Library.Imaging;
2018-11-15 00:16:25 +00:00
using Roadie.Library.Models;
using Roadie.Library.Models.Pagination;
using Roadie.Library.Models.Users;
using Roadie.Library.Scrobble;
2018-11-15 00:16:25 +00:00
using Roadie.Library.Utility;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
namespace Roadie.Api.Services
{
public class PlayActivityService : ServiceBase, IPlayActivityService
2018-11-15 00:16:25 +00:00
{
2018-11-17 02:44:08 +00:00
protected IHubContext<PlayActivityHub> PlayActivityHub { get; }
2019-06-30 22:14:36 +00:00
protected IScrobbleHandler ScrobblerHandler { get; }
public PlayActivityService(IRoadieSettings configuration, IRoadieDbContext dbContext, ICacheManager cacheManager,
ILogger logger, ScrobbleHandler scrobbleHandler)
: base(configuration, null, dbContext, cacheManager, logger, null)
{
ScrobblerHandler = scrobbleHandler;
}
public PlayActivityService(IRoadieSettings configuration, IHttpEncoder httpEncoder, IHttpContext httpContext,
IRoadieDbContext dbContext, ICacheManager cacheManager, ILogger<PlayActivityService> logger,
2019-09-05 02:04:20 +00:00
IScrobbleHandler scrobbleHandler, IHubContext<PlayActivityHub> playActivityHub)
2018-11-15 00:16:25 +00:00
: base(configuration, httpEncoder, dbContext, cacheManager, logger, httpContext)
{
2019-06-30 22:14:36 +00:00
PlayActivityHub = playActivityHub;
ScrobblerHandler = scrobbleHandler;
2018-11-15 00:16:25 +00:00
}
private async Task PublishPlayActivity(User roadieUser, ScrobbleInfo scrobble, bool isNowPlaying)
2019-09-05 02:04:20 +00:00
{
// Only broadcast if the user is not public and played duration is more than half of duration
if (roadieUser?.IsPrivate != true &&
scrobble.ElapsedTimeOfTrackPlayed.TotalSeconds > scrobble.TrackDuration.TotalSeconds / 2)
{
var sw = Stopwatch.StartNew();
var track = await DbContext.Tracks
.Include(x => x.ReleaseMedia)
.Include(x => x.ReleaseMedia.Release)
.Include(x => x.ReleaseMedia.Release.Artist)
.Include(x => x.TrackArtist)
.FirstOrDefaultAsync(x => x.RoadieId == scrobble.TrackId)
.ConfigureAwait(false);
var user = DbContext.Users.FirstOrDefault(x => x.RoadieId == roadieUser.UserId);
var userTrack =
DbContext.UserTracks.FirstOrDefault(x => x.UserId == roadieUser.Id && x.TrackId == track.Id);
var pl = new PlayActivityList
{
Artist = new DataToken
{
Text = track.ReleaseMedia.Release.Artist.Name,
Value = track.ReleaseMedia.Release.Artist.RoadieId.ToString()
},
TrackArtist = track.TrackArtist == null
? null
: new DataToken
{
Text = track.TrackArtist.Name,
Value = track.TrackArtist.RoadieId.ToString()
},
Release = new DataToken
{
Text = track.ReleaseMedia.Release.Title,
Value = track.ReleaseMedia.Release.RoadieId.ToString()
},
Track = TrackList.FromDataTrack(null,
track,
track.ReleaseMedia.MediaNumber,
track.ReleaseMedia.Release,
track.ReleaseMedia.Release.Artist,
track.TrackArtist,
HttpContext.BaseUrl,
ImageHelper.MakeTrackThumbnailImage(Configuration, HttpContext, track.RoadieId),
ImageHelper.MakeReleaseThumbnailImage(Configuration, HttpContext, track.ReleaseMedia.Release.RoadieId),
ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, track.ReleaseMedia.Release.Artist.RoadieId),
ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, track.TrackArtist == null
? null
: (Guid?)track.TrackArtist.RoadieId)),
User = new DataToken
{
Text = roadieUser.UserName,
Value = roadieUser.UserId.ToString()
},
ArtistThumbnail = ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, track.TrackArtist != null
? track.TrackArtist.RoadieId
: track.ReleaseMedia.Release.Artist.RoadieId),
PlayedDateDateTime = scrobble.TimePlayed,
IsNowPlaying = isNowPlaying,
Rating = track.Rating,
ReleasePlayUrl = $"{HttpContext.BaseUrl}/play/release/{track.ReleaseMedia.Release.RoadieId}",
ReleaseThumbnail = ImageHelper.MakeReleaseThumbnailImage(Configuration, HttpContext, track.ReleaseMedia.Release.RoadieId),
TrackPlayUrl = $"{HttpContext.BaseUrl}/play/track/{track.RoadieId}.mp3",
UserRating = userTrack?.Rating,
UserThumbnail = ImageHelper.MakeUserThumbnailImage(Configuration, HttpContext, roadieUser.UserId)
};
try
{
await PlayActivityHub.Clients.All.SendAsync("SendActivityAsync", pl).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogError(ex);
}
}
2019-09-05 02:04:20 +00:00
}
public async Task<Library.Models.Pagination.PagedResult<PlayActivityList>> ListAsync(PagedRequest request, User roadieUser = null, DateTime? newerThan = null)
2019-01-08 22:40:26 +00:00
{
try
{
var sw = new Stopwatch();
sw.Start();
2019-06-30 22:14:36 +00:00
var result = from t in DbContext.Tracks
join rm in DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
join r in DbContext.Releases on rm.ReleaseId equals r.Id
join trackArtist in DbContext.Artists on t.ArtistId equals trackArtist.Id into tas
from trackArtist in tas.DefaultIfEmpty()
join usertrack in DbContext.UserTracks on t.Id equals usertrack.TrackId
join u in DbContext.Users on usertrack.UserId equals u.Id
join releaseArtist in DbContext.Artists on r.ArtistId equals releaseArtist.Id
where newerThan == null || usertrack.LastPlayed >= newerThan
where roadieUser == null && !(u.IsPrivate ?? false) || roadieUser != null && usertrack != null &&
usertrack.User.Id == roadieUser.Id
where !request.FilterRatedOnly || roadieUser == null && t.Rating > 0 ||
roadieUser != null && usertrack.Rating > 0
where request.FilterValue.Length == 0 || request.FilterValue.Length > 0 && (
2019-09-05 02:04:20 +00:00
t.Title != null && t.Title.Contains(request.Filter, StringComparison.OrdinalIgnoreCase) ||
t.AlternateNames != null && t.AlternateNames.Contains(request.Filter, StringComparison.OrdinalIgnoreCase))
2019-06-30 22:14:36 +00:00
select new PlayActivityList
{
Release = new DataToken
{
Text = r.Title,
Value = r.RoadieId.ToString()
},
Track = TrackList.FromDataTrack(null,
t,
rm.MediaNumber,
r,
releaseArtist,
trackArtist,
HttpContext.BaseUrl,
ImageHelper.MakeTrackThumbnailImage(Configuration, HttpContext, t.RoadieId),
ImageHelper.MakeReleaseThumbnailImage(Configuration, HttpContext, r.RoadieId),
ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, releaseArtist.RoadieId),
ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, trackArtist == null ? null : (Guid?)trackArtist.RoadieId)),
2019-06-30 22:14:36 +00:00
User = new DataToken
{
Text = u.UserName,
Value = u.RoadieId.ToString()
},
Artist = new DataToken
{
Text = releaseArtist.Name,
Value = releaseArtist.RoadieId.ToString()
},
TrackArtist = trackArtist == null
? null
: new DataToken
{
Text = trackArtist.Name,
Value = trackArtist.RoadieId.ToString()
},
PlayedDateDateTime = usertrack.LastPlayed,
ReleasePlayUrl = $"{HttpContext.BaseUrl}/play/release/{r.RoadieId}",
Rating = t.Rating,
UserRating = usertrack.Rating,
TrackPlayUrl = $"{HttpContext.BaseUrl}/play/track/{t.RoadieId}.mp3",
ArtistThumbnail =
ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext,
2019-06-30 22:14:36 +00:00
trackArtist != null ? trackArtist.RoadieId : releaseArtist.RoadieId),
ReleaseThumbnail = ImageHelper.MakeReleaseThumbnailImage(Configuration, HttpContext, r.RoadieId),
UserThumbnail = ImageHelper.MakeUserThumbnailImage(Configuration, HttpContext, u.RoadieId)
2019-06-30 22:14:36 +00:00
};
2018-11-17 02:14:32 +00:00
2019-06-30 22:14:36 +00:00
var sortBy = string.IsNullOrEmpty(request.Sort)
? request.OrderValue(new Dictionary<string, string> { { "PlayedDateDateTime", "DESC" } })
: request.OrderValue();
2019-01-08 22:40:26 +00:00
var rowCount = result.Count();
var rows = await result.OrderBy(sortBy).Skip(request.SkipValue).Take(request.LimitValue).ToArrayAsync().ConfigureAwait(false);
2019-01-08 22:40:26 +00:00
sw.Stop();
return new Library.Models.Pagination.PagedResult<PlayActivityList>
2019-01-08 22:40:26 +00:00
{
TotalCount = rowCount,
CurrentPage = request.PageValue,
TotalPages = (int)Math.Ceiling((double)rowCount / request.LimitValue),
OperationTime = sw.ElapsedMilliseconds,
Rows = rows
};
2019-01-08 22:40:26 +00:00
}
catch (Exception ex)
{
2019-06-30 22:14:36 +00:00
Logger.LogError(ex);
2019-01-08 22:40:26 +00:00
}
2019-06-30 22:14:36 +00:00
return new Library.Models.Pagination.PagedResult<PlayActivityList>();
}
public async Task<OperationResult<bool>> NowPlayingAsync(User roadieUser, ScrobbleInfo scrobble)
{
var scrobbleResult = await ScrobblerHandler.NowPlaying(roadieUser, scrobble).ConfigureAwait(false);
2019-09-05 02:04:20 +00:00
if (!scrobbleResult.IsSuccess)
{
return scrobbleResult;
}
await PublishPlayActivity(roadieUser, scrobble, true).ConfigureAwait(false);
return scrobbleResult;
}
public async Task<OperationResult<bool>> ScrobbleAsync(User roadieUser, ScrobbleInfo scrobble)
{
var scrobbleResult = await ScrobblerHandler.Scrobble(roadieUser, scrobble).ConfigureAwait(false);
if (!scrobbleResult.IsSuccess)
2019-06-30 22:14:36 +00:00
{
return scrobbleResult;
}
await PublishPlayActivity(roadieUser, scrobble, false).ConfigureAwait(false);
return scrobbleResult;
}
2018-11-15 00:16:25 +00:00
}
2018-11-16 03:37:00 +00:00
}