1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/components/syncPlay/groupSelectionMenu.js

189 lines
6.1 KiB
JavaScript
Raw Normal View History

2020-09-08 02:05:02 -04:00
import { ConnectionManager, Events } from 'jellyfin-apiclient';
2020-08-16 20:24:45 +02:00
import { playbackManager } from '../playback/playbackmanager';
2020-08-14 08:46:34 +02:00
import syncPlayManager from './syncPlayManager';
import loading from '../loading/loading';
import toast from '../toast/toast';
import actionsheet from '../actionSheet/actionSheet';
import globalize from '../../scripts/globalize';
import playbackPermissionManager from './playbackPermissionManager';
2020-04-03 18:49:19 +02:00
/**
* Gets active player id.
* @returns {string} The player's id.
*/
function getActivePlayerId () {
2020-10-07 21:12:14 +09:00
const info = playbackManager.getPlayerInfo();
2020-04-03 18:49:19 +02:00
return info ? info.id : null;
}
/**
* Used when user needs to join a group.
* @param {HTMLElement} button - Element where to place the menu.
* @param {Object} user - Current user.
* @param {Object} apiClient - ApiClient.
2020-04-03 18:49:19 +02:00
*/
function showNewJoinGroupSelection (button, user, apiClient) {
2020-05-05 12:01:43 +02:00
const sessionId = getActivePlayerId() || 'none';
const inSession = sessionId !== 'none';
const policy = user.localUser ? user.localUser.Policy : {};
let playingItemId;
try {
const playState = playbackManager.getPlayerState();
playingItemId = playState.NowPlayingItem.Id;
2020-05-05 12:01:43 +02:00
console.debug('Item', playingItemId, 'is currently playing.');
} catch (error) {
2020-05-05 12:01:43 +02:00
playingItemId = '';
console.debug('No item is currently playing.');
}
2020-04-03 18:49:19 +02:00
2020-07-07 18:20:53 +02:00
apiClient.getSyncPlayGroups().then(function (response) {
2020-05-05 12:01:43 +02:00
response.json().then(function (groups) {
2020-10-07 21:12:14 +09:00
const menuItems = groups.map(function (group) {
2020-04-03 18:49:19 +02:00
return {
2020-05-05 12:01:43 +02:00
name: group.PlayingItemName,
icon: 'group',
2020-04-03 18:49:19 +02:00
id: group.GroupId,
selected: false,
2020-05-05 12:01:43 +02:00
secondaryText: group.Participants.join(', ')
2020-04-03 18:49:19 +02:00
};
2020-04-01 17:53:14 +02:00
});
2020-05-06 23:41:54 +02:00
if (inSession && policy.SyncPlayAccess === 'CreateAndJoinGroups') {
2020-04-03 18:49:19 +02:00
menuItems.push({
2020-05-06 23:41:54 +02:00
name: globalize.translate('LabelSyncPlayNewGroup'),
2020-05-05 12:01:43 +02:00
icon: 'add',
id: 'new-group',
2020-04-03 18:49:19 +02:00
selected: true,
2020-05-06 23:41:54 +02:00
secondaryText: globalize.translate('LabelSyncPlayNewGroupDescription')
2020-04-03 18:49:19 +02:00
});
}
2020-04-01 17:53:14 +02:00
2020-04-03 18:49:19 +02:00
if (menuItems.length === 0) {
2020-05-06 23:41:54 +02:00
if (inSession && policy.SyncPlayAccess === 'JoinGroups') {
toast({
2020-05-06 23:41:54 +02:00
text: globalize.translate('MessageSyncPlayCreateGroupDenied')
});
} else {
2020-04-03 18:49:19 +02:00
toast({
2020-05-06 23:41:54 +02:00
text: globalize.translate('MessageSyncPlayNoGroupsAvailable')
2020-04-03 18:49:19 +02:00
});
}
2020-04-03 18:49:19 +02:00
loading.hide();
return;
}
2020-04-01 17:53:14 +02:00
2020-10-07 21:12:14 +09:00
const menuOptions = {
2020-05-06 23:41:54 +02:00
title: globalize.translate('HeaderSyncPlaySelectGroup'),
2020-04-01 17:53:14 +02:00
items: menuItems,
positionTo: button,
resolveOnClick: true,
border: true
};
actionsheet.show(menuOptions).then(function (id) {
2020-05-05 12:01:43 +02:00
if (id == 'new-group') {
2020-07-07 18:20:53 +02:00
apiClient.createSyncPlayGroup();
2020-07-15 06:23:36 +09:00
} else if (id) {
2020-07-07 18:20:53 +02:00
apiClient.joinSyncPlayGroup({
GroupId: id,
PlayingItemId: playingItemId
2020-04-03 18:49:19 +02:00
});
2020-04-01 17:53:14 +02:00
}
2020-05-05 12:01:43 +02:00
}).catch((error) => {
2020-05-06 23:41:54 +02:00
console.error('SyncPlay: unexpected error listing groups:', error);
2020-05-05 12:01:43 +02:00
});
2020-04-03 18:49:19 +02:00
loading.hide();
2020-04-01 17:53:14 +02:00
});
2020-04-03 18:49:19 +02:00
}).catch(function (error) {
console.error(error);
loading.hide();
toast({
2020-05-06 23:41:54 +02:00
text: globalize.translate('MessageSyncPlayErrorAccessingGroups')
});
2020-04-03 18:49:19 +02:00
});
}
/**
* Used when user has joined a group.
* @param {HTMLElement} button - Element where to place the menu.
* @param {Object} user - Current user.
* @param {Object} apiClient - ApiClient.
2020-04-03 18:49:19 +02:00
*/
function showLeaveGroupSelection (button, user, apiClient) {
2020-04-03 18:49:19 +02:00
const sessionId = getActivePlayerId();
if (!sessionId) {
2020-05-06 23:41:54 +02:00
syncPlayManager.signalError();
toast({
2020-05-06 23:41:54 +02:00
text: globalize.translate('MessageSyncPlayErrorNoActivePlayer')
});
2020-05-05 12:01:43 +02:00
showNewJoinGroupSelection(button, user, apiClient);
return;
}
2020-04-03 18:49:19 +02:00
const menuItems = [{
2020-05-06 23:41:54 +02:00
name: globalize.translate('LabelSyncPlayLeaveGroup'),
2020-05-05 12:01:43 +02:00
icon: 'meeting_room',
id: 'leave-group',
2020-04-03 18:49:19 +02:00
selected: true,
2020-05-06 23:41:54 +02:00
secondaryText: globalize.translate('LabelSyncPlayLeaveGroupDescription')
2020-04-03 18:49:19 +02:00
}];
2020-10-07 21:12:14 +09:00
const menuOptions = {
2020-05-06 23:41:54 +02:00
title: globalize.translate('HeaderSyncPlayEnabled'),
2020-04-03 18:49:19 +02:00
items: menuItems,
positionTo: button,
resolveOnClick: true,
border: true
};
2020-04-01 17:53:14 +02:00
2020-04-03 18:49:19 +02:00
actionsheet.show(menuOptions).then(function (id) {
2020-05-05 12:01:43 +02:00
if (id == 'leave-group') {
2020-07-07 18:20:53 +02:00
apiClient.leaveSyncPlayGroup();
2020-04-01 17:53:14 +02:00
}
2020-05-05 12:01:43 +02:00
}).catch((error) => {
2020-05-06 23:41:54 +02:00
console.error('SyncPlay: unexpected error showing group menu:', error);
2020-05-05 12:01:43 +02:00
});
2020-04-01 17:53:14 +02:00
2020-04-03 18:49:19 +02:00
loading.hide();
}
2020-04-01 17:53:14 +02:00
2020-05-06 23:41:54 +02:00
// Register to SyncPlay events
let syncPlayEnabled = false;
2020-09-08 02:05:02 -04:00
Events.on(syncPlayManager, 'enabled', function (e, enabled) {
2020-05-06 23:41:54 +02:00
syncPlayEnabled = enabled;
2020-04-01 17:53:14 +02:00
});
2020-04-03 18:49:19 +02:00
/**
2020-05-06 23:41:54 +02:00
* Shows a menu to handle SyncPlay groups.
2020-04-03 18:49:19 +02:00
* @param {HTMLElement} button - Element where to place the menu.
*/
2020-08-14 08:46:34 +02:00
export default function show (button) {
loading.show();
2020-04-15 18:09:34 +02:00
// TODO: should feature be disabled if playback permission is missing?
playbackPermissionManager.check().then(() => {
2020-05-05 12:01:43 +02:00
console.debug('Playback is allowed.');
2020-04-15 18:09:34 +02:00
}).catch((error) => {
2020-05-05 12:01:43 +02:00
console.error('Playback not allowed!', error);
2020-04-15 18:09:34 +02:00
toast({
2020-05-06 23:41:54 +02:00
text: globalize.translate('MessageSyncPlayPlaybackPermissionRequired')
2020-04-15 18:09:34 +02:00
});
});
2020-09-08 13:35:17 -04:00
const apiClient = window.ConnectionManager.currentApiClient();
2020-08-16 20:24:45 +02:00
ConnectionManager.user(apiClient).then((user) => {
2020-05-06 23:41:54 +02:00
if (syncPlayEnabled) {
showLeaveGroupSelection(button, user, apiClient);
} else {
showNewJoinGroupSelection(button, user, apiClient);
}
}).catch((error) => {
console.error(error);
loading.hide();
toast({
2020-05-06 23:41:54 +02:00
text: globalize.translate('MessageSyncPlayNoGroupsAvailable')
});
});
2020-04-03 18:49:19 +02:00
}