import qs from 'qs'; import { postgrestLocalUrl, postgrestUrl } from './constants' import { ITagsResponse, ITag, ITagResponse } from './tags'; import { IMeta } from '@futureporn/types'; import { getCountFromHeaders } from './fetchers'; export interface ITimestamp { id: number; time: number; tag_name: string; tn_short: string; tag_id: number; vod_id: number; tag: ITagResponse; created_at: string; creator_id: number; } export interface ITimestampResponse { data: ITimestamp; meta: IMeta; } export interface ITimestampsResponse { data: ITimestamp[]; meta: IMeta; } function truncateString(str: string, maxLength: number) { if (str.length <= maxLength) { return str; } return str.substring(0, maxLength - 1) + '…'; } export function deleteTimestamp(authData: IAuthData, tsId: number) { return fetch(`${postgrestLocalUrl}/timestamps/deleteMine/${tsId}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${authData.accessToken}`, 'Content-Type': 'application/json' } }) .then((res) => { if (!res.ok) throw new Error(res.statusText); else return res.json(); }) .catch((e) => { console.error(e); // setError('root.serverError', { message: e.message }) }) } export async function createTimestamp( authData: IAuthData, tagId: number, vodId: number, time: number ): Promise { if (!authData?.user?.id || !authData?.accessToken) throw new Error('User must be logged in to create timestamps'); const query = qs.stringify({ populate: '*' }); const response = await fetch(`${postgrestLocalUrl}/timestamps?${query}`, { method: 'POST', headers: { 'Authorization': `Bearer ${authData.accessToken}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ data: { time: Math.floor(time), tag: tagId, vod: vodId, creatorId: authData.user.id } }) }); const json = await response.json(); if (!response.ok) { throw new Error(json?.error?.message || response.statusText); } return json.data; } export async function getTimestampsVodLinksForVod(vodId: number, page: number = 1, pageSize: number = 25): Promise { // const query = qs.stringify({ // filters: { // vod: { // id: { // $eq: vodId, // }, // }, // }, // populate: '*', // sort: 'time:asc', // pagination: { // page: page, // pageSize: pageSize, // }, // }); const query = `select=*,tags_vods(tag_id,tags(*))&id=eq.${vodId}`; const response = await fetch(`${postgrestUrl}/vods?${query}`, { headers: { "Prefer": "count=exact" } }); // /vods?id=eq.326&select=timestamps_vod_links(timestamps(timestamps_tag_links(tags(id,name)))) const data = await response.json() as ITimestamp[]; return data; }