28 lines
1.2 KiB
TypeScript
28 lines
1.2 KiB
TypeScript
import { HttpClient, HttpParams } from '@angular/common/http';
|
|
import { firstValueFrom } from 'rxjs';
|
|
import type { ProviderAdapter, ProviderSearchParams, SearchResult } from '../models';
|
|
import { VideoItem } from 'src/app/shared/models/video-item.model';
|
|
|
|
export class PtAdapter implements ProviderAdapter<VideoItem> {
|
|
key = 'pt' as const;
|
|
label = 'PeerTube';
|
|
constructor(private http: HttpClient) {}
|
|
async search(params: ProviderSearchParams, _signal: AbortSignal): Promise<SearchResult<VideoItem>> {
|
|
let httpParams = new HttpParams().set('q', params.q).set('providers', 'pt');
|
|
if (params.pageToken) httpParams = httpParams.set('page', params.pageToken);
|
|
if (params.sort) httpParams = httpParams.set('sort', params.sort);
|
|
const res = await firstValueFrom(this.http.get<any>(`/api/search`, { params: httpParams }));
|
|
const items: VideoItem[] = (res?.groups?.pt || []).map((it: any) => ({
|
|
id: it.id,
|
|
provider: 'peertube',
|
|
title: it.title,
|
|
channelName: it.uploaderName,
|
|
durationSec: it.duration,
|
|
thumbnailUrl: it.thumbnail,
|
|
viewCount: undefined,
|
|
publishedAt: undefined,
|
|
}));
|
|
return { items, total: Array.isArray(res?.groups?.pt) ? res.groups.pt.length : 0 };
|
|
}
|
|
}
|