Files
baserow/web-frontend/modules/database/components/field/DatabaseWorkflowActionWithService.vue
T
alamin-br 0f8d63e4e4 feat[4b/6] Button field: send an email (#5973)
* feat: let a button field send an email

A button can now send an email when it is clicked, through the SMTP
server this installation is configured with. A button's actions carry no
integration, so the form drops the integration branch of the shared SMTP
form and the action always sends through the instance.

Without an instance SMTP server the action would fail on every click, so
it is refused when it is configured instead, and the editor shows the
reason rather than a generic error. The refusal covers creating an
action, swapping an existing one to this type, and clicking a button that
already carries one.

A failure names neither the mail host nor its port: the clicker is told
which action could not be completed and nothing more.

* fix: refuse the email action on an instance that only prints what it sends

Without SMTP configured, Baserow keeps a backend that writes the message
locally and reports that it sent, so the guard never fired on the very
installation it was written for: the action was offered, the click said it
worked, and nothing arrived. What the instance does with a message it is
given now decides, rather than EMAIL_HOST, which Django defaults to
localhost whether or not anything is listening.

The instance setting is also pinned when an action is prepared. The form
does not offer the choice, but the endpoint took whatever it was given,
and a service that sends through nothing fails on every click.

A click that may not dispatch no longer learns how this installation is
configured: the refusal now runs after the permission check, and once per
type rather than per action.

Drops the editor's own toast for a refused type. The shared error handler
already shows the reason on a 4xx, and replacing the handler with a stub
broke every caller that asks it what kind of failure it was.

Also sets the SMTP form's instance flag where the form takes its defaults,
so opening an action no longer reports a change the user never made, and
fixes the mail fixture, which read one page of a catcher the whole suite
shares.

* fix: scope the mail cleanup, and only hide the failures that name an address

Emptying the catcher in `beforeAll` runs once per worker, and the suite
runs three of them locally, so one worker could delete the message another
was waiting for. Pointed at a dev stack it also took whatever mail the
developer had captured. A test now drops the message it read, by id.

The clicker was told "the request could not be completed" for failures
that name nothing about this installation, such as a server refusing the
encryption. What is withheld is now named: the failures whose message
carries the address the request was going to. Everything else a service
raises is about the values it was handed, and is the only thing telling
the clicker what to fix.

* fix: keep the mail lookup independent of the catcher, and say when sending is off

The catcher is asked for the message by subject rather than read a page at
a time. A dev stack's is long lived and holds whatever the developer
captured, so the one being looked for can be arbitrarily far down, and the
purge that used to hide that was itself taking other workers' mail.

Whether this installation can send is now decided in one place, the SMTP
service type, so the flag the API already exposes for it is true rather
than a check the button field kept to itself. A backend the settings do
not name at all counts as unable to send: the service asks for it by name
and would fail on every click. The editor reads that flag and says so on
an action whose instance has stopped being able to send, which until now
only a click would reveal.

A sequence carrying two deactivated types names the same one every time.

Tests cover what the endpoints answer: the create, type swap and dispatch
refusals, their error code and reason, and that a refused click leaves the
rate limit alone.

* fix: say an instance cannot send email before the action is saved

An action being configured has not been saved yet, so there is no service to
ask whether this installation can send. Until now the first thing to say so
was the refusal on save. The settings the editor already loads now carry it,
and the email option in the action dropdown is disabled with the reason beside
it.

The two ways of being unable to send are told apart. Sending turned off by an
administrator and an instance with no mail server each get their own reason,
returned as a code so the editor words and translates it.

The mail backend is matched as whole path segments, so a custom backend such
as `myapp.console_relay` is not read as the console one.

The setting behind all of this is documented, including that turning it off
disables a button field's email action altogether.

* fix: pin the mail server as part of the write that creates the service

A database action carries no integration, so the service is pinned to the
instance server whatever the caller asked for. That pin was applied after the
base had already saved the service, which updated the row a second time. It
now travels in the values the base saves, so a create writes the service once.

* test: cover what an exported email action carries and what it drops

The HTTP action had import and duplication coverage, the email one had none.
These pin the field references a copy has to rewrite, that an export keeps the
action pinned to the instance server, and that an instance which cannot send
still imports the action as configured rather than dropping or repairing it.

* fix: hold a button's lock for as long as its email actions can wait

The lock's TTL read a `timeout` field off each service. An email service has
none, so a click that only sends email got the default floor however many
sends it chained, and a lock expiring mid sequence lets a second click send
the same mail again.

Services are asked now. The HTTP one answers with its timeout, the email one
with three times the SMTP timeout, which is per socket operation rather than
for the send as a whole.

* docs: say what the deactivation hooks take and give back

The hooks a type overrides to say it cannot be used here were documented
unevenly: no return described, no raise named, and the SMTP overrides took
untyped arguments.

* fix: let an unsaved email action be read by the one after it

A service has no schema until it is saved, so an email added in the editor was
missing from the next action's explorer until the field was saved and reopened.
An email answers with whether it went out and nothing else, which is known
before anything is sent, so the type offers that shape itself.

* test: prove the chained email arrived, and say where the catcher is

The chained test only showed that the action after the email ran. It now waits
for the message itself, under a subject unique to the run, and drops it in a
`finally` so a failed assertion leaves nothing behind for later runs to read.

Which mail catcher the suite reads is documented: the local runner follows the
dev stack's own MailHog port rather than assuming the default instance, and
the two mail variables are written down with when each applies.

* fix: keep the email action pinned through an edit, and size its lock for a real send

An update is not refused the way a create is, and the service type drops the
instance server while sending is off. An action edited then came out unpinned
and failed on every click once sending was back. The action type restores the
pin after the base has saved, writing only in that case.

The lock's share for an email is sized for the conversation a send really is:
the timeout applies to every socket operation, and there are a dozen or more
of them, not three.

Whether the instance can send is asked two ways now. The Application Builder
and automations keep the setting-and-host check they always had, so an
installation without a mail server behaves as before for them. The stricter
check, where a backend that only prints counts as no server, is the button
field's alone: it has no integration to fall back on. `is_deactivated` is
derived from the reason rather than computed a second time.

* refactor: let every email editor see the answer's shape, and decide deactivation once

The `{success}` shape an unsaved email answers with lived on the button field's
action type alone, so the builder and automations still showed nothing for an
unsaved email. It moves to the shared service type, which every editor reads
through.

The action type's `getErrorMessage` repeated what the base already does with
`isDeactivatedReason`, and only differed for tests that passed no workspace.
The override goes, and the tests pass the workspace the action list passes.

* test: read the dev stack's mail catcher by default

A plain `yarn run test` after the local runner has finished fell back to the
containerized stack's port, which the README says it does not. `just e2e` and
CI set the URL themselves.

* fix: line up what an email action promises with what it can do

Four follow-ups from the review of the email action.

The editor and the send now ask the same question about the instance mail
server. A mail backend with no host to give it reported the action as
available and then failed the click, because a database action has no
integration to fall back on.

What an email says no longer travels in an export that leaves the
instance. The table schema handed to formula AI is serialized that way,
and reading a formula field needs far less permission than configuring a
button, so recipients, subject and body are blanked there as an HTTP
action's headers already are. A snapshot and a duplicate keep them.

A server that answers and then refuses, over TLS or over credentials,
now counts against the rate limit. The instance had already opened the
connection, so giving the slot back made that traffic free to repeat.

And ADR 006 records that the email action sends through the instance
server rather than an SMTP integration, which is the opposite of what it
said.
2026-09-03 13:49:27 +02:00

203 lines
6.7 KiB
Vue

<template>
<div>
<component
:is="serviceType.formComponent"
v-bind="formProps"
@values-changed="values.service = { ...defaultValues.service, ...$event }"
/>
<div v-if="capturesSampleData" class="button-field-action-form__payload">
<SampleDataViewer
v-if="sampleData"
:sample-data="sampleData"
:is-error="capturedNothing"
:modal-title="
$t('databaseWorkflowActionWithService.payloadModalTitle', {
actionLabel: workflowActionType.label,
})
"
:modal-subtitle="
$t('databaseWorkflowActionWithService.payloadModalSubTitle')
"
/>
<Alert v-else type="info-neutral" class="margin-bottom-0">
<p>{{ $t('databaseWorkflowActionWithService.nothingCapturedYet') }}</p>
</Alert>
</div>
</div>
</template>
<script>
import form from '@baserow/modules/core/mixins/form'
import SampleDataViewer from '@baserow/modules/core/components/SampleDataViewer'
import FieldService from '@baserow/modules/database/services/field'
import { notifyIf } from '@baserow/modules/core/utils/error'
import { FIELDS_UNAVAILABLE } from '@baserow/modules/database/utils/buttonField'
import { DatabaseApplicationType } from '@baserow/modules/database/applicationTypes'
export default {
name: 'DatabaseWorkflowActionWithService',
components: { SampleDataViewer },
mixins: [form],
inject: {
// The sub-form collects these so the data explorer can describe what an
// unsaved action will return.
registerTableFields: { from: 'registerTableFields', default: null },
},
props: {
workflowAction: {
type: Object,
required: true,
},
database: {
type: Object,
required: true,
},
},
data() {
return {
allowedValues: ['service'],
values: {
service: {},
},
// Null until the first fetch, so the form keeps using the saved schema.
mappableFields: null,
fieldsLoading: false,
// Bumped per fetch, so an overtaken response is dropped.
fetchToken: 0,
}
},
computed: {
workflowActionType() {
return this.$registry.get(
'databaseWorkflowActionType',
this.workflowAction.type
)
},
serviceType() {
return this.workflowActionType.serviceType
},
// A type whose result only a real answer can describe, so the editor has
// something to say about it whether or not a click has answered yet.
capturesSampleData() {
return this.workflowActionType.capturesSampleData
},
/**
* What the last click remembered, from the saved service rather than the
* buffered values: nothing in this editor can produce it. A stored error
* describes no shape, so it counts as nothing captured.
*/
/** Why the last click described nothing, when that is what happened. */
capturedNothing() {
return Boolean(this.defaultValues.service?.sample_data?._error)
},
sampleData() {
const sample = this.defaultValues.service?.sample_data
if (!sample) {
return null
}
// A click that answered 404, timed out, or came back too big leaves the
// reason instead of a shape. Shown in place of the note asking for a
// click that has already happened.
if (sample._error) {
return sample._error
}
// What is stored is the whole dispatch result. The explorer's paths
// start inside its `data`, so the viewer shows what they name.
return sample.data ?? null
},
// Delete row has no field mappings, so its form takes neither prop.
supportsFieldMappings() {
return this.workflowActionType.mapsFields
},
formProps() {
const props = {
application: this.database,
service: this.defaultValues.service,
serviceType: this.serviceType,
databases: this.workspaceDatabases,
defaultValues: this.defaultValues.service,
...this.workflowActionType.serviceFormProps,
}
if (this.supportsFieldMappings) {
props.mappableFields = this.mappableFields
props.loading = this.fieldsLoading
// Nothing is saved on a table change here, so the form must not raise
// a spinner that only its `loading` prop can lower.
props.savesOnTableChange = false
}
return props
},
selectedTableId() {
return this.values.service?.table_id || null
},
/**
* A button field has no integration to source a table list from, so every
* database in the field's workspace is offered, its own included.
*/
workspaceDatabases() {
return this.$store.getters['application/getAllOfWorkspace'](
this.database.workspace
).filter(
(application) => application.type === DatabaseApplicationType.getType()
)
},
},
watch: {
selectedTableId: {
immediate: true,
handler(tableId) {
this.fetchMappableFields(tableId)
},
},
},
methods: {
/**
* The form normally derives its mappings from the service schema, which
* only a saved service carries. This editor buffers its changes, so a new
* action has no schema and needs the fields fetched for it.
*/
async fetchMappableFields(tableId) {
if (!this.supportsFieldMappings) {
return
}
// Taken before the await, so anything already in flight is superseded.
const token = ++this.fetchToken
if (tableId === null) {
this.mappableFields = null
// The superseded fetch will not lower the spinner now that its token
// is stale.
this.fieldsLoading = false
return
}
this.fieldsLoading = true
try {
const { data } = await FieldService(this.$client).fetchAll(tableId)
if (token !== this.fetchToken) {
return
}
// The same filter the schema applies. Unfiltered for the explorer,
// which can read a created row's read only fields.
this.mappableFields = data.filter((field) => !field.read_only)
this.registerTableFields?.(tableId, data)
} catch (error) {
if (token !== this.fetchToken) {
return
}
this.mappableFields = []
// Marked rather than left alone, so the explorer offers this action's
// `id` and nothing else. Without it the schema falls back to the last
// save's, which describes the table it pointed at before.
this.registerTableFields?.(tableId, FIELDS_UNAVAILABLE)
notifyIf(error, 'field')
} finally {
// Only the newest fetch owns the spinner, or an overtaken one
// uncovers the previous table's mappings mid flight.
if (token === this.fetchToken) {
this.fieldsLoading = false
}
}
},
},
}
</script>