gristlabs_grist-core/app/server/lib/OnDemandActions.ts
Paul Fitzpatrick 603238e966 (core) Adds a UI panel for managing webhooks
Summary:
This adds a UI panel for managing webhooks. Work started by Cyprien Pindat. You can find the UI on a document's settings page. Main changes relative to Cyprien's demo:

  * Changed behavior of virtual table to be more consistent with the rest of Grist, by factoring out part of the implementation of on-demand tables.
  * Cell values that would create an error can now be denied and reverted (as for the rest of Grist).
  * Changes made by other users are integrated in a sane way.
  * Basic undo/redo support is added using the regular undo/redo stack.
  * The table list in the drop-down is now updated if schema changes.
  * Added a notification from back-end when webhook status is updated so constant polling isn't needed to support multi-user operation.
  *  Factored out webhook specific logic from general virtual table support.
  * Made a bunch of fixes to various broken behavior.
  * Added tests.

The code remains somewhat unpolished, and behavior in the presence of errors is imperfect in general but may be adequate for this case.

I assume that we'll soon be lifting the restriction on the set of domains that are supported for webhooks - otherwise we'd want to provide some friendly way to discover that list of supported domains rather than just throwing an error.

I don't actually know a lot about how the front-end works - it looks like tables/columns/fields/sections can be safely added if they have string ids that won't collide with bone fide numeric ids from the back end. Sneaky.

Contains a migration, so needs an extra reviewer for that.

Test Plan: added tests

Reviewers: jarek, dsagal

Reviewed By: jarek, dsagal

Differential Revision: https://phab.getgrist.com/D3856
2023-05-08 18:25:27 -04:00

49 lines
1.8 KiB
TypeScript

import {AlternateActions, AlternateStorage} from 'app/common/AlternateActions';
import {DocData} from 'app/common/DocData';
import {TableData} from 'app/common/TableData';
import {IndexColumns} from 'app/server/lib/DocStorage';
export type {ProcessedAction} from 'app/common/AlternateActions';
export type OnDemandStorage = AlternateStorage;
/**
* Handle converting UserActions to DocActions for onDemand tables.
*/
export class OnDemandActions extends AlternateActions {
private _tablesMeta: TableData = this._docData.getMetaTable('_grist_Tables');
private _columnsMeta: TableData = this._docData.getMetaTable('_grist_Tables_column');
constructor(_storage: OnDemandStorage, private _docData: DocData,
private _forceOnDemand: boolean = false) {
super(_storage);
}
// TODO: Ideally a faster data structure like an index by tableId would be used to decide whether
// the table is onDemand.
public isOnDemand(tableId: string): boolean {
if (this._forceOnDemand) { return true; }
const tableRef = this._tablesMeta.findRow('tableId', tableId);
// OnDemand tables must have a record in the _grist_Tables metadata table.
return tableRef ? Boolean(this._tablesMeta.getValue(tableRef, 'onDemand')) : false;
}
public usesAlternateStorage(tableId: string): boolean {
return this.isOnDemand(tableId);
}
/**
* Compute the indexes we would like to have, given the current schema.
*/
public getDesiredIndexes(): IndexColumns[] {
const desiredIndexes: IndexColumns[] = [];
for (const c of this._columnsMeta.getRecords()) {
const t = this._tablesMeta.getRecord(c.parentId as number);
if (t && t.onDemand && c.type && (c.type as string).startsWith('Ref:')) {
desiredIndexes.push({tableId: t.tableId as string, colId: c.colId as string});
}
}
return desiredIndexes;
}
}