2025-07-01 14:47:03 -07:00
|
|
|
import { CommandRuntime, CommandDeclaration, CommandContext } from '../services/CommandService';
|
|
|
|
|
import { _ } from '../locale';
|
|
|
|
|
import ShareService from '../services/share/ShareService';
|
2023-07-27 16:05:56 +01:00
|
|
|
import Logger from '@joplin/utils/Logger';
|
2025-07-01 14:47:03 -07:00
|
|
|
import shim from '../shim';
|
2021-11-21 14:35:01 +00:00
|
|
|
|
|
|
|
|
const logger = Logger.create('leaveSharedFolder');
|
2021-10-13 18:02:54 +01:00
|
|
|
|
|
|
|
|
export const declaration: CommandDeclaration = {
|
|
|
|
|
name: 'leaveSharedFolder',
|
|
|
|
|
label: () => _('Leave notebook...'),
|
|
|
|
|
};
|
|
|
|
|
|
2025-07-01 14:47:03 -07:00
|
|
|
interface Options {
|
|
|
|
|
force?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 18:02:54 +01:00
|
|
|
export const runtime = (): CommandRuntime => {
|
|
|
|
|
return {
|
2025-07-01 14:47:03 -07:00
|
|
|
execute: async (_context: CommandContext, folderId: string = null, { force = false }: Options = {}) => {
|
|
|
|
|
const answer = force ? true : await shim.showConfirmationDialog(
|
|
|
|
|
_('This will remove the notebook from your collection and you will no longer have access to its content. Do you wish to continue?'),
|
|
|
|
|
);
|
2021-10-13 18:02:54 +01:00
|
|
|
if (!answer) return;
|
2021-11-21 14:35:01 +00:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Since we are going to delete the notebook, do some extra safety checks. In particular:
|
|
|
|
|
// - Check that the notebook is indeed being shared.
|
|
|
|
|
// - Check that it does **not** belong to the current user.
|
|
|
|
|
|
|
|
|
|
const shares = await ShareService.instance().refreshShares();
|
|
|
|
|
const share = shares.find(s => s.folder_id === folderId);
|
|
|
|
|
if (!share) throw new Error(_('Could not verify the share status of this notebook - aborting. Please try again when you are connected to the internet.'));
|
|
|
|
|
|
2021-12-20 15:47:50 +01:00
|
|
|
await ShareService.instance().leaveSharedFolder(folderId, share.user.id);
|
2021-11-21 14:35:01 +00:00
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(error);
|
2025-01-08 04:30:16 -08:00
|
|
|
await shim.showErrorDialog(_('Error: %s', error.message));
|
2021-11-21 14:35:01 +00:00
|
|
|
}
|
2021-10-13 18:02:54 +01:00
|
|
|
},
|
|
|
|
|
enabledCondition: 'joplinServerConnected && folderIsShareRootAndNotOwnedByUser',
|
|
|
|
|
};
|
|
|
|
|
};
|