jellyfin-web/dashboard-ui/bower_components/emby-apiclient/apiclientex.js

373 lines
11 KiB
JavaScript
Raw Normal View History

define(['apiclientcore', 'localassetmanager', 'events'], function (apiclientcorefactory, localassetmanager, events) {
'use strict';
var localPrefix = 'local:';
var localViewPrefix = 'localview:';
/**
* Creates a new api client instance
* @param {String} serverAddress
* @param {String} clientName s
* @param {String} applicationVersion
*/
return function (serverAddress, clientName, applicationVersion, deviceName, deviceId, devicePixelRatio) {
var apiclientcore = new apiclientcorefactory(serverAddress, clientName, applicationVersion, deviceName, deviceId, devicePixelRatio);
2017-01-11 21:27:01 -07:00
var self = Object.assign(this, apiclientcore);
events.on(apiclientcore, 'websocketmessage', onWebSocketMessage);
2017-01-11 21:27:01 -07:00
Object.defineProperty(self, 'onAuthenticated', {
get: function () { return apiclientcore.onAuthenticated; },
set: function (newValue) { apiclientcore.onAuthenticated = newValue; }
});
2017-01-11 21:27:01 -07:00
Object.defineProperty(self, 'enableAutomaticBitrateDetection', {
get: function () { return apiclientcore.enableAutomaticBitrateDetection; },
set: function (newValue) { apiclientcore.enableAutomaticBitrateDetection = newValue; }
});
function getUserViews(userId) {
return apiclientcore.getUserViews(userId).then(function (result) {
var serverInfo = apiclientcore.serverInfo();
if (serverInfo) {
return getLocalView(serverInfo.Id, userId).then(function (localView) {
if (localView) {
result.Items.push(localView);
result.TotalRecordCount++;
}
return Promise.resolve(result);
});
}
return Promis.resolve(result);
});
}
function getLocalView(serverId, userId) {
return localassetmanager.getViews(serverId, userId).then(function (views) {
var localView = null;
if (views.length > 0) {
localView = {
Name: 'Offline Items',
ServerId: serverId,
Id: 'localview',
Type: 'localview'
};
}
return Promise.resolve(localView);
});
}
function getItems(userId, options) {
var serverInfo = apiclientcore.serverInfo();
2017-01-22 20:54:51 -07:00
var i;
if (serverInfo && options.ParentId === 'localview') {
return localassetmanager.getViews(serverInfo.Id, userId).then(function (items) {
var result = {
Items: items,
TotalRecordCount: items.length
};
return Promise.resolve(result);
});
2017-01-22 20:54:51 -07:00
} else if (serverInfo && options && (startsWith(options.ParentId, localViewPrefix) || startsWith(options.ParentId, localPrefix))) {
return localassetmanager.getViewItems(serverInfo.Id, userId, options.ParentId).then(function (items) {
items.forEach(function (item) {
item.Id = localPrefix + item.Id;
});
2017-01-22 20:54:51 -07:00
items.sort(function (a, b) { return a.SortName.toLowerCase().localeCompare(b.SortName.toLowerCase()); });
var result = {
Items: items,
TotalRecordCount: items.length
};
return Promise.resolve(result);
});
} else if (options && options.ExcludeItemIds && options.ExcludeItemIds.length) {
2017-01-22 20:54:51 -07:00
var exItems = options.ExcludeItemIds.split(',');
2017-01-22 20:54:51 -07:00
for (i = 0; i < exItems.length; i++) {
if (startsWith(exItems[i], localPrefix)) {
2017-01-22 20:54:51 -07:00
return Promise.resolve(createEmptyList());
}
}
} else if (options && options.Ids && options.Ids.length) {
var ids = options.Ids.split(',');
var hasLocal = false;
for (i = 0; i < ids.length; i++) {
if (startsWith(ids[i], localPrefix)) {
hasLocal = true;
}
}
2017-01-22 20:54:51 -07:00
if (hasLocal) {
return localassetmanager.getItemsFromIds(serverInfo.Id, ids).then(function (items) {
items.forEach(function (item) {
item.Id = localPrefix + item.Id;
});
var result = {
Items: items,
TotalRecordCount: items.length
};
return Promise.resolve(result);
});
}
}
return apiclientcore.getItems(userId, options);
}
function getItem(userId, itemId) {
if (itemId) {
itemId = itemId.toString();
}
2016-12-29 12:16:33 -07:00
var serverInfo;
if (startsWith(itemId, localViewPrefix)) {
2016-12-29 12:16:33 -07:00
serverInfo = apiclientcore.serverInfo();
if (serverInfo) {
return localassetmanager.getViews(serverInfo.Id, userId).then(function (items) {
var views = items.filter(function (item) {
return item.Id === itemId;
});
if (views.length > 0) {
return Promise.resolve(views[0]);
}
// TODO: Test consequence of this
return Promise.reject();
});
}
}
if (startsWith(itemId, localPrefix)) {
2016-12-29 12:16:33 -07:00
serverInfo = apiclientcore.serverInfo();
if (serverInfo) {
return localassetmanager.getLocalItem(serverInfo.Id, stripStart(itemId, localPrefix)).then(function (item) {
item.Item.Id = localPrefix + item.Item.Id;
return Promise.resolve(item.Item);
});
}
}
return apiclientcore.getItem(userId, itemId);
}
2017-01-22 20:54:51 -07:00
function getNextUpEpisodes(options) {
if (options.SeriesId) {
if (startsWith(options.SeriesId, localPrefix)) {
return Promise.resolve(createEmptyList());
}
}
return apiclientcore.getNextUpEpisodes(options);
}
function getSeasons(itemId, options) {
if (startsWith(itemId, localPrefix)) {
options.ParentId = itemId;
return getItems(apiclientcore.getCurrentUserId(), options);
}
return apiclientcore.getSeasons(itemId, options);
}
function getEpisodes(itemId, options) {
if (startsWith(options.SeasonId, localPrefix)) {
options.ParentId = options.SeasonId;
return getItems(apiclientcore.getCurrentUserId(), options);
}
return apiclientcore.getEpisodes(itemId, options);
}
function getThemeMedia(userId, itemId, inherit) {
if (startsWith(itemId, localViewPrefix) || startsWith(itemId, localPrefix)) {
return Promise.reject();
}
return apiclientcore.getThemeMedia(userId, itemId, inherit);
}
function getSimilarItems(itemId, options) {
if (startsWith(itemId, localPrefix)) {
return Promise.resolve(createEmptyList());
}
return apiclientcore.getSimilarItems(itemId, options);
}
function updateFavoriteStatus(userId, itemId, isFavorite) {
if (startsWith(itemId, localPrefix)) {
return Promise.resolve();
}
return apiclientcore.updateFavoriteStatus(userId, itemId, isFavorite);
}
function getScaledImageUrl(itemId, options) {
if (startsWith(itemId, localPrefix)) {
var serverInfo = apiclientcore.serverInfo();
var id = stripStart(itemId, localPrefix);
return localassetmanager.getImageUrl(serverInfo.Id, id, options.type, 0);
}
return apiclientcore.getScaledImageUrl(itemId, options);
}
function onWebSocketMessage(e, msg) {
events.trigger(self, 'websocketmessage', [msg]);
}
// **************** Helper functions
function startsWith(str, find) {
if (str && find && str.length > find.length) {
if (str.indexOf(find) === 0) {
return true;
}
}
return false;
}
function stripStart(str, find) {
if (startsWith(str, find)) {
return str.substr(find.length);
}
return str;
}
function createEmptyList() {
var result = {
2016-12-29 12:16:33 -07:00
Items: [],
TotalRecordCount: 0
2016-12-29 12:16:33 -07:00
};
return result;
}
2017-01-24 12:54:18 -07:00
function getPlaybackInfo(itemId, options, deviceProfile) {
return localassetmanager.getLocalItem(apiclientcore.serverId(), stripStart(itemId, localPrefix)).then(function (item) {
// TODO: This was already done during the sync process, right? If so, remove it
var mediaSources = item.Item.MediaSources.map(function (m) {
m.SupportsDirectPlay = true;
m.SupportsDirectStream = false;
m.SupportsTranscoding = false;
return m;
});
return {
MediaSources: mediaSources
};
});
}
// "Override" methods
2017-01-24 12:54:18 -07:00
self.detectBitrate = function () {
return Promise.reject();
};
self.reportPlaybackStart = function (options) {
if (!options) {
throw new Error("null options");
}
return Promise.resolve();
};
self.reportPlaybackProgress = function (options) {
if (!options) {
throw new Error("null options");
}
return Promise.resolve();
};
self.reportPlaybackStopped = function (options) {
if (!options) {
throw new Error("null options");
}
return Promise.resolve();
};
self.getIntros = function (itemId) {
return Promise.resolve({
Items: [],
TotalRecordCount: 0
});
};
self.getUserViews = getUserViews;
self.getItems = getItems;
self.getItem = getItem;
2017-01-22 20:54:51 -07:00
self.getSeasons = getSeasons;
self.getEpisodes = getEpisodes;
self.getThemeMedia = getThemeMedia;
2017-01-22 20:54:51 -07:00
self.getNextUpEpisodes = getNextUpEpisodes;
self.getSimilarItems = getSimilarItems;
self.updateFavoriteStatus = updateFavoriteStatus;
self.getScaledImageUrl = getScaledImageUrl;
2017-01-24 12:54:18 -07:00
self.getPlaybackInfo = getPlaybackInfo;
2016-12-29 12:16:33 -07:00
};
});