Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] model: prevent UI plugins to refuse core commands #2120

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
GridRenderingContext,
isCoreCommand,
LAYERS,
LocalCommand,
UID,
} from "./types/index";
import { NotifyUIEvent } from "./types/ui";
Expand Down Expand Up @@ -267,7 +268,11 @@ export class Model extends EventBus<any> implements CommandDispatcher {
}

get handlers(): CommandHandler<Command>[] {
return [this.range, ...this.corePlugins, ...this.allUIPlugins, this.history];
return [...this.coreHandlers, ...this.allUIPlugins, this.history];
}

get coreHandlers(): CommandHandler<Command>[] {
return [this.range, ...this.corePlugins];
}

get allUIPlugins(): UIPlugin[] {
Expand Down Expand Up @@ -419,6 +424,18 @@ export class Model extends EventBus<any> implements CommandDispatcher {
* Check if the given command is allowed by all the plugins and the history.
*/
private checkDispatchAllowed(command: Command): DispatchResult {
if (isCoreCommand(command)) {
return this.checkDispatchAllowedCoreCommand(command);
}
return this.checkDispatchAllowedLocalCommand(command);
}

private checkDispatchAllowedCoreCommand(command: CoreCommand): DispatchResult {
const results = this.coreHandlers.map((handler) => handler.allowDispatch(command));
return new DispatchResult(results.flat());
}

private checkDispatchAllowedLocalCommand(command: LocalCommand): DispatchResult {
const results = this.handlers.map((handler) => handler.allowDispatch(command));
return new DispatchResult(results.flat());
}
Expand Down
5 changes: 2 additions & 3 deletions src/plugins/ui_core_views/filter_evaluation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
Border,
CellPosition,
CellValueType,
Command,
CommandResult,
ExcelFilterData,
ExcelWorkbookData,
Expand All @@ -24,7 +23,7 @@ import {
Zone,
} from "../../types";
import { UIPlugin } from "../ui_plugin";
import { UpdateFilterCommand } from "./../../types/commands";
import { Command, LocalCommand, UpdateFilterCommand } from "./../../types/commands";

export class FilterEvaluationPlugin extends UIPlugin {
static getters = [
Expand All @@ -41,7 +40,7 @@ export class FilterEvaluationPlugin extends UIPlugin {
hiddenRows: Set<number> = new Set();
isEvaluationDirty = false;

allowDispatch(cmd: Command) {
Copy link
Contributor Author

@hokolomopo hokolomopo Feb 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note : I changed the allowDispatch interface of the current local plugins, but I couldn't find a way to type UI plugins correctly to prevent them from handling core commands. Someone could create a UI plugin w/ allowDispatch(cmd : LocalCommand | CoreCommand) and ts wouldn't complain.

We can do shit like that in ts, w/o typing error:
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LucasLefevre any ideas?

Copy link
Collaborator

@LucasLefevre LucasLefevre Mar 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't prevent that.

Nothing prevents a subclass to have a more general API than its parent, as long as it implements the parent's interface.

class A {
  coucou(a: number) {
    return true
  }
}
class B extends A {
  coucou(n: number | string) {
    return true
  }
}

Let's say I have a: A; but a is actually an instance of B under the hood, but I don't know it. It's totally fine, I don't care that a can actually handle/do more stuff, I just use it as an A and will give it only numbers

However, if it doesn't have the same API than the parent, then it does work.

class B2 extends A {
  // error: Type 'number' is not assignable to type 'string'
  coucou(n: string) {
    return true
  }
}

Here since LocalCommand (parent API) is assignable to LocalCommand | CoreCommand (child class), everything is fine.

cc @anhe-odoo

allowDispatch(cmd: LocalCommand): CommandResult {
switch (cmd.type) {
case "UPDATE_FILTER":
if (!this.getters.getFilterId(cmd)) {
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/ui_core_views/sheetview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
EdgeScrollInfo,
Figure,
HeaderIndex,
LocalCommand,
Pixel,
Position,
Rect,
Expand Down Expand Up @@ -115,7 +116,7 @@ export class SheetViewPlugin extends UIPlugin {
// Command Handling
// ---------------------------------------------------------------------------

allowDispatch(cmd: Command) {
allowDispatch(cmd: LocalCommand): CommandResult | CommandResult[] {
switch (cmd.type) {
case "SET_VIEWPORT_OFFSET":
return this.checkScrollingDirection(cmd);
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/ui_feature/autofill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
GridRenderingContext,
HeaderIndex,
LAYERS,
LocalCommand,
Tooltip,
Zone,
} from "../../types/index";
Expand Down Expand Up @@ -94,7 +95,7 @@ export class AutofillPlugin extends UIPlugin {
// Command Handling
// ---------------------------------------------------------------------------

allowDispatch(cmd: Command): CommandResult {
allowDispatch(cmd: LocalCommand): CommandResult {
switch (cmd.type) {
case "AUTOFILL_SELECT":
const sheetId = this.getters.getActiveSheetId();
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/ui_feature/cell_popovers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { positionToZone } from "../../helpers";
import { cellPopoverRegistry } from "../../registries/cell_popovers_registry";
import { CellPosition, Command, CommandResult, Position, Rect } from "../../types";
import { CellPosition, Command, CommandResult, LocalCommand, Position, Rect } from "../../types";
import {
CellPopoverType,
ClosedCellPopover,
Expand All @@ -20,7 +20,7 @@ export class CellPopoverPlugin extends UIPlugin {

private persistentPopover?: CellPosition & { type: CellPopoverType };

allowDispatch(cmd: Command) {
allowDispatch(cmd: LocalCommand): CommandResult {
switch (cmd.type) {
case "OPEN_CELL_POPOVER":
try {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/ui_feature/selection_input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "../../helpers/index";
import { StreamCallbacks } from "../../selection_stream/event_stream";
import { SelectionEvent } from "../../types/event_stream";
import { Command, CommandResult, Highlight, LAYERS, UID } from "../../types/index";
import { Command, CommandResult, Highlight, LAYERS, LocalCommand, UID } from "../../types/index";
import { UIPlugin, UIPluginConfig } from "../ui_plugin";

export interface RangeInputValue {
Expand Down Expand Up @@ -50,7 +50,7 @@ export class SelectionInputPlugin extends UIPlugin implements StreamCallbacks<Se
// Command Handling
// ---------------------------------------------------------------------------

allowDispatch(cmd: Command): CommandResult {
allowDispatch(cmd: LocalCommand): CommandResult {
switch (cmd.type) {
case "ADD_EMPTY_RANGE":
if (this.inputHasSingleRange && this.ranges.length === 1) {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/ui_feature/selection_inputs_manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { positionToZone, rangeReference, splitReference } from "../../helpers/index";
import { Command, CommandResult, Highlight, LAYERS, UID } from "../../types/index";
import { Command, CommandResult, Highlight, LAYERS, LocalCommand, UID } from "../../types/index";
import { UIPlugin, UIPluginConfig } from "../ui_plugin";
import { RangeInputValue, SelectionInputPlugin } from "./selection_input";

Expand Down Expand Up @@ -33,7 +33,7 @@ export class SelectionInputsManagerPlugin extends UIPlugin {
// Command Handling
// ---------------------------------------------------------------------------

allowDispatch(cmd: Command): CommandResult {
allowDispatch(cmd: LocalCommand): CommandResult {
switch (cmd.type) {
case "FOCUS_RANGE":
const index = this.currentInput?.getIndex(cmd.rangeId);
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/ui_feature/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CommandResult,
EvaluatedCell,
HeaderIndex,
LocalCommand,
Position,
SortCommand,
SortDirection,
Expand All @@ -21,7 +22,7 @@ import { UIPlugin } from "../ui_plugin";
export class SortPlugin extends UIPlugin {
static getters = ["getContiguousZone"] as const;

allowDispatch(cmd: Command) {
allowDispatch(cmd: LocalCommand): CommandResult | CommandResult[] {
switch (cmd.type) {
case "SORT_CELLS":
if (!isInside(cmd.col, cmd.row, cmd.zone)) {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/ui_feature/ui_sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
PADDING_AUTORESIZE_HORIZONTAL,
} from "../../constants";
import { computeIconWidth, computeTextWidth, positions } from "../../helpers/index";
import { Command, CommandResult, UID } from "../../types";
import { Command, CommandResult, LocalCommand, UID } from "../../types";
import {
CellPosition,
Dimension,
Expand Down Expand Up @@ -32,7 +32,7 @@ export class SheetUIPlugin extends UIPlugin {
// Command Handling
// ---------------------------------------------------------------------------

allowDispatch(cmd: Command): CommandResult {
allowDispatch(cmd: LocalCommand): CommandResult {
switch (cmd.type) {
case "AUTORESIZE_ROWS":
case "AUTORESIZE_COLUMNS":
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/ui_stateful/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
GridRenderingContext,
isCoreCommand,
LAYERS,
LocalCommand,
Zone,
} from "../../types/index";
import { UIPlugin } from "../ui_plugin";
Expand Down Expand Up @@ -47,7 +48,7 @@ export class ClipboardPlugin extends UIPlugin {
// Command Handling
// ---------------------------------------------------------------------------

allowDispatch(cmd: Command): CommandResult {
allowDispatch(cmd: LocalCommand): CommandResult {
switch (cmd.type) {
case "CUT":
const zones = cmd.target || this.getters.getSelectedZones();
Expand Down
15 changes: 7 additions & 8 deletions src/plugins/ui_stateful/edition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@ import {
import { loopThroughReferenceType } from "../../helpers/reference_type";
import { _lt } from "../../translation";
import {
AddColumnsRowsCommand,
CellPosition,
CellValueType,
Command,
CommandResult,
Format,
HeaderIndex,
Highlight,
LocalCommand,
Range,
RangePart,
RemoveColumnsRowsCommand,
UID,
Zone,
} from "../../types";
import { SelectionEvent } from "../../types/event_stream";
import {
AddColumnsRowsCommand,
Command,
CommandResult,
HeaderIndex,
RemoveColumnsRowsCommand,
} from "../../types/index";
import { UIPlugin } from "../ui_plugin";

type EditionMode =
Expand Down Expand Up @@ -81,7 +80,7 @@ export class EditionPlugin extends UIPlugin {
// Command Handling
// ---------------------------------------------------------------------------

allowDispatch(cmd: Command): CommandResult {
allowDispatch(cmd: LocalCommand): CommandResult {
switch (cmd.type) {
case "CHANGE_COMPOSER_CURSOR_SELECTION":
return this.validateSelection(this.currentContent.length, cmd.start, cmd.end);
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/ui_stateful/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
GridRenderingContext,
HeaderIndex,
LAYERS,
LocalCommand,
MoveColumnsRowsCommand,
RemoveColumnsRowsCommand,
Selection,
Expand Down Expand Up @@ -132,7 +133,7 @@ export class GridSelectionPlugin extends UIPlugin {
// Command Handling
// ---------------------------------------------------------------------------

allowDispatch(cmd: Command): CommandResult {
allowDispatch(cmd: LocalCommand): CommandResult {
switch (cmd.type) {
case "ACTIVATE_SHEET":
try {
Expand Down
24 changes: 22 additions & 2 deletions tests/collaborative/collaborative.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Model } from "../../src";
import { Model, UIPlugin } from "../../src";
import { DEFAULT_REVISION_ID, MESSAGE_VERSION } from "../../src/constants";
import { args, functionRegistry } from "../../src/functions";
import { getDefaultCellHeight, range, toCartesian, toZone } from "../../src/helpers";
import { CommandResult, CoreCommand } from "../../src/types";
import { featurePluginRegistry } from "../../src/plugins";
import { Command, CommandResult, CoreCommand } from "../../src/types";
import { CollaborationMessage } from "../../src/types/collaborative/transport_service";
import {
activateSheet,
Expand Down Expand Up @@ -1010,3 +1011,22 @@ describe("Multi users synchronisation", () => {
);
});
});

test("UI plugins cannot refuse core command and de-synchronize the users", () => {
class MyUIPlugin extends UIPlugin {
allowDispatch(cmd: Command) {
if (cmd.type === "UPDATE_CELL") {
return this.getters.getClient().name === "Alice"
? CommandResult.Success
: CommandResult.CancelledForUnknownReason;
}
return CommandResult.Success;
}
}
featurePluginRegistry.add("myUIPlugin", MyUIPlugin);
const { alice, bob } = setupCollaborativeEnv();

setCellContent(alice, "A1", "hello");
expect([alice, bob]).toHaveSynchronizedValue((user) => getCellContent(user, "A1"), "hello");
featurePluginRegistry.remove("myUIPlugin");
});
17 changes: 17 additions & 0 deletions tests/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ describe("Model", () => {
featurePluginRegistry.remove("myUIPlugin");
});

test("UI plugins cannot refuse core commands", () => {
class MyUIPlugin extends UIPlugin {
allowDispatch(cmd: Command) {
if (cmd.type === "UPDATE_CELL") {
return CommandResult.CancelledForUnknownReason;
}
return CommandResult.Success;
}
}
featurePluginRegistry.add("myUIPlugin", MyUIPlugin);
const model = new Model();

setCellContent(model, "A1", "hello");
expect(getCellContent(model, "A1")).toBe("hello");
featurePluginRegistry.remove("myUIPlugin");
});

test("Can open a model in readonly mode", () => {
const model = new Model({}, { mode: "readonly" });
expect(model.getters.isReadonly()).toBe(true);
Expand Down