Commit 0fe649ce authored by Aryan Noshirvnai's avatar Aryan Noshirvnai

Add 30-second preview playback for detected Telegram audio files

Lets a user audition a found chat song through the shared player before
deciding whether to save it, via the new backend preview stream endpoint.
parent a22bc8cd
Pipeline #1130 failed with stage
import { apiFetch } from './client' import { apiFetch, buildStreamUrl } from './client'
export interface TokenPair { export interface TokenPair {
access_token: string access_token: string
...@@ -86,6 +86,11 @@ export function getChatMusic(chatId: number) { ...@@ -86,6 +86,11 @@ export function getChatMusic(chatId: number) {
return apiFetch<{ status: string; music: ChatMusic[] }>(`/auth/telegram/chats/${chatId}/music`) return apiFetch<{ status: string; music: ChatMusic[] }>(`/auth/telegram/chats/${chatId}/music`)
} }
/** 30-second preview clip of a not-yet-saved chat music file, for auditioning before Save. */
export async function fetchChatMusicPreviewUrl(chatId: number, messageId: number): Promise<string> {
return buildStreamUrl(`/auth/telegram/chats/${chatId}/music/${messageId}/preview`)
}
export type UserRole = 'user' | 'business_admin' | 'super_admin' export type UserRole = 'user' | 'business_admin' | 'super_admin'
export interface CurrentUser { export interface CurrentUser {
......
...@@ -92,6 +92,7 @@ const BACKEND_DONE: DoneGroup[] = [ ...@@ -92,6 +92,7 @@ const BACKEND_DONE: DoneGroup[] = [
'Real pipeline: Demucs stem separation, BPM/key detection, Gemini-planned arrangement, mastering', 'Real pipeline: Demucs stem separation, BPM/key detection, Gemini-planned arrangement, mastering',
'Auth-scoped end to end - job status and library-song sourcing both scoped to the owner (was an IDOR)', 'Auth-scoped end to end - job status and library-song sourcing both scoped to the owner (was an IDOR)',
'Library-sourced only now, not raw uploads - skips a fresh identify_audio() call and feeds the already-known title/artist/genre into the planning prompt instead', 'Library-sourced only now, not raw uploads - skips a fresh identify_audio() call and feeds the already-known title/artist/genre into the planning prompt instead',
'Finished remix is library-only, not directly downloadable (2026-08-23) - also closed a real access-control gap: the old download_url pointed at the unauthenticated /downloads static mount with no ownership check at all',
], ],
}, },
{ {
...@@ -109,6 +110,7 @@ const BACKEND_DONE: DoneGroup[] = [ ...@@ -109,6 +110,7 @@ const BACKEND_DONE: DoneGroup[] = [
'On-demand background scan job per chat (ChatScanJob) - lists the chat, then tags + saves every song via the same pipeline /music/tag uses for one song, polled like a RemixJob', 'On-demand background scan job per chat (ChatScanJob) - lists the chat, then tags + saves every song via the same pipeline /music/tag uses for one song, polled like a RemixJob',
'Guardrails: 50 messages/scan, 5 scans/user/day, no two concurrent scans of the same chat, 1.5s pace between songs', 'Guardrails: 50 messages/scan, 5 scans/user/day, no two concurrent scans of the same chat, 1.5s pace between songs',
'Per-song outcome tracking (saved/skipped_duplicate/failed), not just one pass/fail for the whole job', 'Per-song outcome tracking (saved/skipped_duplicate/failed), not just one pass/fail for the whole job',
'30-second preview stream endpoint for a not-yet-saved chat music file (2026-08-23) - ffmpeg -t caps read/encode at the cutoff so cost doesn\'t scale with the source file\'s real length, reuses the same token-in-query-param stream auth as every other direct <audio> endpoint',
], ],
}, },
] ]
...@@ -145,6 +147,7 @@ const FRONTEND_DONE: DoneGroup[] = [ ...@@ -145,6 +147,7 @@ const FRONTEND_DONE: DoneGroup[] = [
'Login page: username/password + register, toggled in place', 'Login page: username/password + register, toggled in place',
'Route guards redirect to /login when signed out instead of showing a blank page', 'Route guards redirect to /login when signed out instead of showing a blank page',
'Telegram linking page: phone + code (+ two-step verification password if the account has it enabled), then browse linked chats and save any found song to the library (tags via /music/tag, one song at a time)', 'Telegram linking page: phone + code (+ two-step verification password if the account has it enabled), then browse linked chats and save any found song to the library (tags via /music/tag, one song at a time)',
'Per-song 30-second preview button in the chat music list, playable through the shared player before deciding whether to save (2026-08-23)',
], ],
}, },
{ {
...@@ -171,6 +174,7 @@ const FRONTEND_DONE: DoneGroup[] = [ ...@@ -171,6 +174,7 @@ const FRONTEND_DONE: DoneGroup[] = [
area: 'Remix', area: 'Remix',
items: [ items: [
'Song picker (up to 5, own library only, search-as-you-type) replaces the old local-file-upload control - no upload-progress UI needed anymore since nothing is uploaded', 'Song picker (up to 5, own library only, search-as-you-type) replaces the old local-file-upload control - no upload-progress UI needed anymore since nothing is uploaded',
'Done state shows "added to your library" + a link to it instead of a download link (2026-08-23) - finished remixes are library-only now',
], ],
}, },
{ {
......
import { render, screen } from '@testing-library/react' import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event' import userEvent from '@testing-library/user-event'
import { MemoryRouter } from 'react-router-dom' import { MemoryRouter } from 'react-router-dom'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
...@@ -221,6 +221,25 @@ describe('TelegramLink', () => { ...@@ -221,6 +221,25 @@ describe('TelegramLink', () => {
expect(screen.getByText('Duran Duran')).toBeInTheDocument() expect(screen.getByText('Duran Duran')).toBeInTheDocument()
}) })
it('plays a 30-second preview of a found song through the shared player when tapped', async () => {
vi.spyOn(authApi, 'getChats').mockResolvedValue({ status: 'success', chats: [CHAT] })
vi.spyOn(authApi, 'getChatMusic').mockResolvedValue({ status: 'success', music: [MUSIC] })
vi.spyOn(authApi, 'fetchChatMusicPreviewUrl').mockResolvedValue('blob:fake-preview-url')
renderPage()
await userEvent.click(await screen.findByText('Music Sharing'))
await screen.findByText('Invisible')
await userEvent.click(screen.getByRole('button', { name: 'Play 30 second preview of Invisible' }))
await waitFor(() => {
expect(authApi.fetchChatMusicPreviewUrl).toHaveBeenCalledWith(1, 1)
})
expect(await screen.findByRole('button', { name: 'Pause Invisible' })).toBeInTheDocument()
// Previewing is separate from saving - it shouldn't trigger the tag/save flow.
expect(screen.getByRole('button', { name: /Save/ })).toBeInTheDocument()
})
it('shows an empty state when a chat has no music', async () => { it('shows an empty state when a chat has no music', async () => {
vi.spyOn(authApi, 'getChats').mockResolvedValue({ status: 'success', chats: [CHAT] }) vi.spyOn(authApi, 'getChats').mockResolvedValue({ status: 'success', chats: [CHAT] })
vi.spyOn(authApi, 'getChatMusic').mockResolvedValue({ status: 'success', music: [] }) vi.spyOn(authApi, 'getChatMusic').mockResolvedValue({ status: 'success', music: [] })
......
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { Check, Download, MessageCircle, Music2 } from 'lucide-react' import { Check, Download, MessageCircle, Music2, Pause, Play } from 'lucide-react'
import { import {
fetchChatMusicPreviewUrl,
getChatMusic, getChatMusic,
getChats, getChats,
linkTelegram, linkTelegram,
...@@ -16,6 +17,18 @@ import { Button } from '../components/ui/Button' ...@@ -16,6 +17,18 @@ import { Button } from '../components/ui/Button'
import { Input } from '../components/ui/Input' import { Input } from '../components/ui/Input'
import { SkeletonRow } from '../components/ui/Skeleton' import { SkeletonRow } from '../components/ui/Skeleton'
import { EmptyState } from '../components/ui/EmptyState' import { EmptyState } from '../components/ui/EmptyState'
import { usePlayer } from '../context/PlayerContext'
function toPreviewPlayable(chatId: number, m: ChatMusic) {
return {
// Negative and keyed by message_id, not a real LibrarySong id - see PlayableSong's
// `streamUrl` doc (same convention every other social feature's play button uses).
id: -m.message_id,
title: m.title,
artist: m.artist,
streamUrl: () => fetchChatMusicPreviewUrl(chatId, m.message_id),
}
}
type Step = 'checking' | 'phone' | 'code' | 'password' | 'linked' type Step = 'checking' | 'phone' | 'code' | 'password' | 'linked'
...@@ -40,6 +53,8 @@ export function TelegramLink() { ...@@ -40,6 +53,8 @@ export function TelegramLink() {
const [savedMeta, setSavedMeta] = useState<Record<number, TagMetadata>>({}) const [savedMeta, setSavedMeta] = useState<Record<number, TagMetadata>>({})
const [saveErrors, setSaveErrors] = useState<Record<number, string>>({}) const [saveErrors, setSaveErrors] = useState<Record<number, string>>({})
const { currentSong, isPlaying, toggle } = usePlayer()
function loadChats() { function loadChats() {
setChatsError(null) setChatsError(null)
getChats() getChats()
...@@ -275,11 +290,20 @@ export function TelegramLink() { ...@@ -275,11 +290,20 @@ export function TelegramLink() {
const saved = savedMeta[m.message_id] const saved = savedMeta[m.message_id]
const saveError = saveErrors[m.message_id] const saveError = saveErrors[m.message_id]
const saving = savingId === m.message_id const saving = savingId === m.message_id
const isCurrent = currentSong?.id === -m.message_id
return ( return (
<li key={m.message_id} className="flex items-center gap-4 px-2 py-3"> <li key={m.message_id} className="flex items-center gap-4 px-2 py-3">
<div className="flex h-12 w-12 shrink-0 items-center justify-center rounded-md bg-neutral-800 text-neutral-600"> <button
<Music2 size={18} strokeWidth={1.75} /> onClick={() => toggle(toPreviewPlayable(selectedChat.id, m))}
</div> aria-label={isCurrent && isPlaying ? `Pause ${m.title}` : `Play 30 second preview of ${m.title}`}
className="flex h-12 w-12 shrink-0 items-center justify-center rounded-md bg-neutral-800 text-neutral-300 transition-colors hover:bg-neutral-700 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-emerald-400"
>
{isCurrent && isPlaying ? (
<Pause size={18} strokeWidth={1.75} />
) : (
<Play size={18} strokeWidth={1.75} />
)}
</button>
<div className="min-w-0 flex-1"> <div className="min-w-0 flex-1">
<p className="truncate font-medium text-white">{saved ? saved.title : m.title}</p> <p className="truncate font-medium text-white">{saved ? saved.title : m.title}</p>
<p className="truncate text-sm text-neutral-400">{saved ? saved.artist : m.artist}</p> <p className="truncate text-sm text-neutral-400">{saved ? saved.artist : m.artist}</p>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment