diff --git a/docs/content/docs/api/reference/campaigns.mdx b/docs/content/docs/api/reference/campaigns.mdx index 18ae91c66..b045c4a88 100644 --- a/docs/content/docs/api/reference/campaigns.mdx +++ b/docs/content/docs/api/reference/campaigns.mdx @@ -392,7 +392,8 @@ A `CampaignAdvancedSettings` object: the campaign id, the `overrides` block, and "auto_pause_on_negative": false, "auto_suppress_on_unsubscribe_keyword": true, "hold_on_out_of_office": true, - "out_of_office_hold_days": 7 + "out_of_office_hold_days": 7, + "stop_all_campaigns_on_reply": false }, "send_time_optimization": { "enabled": false, @@ -439,7 +440,7 @@ Replace the campaign's advanced overrides. **Scope** `WRITE_CAMPAIGNS` · **Org "bounce_pipeline": { "enabled": true, "auto_suppress_on_bounce": true, "auto_suppress_on_complaint": true, "auto_suppress_on_unsubscribe": true, "auto_pause_campaign_on_spike": true, "pause_bounce_rate_threshold": 8, "pause_complaint_rate_threshold": 1.5 }, "task_reliability": { "enabled": true, "dlq_enabled": true, "max_attempts": 5, "execution_window_seconds": 300 }, "ab_testing": { "enabled": true, "default_winning_rule": "reply_rate", "auto_promote_winner": false, "min_sample_size": 30 }, - "reply_intent": { "enabled": true, "positive_keywords": [], "negative_keywords": [], "out_of_office_keywords": [], "question_keywords": [], "auto_create_crm_task": true, "crm_task_intents": ["positive", "question", "neutral", "negative"], "auto_pause_on_negative": false, "auto_suppress_on_unsubscribe_keyword": true, "hold_on_out_of_office": true, "out_of_office_hold_days": 7 }, + "reply_intent": { "enabled": true, "positive_keywords": [], "negative_keywords": [], "out_of_office_keywords": [], "question_keywords": [], "auto_create_crm_task": true, "crm_task_intents": ["positive", "question", "neutral", "negative"], "auto_pause_on_negative": false, "auto_suppress_on_unsubscribe_keyword": true, "hold_on_out_of_office": true, "out_of_office_hold_days": 7, "stop_all_campaigns_on_reply": false }, "send_time_optimization": { "enabled": true, "use_contact_timezone": true, "default_contact_timezone": "UTC", "preferred_hours": [9, 14], "weekend_weight_multiplier": 0.5 }, "preflight": { "enabled": true, "check_tracking_domain": true, "check_unsubscribe_header": true, "check_ab_variant_configured": false, "check_daily_limit": true, "check_schedule_window": true, "check_content_score": true, "min_content_score": 60 }, "dashboard": { "enabled": true, "show_suppression_log": true, "show_intent_summary": true, "show_dlq_stats": true } @@ -877,7 +878,7 @@ Read whether one contact's flow inside this campaign is currently held. A hold p } ``` -`hold` is absent when the lead is not held, including for a dated hold that has since expired. `until` is absent when the hold has no end, in which case only a resume lifts it. `source` is `out_of_office`, `inbox_tagging`, `manual`, or `cc` while the contact is [copied on another lead's emails](#set-a-leads-cc) in this campaign; a `cc` hold's `reason` is that lead's address. +`hold` is absent when the lead is not held, including for a dated hold that has since expired. `until` is absent when the hold has no end, in which case only a resume lifts it. `source` is `out_of_office`, `inbox_tagging` (a classified reply, or with `reply_intent.stop_all_campaigns_on_reply` on, a reply to another campaign), `manual`, or `cc` while the contact is [copied on another lead's emails](#set-a-leads-cc) in this campaign; a `cc` hold's `reason` is that lead's address. ### Errors diff --git a/docs/content/docs/api/reference/contacts.mdx b/docs/content/docs/api/reference/contacts.mdx index df774a9d0..f91b60f82 100644 --- a/docs/content/docs/api/reference/contacts.mdx +++ b/docs/content/docs/api/reference/contacts.mdx @@ -262,6 +262,7 @@ Auth: **Scope** `BULK_CONTACTS` · **Org permission** `manage_contacts` | `contacts` | string[] | Yes, unless `all` | Contact IDs to edit (1 to 10,000). | | `all`, `filters`, `exclude` | — | No | Select by filter instead; see [selecting contacts](#selecting-contacts-for-a-bulk-action). | | `add_campaigns` | string[] | No | Campaign IDs to add. | +| `skip_active_in_other_campaigns` | boolean | No | Leave a contact out of `add_campaigns` while their sequence is still running in another campaign: queued, sending, or held, in a campaign that is not a draft and not completed. Contacts who replied, bounced, unsubscribed, failed or finished every step elsewhere are added. Defaults to `false`. | | `remove_campaigns` | string[] | No | Campaign IDs to remove. | | `add_categories` | string[] | No | Label IDs to add. | | `remove_categories` | string[] | No | Label IDs to remove. | @@ -428,6 +429,7 @@ Send `multipart/form-data` with a `file` field and an `options` field containing | `has_header` | boolean | Yes | Whether the first row is a header. | | `category_ids` | string[] | No | Label IDs to put on imported contacts. | | `campaign_ids` | string[] | No | Campaigns to add imported contacts to. | +| `skip_active_in_other_campaigns` | boolean | No | Keep a contact who already exists out of `campaign_ids` while their sequence is still running in another campaign, as `skip_active_in_other_campaigns` on a [bulk update](#bulk-update-contacts) defines it. The row is still imported or updated under `dedup`, and still gets its labels and segments. Defaults to `false`. | | `segment_ids` | string[] | No | Segments to pin imported contacts into, as a manual include override. Applies to imported, updated, and skipped-but-linked contacts alike. An id that is not a valid UUID, or that names no segment in the organization, is rejected with `400` before any row is written. | | `subscribed_default` | boolean | No | Subscription state for new contacts when no subscribed column is mapped. Defaults to true. | @@ -1316,9 +1318,9 @@ Sequence action steps `add_to_segment` and `remove_from_segment` take a `segment `POST /segments/:id/add-to-campaign` ```json -{ "campaign_id": "…" } +{ "campaign_id": "…", "skip_active_in_other_campaigns": true } ``` -Enrols every current member as a lead. Contacts already in the campaign are skipped, each new lead gets a `campaign_added` activity, and a running campaign is woken so the leads are scheduled. Returns `{ "campaign_id", "added", "members" }`. This is a snapshot: later members are not added until the call is repeated. Safe to retry. It counts as choosing the leads it adds, so it clears any hand-made removal and the rows it writes are never withdrawn by [detaching a segment](/api/reference/campaigns/#replace-linked-segments). Members that are already leads are left exactly as they are, provenance included. For a live link that keeps enrolling members as they join the segment, see [linked segments](/api/reference/campaigns/#list-linked-segments). +Enrols every current member as a lead. Contacts already in the campaign are skipped, each new lead gets a `campaign_added` activity, and a running campaign is woken so the leads are scheduled. With `skip_active_in_other_campaigns` (optional, default `false`), a member whose sequence is still running in another campaign is left out too, as on a [bulk update](#bulk-update-contacts), and keeps any hand-made removal. Returns `{ "campaign_id", "added", "members" }`. This is a snapshot: later members are not added until the call is repeated. Safe to retry. It counts as choosing the leads it adds, so it clears any hand-made removal and the rows it writes are never withdrawn by [detaching a segment](/api/reference/campaigns/#replace-linked-segments). Members that are already leads are left exactly as they are, provenance included. For a live link that keeps enrolling members as they join the segment, see [linked segments](/api/reference/campaigns/#list-linked-segments). Auth: **Scope** `WRITE_CAMPAIGNS` · **Org permission** `manage_campaigns` diff --git a/docs/content/docs/api/reference/deliverability-ops.mdx b/docs/content/docs/api/reference/deliverability-ops.mdx index b9475c115..065059dd3 100644 --- a/docs/content/docs/api/reference/deliverability-ops.mdx +++ b/docs/content/docs/api/reference/deliverability-ops.mdx @@ -53,7 +53,8 @@ Returns the `AdvancedOutreachSettings` object directly (not wrapped in an envelo "auto_pause_on_negative": false, "auto_suppress_on_unsubscribe_keyword": true, "hold_on_out_of_office": true, - "out_of_office_hold_days": 7 + "out_of_office_hold_days": 7, + "stop_all_campaigns_on_reply": false }, "send_time_optimization": { "enabled": false, @@ -102,6 +103,10 @@ The `unsubscribe` block is the workspace default for the opt-out appended after `reply_intent.hold_on_out_of_office` defaults to `true`. When a recipient's mailbox answers a campaign step with an away message, the contact's next step in that campaign is held until they are back: the return date in the auto-reply when one can be read, plus a working day, and `out_of_office_hold_days` (1 to 90, default `7`) when none can. An auto-reply is never counted as a human reply, so without this the follow-up goes out on schedule while the person is away. `out_of_office_keywords` feeds the reply-intent record and is workspace-editable; detection itself carries its own multilingual vocabulary and does not depend on it. See [out of office and pausing one lead](/guides/campaigns/#out-of-office-and-pausing-one-lead) and [pause a lead](/api/reference/campaigns/#pause-a-lead). + +`reply_intent.stop_all_campaigns_on_reply` defaults to `false`. It takes effect only while `reply_intent.enabled` is `true` (the default): with reply automation off, no reply holds anything. Set it to `true` and a human reply also holds the contact in every other campaign they are a lead of that has not completed, for 365 days, with the reason `replied to another campaign` and the source `inbox_tagging`. Automatic replies never trigger it, the answered campaign is left to its own stop on reply, and a member's own pause is never replaced. Lift it like any hold, with [resume a lead](/api/reference/campaigns/#pause-a-lead). Omitting the field keeps it off. See [other campaigns the contact is in](/guides/sequences/#other-campaigns-the-contact-is-in). + + `send_time_optimization.enabled` defaults to `false`. Set it to `true` and campaign scheduling holds each send until the recipient's local clock reaches one of `preferred_hours`, resolving the recipient's timezone from the contact's `timezone` custom field, then the country-code suffix of its email domain, then `default_contact_timezone`. It can only delay a send: the campaign window, the mailbox's sending profile, its daily cap, and the campaign end date all still bind. See [Sending behavior](/guides/sending-behavior/). diff --git a/docs/content/docs/guides/campaigns.mdx b/docs/content/docs/guides/campaigns.mdx index 1e287dce6..46bc5b3e4 100644 --- a/docs/content/docs/guides/campaigns.mdx +++ b/docs/content/docs/guides/campaigns.mdx @@ -258,7 +258,7 @@ Anything above `50`/day per cold mailbox needs positive reputation signals and a ### Adding leads -The **Leads** tab takes contacts four ways. **From contacts** opens a picker over the workspace contact list: search by name, email or company, filter by label, tick individual people or **Select loaded**, or **Select all matching** to add everyone the search returns, up to `250,000` at a time (narrow the search and repeat past that). Contacts already in the campaign are marked as leads and skipped. **Import** runs the file import wizard with this campaign preselected (its Options step can also pin the file into segments), **Sheet sync** attaches a Google Sheet, and **Add lead** creates a single new contact in the campaign. Any workspace member with contact access can add leads to any campaign in the workspace, whoever created it. +The **Leads** tab takes contacts four ways. **From contacts** opens a picker over the workspace contact list: search by name, email or company, filter by label, tick individual people or **Select loaded**, or **Select all matching** to add everyone the search returns, up to `250,000` at a time (narrow the search and repeat past that). Contacts already in the campaign are marked as leads and skipped. Tick **Skip anyone running in another campaign** in the picker's footer to leave out contacts whose sequence is still queued, sending or held in another campaign that is not a draft and not completed; the same option is on the import wizard, a segment's **Add to campaign** and the contacts page's bulk edit. **Import** runs the file import wizard with this campaign preselected (its Options step can also pin the file into segments), **Sheet sync** attaches a Google Sheet, and **Add lead** creates a single new contact in the campaign. Any workspace member with contact access can add leads to any campaign in the workspace, whoever created it. **Segments** on the same toolbar links [segments](/guides/segments/) to the campaign as a live audience, up to 20 per campaign. Linking enrols every current member immediately and turns on **Keep running for new leads**, and contacts who enter a linked segment later are enrolled on their own, within a couple of minutes. An active campaign wakes to send to them (a campaign waiting for leads picks up straight away), a finished one restarts through the usual launch checks, and a paused one accumulates them for later. A contact who leaves a linked segment keeps their lead row, but detaching the segment itself takes its leads back out, so swapping one segment for another leaves the campaign with the new list rather than both. Leads the campaign has already emailed stay, as do any you added by hand and anyone a segment that stayed linked still covers; the dialog asks before a save that costs leads. See [linking a segment to a campaign](/guides/segments/#linking-a-segment-to-a-campaign). diff --git a/docs/content/docs/guides/contacts-crm.mdx b/docs/content/docs/guides/contacts-crm.mdx index 25c3fecf2..189750072 100644 --- a/docs/content/docs/guides/contacts-crm.mdx +++ b/docs/content/docs/guides/contacts-crm.mdx @@ -102,6 +102,8 @@ Two columns may fill the same field, which is how a `Phone` and a `Mobile` colum `Dana@Acme.com` and `dana@acme.com` are the same contact, and a contact belongs to the workspace, so an address a teammate already added counts as one you have. Choose **Leave as is** (their details stay untouched) or **Update details** (empty details are filled in from the file). The API also accepts `create_duplicate`, which updates too, since one address is one contact per workspace. Skipping still adds the contact to the campaigns, segments, and labels the import targets: it leaves their fields alone, it does not leave them out of the list. If the same address appears twice in one file it becomes one contact, and the extra rows count as skipped with the line they repeat. + +To keep people another campaign is still emailing out of this import's campaigns, tick **Skip contacts already running in another campaign** under **Campaigns** on **Review**. A contact counts as running when their sequence in a campaign that is not a draft and not completed is still queued, sending or held. One who replied, bounced, unsubscribed or finished every step there is added as usual. Skipped contacts are still imported or updated and still get the import's labels and segments; only the campaign enrolment is left out. A segment [linked to a campaign](/guides/segments/#linking-a-segment-to-a-campaign) still enrols everyone in it, so pinning a skipped contact into a segment that feeds the same campaign enrols them through the link. It is off by default. An address you already hold as a contact in another workspace you belong to cannot be added to this one, and that row fails with a reason saying so; the other workspace's contact is never changed by an import here. diff --git a/docs/content/docs/guides/segments.mdx b/docs/content/docs/guides/segments.mdx index 26071aeee..8fe7844e6 100644 --- a/docs/content/docs/guides/segments.mdx +++ b/docs/content/docs/guides/segments.mdx @@ -111,7 +111,7 @@ A segment with no conditions at all is a plain list: it holds exactly the contac ## Using a segment - **Browse**: a segment page lists its current members with the same table, filters, detail drawer and bulk actions as the contacts page, including [**Select all matching**](/guides/contacts-crm/#selecting-rows) so an action covers every member and not only the rows loaded so far, for selections up to `250,000` contacts. -- **Add to campaign**: enrols every current member as a lead of the campaign you pick, from the segment page or with **From segment** on a campaign's Leads tab. Contacts already in that campaign are skipped, and a running campaign wakes up to schedule the new leads. This is a snapshot: contacts who join the segment later are not added until you run it again. To keep a campaign fed automatically, [link the segment](#linking-a-segment-to-a-campaign) instead. +- **Add to campaign**: enrols every current member as a lead of the campaign you pick, from the segment page or with **From segment** on a campaign's Leads tab. Contacts already in that campaign are skipped, and a running campaign wakes up to schedule the new leads. Tick **Skip contacts already running in another campaign** to also leave out members whose sequence is still running in another campaign. This is a snapshot: contacts who join the segment later are not added until you run it again. To keep a campaign fed automatically, [link the segment](#linking-a-segment-to-a-campaign) instead. - **Link to campaign**: attaches the segment to a campaign as a live audience, so contacts who join the segment later become leads on their own. See [below](#linking-a-segment-to-a-campaign). - **New campaign**: pick the segment as a lead list on the first step of **Campaigns** > **New campaign**, which shows how many leads it holds and how long the mailbox pool needs to reach them. See [create a campaign](/guides/campaigns/#create-a-campaign). - **Filter**: the contact list's filter bar has a **Segment** pill, and the same scope carries into an export. diff --git a/docs/content/docs/guides/sequences.mdx b/docs/content/docs/guides/sequences.mdx index 1db5d072f..1b56f73b0 100644 --- a/docs/content/docs/guides/sequences.mdx +++ b/docs/content/docs/guides/sequences.mdx @@ -349,6 +349,12 @@ A new email from a contact that answers no thread of yours counts toward the las A reply that lands in the mailbox a send's reply-to named counts the same way, although that mailbox never wrote to the contact. That is what a [shared reply inbox](/guides/mailboxes/#a-shared-reply-inbox) relies on: the reply stops the sequence, runs the reply branch, and is credited to the mailbox that sent the email. +### Other campaigns the contact is in + +By default a reply stops only the campaign it answered. A contact who is also a lead of another campaign keeps receiving that campaign's steps. To stop those too, turn on **Settings > Sending > Replies > Hold the contact in every other campaign**. A human reply then holds the contact in every other campaign they are a lead of that has not completed, for a year. Out-of-office and other automatic replies never trigger it. The campaign they answered is left to its own **Stop on reply** and reply branches. + +The hold works like any other: it shows on each campaign's **Leads** tab with the reason "replied to another campaign", and **Resume now** lifts it. It never shortens or replaces a pause a member set by hand. The setting is `reply_intent.stop_all_campaigns_on_reply` in the [outreach settings](/api/reference/deliverability-ops/). + When an upgrade repairs a reply marker that was provably created by a synchronized Sent message, the contact resumes at the next due step and the campaign reply totals update with it. An upgrade also restores provable human replies that were received by one campaign mailbox but referenced a thread started by another mailbox in the workspace. Warmbly recalculates address verification from the corrected delivery, open, click, reply, and bounce evidence. It leaves ambiguous historical replies untouched. diff --git a/docs/content/docs/guides/unsubscribe.mdx b/docs/content/docs/guides/unsubscribe.mdx index 76158dc31..0e829954a 100644 --- a/docs/content/docs/guides/unsubscribe.mdx +++ b/docs/content/docs/guides/unsubscribe.mdx @@ -37,7 +37,7 @@ To use a different mode for one campaign, set it under **Preferences** on that c ## Good to know -- A reply asking to stop, such as "remove me", puts the sender on the suppression list. A reply saying "not interested" does not. +- A reply asking to stop, such as "remove me" or just "STOP", puts the sender on the suppression list. A reply saying "not interested" does not. - **Contacts > Suppression list** is workspace-wide: no campaign emails an address on it. - A bare domain such as `acme.com` suppresses every address at it. - An unsubscribe link opens a confirmation page, and nothing happens until the recipient presses its button. @@ -121,6 +121,8 @@ Two things to know. Gmail only shows its button for mail it already classes as b Warmbly reads every reply to a campaign email. One that asks to stop, in wording such as "unsubscribe", "remove me", "stop emailing me", "opt out" or "do not contact", puts the sender on the suppression list immediately. Replies in every tagging language are read the same way, whatever languages the workspace tags in: "bitte austragen", "désinscrivez-moi", "denme de baja", "wypiszcie mnie", "отпишите меня" or "配信停止" opt the sender out too. The quoted history is cut in each of those languages as well, so a campaign that carries its own opt-out line in German or Japanese does not opt out everyone who answers it. Only the new text of the reply is read: the quoted history below it carries your own opt-out wording and is ignored. Phrases match on whole words, so "stop by our booth" does not opt anyone out. +A reply that is nothing but the command opts out too: "STOP", "Stop.", "stop please", "Remove", "Unsubscribe", "opt out", and the single-word "Stopp", "Abmelden", "Austragen", "Afmelden" or "Baja", in any case and with any punctuation. A greeting above the word is ignored, and so is what a client or a person adds below it: a sign-off such as "Thanks," or "Best regards, Jane", a name line (with pronouns if you like), a signature block below a "-- " line, or a mobile footer such as "Sent from my iPhone". Any other line below the command means the reply is read as a reply, not an opt-out. The word counts only when it is the whole message: "Stop by tomorrow", "I can't stop thinking about this" and "remove the attachment" opt nobody out, and neither does "Stop" followed by a sentence. + Only a person answering you can opt out this way: someone replying in one of your campaign threads, or one of your contacts writing to you. Everything else that lands in a mailbox is left alone, even when it says "unsubscribe": - a bounce or delivery failure, which returns your own email (and its unsubscribe header) inside the notice diff --git a/internal/app/advanced/incoming_reply_test.go b/internal/app/advanced/incoming_reply_test.go index c1513d10b..6a382b718 100644 --- a/internal/app/advanced/incoming_reply_test.go +++ b/internal/app/advanced/incoming_reply_test.go @@ -20,6 +20,7 @@ type incomingReplyAdvancedRepo struct { intents int intentOff bool holdOff bool + stopAll bool languages []string } @@ -129,6 +130,7 @@ func (r *incomingReplyAdvancedRepo) GetOutreachSettings(context.Context, uuid.UU settings := models.DefaultAdvancedOutreachSettings() settings.ReplyIntent.Enabled = !r.intentOff settings.ReplyIntent.HoldOnOutOfOffice = !r.holdOff + settings.ReplyIntent.StopAllCampaignsOnReply = r.stopAll settings.InboxTagging.Languages = r.languages return &settings, nil } diff --git a/internal/app/advanced/reply_hold_others_test.go b/internal/app/advanced/reply_hold_others_test.go new file mode 100644 index 000000000..84dc2c47c --- /dev/null +++ b/internal/app/advanced/reply_hold_others_test.go @@ -0,0 +1,138 @@ +package advanced + +import ( + "context" + "errors" + "testing" + "time" + + "github.com/google/uuid" + + "github.com/warmbly/warmbly/internal/models" +) + +// elsewhereRecorder records the holds a reply writes across campaigns. +type elsewhereRecorder struct { + *incomingReplyProgressRepo + contacts []uuid.UUID + except []uuid.UUID + until *time.Time + source string + everywhere int + failWith error +} + +func (r *elsewhereRecorder) HoldLeadElsewhere(_ context.Context, contact, except uuid.UUID, until *time.Time, _, source string) ([]uuid.UUID, error) { + r.contacts = append(r.contacts, contact) + r.except = append(r.except, except) + r.until, r.source = until, source + if r.failWith != nil { + return nil, r.failWith + } + return []uuid.UUID{uuid.New()}, nil +} + +func (r *elsewhereRecorder) HoldLeadEverywhere(context.Context, uuid.UUID, *time.Time, string, string) ([]uuid.UUID, error) { + r.everywhere++ + return nil, nil +} + +type replyHoldFixture struct { + svc *service + rec *elsewhereRecorder + accountID uuid.UUID + contactID uuid.UUID + answered uuid.UUID +} + +func newReplyHoldService(stopAll bool) replyHoldFixture { + orgID, accountID, contactID := uuid.New(), uuid.New(), uuid.New() + account := &models.Email{ID: accountID, OrganizationID: &orgID, Email: "owner@acme.test"} + svc, progress := newIncomingReplyService(account, &models.Contact{ID: contactID, Email: "lead@example.com"}, contactID) + progress.advanced.stopAll = stopAll + rec := &elsewhereRecorder{incomingReplyProgressRepo: progress} + svc.campaignProgressRepo = rec + return replyHoldFixture{svc: svc, rec: rec, accountID: accountID, contactID: contactID, + answered: *svc.taskRepo.(incomingReplyTaskRepo).campaign.CampaignID} +} + +func replyFromLead(accountID uuid.UUID, subject, body string, flags []string) *models.EmailMessageStoreData { + return &models.EmailMessageStoreData{ + ID: uuid.New(), EmailID: accountID, Folder: models.FolderInbox, + FromAddr: []string{"Lead "}, + ToAddr: []string{"owner@acme.test"}, + InReplyTo: []string{""}, + Subject: subject, Snippet: body, BodyText: body, + Flags: flags, + } +} + +// With the setting on, a human reply holds the contact in every campaign but +// the one it answered, as a dated automatic hold. +func TestHumanReplyHoldsOtherCampaignsWhenEnabled(t *testing.T) { + f := newReplyHoldService(true) + if xerr := f.svc.ProcessIncomingReply(context.Background(), f.accountID, + replyFromLead(f.accountID, "Re: Hello", "Sounds good, let's talk Tuesday.", nil)); xerr != nil { + t.Fatal(xerr) + } + if len(f.rec.except) != 1 || f.rec.except[0] != f.answered { + t.Fatalf("HoldLeadElsewhere excepts %v, want once for the answered campaign %s", f.rec.except, f.answered) + } + if f.rec.contacts[0] != f.contactID { + t.Fatalf("held contact %s, want the replying contact %s", f.rec.contacts[0], f.contactID) + } + if f.rec.source != models.LeadHoldSourceInboxTagging { + t.Fatalf("hold source = %q, want an automatic hold a member can resume", f.rec.source) + } + if f.rec.until == nil || time.Until(*f.rec.until) < 300*24*time.Hour { + t.Fatalf("hold lifts at %v, want about a year out", f.rec.until) + } +} + +// A hold that cannot be written leaves the reply unprocessed, so it is retried. +func TestFailedReplyHoldLeavesTheReplyToRetry(t *testing.T) { + f := newReplyHoldService(true) + f.rec.failWith = errors.New("connection reset") + if xerr := f.svc.ProcessIncomingReply(context.Background(), f.accountID, + replyFromLead(f.accountID, "Re: Hello", "Sounds good, let's talk Tuesday.", nil)); xerr == nil { + t.Fatal("a failed hold was swallowed") + } + if f.rec.completed != 0 { + t.Fatalf("the reply was marked processed %d times after a failed hold", f.rec.completed) + } +} + +// Off, the default, a reply stops only the campaign it answered. +func TestHumanReplyHoldsNothingElseByDefault(t *testing.T) { + if models.DefaultAdvancedOutreachSettings().ReplyIntent.StopAllCampaignsOnReply { + t.Fatal("stop_all_campaigns_on_reply defaults on, want off") + } + f := newReplyHoldService(false) + if xerr := f.svc.ProcessIncomingReply(context.Background(), f.accountID, + replyFromLead(f.accountID, "Re: Hello", "Sounds good, let's talk Tuesday.", nil)); xerr != nil { + t.Fatal(xerr) + } + if len(f.rec.except) != 0 { + t.Fatalf("HoldLeadElsewhere called %d times with the setting off", len(f.rec.except)) + } +} + +// An auto-reply is not the person answering, so it never holds other +// campaigns; an away message holds the lead through its own path instead. +func TestAutoReplyHoldsNoOtherCampaigns(t *testing.T) { + f := newReplyHoldService(true) + if xerr := f.svc.ProcessIncomingReply(context.Background(), f.accountID, + replyFromLead(f.accountID, "Automatic reply: Hello", "I am out of the office until Monday.", + []string{"Auto-Submitted:auto-replied"})); xerr != nil { + t.Fatal(xerr) + } + if f.rec.replied != 0 || f.rec.classified != 1 { + t.Fatalf("auto-reply not read as automated: replied=%d classified=%d", f.rec.replied, f.rec.classified) + } + if len(f.rec.except) != 0 { + t.Fatalf("an auto-reply held other campaigns %d times", len(f.rec.except)) + } + if f.rec.everywhere != 1 { + t.Fatalf("an out-of-office auto-reply held the lead everywhere %d times, want once", f.rec.everywhere) + } +} diff --git a/internal/app/advanced/service.go b/internal/app/advanced/service.go index 3a576df85..9eeab4f04 100644 --- a/internal/app/advanced/service.go +++ b/internal/app/advanced/service.go @@ -1302,7 +1302,8 @@ func (s *service) ProcessIncomingReply(ctx context.Context, emailAccountID uuid. // when an out-of-office hold lifts, for the notification to name. replyClaimToken := uuid.Nil replyClaimCompleted := false - + // heldOthers is a reply that held the contact in their other campaigns. + heldOthers := false var campaignID *uuid.UUID var sequenceID *uuid.UUID var contactID *uuid.UUID @@ -1504,6 +1505,14 @@ func (s *service) ProcessIncomingReply(ctx context.Context, emailAccountID uuid. } return nil } + // Before the processed marker, so a failed hold leaves the reply to be retried. + if settings.ReplyIntent.Enabled && settings.ReplyIntent.StopAllCampaignsOnReply { + held, err := s.holdOtherCampaigns(ctx, ctID, cID) + if err != nil { + return toErrx(err) + } + heldOthers = held + } } if s.evidence != nil { kind := "replied" @@ -1620,9 +1629,16 @@ func (s *service) ProcessIncomingReply(ctx context.Context, emailAccountID uuid. } actionTaken := "" + if heldOthers { + actionTaken = "held_other_campaigns" + } if settings.ReplyIntent.AutoPauseOnNegative && intent == models.ReplyIntentNegative && campaignID != nil { _ = s.campaignRepo.UpdateStatus(ctx, *campaignID, "paused") - actionTaken = "paused_campaign" + if actionTaken == "" { + actionTaken = "paused_campaign" + } else { + actionTaken += ",paused_campaign" + } } // Reply-based opt-out is what makes the plain "just reply and I'll stop" @@ -1882,6 +1898,19 @@ func ptrTime(t time.Time) *time.Time { return &t } +// holdOtherCampaigns parks a contact who replied in every other campaign they +// are a lead of, as a dated automatic hold a member resumes from the Leads tab. +// Idempotent, so a failure is returned and the unprocessed reply is retried. +func (s *service) holdOtherCampaigns(ctx context.Context, contactID, answered uuid.UUID) (bool, error) { + until := time.Now().UTC().AddDate(0, 0, models.ReplyHoldOtherCampaignsDays) + held, err := s.campaignProgressRepo.HoldLeadElsewhere(ctx, contactID, answered, &until, + "replied to another campaign", models.LeadHoldSourceInboxTagging) + if err != nil { + return false, err + } + return len(held) > 0, nil +} + // verifyEventOwnership refuses a deliverability event whose campaign, contact // or task belongs to another organization. Anything absent is fine; anything // present has to resolve inside the caller's workspace. diff --git a/internal/app/contact/import.go b/internal/app/contact/import.go index df695b4da..f1936f496 100644 --- a/internal/app/contact/import.go +++ b/internal/app/contact/import.go @@ -724,9 +724,10 @@ func (s *contactService) applyImportChunk( continue } if _, xerr := s.contactRepository.BulkUpdate(ctx, plan.userID, plan.orgID, &models.BulkEditContactsData{ - ContactSelection: models.ContactSelection{Contacts: group.contactIDs}, - AddCampaigns: group.campaigns, - AddCategories: group.categories, + ContactSelection: models.ContactSelection{Contacts: group.contactIDs}, + AddCampaigns: group.campaigns, + SkipActiveInOtherCampaigns: plan.opts.SkipActiveInOtherCampaigns, + AddCategories: group.categories, }); xerr != nil { for _, i := range group.members { linkErr[i] = xerr.Message diff --git a/internal/app/replyclassify/lexicon.go b/internal/app/replyclassify/lexicon.go index f5e648536..89b594e58 100644 --- a/internal/app/replyclassify/lexicon.go +++ b/internal/app/replyclassify/lexicon.go @@ -20,13 +20,14 @@ import ( // 4. Clear rejection phrases => negative. func classifyLexicon(in Input) (Result, bool) { // The subject ends its own sentence, so a negator there never reaches the body. - text := quoteFolder.Replace(strings.ToLower(strings.TrimSpace(in.Subject + ".\n" + StripQuoted(in.BodyText, slices.Concat(in.Languages, optOutLanguages)...)))) + body := StripQuoted(in.BodyText, slices.Concat(in.Languages, optOutLanguages)...) + text := quoteFolder.Replace(strings.ToLower(strings.TrimSpace(in.Subject + ".\n" + body))) if text == "" { return Result{}, false } // 1. Compliance / opt-out (highest priority). - if matchesOptOut(text) { + if matchesOptOut(text) || isOptOutCommand(body) { return Result{Class: ClassUnsubscribe, Confidence: 0.9, Source: SourceLexicon}, true } @@ -66,7 +67,8 @@ func classifyLexicon(in Input) (Result, bool) { // unsubscribeKeywords are explicit opt-out requests. Compliance-first: any of // these short-circuits to "unsubscribe" before sentiment is considered. Each -// is matched on word boundaries, so "stop" alone never fires on "stop by". +// is matched on word boundaries, so "stop" alone never fires on "stop by"; a +// reply that is only the word is isOptOutCommand's. var unsubscribeKeywords = []string{ "unsubscribe", "opt out", @@ -229,7 +231,7 @@ func matchesOptOut(lowerText string) bool { // included. It is not a decision: it finds the message an earlier reading of // the whole body acted on. func MentionsOptOut(subject, body string) bool { - return matchesOptOut(strings.ToLower(subject + "\n" + body)) + return matchesOptOut(strings.ToLower(subject+"\n"+body)) || isOptOutCommand(StripQuoted(body, optOutLanguages...)) } // IsOptOut reports whether a reply, read without its quoted history, asks to @@ -237,11 +239,12 @@ func MentionsOptOut(subject, body string) bool { // the quoted original carries the sender's own opt-out line, so matching the // whole body would opt out everyone who replies. func IsOptOut(subject, body string) bool { - text := strings.ToLower(strings.TrimSpace(subject + "\n" + StripQuoted(body, optOutLanguages...))) + reply := StripQuoted(body, optOutLanguages...) + text := strings.ToLower(strings.TrimSpace(subject + "\n" + reply)) if text == "" { return false } - return matchesOptOut(text) + return matchesOptOut(text) || isOptOutCommand(reply) } // quoteMarker begins the quoted history a mail client appends to a reply. diff --git a/internal/app/replyclassify/optout_command.go b/internal/app/replyclassify/optout_command.go new file mode 100644 index 000000000..4d6a896ee --- /dev/null +++ b/internal/app/replyclassify/optout_command.go @@ -0,0 +1,167 @@ +package replyclassify + +import ( + "regexp" + "strings" + "unicode" + "unicode/utf8" +) + +// The command words stand alone only when the reply is nothing else, which is +// why "stop" is not in unsubscribeKeywords. Besides English, the one word a +// German, Dutch, Nordic or Spanish reader types for the same thing. +const ( + commandWords = `(?:stop|stopp|remove|remove me|unsubscribe|unsubscribe me|opt out|optout|abmelden|austragen|afmelden|baja)` + commandPolite = `(?:please|pls|plz|now|thanks|thank you|thx|ty|bitte|danke|graag|alstublieft|aub|por favor|gracias|svp|merci|tack)` +) + +// optOutCommand matches one line, read through commandText, that is only an +// opt-out command and the courtesy around it: "STOP!!", "Remove.", "please stop". +var optOutCommand = regexp.MustCompile(`^(?:` + commandPolite + ` )*` + commandWords + `(?: (?:` + commandWords + `|` + commandPolite + `))*$`) + +// commandGreetings and commandSignOffs open a salutation or sign-off line, +// read through commandText; only a name may follow them on the line. +var ( + commandGreetings = wordSet("hi", "hello", "hey", "dear", "hallo", "hola", "bonjour", "hej", "good morning", "good afternoon", "guten tag", "moin") + commandSignOffs = wordSet("thanks", "thank you", "thx", "ty", "many thanks", "cheers", "regards", "best", "best regards", "kind regards", + "warm regards", "sincerely", "br", "kr", "mfg", "lg", "vg", "viele gruesse", "beste gruesse", "freundliche gruesse", + "mit freundlichen gruessen", "gruss", "gruesse", "saludos", "cordialement", "bien a vous", "met vriendelijke groet", + "groeten", "mvh", "hilsen", "med vanlig halsning") +) + +// commandFooters are whole mobile-client footer lines, read through commandText. +var commandFooters = wordSet( + "sent from my iphone", "sent from my ipad", "sent from my android", "sent from my android phone", "sent from my mobile", + "sent from my mobile device", "sent from my phone", "sent from my samsung galaxy smartphone", "sent from my blackberry", + "sent from mail for windows", "sent from mail for windows 10", "sent from outlook for ios", "sent from outlook for android", + "get outlook for ios", "get outlook for android", "sent from yahoo mail for iphone", "sent from yahoo mail on android", + "sent from gmail mobile", "von meinem iphone gesendet", "von meinem ipad gesendet", "von meinem smartphone gesendet", + "enviado desde mi iphone", "enviado desde mi ipad", "envoye de mon iphone", "envoye de mon ipad", + "verzonden vanaf mijn iphone", "verstuurd vanaf mijn iphone", "skickat fran min iphone", "sendt fra min iphone", +) + +// proseWords are words no name carries, even capitalized. +var proseWords = wordSet("i", "me", "my", "we", "us", "our", "you", "your", "it", "is", "are", "am", "was", "be", "but", "not", + "do", "would", "could", "can", "will", "if", "so", "just", "actually", "love", "like", "want", "let", "lets", + "interested", "please", "call", "send", "tell", "talk", "great", "sounds", "meeting", "demo", "price", "pricing", + "yes", "no", "sure", "ok", "okay", "maybe", "later", "today", "tomorrow", "monday", "tuesday", "wednesday", "thursday", + "friday", "saturday", "sunday", "noon", "next", "week", "month", "year", "soon", "now", "asap", "time", "here", "there", + "the", "a", "an", "at", "of", "and", "for", "to", "in", "on", "with", + "ich", "wir", "sie", "ist", "aber", "nicht") + +// commandPronouns is a pronoun note after a name: "(she/her)". +var commandPronouns = regexp.MustCompile(`(?i)^\((?:she|he|they|ze|xe)/(?:her|him|them|hers|his|theirs|zir|xem)(?:/[a-z]+)?\)$`) + +// commandMaxLines bounds the lines a command may carry above a delimiter. +const commandMaxLines = 4 + +func wordSet(words ...string) map[string]bool { + out := make(map[string]bool, len(words)) + for _, w := range words { + out[w] = true + } + return out +} + +// commandText folds a line for the command patterns: case, accents and +// punctuation are dropped, words single-spaced. +func commandText(line string) string { + folded := []rune(foldOptOut(line)) + for i, r := range folded { + if !unicode.IsLetter(r) && !unicode.IsNumber(r) { + folded[i] = ' ' + } + } + return strings.Join(strings.Fields(string(folded)), " ") +} + +// isOptOutCommand reports a reply, quoted history already cut, that says +// nothing but an opt-out command: its first line after any greeting is the +// command, and the lines below pass signatureOnly. +func isOptOutCommand(body string) bool { + lines := strings.Split(strings.ReplaceAll(body, "\r\n", "\n"), "\n") + for i, raw := range lines { + switch { + case optOutCommand.MatchString(commandText(raw)): + return signatureOnly(lines[i+1:]) + case strings.TrimSpace(raw) == "" || ledByName(raw, commandGreetings): + continue + default: + return false + } + } + return false +} + +// signatureOnly is a closed whitelist of what may follow the command: blank +// lines, whole mobile footers, sign-off lines, name lines, and a "--" +// delimiter, below which nothing is read. Any other line means it is not an +// opt-out. +func signatureOnly(lines []string) bool { + n := 0 + for _, raw := range lines { + ln := strings.TrimSpace(raw) + switch { + case ln == "" || commandFooters[commandText(ln)]: + continue + case ln == "--": + return true + } + if n++; n > commandMaxLines { + return false + } + if !ledByName(ln, commandSignOffs) && !nameLine(strings.Fields(ln)) { + return false + } + } + return true +} + +// ledByName reports a line that is one of the phrases, optionally followed by +// a name: "Thanks,", "Best regards, Jane", "Hi Jane,". +func ledByName(raw string, phrases map[string]bool) bool { + tokens := strings.Fields(raw) + for k := min(len(tokens), 4); k > 0; k-- { + if phrases[commandText(strings.Join(tokens[:k], " "))] { + return k == len(tokens) || nameLine(tokens[k:]) + } + } + return false +} + +// nameLine is one to three capitalized name words or initials, optionally +// followed by a pronoun note; a trailing comma or period is allowed. +func nameLine(tokens []string) bool { + if len(tokens) > 0 && commandPronouns.MatchString(strings.TrimRight(tokens[len(tokens)-1], ",.")) { + tokens = tokens[:len(tokens)-1] + } + if len(tokens) == 0 || len(tokens) > 3 { + return false + } + for i, t := range tokens { + if i == len(tokens)-1 { + t = strings.TrimRight(t, ",.") + } + if !nameWord(t) { + return false + } + } + return true +} + +// nameWord is a capitalized letters-only word, or an initial such as "J.". +func nameWord(t string) bool { + if word, ok := strings.CutSuffix(t, "."); ok && utf8.RuneCountInString(word) == 1 { + t = word + } + first, _ := utf8.DecodeRuneInString(t) + if t == "" || !unicode.IsUpper(first) || proseWords[strings.ToLower(t)] { + return false + } + for _, r := range t { + if !unicode.IsLetter(r) && !unicode.Is(unicode.Mn, r) { + return false + } + } + return true +} diff --git a/internal/app/replyclassify/optout_command_test.go b/internal/app/replyclassify/optout_command_test.go new file mode 100644 index 000000000..489ac8f47 --- /dev/null +++ b/internal/app/replyclassify/optout_command_test.go @@ -0,0 +1,100 @@ +package replyclassify + +import "testing" + +// A reply that is only an opt-out command opts out; the same word inside a +// sentence does not. +func TestIsOptOutCommandReply(t *testing.T) { + optOut := map[string]string{ + "bare": "STOP", + "lower": "stop", + "period": "Stop.", + "bangs": "STOP!!", + "please after": "stop please", + "please before": "Please stop.", + "remove": "Remove", + "remove me": "remove me!", + "unsubscribe": "UNSUBSCRIBE", + "opt out": "Opt out", + "optout": "optout", + "german": "Stopp bitte", + "spanish": "Baja", + "iphone footer": "Stop\n\nSent from my iPhone", + "outlook footer": "STOP\r\n\r\nGet Outlook for Android", + "sign-off": "Stop.\n\nThanks,\nJohn", + "signature": "Stop\n\nJohn Smith\n--\nVP Sales, Acme Inc\n+1 555 123 4567", + "delimiter": "stop\n-- \nJohn Smith\nThis line is a long company tagline that goes on and on about the company", + "greeting": "Hi Jane,\n\nStop.\n\nBest,\nJohn", + "quoted history": "STOP\n\nOn Tue, Sep 1, 2026 Jane wrote:\n> Do you have ten minutes next week?", + "crlf": "Stop\r\n", + "leading blank": "\n\n stop \n", + "thanks after": "Remove, thanks", + "repeated": "stop stop stop", + "disclaimer foot": "Stop\n\n-- \nThis email and any attachments are confidential and intended solely for the addressee.", + "full signature": "STOP\n\nThanks,\nJohn Smith\nSent from my iPhone", + "dash delimiter": "Stop.\n--\nJane Doe | Acme", + "title and phone": "Stop\n\nBest regards,\nJane Doe\n--\nHead of Sales, Acme Inc.\n+49 (30) 1234-567\njane@acme.example", + "german footer": "Stopp\n\nVon meinem iPhone gesendet", + "notice": "Remove\n\n--\nCONFIDENTIALITY NOTICE: The contents of this message are private.", + "pronouns": "STOP\nJane Doe (she/her)", + "pronouns comma": "STOP\nJane Doe (she/her),", + "sign-off name": "Stop\n\nBest regards, J. Smith", + } + for name, body := range optOut { + if !IsOptOut("Re: Quick question", body) { + t.Errorf("%s: IsOptOut(%q) = false, want true", name, body) + } + if !MentionsOptOut("Re: Quick question", body) { + t.Errorf("%s: MentionsOptOut(%q) = false, want true", name, body) + } + if r, ok := classifyLexicon(Input{BodyText: body}); !ok || r.Class != ClassUnsubscribe { + t.Errorf("%s: classifyLexicon(%q) = %+v, want unsubscribe", name, body, r) + } + } + + notOptOut := map[string]string{ + "booth": "Stop by our booth next week!", + "stop by": "stop by tomorrow", + "cant stop": "I can't stop thinking about this.", + "attachment": "remove the attachment", + "wrapped sentence": "Stop\nby our booth next week", + "prose under": "Stop\n\nActually I'd love a demo", + "question under": "Stop.\n\nCan we talk next week?", + "stop sign": "Stop sign", + "trailing word": "Thanks for reaching out. Stop", + "stop the press": "Stop the press, this is great", + "empty": "", + "greeting only": "Hi Jane,", + "bus stop": "bus stop", + "long text below": "Stop\n\nWe looked at this last quarter and decided to build it in house instead", + "thanks lets talk": "Stop\nThanks, let's talk.", + "great deal": "Stop\nGreat deal, let's talk.", + "prose after sign": "Stop\nThanks,\nActually I'd love a demo", + "this email prose": "Stop\nThis email is great, call me", + "confidential": "Stop\nConfidential: we are evaluating vendors now", + "disclaimer word": "Stop\nDisclaimer I want a demo", + "sign-off prose": "Stop\nBest time to call is Monday", + "greeting prose": "Hey can we talk\nStop", + "name then prose": "Stop\n\nJohn\nwhen are you free", + "title no delim": "Stop\n\nJohn Smith\nVP Sales, Acme Inc", + "disclaimer bare": "Stop\n\nThis email and any attachments are confidential and intended solely for the addressee.", + "disclaimer prose": "Stop\nThis email is confidential, but please call me", + "dash prose": "Stop\n-- actually, I'd like a demo", + "title-cased": "Stop\nMeeting at Noon", + "too many lines": "Stop\nJohn\nJane\nJim\nJoe\nJack", + "footer prefix": "Stop\nSent from my iPhone because I want a demo", + "after footer": "Stop\nSent from my iPhone\nActually I'd like a demo", + "next week": "Stop\nNext Week", + } + for name, body := range notOptOut { + if IsOptOut("Re: Stop", body) { + t.Errorf("%s: IsOptOut(%q) = true, want false", name, body) + } + if MentionsOptOut("Re: Stop", body) { + t.Errorf("%s: MentionsOptOut(%q) = true, want false", name, body) + } + if r, ok := classifyLexicon(Input{BodyText: body}); ok && r.Class == ClassUnsubscribe { + t.Errorf("%s: classifyLexicon(%q) = %+v, want no unsubscribe", name, body, r) + } + } +} diff --git a/internal/app/segment/service.go b/internal/app/segment/service.go index a64e28911..e30fb2aa7 100644 --- a/internal/app/segment/service.go +++ b/internal/app/segment/service.go @@ -344,7 +344,7 @@ func (s *service) AddToCampaign(ctx context.Context, orgID uuid.UUID, actor stri if err != nil { return nil, errx.New(errx.BadRequest, "invalid campaign id") } - res, xerr := s.repo.AddToCampaign(ctx, orgID, actor, id, campaignID) + res, xerr := s.repo.AddToCampaign(ctx, orgID, actor, id, campaignID, in.SkipActiveInOtherCampaigns) if xerr != nil { return nil, xerr } diff --git a/internal/models/advanced_outreach.go b/internal/models/advanced_outreach.go index a21590756..3841a796a 100644 --- a/internal/models/advanced_outreach.go +++ b/internal/models/advanced_outreach.go @@ -59,8 +59,16 @@ type ReplyIntentSettings struct { // OutOfOfficeHoldDays is the hold applied when the auto-reply carries no // return date we can read. Clamped to OOOHoldDaysMin..OOOHoldDaysMax. OutOfOfficeHoldDays int `json:"out_of_office_hold_days"` + // StopAllCampaignsOnReply holds a contact in every other campaign they are + // a lead of when they reply to one, for ReplyHoldOtherCampaignsDays. + // Off by default: a reply stops only the campaign it answered. + StopAllCampaignsOnReply bool `json:"stop_all_campaigns_on_reply"` } +// ReplyHoldOtherCampaignsDays is how long a reply holds the contact's other +// campaigns. Finite like every automatic hold; a member resumes it sooner. +const ReplyHoldOtherCampaignsDays = 365 + // InboxTaggingSettings are the actions a workspace lets a classified reply // take. The three reversible ones default on; the suppression defaults off, // because it is the one the system cannot undo. @@ -1148,6 +1156,7 @@ func DefaultAdvancedOutreachSettings() AdvancedOutreachSettings { AutoSuppressOnUnsubWord: true, HoldOnOutOfOffice: true, OutOfOfficeHoldDays: OOOHoldDaysDefault, + StopAllCampaignsOnReply: false, }, InboxTagging: InboxTaggingSettings{ // The reversible actions are on from the start, so a classified diff --git a/internal/models/contact.go b/internal/models/contact.go index 546f97e9a..6a3b0447f 100644 --- a/internal/models/contact.go +++ b/internal/models/contact.go @@ -933,6 +933,10 @@ type BulkEditContactsData struct { Fields []BulkEditContactsField `json:"fields"` Subscribe *bool `json:"subscribe"` + // SkipActiveInOtherCampaigns leaves out of add_campaigns a contact whose + // sequence is still running in another campaign. + SkipActiveInOtherCampaigns bool `json:"skip_active_in_other_campaigns,omitempty"` + // SkipRows suppresses the hydrated rows in the response. Set by the // handler for a filter-shaped selection, which can name far more contacts // than are worth serializing back. Never part of the request body. diff --git a/internal/models/contact_import.go b/internal/models/contact_import.go index 8a97587f6..b1f948e16 100644 --- a/internal/models/contact_import.go +++ b/internal/models/contact_import.go @@ -138,6 +138,10 @@ type ContactImportCommit struct { HasHeader bool `json:"has_header"` CategoryIDs []string `json:"category_ids,omitempty"` CampaignIDs []string `json:"campaign_ids,omitempty"` + // SkipActiveInOtherCampaigns keeps an existing contact out of + // CampaignIDs while their sequence is still running in another campaign. + // The row itself is imported as usual. + SkipActiveInOtherCampaigns bool `json:"skip_active_in_other_campaigns,omitempty"` // SegmentIDs pins every imported row into these segments as a manual // include override, the same write the "Add to segment" bulk action does. SegmentIDs []string `json:"segment_ids,omitempty"` diff --git a/internal/models/segment.go b/internal/models/segment.go index 8d85de1d6..af8d1957c 100644 --- a/internal/models/segment.go +++ b/internal/models/segment.go @@ -248,6 +248,9 @@ type SegmentMembersWrite struct { // SegmentAddToCampaign enrols the segment's current members as leads. type SegmentAddToCampaign struct { CampaignID string `json:"campaign_id"` + // SkipActiveInOtherCampaigns leaves out a member whose sequence is still + // running in another campaign. + SkipActiveInOtherCampaigns bool `json:"skip_active_in_other_campaigns,omitempty"` } // SegmentAddToCampaignResult reports how many leads were actually new. diff --git a/internal/repository/active_elsewhere_live_test.go b/internal/repository/active_elsewhere_live_test.go new file mode 100644 index 000000000..ce971903b --- /dev/null +++ b/internal/repository/active_elsewhere_live_test.go @@ -0,0 +1,148 @@ +package repository + +import ( + "context" + "testing" + "time" + + "github.com/warmbly/warmbly/internal/models" +) + +// A contact whose sequence is still running in a launched campaign is left out +// of another campaign when the caller asks; finished elsewhere, it joins. +func TestLiveBulkAddSkipsContactsActiveInAnotherCampaign(t *testing.T) { + handle, pool := liveContactDB(t) + f := newSharedOrgFixture(t, pool) + repo := NewContactRepostory(handle) + ctx := context.Background() + + if _, err := pool.Exec(ctx, `UPDATE campaigns SET status = 'active' WHERE id = $1`, f.other); err != nil { + t.Fatal(err) + } + if _, err := pool.Exec(ctx, `INSERT INTO campaign_leads (contact_id, campaign_id) VALUES ($1, $2)`, f.contact, f.other); err != nil { + t.Fatal(err) + } + isLead := func() bool { + var n int + if err := pool.QueryRow(ctx, `SELECT COUNT(*) FROM campaign_leads WHERE campaign_id = $1 AND contact_id = $2`, f.campaign, f.contact).Scan(&n); err != nil { + t.Fatal(err) + } + return n == 1 + } + add := func(skip bool) { + t.Helper() + if _, xerr := repo.BulkUpdate(ctx, f.owner.String(), f.org, &models.BulkEditContactsData{ + ContactSelection: models.ContactSelection{Contacts: []string{f.contact.String()}}, + AddCampaigns: []string{f.campaign.String()}, + SkipActiveInOtherCampaigns: skip, + }); xerr != nil { + t.Fatalf("bulk add: %v", xerr) + } + } + + add(true) + if isLead() { + t.Fatal("a contact queued in an active campaign was enrolled with the skip on") + } + + // A draft campaign is not running yet, so it does not hold the contact back. + if _, err := pool.Exec(ctx, `UPDATE campaigns SET status = 'draft' WHERE id = $1`, f.other); err != nil { + t.Fatal(err) + } + add(true) + if !isLead() { + t.Fatal("a contact only in a draft campaign was skipped") + } +} + +// A reply holds the contact everywhere except the campaign it answered. +func TestLiveHoldLeadElsewhereLeavesTheAnsweredCampaign(t *testing.T) { + _, pool := liveContactDB(t) + f := newSharedOrgFixture(t, pool) + ctx := context.Background() + if _, err := pool.Exec(ctx, `INSERT INTO campaign_leads (contact_id, campaign_id) VALUES ($1, $2), ($1, $3)`, f.contact, f.campaign, f.other); err != nil { + t.Fatal(err) + } + until := time.Now().Add(365 * 24 * time.Hour) + held, err := NewCampaignProgressRepository(pool).HoldLeadElsewhere(ctx, f.contact, f.campaign, &until, + "replied to another campaign", models.LeadHoldSourceInboxTagging) + if err != nil { + t.Fatal(err) + } + if len(held) != 1 || held[0] != f.other { + t.Fatalf("held %v, want only %s", held, f.other) + } + var answeredHeld bool + if err := pool.QueryRow(ctx, `SELECT paused_at IS NOT NULL FROM campaign_leads WHERE campaign_id = $1 AND contact_id = $2`, f.campaign, f.contact).Scan(&answeredHeld); err != nil { + t.Fatal(err) + } + if answeredHeld { + t.Fatal("the answered campaign was held too") + } +} + +// A segment enrol with the skip on leaves out a member queued in another +// active campaign, and a hand-picked add with the skip on does not claim that +// member's segment lead as manual. +func TestLiveSegmentAndClaimHonourTheSkip(t *testing.T) { + handle, pool := liveContactDB(t) + f := newSharedOrgFixture(t, pool) + ctx := context.Background() + t.Cleanup(func() { + if _, err := pool.Exec(context.Background(), `DELETE FROM segments WHERE organization_id = $1`, f.org); err != nil { + t.Errorf("cleanup segments: %v", err) + } + }) + exec := func(sql string, args ...any) { + t.Helper() + if _, err := pool.Exec(ctx, sql, args...); err != nil { + t.Fatal(err) + } + } + exec(`UPDATE campaigns SET status = 'active' WHERE id = $1`, f.other) + exec(`INSERT INTO campaign_leads (contact_id, campaign_id) VALUES ($1, $2)`, f.contact, f.other) + + segments := NewSegmentRepository(handle) + seg, xerr := segments.Create(ctx, f.org, &f.owner, &models.Segment{ + Name: "Pied Piper", Color: "#0284c7", Match: models.SegmentMatchAll, + Conditions: []models.SegmentCondition{{Field: "company", Operator: "equals", Value: "pied piper"}}, + }) + if xerr != nil { + t.Fatalf("create segment: %v", xerr) + } + out, xerr := segments.AddToCampaign(ctx, f.org, f.owner.String(), seg.ID, f.campaign, true) + if xerr != nil || out.Members != 1 || out.Added != 0 { + t.Fatalf("skip on = %+v, %v; want the one member left out", out, xerr) + } + out, xerr = segments.AddToCampaign(ctx, f.org, f.owner.String(), seg.ID, f.campaign, false) + if xerr != nil || out.Added != 1 { + t.Fatalf("skip off = %+v, %v; want the member added", out, xerr) + } + + exec(`UPDATE campaign_leads SET source = 'segment' WHERE campaign_id = $1 AND contact_id = $2`, f.campaign, f.contact) + source := func() string { + var s string + if err := pool.QueryRow(ctx, `SELECT source FROM campaign_leads WHERE campaign_id = $1 AND contact_id = $2`, f.campaign, f.contact).Scan(&s); err != nil { + t.Fatal(err) + } + return s + } + add := func(skip bool) { + t.Helper() + if _, xerr := NewContactRepostory(handle).BulkUpdate(ctx, f.owner.String(), f.org, &models.BulkEditContactsData{ + ContactSelection: models.ContactSelection{Contacts: []string{f.contact.String()}}, + AddCampaigns: []string{f.campaign.String()}, + SkipActiveInOtherCampaigns: skip, + }); xerr != nil { + t.Fatalf("bulk add: %v", xerr) + } + } + add(true) + if got := source(); got != "segment" { + t.Fatalf("source = %q after a skipped add, want the segment lead left as it was", got) + } + add(false) + if got := source(); got != "manual" { + t.Fatalf("source = %q after a plain add, want manual", got) + } +} diff --git a/internal/repository/pg_campaign_progress.go b/internal/repository/pg_campaign_progress.go index 875dde733..7a6e07d9e 100644 --- a/internal/repository/pg_campaign_progress.go +++ b/internal/repository/pg_campaign_progress.go @@ -332,6 +332,8 @@ type CampaignProgressRepository interface { // in two live campaigns used to have one of them held while the other kept // writing to the empty desk (issue #518). HoldLeadEverywhere(ctx context.Context, contactID uuid.UUID, until *time.Time, reason, source string) ([]uuid.UUID, error) + // HoldLeadElsewhere is HoldLeadEverywhere except in exceptCampaignID. + HoldLeadElsewhere(ctx context.Context, contactID, exceptCampaignID uuid.UUID, until *time.Time, reason, source string) ([]uuid.UUID, error) // ResumeLead lifts the hold now and reports whether there was one to lift. // The held time is dropped rather than carried, because "resume now" means // now: the step's remaining wait is only preserved when a hold ends on its @@ -2547,6 +2549,12 @@ func (r *campaignProgressRepository) HoldLead(ctx context.Context, campaignID, c // desk. Every automatic hold carries an end, so one written into a campaign // that never launches expires on its own. func (r *campaignProgressRepository) HoldLeadEverywhere(ctx context.Context, contactID uuid.UUID, until *time.Time, reason, source string) ([]uuid.UUID, error) { + return r.HoldLeadElsewhere(ctx, contactID, uuid.Nil, until, reason, source) +} + +// HoldLeadElsewhere is HoldLeadEverywhere leaving out one campaign, the one a +// reply answered, whose own flow decides what that reply does. +func (r *campaignProgressRepository) HoldLeadElsewhere(ctx context.Context, contactID, exceptCampaignID uuid.UUID, until *time.Time, reason, source string) ([]uuid.UUID, error) { guard := "" if automaticHoldSource(source) { guard = automaticHoldGuard("cl", "$2") @@ -2562,9 +2570,10 @@ func (r *campaignProgressRepository) HoldLeadEverywhere(ctx context.Context, con FROM campaigns c WHERE cl.contact_id = $1 AND c.id = cl.campaign_id + AND cl.campaign_id <> $5 AND c.status <> 'completed'`+guard+` RETURNING cl.campaign_id`, - contactID, until, reason, source) + contactID, until, reason, source, exceptCampaignID) if err != nil { return nil, err } diff --git a/internal/repository/pg_contact.go b/internal/repository/pg_contact.go index b4cc0fa20..d30cd63ba 100644 --- a/internal/repository/pg_contact.go +++ b/internal/repository/pg_contact.go @@ -2221,6 +2221,18 @@ func leadStatusClause(status, cp string) string { } } +// activeElsewhereClause matches a contact `c` whose sequence is still in +// flight (queued, processing or held) in a launched, unfinished campaign other +// than campaignCol, by the same derivation the Leads view shows. +func activeElsewhereClause(campaignCol string) string { + const cp = "ol.campaign_id" + return `EXISTS (SELECT 1 FROM campaign_leads ol JOIN campaigns oc ON oc.id = ol.campaign_id + WHERE ol.contact_id = c.id AND oc.organization_id = c.organization_id + AND ol.campaign_id <> ` + campaignCol + ` AND oc.status NOT IN ('draft', 'completed') + AND (` + leadStatusClause(models.LeadStatusPending, cp) + ` OR ` + leadStatusClause(models.LeadStatusActive, cp) + + ` OR ` + leadStatusClause(models.LeadStatusPaused, cp) + `))` +} + // leadHeldClause is the "this lead's flow is parked right now" predicate for a // contact-scoped query, built on the one definition of live in liveHold. The // Leads-view status filter, the scope-chip counts and the row-level status all @@ -2925,6 +2937,11 @@ func (r *contactRepository) BulkUpdate(ctx context.Context, userID string, orgID } if len(data.AddCampaigns) > 0 { + // A skipped pair is left exactly as it was, its removal record included. + skip := "" + if data.SkipActiveInOtherCampaigns { + skip = " AND NOT " + activeElsewhereClause("cam.id") + } // A manual add clears the removal record: the user changed their mind, // so linked segments may manage this pair again. if xerr := link(`WITH cleared AS ( @@ -2935,7 +2952,7 @@ func (r *contactRepository) BulkUpdate(ctx context.Context, userID string, orgID AND c.organization_id = $1 AND cam.organization_id = $1 AND r.contact_id = ANY($2) - AND r.campaign_id = ANY($3::uuid[]) + AND r.campaign_id = ANY($3::uuid[])`+skip+` ) INSERT INTO campaign_leads (contact_id, campaign_id) SELECT c.id, cam.id @@ -2944,7 +2961,7 @@ func (r *contactRepository) BulkUpdate(ctx context.Context, userID string, orgID WHERE c.organization_id = $1 AND c.id = ANY($2) AND cam.id = ANY($3::uuid[]) - AND cam.organization_id = $1 + AND cam.organization_id = $1`+skip+` ON CONFLICT DO NOTHING RETURNING contact_id, campaign_id`, models.ActivityCampaignAdded, logCampaignLinks, orgID, data.Contacts, data.AddCampaigns); xerr != nil { @@ -2952,7 +2969,12 @@ func (r *contactRepository) BulkUpdate(ctx context.Context, userID string, orgID } // Claims leads a linked segment had enrolled, so detaching that // segment later leaves hand-picked leads alone. - if _, err := tx.Exec(ctx, claimLeadsManualSQL, data.AddCampaigns, data.Contacts, orgID); err != nil { + claim := claimLeadsManualSQL + if data.SkipActiveInOtherCampaigns { + claim += ` AND NOT EXISTS (SELECT 1 FROM contacts c WHERE c.id = campaign_leads.contact_id AND ` + + activeElsewhereClause("campaign_leads.campaign_id") + `)` + } + if _, err := tx.Exec(ctx, claim, data.AddCampaigns, data.Contacts, orgID); err != nil { db.CaptureError(err, "", nil, "campaign_leads claim") return nil, errx.InternalError() } diff --git a/internal/repository/pg_segment.go b/internal/repository/pg_segment.go index b062eeaea..090d1d2a5 100644 --- a/internal/repository/pg_segment.go +++ b/internal/repository/pg_segment.go @@ -32,7 +32,7 @@ type SegmentRepository interface { // MemberModes reports the manual override of each listed contact. MemberModes(ctx context.Context, segmentID uuid.UUID, contactIDs []uuid.UUID) (map[uuid.UUID]models.SegmentMemberMode, *errx.Error) // AddToCampaign enrols every current member of the segment as a lead. - AddToCampaign(ctx context.Context, orgID uuid.UUID, actor string, segmentID, campaignID uuid.UUID) (*models.SegmentAddToCampaignResult, *errx.Error) + AddToCampaign(ctx context.Context, orgID uuid.UUID, actor string, segmentID, campaignID uuid.UUID, skipActiveElsewhere bool) (*models.SegmentAddToCampaignResult, *errx.Error) // ListForCampaign lists the segments linked to a campaign. ListForCampaign(ctx context.Context, orgID, campaignID uuid.UUID) ([]models.CampaignSegmentLink, *errx.Error) // SetForCampaign replaces the campaign's linked segments, withdrawing the @@ -369,7 +369,7 @@ func (r *segmentRepository) MemberModes(ctx context.Context, segmentID uuid.UUID return out, nil } -func (r *segmentRepository) AddToCampaign(ctx context.Context, orgID uuid.UUID, actor string, segmentID, campaignID uuid.UUID) (*models.SegmentAddToCampaignResult, *errx.Error) { +func (r *segmentRepository) AddToCampaign(ctx context.Context, orgID uuid.UUID, actor string, segmentID, campaignID uuid.UUID, skipActiveElsewhere bool) (*models.SegmentAddToCampaignResult, *errx.Error) { tx, err := r.DB.Begin(ctx) if err != nil { db.CaptureError(err, "", nil, "begin") @@ -403,7 +403,7 @@ func (r *segmentRepository) AddToCampaign(ctx context.Context, orgID uuid.UUID, return nil, errx.InternalError() } - links, err := insertSegmentLeads(ctx, tx, orgID, actorID(actor), clause, args, campaignID, leadSourceManual) + links, err := insertSegmentLeads(ctx, tx, orgID, actorID(actor), clause, args, campaignID, leadSourceManual, skipActiveElsewhere) if err != nil { db.CaptureError(err, "segment enrol", nil, "query") return nil, errx.InternalError() @@ -443,9 +443,13 @@ const claimLeadsManualSQL = `UPDATE campaign_leads SET source = 'manual' // insertSegmentLeads enrols every contact matching the precompiled segment // clause as a lead, logging a campaign_added activity for each row that was // actually new. The campaign is bound after the clause's own parameters. -func insertSegmentLeads(ctx context.Context, tx pgx.Tx, orgID uuid.UUID, actor *uuid.UUID, clause string, args []any, campaignID uuid.UUID, src leadSource) ([]contactLink, error) { +// skipActiveElsewhere leaves a member running in another campaign untouched. +func insertSegmentLeads(ctx context.Context, tx pgx.Tx, orgID uuid.UUID, actor *uuid.UUID, clause string, args []any, campaignID uuid.UUID, src leadSource, skipActiveElsewhere bool) ([]contactLink, error) { args = append(args, campaignID) cp := len(args) + if skipActiveElsewhere { + clause = "(" + clause + ") AND NOT " + activeElsewhereClause(fmt.Sprintf("$%d", cp)) + } // members is the clause as a contact-id subquery, reused by the three // statements below so they cannot drift apart. members := fmt.Sprintf(`SELECT c.id FROM contacts c WHERE c.organization_id = $1 AND (%s)`, clause) @@ -915,7 +919,7 @@ func syncCampaignSegmentsTx(ctx context.Context, tx pgx.Tx, orgID, campaignID uu if clause == "FALSE" { continue } - links, lerr := insertSegmentLeads(ctx, tx, orgID, nil, clause, args, campaignID, leadSourceSegment) + links, lerr := insertSegmentLeads(ctx, tx, orgID, nil, clause, args, campaignID, leadSourceSegment, false) if lerr != nil { db.CaptureError(lerr, "segment enrol", nil, "query") return 0, errx.InternalError() diff --git a/internal/repository/segment_audience_live_test.go b/internal/repository/segment_audience_live_test.go index 083ccc16f..216ccb262 100644 --- a/internal/repository/segment_audience_live_test.go +++ b/internal/repository/segment_audience_live_test.go @@ -315,7 +315,7 @@ func TestLiveCampaignAudienceAReimportClaimsASegmentLead(t *testing.T) { // The one-shot "add to campaign" adds nobody here and changes nobody's // provenance: re-stamping a whole linked audience because somebody pressed // "Add back" is what would put the old list back for good. - out, xerr := segments.AddToCampaign(ctx, f.org, f.owner.String(), segA, f.campaign) + out, xerr := segments.AddToCampaign(ctx, f.org, f.owner.String(), segA, f.campaign, false) if xerr != nil || out.Added != 0 { t.Fatalf("one-shot enrol = %+v, %v", out, xerr) } diff --git a/internal/repository/segment_live_test.go b/internal/repository/segment_live_test.go index 7895d25fe..27244a459 100644 --- a/internal/repository/segment_live_test.go +++ b/internal/repository/segment_live_test.go @@ -217,14 +217,14 @@ func TestLiveSegmentOverridesNestingAndCampaign(t *testing.T) { } // Enrolling the segment adds only the members not already leads. - out, xerr := repo.AddToCampaign(ctx, f.org, f.mate.String(), acme.ID, f.other) + out, xerr := repo.AddToCampaign(ctx, f.org, f.mate.String(), acme.ID, f.other, false) if xerr != nil { t.Fatalf("add to campaign: %v", xerr) } if out.Added != 2 || out.Members != 2 { t.Fatalf("add to campaign = %+v", out) } - out, xerr = repo.AddToCampaign(ctx, f.org, f.mate.String(), acme.ID, f.other) + out, xerr = repo.AddToCampaign(ctx, f.org, f.mate.String(), acme.ID, f.other, false) if xerr != nil || out.Added != 0 { t.Fatalf("second add = %+v, %v", out, xerr) } @@ -326,7 +326,7 @@ func TestLiveSegmentCampaignLinks(t *testing.T) { if _, xerr := contacts.BulkUpdate(ctx, f.owner.String(), f.org, removeBob); xerr != nil { t.Fatalf("remove again: %v", xerr) } - out, xerr := repo.AddToCampaign(ctx, f.org, f.owner.String(), acme.ID, f.other) + out, xerr := repo.AddToCampaign(ctx, f.org, f.owner.String(), acme.ID, f.other, false) if xerr != nil || out.Added != 1 { t.Fatalf("one-shot after removal = %+v, %v", out, xerr) } diff --git a/web/src/app/app/settings/_components/SectionShell.tsx b/web/src/app/app/settings/_components/SectionShell.tsx index 5af4d13ae..3761153d6 100644 --- a/web/src/app/app/settings/_components/SectionShell.tsx +++ b/web/src/app/app/settings/_components/SectionShell.tsx @@ -169,10 +169,13 @@ export function Toggle({ on, onChange, disabled, + label, }: { on: boolean; onChange: (next: boolean) => void; disabled?: boolean; + /** Accessible name for the switch, usually the row's label. */ + label?: string; }) { return (