Skip to content

File actions with command pattern #26296

Closed

Description

@PVince81

In order to fulfil the following requirements:

We need to move the file actions to a more abstract level, decoupled from the file lists.

The idea is to use the command pattern.

Defining a file action, based on the existing FileActions, for example favoriting a file:

    fileActions.registerAction({
        name: 'Favorite',
        displayName: t('files', 'Favorite'),
        mime: 'all',
        permissions: OC.PERMISSION_READ,
        type: OCA.Files.FileActions.TYPE_INLINE,
        render: function(actionSpec, isDefault, context) { ... },
        actionHandler: function(fileName, context) {
            // this is new: return a FileActionCommand instance
            return new FavoriteFileActionCommand(context);
        });

Very crude example, most of the code should be moved to a FileActionCommand base class:

    FavoriteFileActionCommand = function(context) { ... };
    FavoriteFileActionCommand.prototype = {
        context: context,
        // promise
        state: null,
        // previous state, for undo
        _previousFavoriteState: null,
        // performs the action, returns a promise
        execute: function() {
            if (this._hasRun) {
                throw "Command already ran";
            }
            this._hasRun = false;
            // code to execute the action based on the context which contains arguments
            return this.state = $.ajax({
                ...
                data: {favorite: !context.isFavorite},
                ...
            });
        },
        revert: function() {
            if (this._hasRun) {
                throw "Cannot revert action that was not run yet";
            }
            // executes the opposite
            this._hasRun = false;
            return this.state = $.ajax({
                ...
                data: {favorite: this._previousFavoriteState},
                ...
            });
        }
    }

Because of the use of promises, it would also be possible to defer execution to later and show an undo notification first, and run the action a few seconds later. This logic could potentially be added into the base class as well FileActionCommand.

  • Whenever a piece of code from the files app or a files app plugin (ex: sidebar tab) wants to perform a file action:
    context = {
        fileName: '/somedir/somefile.txt'
    };
    var action = this.fileActions.getFileActionCommand(context);
    // add to the action runner
    FileActionsCommandRunner.add(action);
  • The file action command runner would be a globally accessible (or restricted scope ?) class in which the actions to execute must be added. They will then be executed in order.
  • In case of bulk actions, for example when deleting multiple files at once, a composite action FileActionCompositeCommand is created with a list of individual FileActionCommand commands to execute in bulk. This means that undoing a composite command would undo all of the sub-commands.
  • If a single action failed, for example moving a single file within a bulk command failed because of file name conflict, we need a mechanism to pop up the conflict dialog in a way that will make it reset and rerun the command with a resolution parameter (or modified context like target file name).

Open questions:

  • do we really need that level of complexity with "revert()" or would an undo notification + delayed action would be enough ? delayed action would remove the need for composite commands
  • how to deal with "move + conflict dialog", should it be implemented inside the action or should the command runner be able to trigger such dialog.

Activity

  1. added this to the backlog milestone on Oct 7, 2016
  2. DeepDiver1975 commented on Oct 7, 2016

    @DeepDiver1975
    Contributor

    do we really need that level of complexity with "revert()" or would an undo notification + delayed action would be enough ? delayed action would remove the need for composite commands

    from my understanding notification and delay should be enough

  3. DeepDiver1975 commented on Oct 7, 2016

    @DeepDiver1975
    Contributor

    how to deal with "move + conflict dialog", should it be implemented inside the action or should the command runner be able to trigger such dialog.

    I'd do that within the action - or would that be tooo complex?

  4. PVince81 commented on Oct 7, 2016

    @PVince81
    ContributorAuthor

    I'd do that within the action - or would that be tooo complex?

    Within the action is probably the simplest way, especially considering that the action itself knows better in which way it needs to resolve itself (like rename target file) after conflict resolution choice. So far can't think of any better way.

  5. ownclouders commented on Dec 21, 2017

    @ownclouders
    Contributor

    Hey, this issue has been closed because the label status/STALE is set and there were no updates for 7 days. Feel free to reopen this issue if you deem it appropriate.

    (This is an automated comment from GitMate.io.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions