"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.
57 lines
1.9 KiB
C#
57 lines
1.9 KiB
C#
using Musify.Application.Configuration;
|
|
using Musify.Application.Contracts;
|
|
using Musify.Application.Tracks.Responses;
|
|
using Musify.Domain.Entities;
|
|
|
|
namespace Musify.Application.Services;
|
|
|
|
public sealed class TrackStreamIssuer(
|
|
IDatabase database,
|
|
IStreamTicketService ticketService,
|
|
TrackConfiguration trackConfiguration,
|
|
StreamGatewayConfiguration streamGatewayConfiguration,
|
|
PlaybackConfiguration playbackConfiguration)
|
|
{
|
|
public async Task<TrackStreamResponse> IssueAsync(
|
|
Guid trackId,
|
|
string audioFolderName,
|
|
long? userId,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var folderPath = trackConfiguration.Routes.BuildProcessedAudioPath(audioFolderName);
|
|
var keyPrefix = $"{folderPath}/";
|
|
|
|
var maxBytes = AnonymousFragmentBytes(userId);
|
|
var ticket = ticketService.IssueTicket(userId, keyPrefix, maxBytes);
|
|
|
|
var manifestUrl = string.Join('/',
|
|
streamGatewayConfiguration.PublicBaseUrl.TrimEnd('/'),
|
|
"media",
|
|
folderPath,
|
|
streamGatewayConfiguration.AudioFileName);
|
|
|
|
Guid? listenId = null;
|
|
if (userId != null)
|
|
{
|
|
var newListeningHistory = new ListeningHistory
|
|
{
|
|
UserId = userId.Value,
|
|
TrackId = trackId,
|
|
};
|
|
await database.ListeningHistories.AddAsync(newListeningHistory, cancellationToken);
|
|
await database.SaveChangesAsync(cancellationToken);
|
|
listenId = newListeningHistory.Id;
|
|
}
|
|
|
|
return new TrackStreamResponse(manifestUrl, ticket.Token, ticket.ExpiresInSeconds, listenId);
|
|
}
|
|
|
|
private long? AnonymousFragmentBytes(long? userId)
|
|
{
|
|
if (userId != null || playbackConfiguration.AnonymousFragmentSeconds <= 0)
|
|
return null;
|
|
|
|
return (long)playbackConfiguration.AnonymousFragmentSeconds * playbackConfiguration.EstimatedAudioBytesPerSecond;
|
|
}
|
|
}
|