2024-05-17 13:52:42 -04:00
|
|
|
import { MINIMUM_VERSION } from '@jellyfin/sdk/lib/versions';
|
2022-10-14 10:53:16 -04:00
|
|
|
import { ConnectionManager, Credentials, ApiClient } from 'jellyfin-apiclient';
|
2022-04-21 13:43:12 -04:00
|
|
|
|
2020-10-17 19:08:56 +01:00
|
|
|
import { appHost } from './apphost';
|
2022-04-10 02:22:13 -04:00
|
|
|
import Dashboard from '../utils/dashboard';
|
2022-10-14 10:53:16 -04:00
|
|
|
import Events from '../utils/events.ts';
|
2020-10-17 19:08:56 +01:00
|
|
|
import { setUserInfo } from '../scripts/settings/userSettings';
|
2022-04-21 13:43:12 -04:00
|
|
|
import appSettings from '../scripts/settings/appSettings';
|
2024-06-01 18:42:05 -04:00
|
|
|
import viewContainer from './viewContainer';
|
2024-05-25 11:50:52 -04:00
|
|
|
import { queryClient } from 'utils/query/queryClient';
|
2022-04-21 13:43:12 -04:00
|
|
|
|
|
|
|
const normalizeImageOptions = options => {
|
|
|
|
if (!options.quality && (options.maxWidth || options.width || options.maxHeight || options.height || options.fillWidth || options.fillHeight)) {
|
|
|
|
options.quality = 90;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getMaxBandwidth = () => {
|
|
|
|
/* eslint-disable compat/compat */
|
|
|
|
if (navigator.connection) {
|
|
|
|
let max = navigator.connection.downlinkMax;
|
|
|
|
if (max && max > 0 && max < Number.POSITIVE_INFINITY) {
|
|
|
|
max /= 8;
|
|
|
|
max *= 1000000;
|
|
|
|
max *= 0.7;
|
|
|
|
return parseInt(max, 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* eslint-enable compat/compat */
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
2020-10-17 19:08:56 +01:00
|
|
|
|
|
|
|
class ServerConnections extends ConnectionManager {
|
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
this.localApiClient = null;
|
|
|
|
|
2024-05-17 13:52:42 -04:00
|
|
|
// Set the apiclient minimum version to match the SDK
|
|
|
|
this._minServerVersion = MINIMUM_VERSION;
|
|
|
|
|
2022-04-21 13:43:12 -04:00
|
|
|
Events.on(this, 'localusersignedout', (_e, logoutInfo) => {
|
2020-10-17 19:08:56 +01:00
|
|
|
setUserInfo(null, null);
|
2024-05-17 13:52:41 -04:00
|
|
|
// Ensure the updated credentials are persisted to storage
|
|
|
|
credentialProvider.credentials(credentialProvider.credentials());
|
2024-05-25 11:50:52 -04:00
|
|
|
// Reset the query cache
|
|
|
|
queryClient.resetQueries();
|
2024-06-01 18:42:05 -04:00
|
|
|
// Reset cached views
|
|
|
|
viewContainer.reset();
|
2021-04-11 13:58:48 +03:00
|
|
|
|
|
|
|
if (window.NativeShell && typeof window.NativeShell.onLocalUserSignedOut === 'function') {
|
|
|
|
window.NativeShell.onLocalUserSignedOut(logoutInfo);
|
|
|
|
}
|
2020-10-17 19:08:56 +01:00
|
|
|
});
|
2022-04-21 13:43:12 -04:00
|
|
|
|
|
|
|
Events.on(this, 'apiclientcreated', (_e, apiClient) => {
|
|
|
|
apiClient.getMaxBandwidth = getMaxBandwidth;
|
|
|
|
apiClient.normalizeImageOptions = normalizeImageOptions;
|
|
|
|
});
|
2020-10-17 19:08:56 +01:00
|
|
|
}
|
|
|
|
|
2020-11-21 21:03:18 +03:00
|
|
|
initApiClient(server) {
|
|
|
|
console.debug('creating ApiClient singleton');
|
2020-10-17 19:08:56 +01:00
|
|
|
|
2020-11-21 21:03:18 +03:00
|
|
|
const apiClient = new ApiClient(
|
|
|
|
server,
|
|
|
|
appHost.appName(),
|
|
|
|
appHost.appVersion(),
|
|
|
|
appHost.deviceName(),
|
|
|
|
appHost.deviceId()
|
|
|
|
);
|
2020-10-17 19:08:56 +01:00
|
|
|
|
2020-11-21 21:03:18 +03:00
|
|
|
apiClient.enableAutomaticNetworking = false;
|
2024-05-25 11:50:40 -04:00
|
|
|
apiClient.manualAddressOnly = true;
|
2020-10-17 19:08:56 +01:00
|
|
|
|
2020-11-21 21:03:18 +03:00
|
|
|
this.addApiClient(apiClient);
|
2020-10-17 19:08:56 +01:00
|
|
|
|
2020-11-21 21:03:18 +03:00
|
|
|
this.setLocalApiClient(apiClient);
|
2020-10-17 19:08:56 +01:00
|
|
|
|
2020-11-21 21:03:18 +03:00
|
|
|
console.debug('loaded ApiClient singleton');
|
2020-10-17 19:08:56 +01:00
|
|
|
}
|
|
|
|
|
2022-04-21 13:43:12 -04:00
|
|
|
connect(options) {
|
|
|
|
return super.connect({
|
|
|
|
enableAutoLogin: appSettings.enableAutoLogin(),
|
2022-04-21 13:58:39 -04:00
|
|
|
...options
|
2022-04-21 13:43:12 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-17 19:08:56 +01:00
|
|
|
setLocalApiClient(apiClient) {
|
|
|
|
if (apiClient) {
|
|
|
|
this.localApiClient = apiClient;
|
|
|
|
window.ApiClient = apiClient;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getLocalApiClient() {
|
|
|
|
return this.localApiClient;
|
|
|
|
}
|
|
|
|
|
2022-10-28 12:53:31 -04:00
|
|
|
/**
|
|
|
|
* Gets the ApiClient that is currently connected.
|
2022-12-08 14:32:50 -05:00
|
|
|
* @returns {ApiClient|undefined} apiClient
|
2022-10-28 12:53:31 -04:00
|
|
|
*/
|
2020-10-17 19:08:56 +01:00
|
|
|
currentApiClient() {
|
|
|
|
let apiClient = this.getLocalApiClient();
|
|
|
|
|
|
|
|
if (!apiClient) {
|
|
|
|
const server = this.getLastUsedServer();
|
|
|
|
|
|
|
|
if (server) {
|
|
|
|
apiClient = this.getApiClient(server.Id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiClient;
|
|
|
|
}
|
|
|
|
|
2024-03-23 03:46:05 -04:00
|
|
|
/**
|
|
|
|
* Gets the ApiClient that is currently connected or throws if not defined.
|
|
|
|
* @async
|
|
|
|
* @returns {Promise<ApiClient>} The current ApiClient instance.
|
|
|
|
*/
|
|
|
|
async getCurrentApiClientAsync() {
|
|
|
|
const apiClient = this.currentApiClient();
|
|
|
|
if (!apiClient) throw new Error('[ServerConnection] No current ApiClient instance');
|
|
|
|
|
|
|
|
return apiClient;
|
|
|
|
}
|
|
|
|
|
2020-10-17 19:08:56 +01:00
|
|
|
onLocalUserSignedIn(user) {
|
|
|
|
const apiClient = this.getApiClient(user.ServerId);
|
|
|
|
this.setLocalApiClient(apiClient);
|
2021-04-11 13:58:48 +03:00
|
|
|
return setUserInfo(user.Id, apiClient).then(() => {
|
|
|
|
if (window.NativeShell && typeof window.NativeShell.onLocalUserSignedIn === 'function') {
|
|
|
|
return window.NativeShell.onLocalUserSignedIn(user, apiClient.accessToken());
|
|
|
|
}
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
2020-10-17 19:08:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-17 13:52:41 -04:00
|
|
|
const credentialProvider = new Credentials();
|
2020-10-17 19:08:56 +01:00
|
|
|
|
|
|
|
const capabilities = Dashboard.capabilities(appHost);
|
|
|
|
|
|
|
|
export default new ServerConnections(
|
2024-05-17 13:52:41 -04:00
|
|
|
credentialProvider,
|
2020-10-17 19:08:56 +01:00
|
|
|
appHost.appName(),
|
|
|
|
appHost.appVersion(),
|
|
|
|
appHost.deviceName(),
|
|
|
|
appHost.deviceId(),
|
|
|
|
capabilities);
|