NewTube/src/services/likes.service.ts

50 lines
1.7 KiB
TypeScript

import { Injectable, inject } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
export interface LikedVideoItem {
provider: string;
video_id: string;
title?: string;
thumbnail?: string;
last_watched_at?: string | null;
}
@Injectable({ providedIn: 'root' })
export class LikesService {
private http = inject(HttpClient);
private apiBase(): string {
try {
const port = window?.location?.port || '';
return port && port !== '4000' ? '/proxy/api' : '/api';
} catch {
return '/api';
}
}
list(limit = 100, q?: string): Observable<LikedVideoItem[]> {
let params = new HttpParams().set('limit', String(limit));
if (typeof q === 'string' && q.trim().length > 0) params = params.set('q', q.trim());
return this.http.get<LikedVideoItem[]>(`${this.apiBase()}/user/likes`, { params, withCredentials: true });
}
like(provider: string, videoId: string): Observable<{ provider: string; video_id: string }> {
return this.http.post<{ provider: string; video_id: string }>(
`${this.apiBase()}/user/likes`,
{ provider, videoId },
{ withCredentials: true }
);
}
unlike(provider: string, videoId: string): Observable<{ removed: boolean }> {
const params = new HttpParams().set('provider', provider).set('videoId', videoId);
return this.http.delete<{ removed: boolean }>(`${this.apiBase()}/user/likes`, { params, withCredentials: true });
}
isLiked(provider: string, videoId: string): Observable<{ liked: boolean }> {
const params = new HttpParams().set('provider', provider).set('videoId', videoId);
return this.http.get<{ liked: boolean }>(`${this.apiBase()}/user/likes/status`, { params, withCredentials: true });
}
}