Files
baserow/web-frontend/modules/builder/realtime.js
Tsering PaljorandJeremie Pardou 250748c8dc feat: Workspace level integrations (#6547)
* Add ADR-013 for workspace level integrations

* Add implementation

* Improve context section and remove technical points

* Move bulk replacement point to v2

* Remove workspace-level ownership on import since manual promotion should handle this.

* Add a glossary section

* Do Lazy creation + Celery migration

* Remove demotion

* Tighten the Options considered section

* Add Decision describing where the integrations are managed

* Update implementation plan after changes to ADR

* Sync plan and adr

* Rename adr and implementation

* Add the shared application that owns workspace integrations

* Create the shared application of every workspace

* Let only workspace admins manage workspace integrations

* Let applications use the integrations of their workspace.

* Trash workspace integrations in their workspace.

* Copy workspace integrations into exports and keep them in duplicates.

* Workspace admins can share integrations with every application in the workspace.

* Load the shared application of each workspace in the frontend and keep it out of application lists.

* Manage workspace integrations from an Integrations page in workspace settings.

* Offer workspace integrations in the integration dropdowns of every application.

* Move promoted integrations to the workspace list in other open tabs.

* Let admins make an app's integration available to the whole workspace from its integration settings.

* Preselect a workspace integration for new nodes and data sources when the app has none of its own.

* fix tests

* Delete implementation file

* More fixes after rebase

* Fix tests

* Let builders of an application see and use the workspace integrations from it.

* Show the workspace integrations, read-only, in the integration settings of applications.

* Keep a dashboard's integration out of promotion to the workspace.

* Leave the shared applications out of the admin dashboard's application count.

* Create the shared application of the workspaces made by the fill commands.

* Say in the promotion modal that an integration nobody authorized will run as the promoting admin.

* Say in the promotion modal that undoing it won't make the integration run as the previous person again.

* Ignore integration ids in sample data when publishing copies the workspace integrations an app uses.

* Let only undoing a promotion move an integration out of the workspace.

* Let the shared application backfill stop itself after 5 minutes and skip workspaces another transaction has locked.

* Say in the workspace integrations settings that published applications and workflows keep their own copy until they're published again.

* Leave the shared application out of the assistant evals' application counts.

* Refresh the workspace integrations when the Builder's data, events, users and integrations panels open.

* Say that anything new using a promoted integration blocks undoing the promotion, even from its own application.

* Warn when a database button sends through a workspace integration with no host or token.

* Refuse roles and invitations on the shared application.

* Fix tests

* Show an application only the workspace integrations of types it can use.

* Say in the promotion modal what builders of other applications can do as the integration.

* Leave the shared application type out of the API docs for creating and updating applications.

* Check who receives a workspace integration event in one permission call.

* Say which workspaces have a shared application and who creates it.

* Let a redone promotion record what uses the integration again.

* Update docstrings

* Stop template visitors from listing the integrations of the template's workspace.

* Let database buttons find their own copy of a workspace integration after an import.

* Say what holds the workspace row lock while a shared application is created.

* Fix tests

* Stop the workspace integration form component from being made reactive.

* Make test runs use a new copy of the workflow after an integration it uses changes.

* Hide the Members settings tab from members who can't list the workspace members.

* Add changelog

* Fix migration conflict

* Fix migration conflict

* Fix test

* Update comments and ADR number

* Link to the workspace integrations from the sidebar's Settings menu.

* fix: refresh agent action errors when workspace integrations change

* fix: move workspace integrations into workspace settings

* fix: use share icon for workspace integration promotion

* refactor: use explicit shared application relations

* feat: centralize default integration selection

* refactor: share integration change notifications

* refactor: prepare integration mappings before import

* refactor: collect integration references during export

* refactor: prepare integration ownership changes without a new hook

* test: restore registered service model after mocking

* test: cover shared integration defaults in Builder

* test: include Slack in Agent workspace integration choices

* Fix migrations

* Stop template visitors from listing the workspace integrations through an application of the template.

* Only offer integrations of the user source's integration type when updating a user source.

* Say the workspace integration copy means published Application Builder apps and automation workflows.

* Use the same spacing under both integration settings sections and enlarge their headings.

* Keep the shared application out of automatic snapshot schedules.

* Fix test after rebase

---------

Co-authored-by: Jeremie Pardou <571533+jrmi@users.noreply.github.com>
2026-11-27 15:49:32 +04:00

610 lines
20 KiB
JavaScript

import { ELEMENT_EVENTS } from '@baserow/modules/builder/enums'
import { generateHash } from '@baserow/modules/core/utils/hashing'
import { isSharedApplication } from '@baserow/modules/core/store/application'
/**
* Returns the matching page and builder for a given page ID.
* Checks both the selected page and the shared page.
*/
const getPageContext = (store, pageId) => {
const selectedPage = store.getters['page/getSelected']
const builder = store.getters['application/get'](selectedPage?.builder_id)
if (!builder) return null // Sometimes we don't have the builder somehow
const sharedPage = store.getters['page/getSharedPage'](builder)
const pages = [selectedPage, sharedPage]
const page = pages.find((p) => p?.id === pageId)
if (!page) return null
return { page, builder, pages }
}
/**
* Returns the application, of any type, for a given application ID, or null when it
* isn't loaded in the store. Used by the integration and user source realtime
* handlers, which are scoped to the application rather than a page.
*/
const getApplicationContext = (store, applicationId) => {
const application = store.getters['application/get'](applicationId)
if (!application) return null
return { application }
}
/**
* Returns the shared application, of any workspace in the store, whose list holds
* the integration, or undefined.
*/
const getSharedApplicationHoldingIntegration = (store, integrationId) => {
return store.getters['workspace/getAll']
.map((workspace) =>
store.getters['application/getSharedApplication'](workspace)
)
.find(
(application) =>
application &&
store.getters['integration/getIntegrations'](application).some(
(integration) => integration.id === integrationId
)
)
}
/**
* Returns the application of the workspace, its shared application included, whose
* list holds the integration, or undefined.
*/
const getApplicationHoldingIntegration = (store, workspace, integrationId) => {
return [
...store.getters['application/getAllOfWorkspace'](workspace),
store.getters['application/getSharedApplication'](workspace),
].find((application) =>
store.getters['integration/getIntegrations'](application).some(
(integration) => integration.id === integrationId
)
)
}
/**
* Re-dispatches the data sources using an integration of the application, so the
* collection elements bound to them refresh. A workspace integration belongs to
* the shared application, but every builder of the workspace can use it: only the
* builder open in the editor has data sources on screen, so its data sources are
* the ones looked up.
*/
const redispatchForIntegration = (store, application, integrationId) => {
let builder = application
if (isSharedApplication(application)) {
const selectedPage = store.getters['page/getSelected']
builder = store.getters['application/get'](selectedPage?.builder_id)
if (builder?.workspace?.id !== application.workspace.id) {
return
}
}
store.dispatch('dataSource/redispatchForIntegration', {
builder,
integrationId,
})
}
const redispatchForAuthorizedIdentity = (
store,
{ workspaceId, identityId }
) => {
const builders = store.getters['application/getAll'].filter(
(application) =>
application.type === 'builder' &&
application.workspace?.id === workspaceId
)
// The workspace integrations, which every builder of the workspace can use.
const sharedApplication = store.getters['application/getSharedApplication']({
id: workspaceId,
})
const applications = [...builders, sharedApplication].filter(Boolean)
applications.forEach((application) => {
application.integrations
.filter(
(integration) =>
integration.type === 'local_baserow' &&
integration.authorized_subject?.type === 'auth.User' &&
integration.authorized_subject.id === identityId
)
.forEach((integration) => {
redispatchForIntegration(store, application, integration.id)
})
})
}
export const registerRealtimeEvents = (realtime) => {
realtime.registerEvent('identity_created', ({ store }, data) => {
redispatchForAuthorizedIdentity(store, {
workspaceId: data.workspace_id,
identityId: data.identity.id,
})
})
realtime.registerEvent('identity_deleted', ({ store }, data) => {
redispatchForAuthorizedIdentity(store, {
workspaceId: data.workspace_id,
identityId: data.identity_id,
})
})
// Page events
realtime.registerEvent('page_created', ({ store }, data) => {
const builder = store.getters['application/get'](data.page.builder_id)
store.dispatch('page/forceCreate', { builder, page: data.page })
})
realtime.registerEvent('page_updated', ({ store }, data) => {
const builder = store.getters['application/get'](data.page.builder_id)
if (builder !== undefined) {
const page = store.getters['page/getAllPages'](builder).find(
(p) => p.id === data.page.id
)
if (page !== undefined) {
store.dispatch('page/forceUpdate', {
builder,
page,
values: data.page,
})
}
}
})
realtime.registerEvent('page_deleted', ({ store }, data) => {
const builder = store.getters['application/get'](data.builder_id)
if (builder !== undefined) {
const page = store.getters['page/getAllPages'](builder).find(
(p) => p.id === data.page_id
)
if (page !== undefined) {
store.dispatch('page/forceDelete', {
builder,
page,
})
}
}
})
realtime.registerEvent('pages_reordered', ({ store, app }, data) => {
const builder = store.getters['application/getAll'].find(
(application) => generateHash(application.id) === data.builder_id
)
if (builder !== undefined) {
store.commit('page/ORDER_PAGES', {
builder,
order: data.order,
isHashed: true,
})
}
})
// Element events
realtime.registerEvent('element_created', ({ store }, data) => {
const ctx = getPageContext(store, data.element.page_id)
if (!ctx) return
// Update the graph first so forceCreate's fallback sees the element already
// in the correct position and skips the end-of-chain append.
if (data.graph) {
store.dispatch('page/forceUpdate', {
page: ctx.page,
values: { graph: data.graph },
})
}
store.dispatch('element/forceCreate', {
page: ctx.page,
element: data.element,
})
})
realtime.registerEvent('elements_created', ({ store }, data) => {
const ctx = getPageContext(store, data.page_id)
if (!ctx) return
if (data.graph) {
store.dispatch('page/forceUpdate', {
page: ctx.page,
values: { graph: data.graph },
})
}
for (const element of data.elements) {
store.dispatch('element/forceCreate', {
page: ctx.page,
element,
})
}
for (const workflowAction of data.workflow_actions || []) {
store.dispatch('builderWorkflowAction/forceCreate', {
page: ctx.page,
workflowAction,
})
}
})
realtime.registerEvent('element_deleted', ({ store }, data) => {
const ctx = getPageContext(store, data.page_id)
if (!ctx) return
// Remove the deleted element and its whole subtree's records...
const elementIds = data.element_ids ?? [data.element_id]
for (const elementId of elementIds) {
store.dispatch('element/forceDelete', {
builder: ctx.builder,
page: ctx.page,
elementId,
})
}
// ...then apply the relinked graph so the remaining siblings/children of the
// deleted element stay connected (forceDelete doesn't touch the graph).
if (data.graph) {
store.dispatch('page/forceUpdate', {
page: ctx.page,
values: { graph: data.graph },
})
}
})
realtime.registerEvent('element_updated', ({ store }, { element }) => {
const ctx = getPageContext(store, element.page_id)
if (!ctx) return
store.dispatch('element/forceUpdate', {
builder: ctx.builder,
page: ctx.page,
element,
values: element,
viaRealtime: true,
})
})
realtime.registerEvent('element_moved', ({ store }, data) => {
// Cross-page move: the element (and any descendants that travelled with it)
// left the source page for the target page. Relocate the records and apply
// both pages' refreshed graphs. Same-page moves omit these fields.
if (data.source_page_id != null && data.source_page_id !== data.page_id) {
const sourceCtx = getPageContext(store, data.source_page_id)
const targetCtx = getPageContext(store, data.page_id)
// Remove the moved elements from the source page (only if it's loaded —
// a client viewing another page won't have it) and refresh its graph.
if (sourceCtx) {
for (const element of data.elements) {
store.dispatch('element/forceDelete', {
builder: sourceCtx.builder,
page: sourceCtx.page,
elementId: element.id,
})
}
// The moved elements' workflow actions travel with them, so remove them
// from the source page too (their page foreign key now points elsewhere).
for (const workflowAction of data.workflow_actions || []) {
store.dispatch('builderWorkflowAction/forceDelete', {
page: sourceCtx.page,
workflowActionId: workflowAction.id,
})
}
store.dispatch('page/forceUpdate', {
page: sourceCtx.page,
values: { graph: data.source_graph },
})
}
// Add the moved elements to the target page. Apply the graph first so
// forceCreate sees them already positioned and skips the root-chain append.
if (targetCtx) {
store.dispatch('page/forceUpdate', {
page: targetCtx.page,
values: { graph: data.graph },
})
for (const element of data.elements) {
store.dispatch('element/forceCreate', {
page: targetCtx.page,
element,
})
}
// Re-add the workflow actions on the target page so they stay associated
// with their (now relocated) elements.
for (const workflowAction of data.workflow_actions || []) {
store.dispatch('builderWorkflowAction/forceCreate', {
page: targetCtx.page,
workflowAction,
})
}
}
return
}
const ctx = getPageContext(store, data.page_id)
if (!ctx) return
store.dispatch('page/forceUpdate', {
page: ctx.page,
values: { graph: data.graph },
})
store.dispatch('element/refreshCachedValues', { page: ctx.page })
})
realtime.registerEvent('elements_moved', ({ store }, { page_id, graph }) => {
const ctx = getPageContext(store, page_id)
if (!ctx) return
store.dispatch('page/forceUpdate', { page: ctx.page, values: { graph } })
store.dispatch('element/refreshCachedValues', { page: ctx.page })
})
// Data source events
realtime.registerEvent('data_source_created', ({ store }, data) => {
const ctx = getPageContext(store, data.data_source.page_id)
if (!ctx) return
store.dispatch('dataSource/forceCreate', {
page: ctx.page,
dataSource: data.data_source,
beforeId: data.before_id,
})
})
realtime.registerEvent('data_source_updated', ({ store }, data) => {
const ctx = getPageContext(store, data.data_source.page_id)
if (!ctx) return
const dataSource = store.getters['dataSource/getPagesDataSourceById'](
ctx.pages,
data.data_source.id
)
if (!dataSource) return
// The data source may currently live on a different page than the event's
// page_id (e.g. sharing/un-sharing — including via undo/redo — moves it
// between a content page and the shared page). Relocate it to the destination
// page first, otherwise forceUpdate would duplicate it across both page lists.
if (dataSource.page_id !== data.data_source.page_id) {
const sourcePage = ctx.pages.find((p) => p?.id === dataSource.page_id)
if (sourcePage) {
store.dispatch('dataSource/forcePageMove', {
pageSource: sourcePage,
pageDest: ctx.page,
dataSourceId: dataSource.id,
})
}
}
store.dispatch('dataSource/forceUpdate', {
page: ctx.page,
dataSource,
values: data.data_source,
})
// Collection elements bound to the data source must fetch their content
// again, e.g. after Kuma changed its filters.
store.dispatch('element/emitElementEvent', {
event: ELEMENT_EVENTS.DATA_SOURCE_AFTER_UPDATE,
elements: ctx.pages
.filter(Boolean)
.flatMap((page) => store.getters['element/getElementsOrdered'](page)),
dataSourceId: dataSource.id,
builder: ctx.builder,
})
})
realtime.registerEvent('data_source_deleted', ({ store }, data) => {
const ctx = getPageContext(store, data.page_id)
if (!ctx) return
store.dispatch('dataSource/forceDelete', {
page: ctx.page,
dataSourceId: data.data_source_id,
})
})
/**
* Apply an integration event before refreshing its dependent data sources.
* The previous shared owner also covers demotion to an unloaded application.
*/
const registerIntegrationEvent = (type, handler) => {
realtime.registerEvent(type, (context, data) => {
const { store } = context
const integrationId = data.integration?.id ?? data.integration_id
const applicationId =
data.integration?.application_id ?? data.application_id
const application = getApplicationContext(
store,
applicationId
)?.application
const previousSharedApplication =
type === 'integration_updated'
? getSharedApplicationHoldingIntegration(store, integrationId)
: null
handler(context, data)
const owner = previousSharedApplication || application
if (owner) {
redispatchForIntegration(store, owner, integrationId)
}
})
}
// Integration events, for every application type and the shared application:
// no other module handles them.
registerIntegrationEvent('integration_created', ({ store }, data) => {
const ctx = getApplicationContext(store, data.integration.application_id)
if (!ctx) return
store.dispatch('integration/forceCreate', {
application: ctx.application,
integration: data.integration,
beforeId: data.before_id,
})
})
registerIntegrationEvent('integration_updated', ({ store }, data) => {
const ctx = getApplicationContext(store, data.integration.application_id)
if (!ctx) {
// Moved out of the workspace to an application this tab doesn't have, like
// the undo of a promotion seen by a builder of another application. It no
// longer belongs in the workspace list, which offers it everywhere.
const sharedApplication = getSharedApplicationHoldingIntegration(
store,
data.integration.id
)
if (sharedApplication) {
store.dispatch('integration/forceDelete', {
application: sharedApplication,
integrationId: data.integration.id,
})
}
return
}
// A promotion, and its undo, move the integration between an application and
// the shared application of its workspace, so another application can still
// hold it. Relocate it first, otherwise it would stay in that list too: an
// application looks up the workspace integrations as well, and would find the
// integration there before the update.
const holder = getApplicationHoldingIntegration(
store,
ctx.application.workspace,
data.integration.id
)
if (holder && holder.id !== ctx.application.id) {
store.dispatch('integration/forceMoveToApplication', {
applicationSource: holder,
applicationDest: ctx.application,
integrationId: data.integration.id,
})
} else if (!holder && isSharedApplication(ctx.application)) {
// A promotion from an application whose integrations weren't loaded here
// reaches the shared application as an update of an integration it doesn't
// have, so it's added like a created one.
store.dispatch('integration/forceCreate', {
application: ctx.application,
integration: data.integration,
})
return
}
const integration = store.getters['integration/getIntegrationById'](
ctx.application,
data.integration.id
)
if (!integration) return
store.dispatch('integration/forceUpdate', {
application: ctx.application,
integration,
values: data.integration,
})
})
registerIntegrationEvent('integration_deleted', ({ store }, data) => {
const ctx = getApplicationContext(store, data.application_id)
if (!ctx) return
store.dispatch('integration/forceDelete', {
application: ctx.application,
integrationId: data.integration_id,
})
})
// User source events
realtime.registerEvent('user_source_created', ({ store }, data) => {
const ctx = getApplicationContext(store, data.user_source.application_id)
if (!ctx) return
store.dispatch('userSource/forceCreate', {
application: ctx.application,
userSource: data.user_source,
beforeId: data.before_id,
})
})
realtime.registerEvent('user_source_updated', ({ store }, data) => {
const ctx = getApplicationContext(store, data.user_source.application_id)
if (!ctx) return
const userSource = store.getters['userSource/getUserSourceById'](
ctx.application,
data.user_source.id
)
if (!userSource) return
store.dispatch('userSource/forceUpdate', {
application: ctx.application,
userSource,
values: data.user_source,
})
})
realtime.registerEvent('user_source_deleted', ({ store }, data) => {
const ctx = getApplicationContext(store, data.application_id)
if (!ctx) return
store.dispatch('userSource/forceDelete', {
application: ctx.application,
userSourceId: data.user_source_id,
})
})
// Workflow action events
realtime.registerEvent('workflow_action_created', ({ store }, data) => {
const ctx = getPageContext(store, data.page_id)
if (!ctx) return
store.dispatch('builderWorkflowAction/forceCreate', {
page: ctx.page,
workflowAction: data.workflow_action,
})
})
realtime.registerEvent('workflow_action_updated', ({ store }, data) => {
const ctx = getPageContext(store, data.page_id)
if (!ctx) return
store.dispatch('builderWorkflowAction/forceUpdate', {
page: ctx.page,
workflowAction: data.workflow_action,
values: data.workflow_action,
viaRealtime: true,
})
})
realtime.registerEvent('workflow_action_deleted', ({ store }, data) => {
const ctx = getPageContext(store, data.page_id)
if (!ctx) return
store.dispatch('builderWorkflowAction/forceDelete', {
page: ctx.page,
workflowActionId: data.workflow_action_id,
})
})
realtime.registerEvent('workflow_actions_reordered', ({ store }, data) => {
const ctx = getPageContext(store, data.page_id)
if (!ctx) return
store.dispatch('builderWorkflowAction/forceOrder', {
page: ctx.page,
order: data.order,
// Scope the reorder to the element the actions belong to, so another
// element's actions aren't reshuffled.
elementId: data.element_id,
})
})
// Theme events
realtime.registerEvent('theme_updated', ({ store }, data) => {
const builder = store.getters['application/get'](data.builder_id)
if (!builder) return
store.dispatch('theme/forceUpdate', {
builder,
values: data.properties,
})
})
}