"Listening time this week" summed the full duration of every track whose
stream ticket was issued, so pressing play and skipping counted the whole
song. Playback is now measured on the client and reported to the API.
Backend:
- ListeningHistory gains PlayedSeconds, LastProgressAt and IsCounted
(migration TrackPlayedSeconds marks existing rows as counted).
- /tracks/{id}/stream returns a listenId; new
PUT /tracks/listens/{listenId}/progress stores the accumulated seconds,
capped by track duration and elapsed wall-clock time, monotonic and
owner-only.
- A listen counts once it reaches min(30s, 50% of the track). Weekly
stats, streak, recent history/albums, mixes and listensCount only use
counted listens; seconds sum PlayedSeconds.
- ListeningHistoryCleanupJob (daily) deletes uncounted listens older
than 8 days.
web-player:
- ListenTracker accumulates played time (seeks are ignored) and flushes
on pause, track change, ended, every 30s, and on pagehide.
- Proxy route /api/listens/[id]/progress; repeat starts a new listen per
loop.
39 lines
1.3 KiB
C#
39 lines
1.3 KiB
C#
using Mediator;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
using Musify.Application.Contracts;
|
|
using Musify.Application.Mixes;
|
|
|
|
namespace Musify.Infrastructure.Jobs
|
|
{
|
|
public class DailyMixGenerationJob(
|
|
IDatabase database,
|
|
IMediator mediator,
|
|
ILogger<DailyMixGenerationJob> logger)
|
|
{
|
|
public async Task RunAsync(CancellationToken cancellationToken)
|
|
{
|
|
var userIds = await database.ListeningHistories
|
|
.AsNoTracking()
|
|
.Where(history => history.IsCounted)
|
|
.Select(history => history.UserId)
|
|
.Distinct()
|
|
.ToListAsync(cancellationToken);
|
|
|
|
logger.LogInformation("Starting daily mix generation for {Count} users", userIds.Count);
|
|
|
|
foreach (var userId in userIds)
|
|
{
|
|
var result = await mediator.Send(new GenerateMixesForUserCommand(userId), cancellationToken);
|
|
if (result.IsError)
|
|
{
|
|
logger.LogWarning("Failed to generate mixes for user {UserId}: {Error}",
|
|
userId, result.FirstError.Description);
|
|
}
|
|
}
|
|
|
|
logger.LogInformation("Finished daily mix generation");
|
|
}
|
|
}
|
|
}
|