chore: update Angular cache and TypeScript build info files
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
import type { ProviderAdapter, ProviderSearchParams, SearchResult, SearchItem, ProviderId } from '../models';
|
||||
|
||||
export abstract class HttpAdapter implements ProviderAdapter {
|
||||
abstract key: ProviderId;
|
||||
abstract label: string;
|
||||
constructor(protected http: HttpClient) {}
|
||||
abstract search(params: ProviderSearchParams, signal: AbortSignal): Promise<SearchResult>;
|
||||
|
||||
protected mapItem(partial: Partial<SearchItem> & { id: string; title: string; watchUrl: string }): SearchItem {
|
||||
return {
|
||||
provider: this.key,
|
||||
channel: undefined,
|
||||
durationSec: undefined,
|
||||
views: undefined,
|
||||
publishedAt: undefined,
|
||||
isLive: false,
|
||||
isShort: false,
|
||||
thumbUrl: undefined,
|
||||
...partial,
|
||||
} as SearchItem;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
import type { ProviderAdapter, ProviderSearchParams, SearchResult } from '../models';
|
||||
|
||||
export class DmAdapter implements ProviderAdapter {
|
||||
key = 'dm' as const;
|
||||
label = 'Dailymotion';
|
||||
constructor(private http: HttpClient) {}
|
||||
async search(params: ProviderSearchParams, _signal: AbortSignal): Promise<SearchResult> {
|
||||
const res = await firstValueFrom(this.http.get<any>(`/api/search`, { params: { q: params.q, providers: 'dm' } }));
|
||||
const items = (res?.groups?.dm || []).map((it: any) => ({
|
||||
id: it.id,
|
||||
provider: this.key,
|
||||
title: it.title,
|
||||
channel: it.uploaderName,
|
||||
durationSec: it.duration,
|
||||
thumbUrl: it.thumbnail,
|
||||
watchUrl: it.url || '',
|
||||
views: undefined,
|
||||
publishedAt: undefined,
|
||||
isLive: it.type === 'live',
|
||||
isShort: it.isShort === true
|
||||
}));
|
||||
return { items, total: Array.isArray(res?.groups?.dm) ? res.groups.dm.length : 0 };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
import type { ProviderAdapter, ProviderSearchParams, SearchResult } from '../models';
|
||||
|
||||
export class OdAdapter implements ProviderAdapter {
|
||||
key = 'od' as const;
|
||||
label = 'Odysee';
|
||||
constructor(private http: HttpClient) {}
|
||||
async search(params: ProviderSearchParams, _signal: AbortSignal): Promise<SearchResult> {
|
||||
const res = await firstValueFrom(this.http.get<any>(`/api/search`, { params: { q: params.q, providers: 'od' } }));
|
||||
const items = (res?.groups?.od || []).map((it: any) => ({
|
||||
id: it.id,
|
||||
provider: this.key,
|
||||
title: it.title,
|
||||
channel: it.uploaderName,
|
||||
durationSec: it.duration,
|
||||
thumbUrl: it.thumbnail,
|
||||
watchUrl: it.url || '',
|
||||
views: undefined,
|
||||
publishedAt: undefined,
|
||||
isLive: false,
|
||||
isShort: it.isShort === true
|
||||
}));
|
||||
return { items, total: Array.isArray(res?.groups?.od) ? res.groups.od.length : 0 };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
import type { ProviderAdapter, ProviderSearchParams, SearchResult } from '../models';
|
||||
|
||||
export class PtAdapter implements ProviderAdapter {
|
||||
key = 'pt' as const;
|
||||
label = 'PeerTube';
|
||||
constructor(private http: HttpClient) {}
|
||||
async search(params: ProviderSearchParams, _signal: AbortSignal): Promise<SearchResult> {
|
||||
const res = await firstValueFrom(this.http.get<any>(`/api/search`, { params: { q: params.q, providers: 'pt' } }));
|
||||
const items = (res?.groups?.pt || []).map((it: any) => ({
|
||||
id: it.id,
|
||||
provider: this.key,
|
||||
title: it.title,
|
||||
channel: it.uploaderName,
|
||||
durationSec: it.duration,
|
||||
thumbUrl: it.thumbnail,
|
||||
watchUrl: it.url || '',
|
||||
views: undefined,
|
||||
publishedAt: undefined,
|
||||
isLive: false,
|
||||
isShort: false
|
||||
}));
|
||||
return { items, total: Array.isArray(res?.groups?.pt) ? res.groups.pt.length : 0 };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
import type { ProviderAdapter, ProviderSearchParams, SearchResult } from '../models';
|
||||
|
||||
export class RuAdapter implements ProviderAdapter {
|
||||
key = 'ru' as const;
|
||||
label = 'Rumble';
|
||||
constructor(private http: HttpClient) {}
|
||||
async search(params: ProviderSearchParams, _signal: AbortSignal): Promise<SearchResult> {
|
||||
const res = await firstValueFrom(this.http.get<any>(`/api/search`, { params: { q: params.q, providers: 'ru' } }));
|
||||
const items = (res?.groups?.ru || []).map((it: any) => ({
|
||||
id: it.id,
|
||||
provider: this.key,
|
||||
title: it.title,
|
||||
channel: it.uploaderName,
|
||||
durationSec: it.duration,
|
||||
thumbUrl: it.thumbnail,
|
||||
watchUrl: it.url || '',
|
||||
views: undefined,
|
||||
publishedAt: undefined,
|
||||
isLive: false,
|
||||
isShort: false
|
||||
}));
|
||||
return { items, total: Array.isArray(res?.groups?.ru) ? res.groups.ru.length : 0 };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
import type { ProviderAdapter, ProviderSearchParams, SearchResult } from '../models';
|
||||
|
||||
export class TwAdapter implements ProviderAdapter {
|
||||
key = 'tw' as const;
|
||||
label = 'Twitch';
|
||||
constructor(private http: HttpClient) {}
|
||||
async search(params: ProviderSearchParams, _signal: AbortSignal): Promise<SearchResult> {
|
||||
const res = await firstValueFrom(this.http.get<any>(`/api/search`, { params: { q: params.q, providers: 'tw' } }));
|
||||
const items = (res?.groups?.tw || []).map((it: any) => ({
|
||||
id: it.id,
|
||||
provider: this.key,
|
||||
title: it.title,
|
||||
channel: it.uploaderName,
|
||||
durationSec: it.duration,
|
||||
thumbUrl: it.thumbnail,
|
||||
watchUrl: it.url || '',
|
||||
views: undefined,
|
||||
publishedAt: undefined,
|
||||
isLive: it.type === 'live',
|
||||
isShort: false
|
||||
}));
|
||||
return { items, total: Array.isArray(res?.groups?.tw) ? res.groups.tw.length : 0 };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
import type { ProviderAdapter, ProviderSearchParams, SearchResult } from '../models';
|
||||
|
||||
export class YtAdapter implements ProviderAdapter {
|
||||
key = 'yt' as const;
|
||||
label = 'YouTube';
|
||||
constructor(private http: HttpClient) {}
|
||||
async search(params: ProviderSearchParams, _signal: AbortSignal): Promise<SearchResult> {
|
||||
const res = await firstValueFrom(this.http.get<any>(`/api/search`, { params: { q: params.q, providers: 'yt' } }));
|
||||
const items = (res?.groups?.yt || []).map((it: any) => ({
|
||||
id: it.id,
|
||||
provider: this.key,
|
||||
title: it.title,
|
||||
channel: it.uploaderName,
|
||||
durationSec: it.duration,
|
||||
thumbUrl: it.thumbnail,
|
||||
watchUrl: it.url || '',
|
||||
views: undefined,
|
||||
publishedAt: undefined,
|
||||
isLive: it.type === 'live',
|
||||
isShort: it.isShort === true
|
||||
}));
|
||||
return { items, total: Array.isArray(res?.groups?.yt) ? res.groups.yt.length : 0 };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
export type ProviderId = 'yt' | 'dm' | 'tw' | 'pt' | 'od' | 'ru';
|
||||
|
||||
export interface SuggestionItemV1 {
|
||||
title: string;
|
||||
id: string;
|
||||
duration?: number;
|
||||
isShort?: boolean;
|
||||
thumbnail?: string;
|
||||
uploaderName?: string;
|
||||
url?: string;
|
||||
type?: string;
|
||||
}
|
||||
|
||||
export interface SearchResponseV1 {
|
||||
q: string;
|
||||
providers: ProviderId[];
|
||||
groups: Record<ProviderId, SuggestionItemV1[]>;
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
sort?: 'relevance' | 'date' | 'views';
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
export type ProviderId = 'yt'|'dm'|'tw'|'pt'|'od'|'ru';
|
||||
|
||||
export interface SearchItem {
|
||||
id: string;
|
||||
provider: ProviderId;
|
||||
title: string;
|
||||
channel?: string;
|
||||
durationSec?: number;
|
||||
views?: number;
|
||||
publishedAt?: string; // ISO
|
||||
isLive?: boolean;
|
||||
isShort?: boolean;
|
||||
thumbUrl?: string;
|
||||
watchUrl: string; // internal or external
|
||||
}
|
||||
|
||||
export interface SearchResult {
|
||||
items: SearchItem[];
|
||||
total?: number;
|
||||
nextPageToken?: string; // or page: string|number
|
||||
}
|
||||
|
||||
export interface ProviderSearchParams {
|
||||
q: string;
|
||||
pageToken?: string; // or page
|
||||
sort?: 'relevance'|'date'|'views'|'duration';
|
||||
time?: 'today'|'7d'|'30d'|'all';
|
||||
length?: 'short'|'medium'|'long'|'all';
|
||||
type?: 'video'|'live'|'shorts'|'all';
|
||||
}
|
||||
|
||||
export interface ProviderAdapter {
|
||||
key: ProviderId;
|
||||
label: string;
|
||||
search(params: ProviderSearchParams, signal: AbortSignal): Promise<SearchResult>;
|
||||
}
|
||||
@@ -1,49 +1,126 @@
|
||||
import { Injectable, inject } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { BehaviorSubject, combineLatest, distinctUntilChanged, debounceTime, map, switchMap } from 'rxjs';
|
||||
import { BehaviorSubject, combineLatest, distinctUntilChanged, debounceTime, map, switchMap, filter, shareReplay, from, of } from 'rxjs';
|
||||
import type { ProviderId } from '../core/providers/provider-registry';
|
||||
import type { SuggestionItemV1, SearchResponseV1 } from './api.v1';
|
||||
import { YtAdapter } from './adapters/yt';
|
||||
import { DmAdapter } from './adapters/dm';
|
||||
import { TwAdapter } from './adapters/tw';
|
||||
import { PtAdapter } from './adapters/pt';
|
||||
import { OdAdapter } from './adapters/od';
|
||||
import { RuAdapter } from './adapters/ru';
|
||||
import type { ProviderAdapter, ProviderSearchParams, SearchResult } from './models';
|
||||
|
||||
export interface SuggestionItem {
|
||||
title: string;
|
||||
id: string;
|
||||
duration?: number;
|
||||
isShort?: boolean;
|
||||
thumbnail?: string;
|
||||
uploaderName?: string;
|
||||
url?: string;
|
||||
type?: string; // 'video' | 'channel' | ...
|
||||
}
|
||||
export type SuggestionItem = SuggestionItemV1;
|
||||
export type SearchResponse = SearchResponseV1;
|
||||
|
||||
export interface SearchResponse {
|
||||
q: string;
|
||||
providers: ProviderId[];
|
||||
groups: Record<ProviderId, SuggestionItem[]>;
|
||||
}
|
||||
type CacheKey = string;
|
||||
interface CacheEntry { t: number; data: SearchResult; }
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class SearchService {
|
||||
private http = inject(HttpClient);
|
||||
|
||||
// Subjects for params from UI/URL
|
||||
readonly q$ = new BehaviorSubject<string>('');
|
||||
readonly providers$ = new BehaviorSubject<ProviderId[] | 'all'>('all');
|
||||
readonly page$ = new BehaviorSubject<number>(1);
|
||||
readonly pageSize$ = new BehaviorSubject<number>(24);
|
||||
readonly sort$ = new BehaviorSubject<'relevance' | 'date' | 'views' | 'duration'>('relevance');
|
||||
|
||||
readonly params$ = combineLatest([this.q$, this.providers$]).pipe(
|
||||
// In-memory cache 60s per (provider, q, params)
|
||||
private cache = new Map<CacheKey, CacheEntry>();
|
||||
private cacheTtlMs = 60_000;
|
||||
|
||||
// Adapters registry
|
||||
private adapters: Record<ProviderId, ProviderAdapter> = {
|
||||
yt: new YtAdapter(this.http),
|
||||
dm: new DmAdapter(this.http),
|
||||
tw: new TwAdapter(this.http),
|
||||
pt: new PtAdapter(this.http),
|
||||
od: new OdAdapter(this.http),
|
||||
ru: new RuAdapter(this.http)
|
||||
};
|
||||
|
||||
readonly params$ = combineLatest([this.q$, this.providers$, this.page$, this.pageSize$, this.sort$]).pipe(
|
||||
debounceTime(120),
|
||||
distinctUntilChanged((a, b) => JSON.stringify(a) === JSON.stringify(b))
|
||||
);
|
||||
|
||||
// Public request stream: aggregates groups by provider, runs adapters in parallel with timeout/abort/retry
|
||||
readonly request$ = this.params$.pipe(
|
||||
switchMap(([q, prov]) => {
|
||||
const providersParam = (prov === 'all')
|
||||
? 'yt,dm,tw,pt,od,ru'
|
||||
: (Array.isArray(prov) && prov.length > 0 ? prov.join(',') : '');
|
||||
return this.http.get<SearchResponse>('/api/search', {
|
||||
params: { q, providers: providersParam }
|
||||
});
|
||||
})
|
||||
filter(([q]) => typeof q === 'string' && q.trim().length >= 2),
|
||||
switchMap(([q, prov, page, pageSize, sort]) => from(this.runAdapters({
|
||||
q: String(q),
|
||||
pageToken: String(page || 1),
|
||||
sort: (sort as any) || 'relevance',
|
||||
// global filter knobs could be added here: time/length/type in the future
|
||||
}, prov)))
|
||||
,
|
||||
// Map normalized items to legacy "groups" response expected by UI
|
||||
map((byProvider) => {
|
||||
const groups = Object.entries(byProvider).reduce((acc, [pid, result]) => {
|
||||
const arr: SuggestionItemV1[] = (result?.items || []).map(it => ({
|
||||
id: it.id,
|
||||
title: it.title,
|
||||
duration: it.durationSec,
|
||||
isShort: it.isShort,
|
||||
thumbnail: it.thumbUrl,
|
||||
uploaderName: it.channel,
|
||||
url: it.watchUrl,
|
||||
type: it.isLive ? 'live' : 'video'
|
||||
}));
|
||||
(acc as any)[pid as ProviderId] = arr;
|
||||
return acc;
|
||||
}, {} as Record<ProviderId, SuggestionItemV1[]>);
|
||||
const providers = Object.keys(byProvider) as ProviderId[];
|
||||
const resp: SearchResponse = { q: this.q$.value, providers, groups };
|
||||
return resp;
|
||||
}),
|
||||
shareReplay(1)
|
||||
);
|
||||
|
||||
// Orchestrate all active providers in parallel with timeout(8s), retry(1) and abort on param change.
|
||||
private async runAdapters(params: ProviderSearchParams, prov: ProviderId[] | 'all') {
|
||||
const active: ProviderId[] = prov === 'all' ? ['yt','dm','tw','pt','od','ru'] : (Array.isArray(prov) ? prov : []);
|
||||
const controller = new AbortController();
|
||||
const signal = controller.signal;
|
||||
const tasks = active.map(async (pid) => {
|
||||
const adapter = this.adapters[pid];
|
||||
if (!adapter) return [pid, { items: [] as any[] } as SearchResult] as const;
|
||||
const key: CacheKey = `${pid}|${params.q}|${params.pageToken}|${params.sort}`;
|
||||
const now = Date.now();
|
||||
const cached = this.cache.get(key);
|
||||
if (cached && (now - cached.t) < this.cacheTtlMs) {
|
||||
return [pid, cached.data] as const;
|
||||
}
|
||||
const perProviderTimeout = 8000;
|
||||
const withTimeout = <T>(p: Promise<T>): Promise<T> => new Promise((resolve, reject) => {
|
||||
const tid = setTimeout(() => reject(new Error('timeout')), perProviderTimeout);
|
||||
p.then(v => { clearTimeout(tid); resolve(v); }).catch(e => { clearTimeout(tid); reject(e); });
|
||||
});
|
||||
const attempt = async (): Promise<SearchResult> => withTimeout(adapter.search(params, signal));
|
||||
try {
|
||||
const res = await attempt().catch(async (e) => {
|
||||
// retry once on network-like errors
|
||||
if (e && (e.name === 'AbortError' || String(e.message || '').includes('abort'))) throw e;
|
||||
try { return await attempt(); } catch (err) { throw err; }
|
||||
});
|
||||
this.cache.set(key, { t: Date.now(), data: res });
|
||||
return [pid, res] as const;
|
||||
} catch {
|
||||
// Swallow errors per provider, return empty
|
||||
return [pid, { items: [] }] as const;
|
||||
}
|
||||
});
|
||||
const results = await Promise.all(tasks);
|
||||
return Object.fromEntries(results) as Record<ProviderId, SearchResult>;
|
||||
}
|
||||
|
||||
// Convenience setter helpers
|
||||
setQuery(q: string) { this.q$.next(q || ''); }
|
||||
setProviders(list: ProviderId[] | 'all') { this.providers$.next(list && (Array.isArray(list) ? list : 'all')); }
|
||||
setPage(page: number) { this.page$.next(Math.max(1, Math.floor(page || 1))); }
|
||||
setPageSize(size: number) { this.pageSize$.next(Math.min(50, Math.max(1, Math.floor(size || 24)))); }
|
||||
setSort(sort: 'relevance' | 'date' | 'views' | 'duration') { this.sort$.next(sort || 'relevance'); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user