jellyfish-web/src/components/itemHelper.js

351 lines
8.7 KiB
JavaScript
Raw Normal View History

2020-08-16 20:24:45 +02:00
import { appHost } from './apphost';
2020-08-14 08:46:34 +02:00
import globalize from '../scripts/globalize';
2018-10-23 01:05:09 +03:00
2020-07-29 10:05:16 +01:00
export function getDisplayName(item, options = {}) {
if (!item) {
throw new Error('null item passed into getDisplayName');
}
2020-07-29 10:05:16 +01:00
if (item.Type === 'Timer') {
item = item.ProgramInfo || item;
}
2020-07-29 10:05:16 +01:00
let name = ((item.Type === 'Program' || item.Type === 'Recording') && (item.IsSeries || item.EpisodeTitle) ? item.EpisodeTitle : item.Name) || '';
2020-07-29 10:05:16 +01:00
if (item.Type === 'TvChannel') {
if (item.ChannelNumber) {
return item.ChannelNumber + ' ' + name;
}
return name;
2018-10-23 01:05:09 +03:00
}
2020-07-29 10:05:16 +01:00
if (item.Type === 'Episode' && item.ParentIndexNumber === 0) {
name = globalize.translate('ValueSpecialEpisodeName', name);
} else if ((item.Type === 'Episode' || item.Type === 'Program') && item.IndexNumber != null && item.ParentIndexNumber != null && options.includeIndexNumber !== false) {
let displayIndexNumber = item.IndexNumber;
let number = displayIndexNumber;
let nameSeparator = ' - ';
if (options.includeParentInfo !== false) {
number = 'S' + item.ParentIndexNumber + ':E' + number;
} else {
nameSeparator = '. ';
}
2018-10-23 01:05:09 +03:00
2020-07-29 10:05:16 +01:00
if (item.IndexNumberEnd) {
displayIndexNumber = item.IndexNumberEnd;
number += '-' + displayIndexNumber;
}
2020-07-29 10:05:16 +01:00
if (number) {
name = name ? (number + nameSeparator + name) : number;
}
2018-10-23 01:05:09 +03:00
}
2020-07-29 10:05:16 +01:00
return name;
}
2020-07-29 10:05:16 +01:00
export function supportsAddingToCollection(item) {
const invalidTypes = ['Genre', 'MusicGenre', 'Studio', 'UserView', 'CollectionFolder', 'Audio', 'Program', 'Timer', 'SeriesTimer'];
2020-07-29 10:05:16 +01:00
if (item.Type === 'Recording') {
if (item.Status !== 'Completed') {
return false;
}
2018-10-23 01:05:09 +03:00
}
2020-07-29 10:05:16 +01:00
return !item.CollectionType && invalidTypes.indexOf(item.Type) === -1 && item.MediaType !== 'Photo' && !isLocalItem(item);
}
2020-07-29 10:05:16 +01:00
export function supportsAddingToPlaylist(item) {
if (item.Type === 'Program') {
return false;
}
if (item.Type === 'TvChannel') {
return false;
}
if (item.Type === 'Timer') {
return false;
}
if (item.Type === 'SeriesTimer') {
return false;
}
if (item.MediaType === 'Photo') {
return false;
}
2020-07-29 10:05:16 +01:00
if (item.Type === 'Recording') {
if (item.Status !== 'Completed') {
return false;
}
2020-07-29 10:05:16 +01:00
}
2020-07-29 10:05:16 +01:00
if (isLocalItem(item)) {
return false;
}
if (item.CollectionType === 'livetv') {
return false;
}
2020-07-29 10:05:16 +01:00
return item.MediaType || item.IsFolder || item.Type === 'Genre' || item.Type === 'MusicGenre' || item.Type === 'MusicArtist';
}
2020-07-29 10:05:16 +01:00
export function canEdit(user, item) {
const itemType = item.Type;
2020-07-29 10:05:16 +01:00
if (itemType === 'UserRootFolder' || itemType === 'UserView') {
return false;
}
2020-07-29 10:05:16 +01:00
if (itemType === 'Program') {
return false;
2018-10-23 01:05:09 +03:00
}
2020-07-29 10:05:16 +01:00
if (itemType === 'Timer') {
return false;
}
if (itemType === 'SeriesTimer') {
return false;
}
if (item.Type === 'Recording') {
if (item.Status !== 'Completed') {
return false;
}
2020-07-29 10:05:16 +01:00
}
2020-07-29 10:05:16 +01:00
if (isLocalItem(item)) {
return false;
2018-10-23 01:05:09 +03:00
}
2020-07-29 10:05:16 +01:00
return user.Policy.IsAdministrator;
}
2020-07-29 10:05:16 +01:00
export function isLocalItem(item) {
if (item && item.Id && item.Id.indexOf('local') === 0) {
return true;
}
2020-07-29 10:05:16 +01:00
return false;
}
export function canIdentify (user, item) {
const itemType = item.Type;
if (itemType === 'Movie' ||
itemType === 'Trailer' ||
itemType === 'Series' ||
itemType === 'BoxSet' ||
itemType === 'Person' ||
itemType === 'Book' ||
itemType === 'MusicAlbum' ||
itemType === 'MusicArtist' ||
itemType === 'MusicVideo') {
if (user.Policy.IsAdministrator) {
if (!isLocalItem(item)) {
return true;
}
2020-07-29 10:05:16 +01:00
}
}
2020-07-29 10:05:16 +01:00
return false;
}
2020-07-29 10:05:16 +01:00
export function canEditImages (user, item) {
const itemType = item.Type;
2020-07-29 10:05:16 +01:00
if (item.MediaType === 'Photo') {
return false;
}
2020-07-29 10:05:16 +01:00
if (itemType === 'UserView') {
if (user.Policy.IsAdministrator) {
return true;
}
2020-07-29 10:05:16 +01:00
return false;
}
2020-07-29 10:05:16 +01:00
if (item.Type === 'Recording') {
if (item.Status !== 'Completed') {
return false;
}
}
2020-07-29 10:05:16 +01:00
return itemType !== 'Timer' && itemType !== 'SeriesTimer' && canEdit(user, item) && !isLocalItem(item);
}
2020-07-29 10:05:16 +01:00
export function canSync (user, item) {
if (user && !user.Policy.EnableContentDownloading) {
return false;
}
2020-07-29 10:05:16 +01:00
if (isLocalItem(item)) {
return false;
}
2020-07-29 10:05:16 +01:00
return item.SupportsSync;
}
2020-07-29 10:05:16 +01:00
export function canShare (item, user) {
if (item.Type === 'Program') {
return false;
}
if (item.Type === 'TvChannel') {
return false;
}
if (item.Type === 'Timer') {
return false;
}
if (item.Type === 'SeriesTimer') {
return false;
}
if (item.Type === 'Recording') {
if (item.Status !== 'Completed') {
return false;
}
}
if (isLocalItem(item)) {
return false;
}
return user.Policy.EnablePublicSharing && appHost.supports('sharing');
}
2020-07-29 10:05:16 +01:00
export function enableDateAddedDisplay (item) {
return !item.IsFolder && item.MediaType && item.Type !== 'Program' && item.Type !== 'TvChannel' && item.Type !== 'Trailer';
}
2020-07-29 10:05:16 +01:00
export function canMarkPlayed (item) {
if (item.Type === 'Program') {
return false;
}
2020-07-29 10:05:16 +01:00
if (item.MediaType === 'Video') {
if (item.Type !== 'TvChannel') {
return true;
}
} else if (item.MediaType === 'Audio') {
if (item.Type === 'AudioPodcast') {
return true;
}
if (item.Type === 'AudioBook') {
return true;
}
}
2020-07-29 10:05:16 +01:00
if (item.Type === 'Series' ||
item.Type === 'Season' ||
item.Type === 'BoxSet' ||
item.MediaType === 'Book' ||
item.MediaType === 'Recording') {
return true;
}
2020-07-29 10:05:16 +01:00
return false;
}
export function canRate (item) {
if (item.Type === 'Program'
|| item.Type === 'Timer'
|| item.Type === 'SeriesTimer'
|| item.Type === 'CollectionFolder'
|| item.Type === 'UserView'
|| item.Type === 'Channel'
|| !item.UserData) {
return false;
}
2020-07-29 10:05:16 +01:00
return true;
}
2020-07-29 10:05:16 +01:00
export function canConvert (item, user) {
if (!user.Policy.EnableMediaConversion) {
return false;
}
2020-07-29 10:05:16 +01:00
if (isLocalItem(item)) {
return false;
}
2020-07-29 10:05:16 +01:00
const mediaType = item.MediaType;
if (mediaType === 'Book' || mediaType === 'Photo' || mediaType === 'Audio') {
return false;
}
2020-07-29 10:05:16 +01:00
const collectionType = item.CollectionType;
if (collectionType === 'livetv') {
return false;
}
2020-07-29 10:05:16 +01:00
const type = item.Type;
if (type === 'Channel' || type === 'Person' || type === 'Year' || type === 'Program' || type === 'Timer' || type === 'SeriesTimer') {
return false;
}
2020-07-29 10:05:16 +01:00
if (item.LocationType === 'Virtual' && !item.IsFolder) {
return false;
}
2020-07-29 10:05:16 +01:00
if (item.IsPlaceHolder) {
return false;
}
2020-07-29 10:05:16 +01:00
return true;
}
2020-07-29 10:05:16 +01:00
export function canRefreshMetadata (item, user) {
if (user.Policy.IsAdministrator) {
const collectionType = item.CollectionType;
if (collectionType === 'livetv') {
return false;
2020-07-29 10:05:16 +01:00
}
2020-07-29 10:05:16 +01:00
if (item.Type !== 'Timer' && item.Type !== 'SeriesTimer' && item.Type !== 'Program' && item.Type !== 'TvChannel' && !(item.Type === 'Recording' && item.Status !== 'Completed')) {
if (!isLocalItem(item)) {
return true;
}
2018-10-23 01:05:09 +03:00
}
2020-07-29 10:05:16 +01:00
}
return false;
}
export function supportsMediaSourceSelection (item) {
if (item.MediaType !== 'Video') {
return false;
}
if (item.Type === 'TvChannel') {
return false;
}
if (!item.MediaSources || (item.MediaSources.length === 1 && item.MediaSources[0].Type === 'Placeholder')) {
return false;
}
if (item.EnableMediaSourceDisplay === false) {
return false;
}
if (item.EnableMediaSourceDisplay == null && item.SourceType && item.SourceType !== 'Library') {
return false;
}
return true;
}
export default {
getDisplayName: getDisplayName,
supportsAddingToCollection: supportsAddingToCollection,
supportsAddingToPlaylist: supportsAddingToPlaylist,
isLocalItem: isLocalItem,
canIdentify: canIdentify,
canEdit: canEdit,
canEditImages: canEditImages,
canSync: canSync,
canShare: canShare,
enableDateAddedDisplay: enableDateAddedDisplay,
canMarkPlayed: canMarkPlayed,
canRate: canRate,
canConvert: canConvert,
canRefreshMetadata: canRefreshMetadata,
supportsMediaSourceSelection: supportsMediaSourceSelection
};