Files
streamyfin/utils/jellyfin/playstate/markAsPlayed.ts
Fredrik Burmester d4d3cbbc43 chore: refactor
2024-08-06 23:53:00 +02:00

51 lines
1.4 KiB
TypeScript

import { Api } from "@jellyfin/sdk";
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models";
import { AxiosError } from "axios";
import { getAuthHeaders } from "../jellyfin";
interface MarkAsPlayedParams {
api: Api | null | undefined;
item: BaseItemDto | null | undefined;
userId: string | null | undefined;
}
/**
* Marks a media item as played and updates its progress to completion.
*
* @param params - The parameters for marking an item as played
* @returns A promise that resolves to true if the operation was successful, false otherwise
*/
export const markAsPlayed = async ({
api,
item,
userId,
}: MarkAsPlayedParams): Promise<boolean> => {
if (!api || !item?.Id || !userId || !item.RunTimeTicks) {
console.error("Invalid parameters for markAsPlayed");
return false;
}
try {
const [playedResponse, progressResponse] = await Promise.all([
api.axiosInstance.post(
`${api.basePath}/UserPlayedItems/${item.Id}`,
{ userId, datePlayed: new Date().toISOString() },
{ headers: getAuthHeaders(api) },
),
api.axiosInstance.post(
`${api.basePath}/Sessions/Playing/Progress`,
{
ItemId: item.Id,
PositionTicks: item.RunTimeTicks,
MediaSourceId: item.Id,
},
{ headers: getAuthHeaders(api) },
),
]);
return playedResponse.status === 200 && progressResponse.status === 200;
} catch (error) {
return false;
}
};