Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct return type for getMetadata function #721

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 61 additions & 28 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import {
getTvShowsApi,
getUserApi
} from '@jellyfin/sdk/lib/utils/api';
import type {
GenericMediaMetadata,
MovieMediaMetadata,
MusicTrackMediaMetadata,
PhotoMediaMetadata,
TvShowMediaMetadata
} from 'chromecast-caf-receiver/cast.framework.messages';
import { JellyfinApi } from './components/jellyfinApi';
import {
PlaybackManager,
Expand Down Expand Up @@ -187,10 +194,20 @@ export function getSenderReportingData(
* @param item - item to look up
* @returns one of the metadata classes in cast.framework.messages.*Metadata
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getMetadata(item: BaseItemDto): any {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let metadata: any;
export function getMetadata(
item: BaseItemDto
):
| GenericMediaMetadata
| MovieMediaMetadata
| MusicTrackMediaMetadata
| PhotoMediaMetadata
| TvShowMediaMetadata {
let metadata:
| GenericMediaMetadata
| MovieMediaMetadata
| MusicTrackMediaMetadata
| PhotoMediaMetadata
| TvShowMediaMetadata;
let posterUrl = '';

if (item.SeriesPrimaryImageTag) {
Expand All @@ -208,80 +225,96 @@ export function getMetadata(item: BaseItemDto): any {
}

if (item.Type == 'Episode') {
metadata = new cast.framework.messages.TvShowMediaMetadata();
metadata.seriesTitle = item.SeriesName;
const tvShowMedata = new cast.framework.messages.TvShowMediaMetadata();

tvShowMedata.seriesTitle = item.SeriesName ?? undefined;

if (item.PremiereDate) {
metadata.originalAirdate = parseISO8601Date(
tvShowMedata.originalAirdate = parseISO8601Date(
item.PremiereDate
).toISOString();
}

if (item.IndexNumber != null) {
metadata.episode = item.IndexNumber;
tvShowMedata.episode = item.IndexNumber;
}

if (item.ParentIndexNumber != null) {
metadata.season = item.ParentIndexNumber;
tvShowMedata.season = item.ParentIndexNumber;
}

metadata = tvShowMedata;
} else if (item.Type == 'Photo') {
metadata = new cast.framework.messages.PhotoMediaMetadata();
const photoMetadata = new cast.framework.messages.PhotoMediaMetadata();

if (item.PremiereDate) {
metadata.creationDateTime = parseISO8601Date(
photoMetadata.creationDateTime = parseISO8601Date(
item.PremiereDate
).toISOString();
}
// TODO more metadata?

metadata = photoMetadata;
} else if (item.Type == 'Audio') {
metadata = new cast.framework.messages.MusicTrackMediaMetadata();
metadata.songName = item.Name;
metadata.artist = item.Artists?.length ? item.Artists.join(', ') : '';
metadata.albumArtist = item.AlbumArtist;
metadata.albumName = item.Album;
const musicTrackMetadata =
new cast.framework.messages.MusicTrackMediaMetadata();

musicTrackMetadata.songName = item.Name ?? undefined;
musicTrackMetadata.artist = item.Artists?.length
? item.Artists.join(', ')
: '';
musicTrackMetadata.albumArtist = item.AlbumArtist ?? undefined;
musicTrackMetadata.albumName = item.Album ?? undefined;

if (item.PremiereDate) {
metadata.releaseDate = parseISO8601Date(
musicTrackMetadata.releaseDate = parseISO8601Date(
item.PremiereDate
).toISOString();
}

if (item.IndexNumber != null) {
metadata.trackNumber = item.IndexNumber;
musicTrackMetadata.trackNumber = item.IndexNumber;
}

if (item.ParentIndexNumber != null) {
metadata.discNumber = item.ParentIndexNumber;
musicTrackMetadata.discNumber = item.ParentIndexNumber;
}

// previously: p.PersonType == 'Type'.. wtf?
const composer = (item.People ?? []).find(
(p: BaseItemPerson) => p.Type == 'Composer'
);

if (composer) {
metadata.composer = composer.Name;
if (composer?.Name) {
musicTrackMetadata.composer = composer.Name;
}

metadata = musicTrackMetadata;
} else if (item.Type == 'Movie') {
metadata = new cast.framework.messages.MovieMediaMetadata();
const movieMetadata = new cast.framework.messages.MovieMediaMetadata();

if (item.PremiereDate) {
metadata.releaseDate = parseISO8601Date(
movieMetadata.releaseDate = parseISO8601Date(
item.PremiereDate
).toISOString();
}

if (item.Studios?.length && item.Studios[0].Name) {
movieMetadata.studio = item.Studios[0].Name;
}

metadata = movieMetadata;
} else {
metadata = new cast.framework.messages.GenericMediaMetadata();
const genericMetadata =
new cast.framework.messages.GenericMediaMetadata();

if (item.PremiereDate) {
metadata.releaseDate = parseISO8601Date(
genericMetadata.releaseDate = parseISO8601Date(
item.PremiereDate
).toISOString();
}

if (item.Studios?.length) {
metadata.studio = item.Studios[0];
}
metadata = genericMetadata;
}

metadata.title = item.Name ?? '????';
Expand Down
Loading