NewTube/src/services/history.service.ts

100 lines
3.5 KiB
TypeScript

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
export interface SearchHistoryItem {
id: string;
query: string;
filters_json?: string | null;
created_at: string;
}
export interface WatchHistoryItem {
id: string;
provider: string;
video_id: string;
title?: string | null;
watched_at: string;
progress_seconds: number;
duration_seconds: number;
last_position_seconds: number;
last_watched_at?: string;
thumbnail?: string | null;
}
@Injectable({ providedIn: 'root' })
export class HistoryService {
constructor(private http: HttpClient) {}
// --- Search ---
recordSearch(query: string, filters?: Record<string, any>): Observable<{ id: string; created_at: string }> {
return this.http.post<{ id: string; created_at: string }>(
'/proxy/api/user/history/search',
{ query, filters },
{ withCredentials: true }
);
}
getSearchHistory(limit = 50, before?: string): Observable<SearchHistoryItem[]> {
const params = new URLSearchParams();
params.set('limit', String(limit));
if (before) params.set('before', before);
return this.http.get<SearchHistoryItem[]>(`/proxy/api/user/history/search?${params.toString()}`, { withCredentials: true });
}
// --- Watch ---
recordWatchStart(provider: string, videoId: string, title?: string | null, watchedAt?: string, thumbnail?: string | null): Observable<WatchHistoryItem> {
return this.http.post<WatchHistoryItem>(
'/proxy/api/user/history/watch',
{ provider, videoId, title: title ?? null, watchedAt, thumbnail: thumbnail ?? null },
{ withCredentials: true }
);
}
updateWatchProgress(id: string, progressSeconds: number, lastPositionSeconds?: number): Observable<WatchHistoryItem> {
return this.http.patch<WatchHistoryItem>(
`/proxy/api/user/history/watch/${encodeURIComponent(id)}`,
{ progressSeconds, lastPositionSeconds },
{ withCredentials: true }
);
}
getWatchHistory(limit = 50, before?: string): Observable<WatchHistoryItem[]> {
const params = new URLSearchParams();
params.set('limit', String(limit));
if (before) params.set('before', before);
return this.http.get<WatchHistoryItem[]>(`/proxy/api/user/history/watch?${params.toString()}`, { withCredentials: true });
}
// --- Delete single items ---
deleteSearchItem(id: string): Observable<void> {
return this.http.delete<void>(`/proxy/api/user/history/search/${encodeURIComponent(id)}`, { withCredentials: true });
}
deleteWatchItem(id: string): Observable<void> {
return this.http.delete<void>(`/proxy/api/user/history/watch/${encodeURIComponent(id)}`, { withCredentials: true });
}
// Recherche dans l'historique des recherches
searchInSearchHistory(query: string, limit = 100): Observable<SearchHistoryItem[]> {
const params = new URLSearchParams();
params.set('q', query);
params.set('limit', String(limit));
return this.http.get<SearchHistoryItem[]>(
`/proxy/api/user/history/search?${params.toString()}`,
{ withCredentials: true }
);
}
// Recherche dans l'historique de visionnage
searchInWatchHistory(query: string, limit = 100): Observable<WatchHistoryItem[]> {
const params = new URLSearchParams();
params.set('q', query);
params.set('limit', String(limit));
return this.http.get<WatchHistoryItem[]>(
`/proxy/api/user/history/watch?${params.toString()}`,
{ withCredentials: true }
);
}
}