Prospects: Listmonk mailing lists, and their opt-outs
Settings → Email → Mailing lists connects Listmonk (the Core's, by default) with an API user and its token; Test reads the lists. A person in Review can be added to a list, with their company, title and signal: a double opt-in list asks them to confirm unless they already agreed; a single opt-in list takes them only once they have; a suppressed address is refused. Their card shows the lists they're on (a new list_members table). Every hour, or on Sync now, everyone blocklisted in Listmonk or unsubscribed from one of its lists goes on the suppression list, with the reason; an address already there keeps its own. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,298 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/api/listmonk"
|
||||
"supermarket/internal/prospects"
|
||||
"supermarket/internal/secrets"
|
||||
"supermarket/internal/signals"
|
||||
)
|
||||
|
||||
// Mailing lists: adding a prospect to a list on Listmonk (Settings →
|
||||
// Email → Mailing lists, and Add to a mailing list on a person in Review).
|
||||
|
||||
type listmonkSettings struct {
|
||||
URL string `json:"url"`
|
||||
User string `json:"user"`
|
||||
}
|
||||
|
||||
const (
|
||||
listmonkKey = "listmonk:settings"
|
||||
listmonkListsKey = "listmonk:lists" // the lists, as last read
|
||||
listsFresh = time.Hour
|
||||
)
|
||||
|
||||
var listmonkToken = secretField{"listmonk", secrets.ListmonkToken, "API token",
|
||||
"In Listmonk: Admin → Users → New, of the API type; it shows the token once.", ""}
|
||||
|
||||
// listmonkConfig is the saved settings, with Listmonk's usual address on
|
||||
// the Core when none is saved.
|
||||
func (s *Server) listmonkConfig() listmonkSettings {
|
||||
var set listmonkSettings
|
||||
_, _ = s.DB.GetJSON(listmonkKey, &set)
|
||||
if set.URL == "" && s.Core != nil {
|
||||
set.URL = "https://listmonk." + s.Core.Domain
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
// listmonk is the client, or nil until a user and token are saved.
|
||||
func (s *Server) listmonk() *listmonk.Client {
|
||||
set := s.listmonkConfig()
|
||||
tok := s.Secrets.Get(secrets.ListmonkToken)
|
||||
if set.URL == "" || set.User == "" || tok == "" {
|
||||
return nil
|
||||
}
|
||||
return listmonk.New(set.URL, set.User, tok)
|
||||
}
|
||||
|
||||
type cachedLists struct {
|
||||
At time.Time `json:"at"`
|
||||
Lists []listmonk.List `json:"lists"`
|
||||
}
|
||||
|
||||
// mailingLists are Listmonk's lists, read at most once an hour (or now,
|
||||
// with fresh).
|
||||
func (s *Server) mailingLists(ctx context.Context, fresh bool) ([]listmonk.List, error) {
|
||||
c := s.listmonk()
|
||||
if c == nil {
|
||||
return nil, nil
|
||||
}
|
||||
var cached cachedLists
|
||||
if ok, _ := s.DB.GetJSON(listmonkListsKey, &cached); ok && !fresh && time.Since(cached.At) < listsFresh {
|
||||
return cached.Lists, nil
|
||||
}
|
||||
lists, err := c.Lists(ctx)
|
||||
if err != nil {
|
||||
return cached.Lists, err
|
||||
}
|
||||
_ = s.DB.PutJSON(listmonkListsKey, cachedLists{At: time.Now(), Lists: lists})
|
||||
return lists, nil
|
||||
}
|
||||
|
||||
// listmonkSave saves the address, the API user and its token.
|
||||
func (s *Server) listmonkSave(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
set := listmonkSettings{URL: strings.TrimRight(strings.TrimSpace(r.PostFormValue("listmonk_url")), "/"), User: strings.TrimSpace(r.PostFormValue("listmonk_user"))}
|
||||
if set.URL != "" && !strings.HasPrefix(set.URL, "https://") && !strings.HasPrefix(set.URL, "http://") {
|
||||
set, _ := s.outreachSettings()
|
||||
s.renderEmail(w, r, set, "Listmonk's address starts with https://")
|
||||
return
|
||||
}
|
||||
if err := s.DB.PutJSON(listmonkKey, set); err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
if err := s.saveSecrets(r, []secretField{listmonkToken}); err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
_, _ = s.DB.Exec(`DELETE FROM settings WHERE key = ?`, listmonkListsKey) // read them again
|
||||
redirect(w, r, "/settings/email#lists", "saved")
|
||||
}
|
||||
|
||||
// listmonkTest reads the lists: the user and token work if it can.
|
||||
func (s *Server) listmonkTest(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
||||
defer cancel()
|
||||
data := map[string]any{}
|
||||
if s.listmonk() == nil {
|
||||
data["Error"] = "Save the API user and its token first."
|
||||
} else if lists, err := s.mailingLists(ctx, true); err != nil {
|
||||
data["Error"] = err.Error()
|
||||
} else {
|
||||
data["Lists"] = lists
|
||||
}
|
||||
s.fragment(w, "email", "listmonk-test", data)
|
||||
}
|
||||
|
||||
// addToList adds a person to a mailing list.
|
||||
func (s *Server) addToList(r *http.Request, id int64) error {
|
||||
c := s.listmonk()
|
||||
if c == nil {
|
||||
return errors.New("set up Listmonk under Settings → Email first")
|
||||
}
|
||||
p, err := prospects.GetContact(s.DB, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if p.Email == "" {
|
||||
return errors.New(p.Name() + " has no email address")
|
||||
}
|
||||
domain := ""
|
||||
if _, after, ok := strings.Cut(p.Email, "@"); ok {
|
||||
domain = after
|
||||
}
|
||||
if sup, err := prospects.IsSuppressed(s.DB, p.Email, domain); err != nil {
|
||||
return err
|
||||
} else if sup {
|
||||
return errors.New(p.Email + " is on your suppression list, so it can't go on a mailing list")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), time.Minute)
|
||||
defer cancel()
|
||||
lists, err := s.mailingLists(ctx, false)
|
||||
if err != nil && len(lists) == 0 {
|
||||
return err
|
||||
}
|
||||
var list *listmonk.List
|
||||
for i := range lists {
|
||||
if fmt.Sprint(lists[i].ID) == r.PostFormValue("list") {
|
||||
list = &lists[i]
|
||||
}
|
||||
}
|
||||
if list == nil {
|
||||
return errors.New("choose a mailing list")
|
||||
}
|
||||
confirmed := r.PostFormValue("agreed") == "on"
|
||||
if !confirmed && !list.Double() {
|
||||
return errors.New(list.Name + " sends its mail without asking first: add someone there only once they've agreed to receive it, or choose a double opt-in list")
|
||||
}
|
||||
attribs := map[string]any{"source": "supermarket"}
|
||||
if p.Title != "" {
|
||||
attribs["title"] = p.Title
|
||||
}
|
||||
if p.CompanyID != 0 {
|
||||
if co, err := prospects.GetCompany(s.DB, p.CompanyID); err == nil {
|
||||
attribs["company"] = co.Name
|
||||
}
|
||||
}
|
||||
if p.SignalID != 0 {
|
||||
if sig, err := signals.Get(s.DB, p.SignalID); err == nil {
|
||||
attribs["signal"] = sig.Slug
|
||||
}
|
||||
}
|
||||
if _, _, err := c.Subscribe(ctx, listmonk.Subscriber{Email: p.Email, Name: p.Name(), Attribs: attribs}, *list, confirmed); err != nil {
|
||||
return err
|
||||
}
|
||||
return prospects.AddedToList(s.DB, id, list.ID, list.Name, confirmed || !list.Double(), time.Now())
|
||||
}
|
||||
|
||||
// reviewLists are the lists Review offers, quickly: what's cached, or a
|
||||
// short read; nil until Listmonk is set up.
|
||||
func (s *Server) reviewLists(r *http.Request) []listmonk.List {
|
||||
if s.listmonk() == nil {
|
||||
return nil
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
lists, _ := s.mailingLists(ctx, false)
|
||||
return lists
|
||||
}
|
||||
|
||||
// onLists are the lists each person was added to, by contact ID.
|
||||
func (s *Server) onLists() map[int64][]prospects.ListMember {
|
||||
m, _ := prospects.ListsOf(s.DB)
|
||||
return m
|
||||
}
|
||||
|
||||
// optOutSync is what the last sync of Listmonk's opt-outs did.
|
||||
type optOutSync struct {
|
||||
At time.Time `json:"at"`
|
||||
Seen int `json:"seen"` // people Listmonk says don't want mail
|
||||
Added int `json:"added"` // of them, newly on the suppression list
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
const (
|
||||
optOutKey = "listmonk:optouts"
|
||||
optOutEvery = time.Hour
|
||||
)
|
||||
|
||||
// syncOptOuts puts everyone blocklisted in Listmonk, or unsubscribed from
|
||||
// one of its lists, on the suppression list (an address already there
|
||||
// keeps its reason), and forgets the list they left.
|
||||
func (s *Server) syncOptOuts(ctx context.Context) (*optOutSync, error) {
|
||||
res := &optOutSync{At: time.Now()}
|
||||
finish := func(err error) (*optOutSync, error) {
|
||||
if err != nil {
|
||||
res.Error = err.Error()
|
||||
}
|
||||
_ = s.DB.PutJSON(optOutKey, res)
|
||||
return res, err
|
||||
}
|
||||
c := s.listmonk()
|
||||
if c == nil {
|
||||
return finish(errors.New("set up Listmonk first"))
|
||||
}
|
||||
lists, err := s.mailingLists(ctx, true)
|
||||
if err != nil {
|
||||
return finish(err)
|
||||
}
|
||||
out, err := c.OptedOut(ctx, lists)
|
||||
if err != nil && len(out) == 0 {
|
||||
return finish(err)
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, o := range out {
|
||||
if !seen[o.Email] {
|
||||
seen[o.Email] = true
|
||||
res.Seen++
|
||||
}
|
||||
if o.List != "" {
|
||||
if _, derr := s.DB.Exec(`DELETE FROM list_members WHERE list_name = ? AND contact_id IN (SELECT id FROM prospect_contacts WHERE email = ?)`, o.List, o.Email); derr != nil {
|
||||
return finish(derr)
|
||||
}
|
||||
}
|
||||
if already, serr := prospects.IsSuppressed(s.DB, o.Email, ""); serr != nil {
|
||||
return finish(serr)
|
||||
} else if already {
|
||||
continue
|
||||
}
|
||||
reason := "Blocklisted in Listmonk"
|
||||
if o.List != "" {
|
||||
reason = "Unsubscribed from “" + o.List + "” in Listmonk"
|
||||
}
|
||||
if serr := prospects.Suppress(s.DB, "email", o.Email, reason, res.At); serr != nil {
|
||||
return finish(serr)
|
||||
}
|
||||
res.Added++
|
||||
}
|
||||
return finish(err) // a list that failed partway still reports its error
|
||||
}
|
||||
|
||||
// listmonkSync syncs the opt-outs now.
|
||||
func (s *Server) listmonkSync(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Minute)
|
||||
defer cancel()
|
||||
res, err := s.syncOptOuts(ctx)
|
||||
if err != nil {
|
||||
redirect(w, r, "/settings/email#lists", "") // the card shows the error
|
||||
return
|
||||
}
|
||||
redirectCounts(w, r, "/settings/email#lists", "optouts-synced", res.Added)
|
||||
}
|
||||
|
||||
// hourlyOptOuts syncs Listmonk's opt-outs once an hour.
|
||||
func (s *Server) hourlyOptOuts(ctx context.Context) {
|
||||
if s.listmonk() == nil {
|
||||
return
|
||||
}
|
||||
var last optOutSync
|
||||
if ok, _ := s.DB.GetJSON(optOutKey, &last); ok && time.Since(last.At) < optOutEvery {
|
||||
return
|
||||
}
|
||||
sctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
|
||||
defer cancel()
|
||||
if res, err := s.syncOptOuts(sctx); err != nil {
|
||||
s.Log.Warn("listmonk: syncing opt-outs", "err", err)
|
||||
} else if res.Added > 0 {
|
||||
s.Log.Info("listmonk: opt-outs added to the suppression list", "count", res.Added)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) lastOptOuts() *optOutSync {
|
||||
var last optOutSync
|
||||
if ok, _ := s.DB.GetJSON(optOutKey, &last); !ok {
|
||||
return nil
|
||||
}
|
||||
return &last
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/prospects"
|
||||
"supermarket/internal/secrets"
|
||||
)
|
||||
|
||||
func TestMailingLists(t *testing.T) {
|
||||
var added []map[string]any
|
||||
lm := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.URL.Path == "/api/lists":
|
||||
json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"results": []map[string]any{
|
||||
{"id": 3, "name": "Newsletter", "optin": "double"}, {"id": 4, "name": "Announcements", "optin": "single"}}}})
|
||||
case r.URL.Path == "/api/subscribers" && r.Method == http.MethodPost:
|
||||
var b map[string]any
|
||||
json.NewDecoder(r.Body).Decode(&b)
|
||||
added = append(added, b)
|
||||
json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"id": 9}})
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer lm.Close()
|
||||
s, h := newTestServer(t, nil)
|
||||
now := time.Now()
|
||||
ann, _, _ := prospects.AddContact(s.DB, prospects.Contact{FirstName: "Ann", LastName: "Lee", Email: "ann@lee.example", Title: "IT lead", Source: prospects.SourceManual}, now)
|
||||
bob, _, _ := prospects.AddContact(s.DB, prospects.Contact{FirstName: "Bob", LastName: "Ray", Email: "bob@nope.example", Source: prospects.SourceManual}, now)
|
||||
prospects.Suppress(s.DB, "domain", "nope.example", "", now)
|
||||
|
||||
if body := do(h, http.MethodGet, "/prospects", nil, nil).Body.String(); strings.Contains(body, "Add to a mailing list") {
|
||||
t.Error("offered before Listmonk is set up")
|
||||
}
|
||||
postForm(h, "/settings/email/listmonk", url.Values{"listmonk_url": {lm.URL}, "listmonk_user": {"sm"}, secrets.ListmonkToken: {"tok"}})
|
||||
if body := postForm(h, "/settings/email/listmonk/test", nil).Body.String(); !strings.Contains(body, "It works: 2 lists") {
|
||||
t.Errorf("test: %s", body)
|
||||
}
|
||||
body := do(h, http.MethodGet, "/prospects", nil, nil).Body.String()
|
||||
if !strings.Contains(body, "Add to a mailing list") || !strings.Contains(body, "Newsletter (double opt-in)") {
|
||||
t.Fatalf("review: no list form")
|
||||
}
|
||||
|
||||
add := func(id int64, list string, agreed bool) *httptest.ResponseRecorder {
|
||||
v := url.Values{"list": {list}, "tab": {"new"}}
|
||||
if agreed {
|
||||
v.Set("agreed", "on")
|
||||
}
|
||||
return postForm(h, "/prospects/contact/"+strconv.FormatInt(id, 10)+"/list", v)
|
||||
}
|
||||
if rec := add(ann, "4", false); !strings.Contains(rec.Body.String(), "sends its mail without asking first") || len(added) != 0 {
|
||||
t.Errorf("single opt-in without agreement: %d %v", rec.Code, added)
|
||||
}
|
||||
if rec := add(bob, "3", false); !strings.Contains(rec.Body.String(), "suppression list") || len(added) != 0 {
|
||||
t.Errorf("suppressed: %d", rec.Code)
|
||||
}
|
||||
if rec := add(ann, "3", false); rec.Code != http.StatusSeeOther || len(added) != 1 {
|
||||
t.Fatalf("add: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if a := added[0]; a["email"] != "ann@lee.example" || a["preconfirm_subscriptions"] != false || a["attribs"].(map[string]any)["title"] != "IT lead" {
|
||||
t.Errorf("sent: %v", a)
|
||||
}
|
||||
if body := do(h, http.MethodGet, "/prospects", nil, nil).Body.String(); !strings.Contains(body, "<strong>Newsletter</strong> (asked to confirm)") {
|
||||
t.Error("the card doesn't say she's on the list")
|
||||
}
|
||||
if rec := add(ann, "4", true); rec.Code != http.StatusSeeOther || added[1]["preconfirm_subscriptions"] != true {
|
||||
t.Errorf("agreed: %d %v", rec.Code, added)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOptOutSync(t *testing.T) {
|
||||
lm := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
q := r.URL.Query()
|
||||
switch {
|
||||
case r.URL.Path == "/api/lists":
|
||||
json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"results": []map[string]any{{"id": 3, "name": "Newsletter", "optin": "double"}}}})
|
||||
case q.Get("query") != "":
|
||||
json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"results": []map[string]any{{"email": "spam@x.example"}, {"email": "kept@x.example"}}, "total": 2}})
|
||||
case q.Get("list_id") == "3":
|
||||
json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"results": []map[string]any{{"email": "ann@lee.example"}}, "total": 1}})
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer lm.Close()
|
||||
s, h := newTestServer(t, nil)
|
||||
now := time.Now()
|
||||
ann, _, _ := prospects.AddContact(s.DB, prospects.Contact{FirstName: "Ann", LastName: "Lee", Email: "ann@lee.example", Source: prospects.SourceManual}, now)
|
||||
prospects.AddedToList(s.DB, ann, 3, "Newsletter", false, now)
|
||||
prospects.Suppress(s.DB, "email", "kept@x.example", "My own reason", now)
|
||||
postForm(h, "/settings/email/listmonk", url.Values{"listmonk_url": {lm.URL}, "listmonk_user": {"sm"}, secrets.ListmonkToken: {"tok"}})
|
||||
|
||||
rec := postForm(h, "/settings/email/listmonk/sync", nil)
|
||||
if !strings.Contains(rec.Header().Get("Location"), "msg=optouts-synced") || !strings.Contains(rec.Header().Get("Location"), "n1=2") {
|
||||
t.Fatalf("sync: %d %s", rec.Code, rec.Header().Get("Location"))
|
||||
}
|
||||
list, _ := prospects.Suppressions(s.DB)
|
||||
reasons := map[string]string{}
|
||||
for _, e := range list {
|
||||
reasons[e.Value] = e.Reason
|
||||
}
|
||||
if reasons["spam@x.example"] != "Blocklisted in Listmonk" || reasons["ann@lee.example"] != "Unsubscribed from “Newsletter” in Listmonk" || reasons["kept@x.example"] != "My own reason" {
|
||||
t.Errorf("reasons: %v", reasons)
|
||||
}
|
||||
if on, _ := prospects.ListsOf(s.DB); len(on[ann]) != 0 {
|
||||
t.Errorf("Ann still shows on the list: %+v", on[ann])
|
||||
}
|
||||
if body := do(h, http.MethodGet, "/settings/email", nil, nil).Body.String(); !strings.Contains(body, "3 people don't want mail; 2 new on the suppression list") {
|
||||
i := strings.Index(body, "Last synced")
|
||||
t.Errorf("the card doesn't show the sync: %s", body[max(0, i):min(len(body), max(0, i)+300)])
|
||||
}
|
||||
// Again: nothing new.
|
||||
if rec := postForm(h, "/settings/email/listmonk/sync", nil); !strings.Contains(rec.Header().Get("Location"), "n1=0") {
|
||||
t.Errorf("again: %s", rec.Header().Get("Location"))
|
||||
}
|
||||
}
|
||||
@@ -64,6 +64,10 @@ func (s *Server) renderEmail(w http.ResponseWriter, r *http.Request, set outreac
|
||||
"Password": mailPassword,
|
||||
"Set": s.Secrets.Has(secrets.MailPassword),
|
||||
"Hours": hours(),
|
||||
"Listmonk": s.listmonkConfig(),
|
||||
"LMToken": listmonkToken,
|
||||
"LMSet": s.Secrets.Has(secrets.ListmonkToken),
|
||||
"OptOuts": s.lastOptOuts(),
|
||||
}})
|
||||
}
|
||||
|
||||
|
||||
+24
-20
@@ -137,26 +137,28 @@ func (s *Server) renderProspects(w http.ResponseWriter, r *http.Request, tab str
|
||||
}
|
||||
}
|
||||
s.render(w, r, "prospects", page{Title: "Review prospects", Nav: "prospects/review", Error: errMsg, Data: map[string]any{
|
||||
"Tab": tab,
|
||||
"Statuses": prospects.Statuses,
|
||||
"Counts": counts,
|
||||
"Companies": companies,
|
||||
"Loose": loose,
|
||||
"Matches": matches,
|
||||
"Synced": sync.Last,
|
||||
"Signals": list,
|
||||
"SignalNames": names,
|
||||
"Signal": signalID,
|
||||
"Events": events,
|
||||
"Stages": signals.Stages[1:], // found is recorded on its own
|
||||
"Channels": signals.Channels,
|
||||
"Back": reviewURL(tab, signalID),
|
||||
"Today": time.Now().Format("2006-01-02"),
|
||||
"Hunter": s.Secrets.Has(secrets.HunterAPIKey),
|
||||
"Runs": runs,
|
||||
"Messages": messages,
|
||||
"Unverified": len(unverified(companies, loose)),
|
||||
"MaxVerify": maxVerify,
|
||||
"Tab": tab,
|
||||
"Statuses": prospects.Statuses,
|
||||
"Counts": counts,
|
||||
"Companies": companies,
|
||||
"Loose": loose,
|
||||
"Matches": matches,
|
||||
"Synced": sync.Last,
|
||||
"Signals": list,
|
||||
"SignalNames": names,
|
||||
"Signal": signalID,
|
||||
"Events": events,
|
||||
"Stages": signals.Stages[1:], // found is recorded on its own
|
||||
"MailingLists": s.reviewLists(r),
|
||||
"OnLists": s.onLists(),
|
||||
"Channels": signals.Channels,
|
||||
"Back": reviewURL(tab, signalID),
|
||||
"Today": time.Now().Format("2006-01-02"),
|
||||
"Hunter": s.Secrets.Has(secrets.HunterAPIKey),
|
||||
"Runs": runs,
|
||||
"Messages": messages,
|
||||
"Unverified": len(unverified(companies, loose)),
|
||||
"MaxVerify": maxVerify,
|
||||
}})
|
||||
}
|
||||
|
||||
@@ -197,6 +199,8 @@ func (s *Server) prospectAction(w http.ResponseWriter, r *http.Request) {
|
||||
if action == "verify" {
|
||||
msg = "verified"
|
||||
}
|
||||
case "contact/list":
|
||||
err, msg = s.addToList(r, id), "listed"
|
||||
case "company/delete", "contact/delete":
|
||||
err, msg = prospects.Delete(s.DB, kind, id), "deleted"
|
||||
case "company/reconsider", "contact/reconsider":
|
||||
|
||||
@@ -227,6 +227,9 @@ func (s *Server) Handler() (http.Handler, error) {
|
||||
mux.HandleFunc("GET /seo/audit/status", s.seoAuditStatus)
|
||||
mux.HandleFunc("GET /seo/report", s.seoReportPage)
|
||||
mux.HandleFunc("GET /seo/next", s.seoNextPage)
|
||||
mux.HandleFunc("POST /settings/email/listmonk", s.listmonkSave)
|
||||
mux.HandleFunc("POST /settings/email/listmonk/test", s.listmonkTest)
|
||||
mux.HandleFunc("POST /settings/email/listmonk/sync", s.listmonkSync)
|
||||
mux.HandleFunc("GET /seo/links", s.seoLinksPage)
|
||||
mux.HandleFunc("POST /seo/links/bing", s.seoLinksBing)
|
||||
mux.HandleFunc("POST /seo/links/{action}", s.seoLinksAction)
|
||||
@@ -569,6 +572,8 @@ var flashes = map[string]string{
|
||||
"link-forgotten": "Forgotten.",
|
||||
"partners-read": "Looked at the partners' pages: %d new places to ask for a link.",
|
||||
"alternatives-read": "Searched: %d new lists that leave you out.",
|
||||
"optouts-synced": "Synced: %d new on the suppression list.",
|
||||
"listed": "Added to the mailing list.",
|
||||
"bing-read": "Read the links from Bing.",
|
||||
"audited": "The audit is done.",
|
||||
"auditing": "Auditing. It reads up to 300 pages politely, so it takes a few minutes; this page updates when it's done.",
|
||||
|
||||
@@ -64,8 +64,39 @@
|
||||
</div>
|
||||
<div id="email-test"></div>
|
||||
</section>
|
||||
|
||||
<form method="post" action="/settings/email/listmonk" autocomplete="off">
|
||||
<section class="card" id="lists">
|
||||
<h3>Mailing lists: Listmonk</h3>
|
||||
<p class="small">Add a prospect to a newsletter or announcement list from Review (<em>Add to a mailing list</em>, on a person). Supermarket only adds people; Listmonk sends the campaigns. On a double opt-in list, Listmonk emails them to confirm unless you say they already agreed.</p>
|
||||
<div class="cols">
|
||||
<label>Listmonk's address <input name="listmonk_url" value="{{.Listmonk.URL}}" placeholder="https://listmonk.example.com"></label>
|
||||
<label>API user <input name="listmonk_user" value="{{.Listmonk.User}}" placeholder="supermarket"><span class="help">In Listmonk: Admin → Users → New, type API, with a role that can manage subscribers and read lists.</span></label>
|
||||
</div>
|
||||
{{template "key-field" (dict "F" .LMToken "Set" .LMSet)}}
|
||||
<div class="row">
|
||||
<button type="submit" class="secondary small">Save</button>
|
||||
{{if .LMSet}}<button hx-disabled-elt="this" type="button" class="secondary small" hx-post="/settings/email/listmonk/test" hx-target="#listmonk-test" hx-indicator="#testing-listmonk">Test</button>
|
||||
<span id="testing-listmonk" class="htmx-indicator">Reading the lists…</span>{{end}}
|
||||
</div>
|
||||
<div id="listmonk-test"></div>
|
||||
{{if .LMSet}}
|
||||
<div class="row">
|
||||
<button type="submit" class="secondary small" formaction="/settings/email/listmonk/sync" formnovalidate>Sync unsubscribes now</button>
|
||||
<span class="small muted">Every hour, everyone blocklisted in Listmonk or unsubscribed from one of its lists goes on your <a href="/prospects/suppressed">suppression list</a>, so they're never added as a prospect or emailed in outreach again.</span>
|
||||
</div>
|
||||
{{with .OptOuts}}<p class="small {{if .Error}}error-text{{else}}muted{{end}}">Last synced {{ago .At}}: {{if .Error}}{{.Error}}{{else}}{{.Seen}} {{if eq .Seen 1}}person doesn't{{else}}people don't{{end}} want mail; {{.Added}} new on the suppression list.{{end}}</p>{{end}}
|
||||
{{end}}
|
||||
</section>
|
||||
</form>
|
||||
{{end}}{{template "settings-end"}}{{end}}
|
||||
|
||||
{{define "email-test"}}
|
||||
<ul class="checks">{{range .}}<li class="{{if .OK}}ok{{else}}bad{{end}}">{{.Text}}</li>{{end}}</ul>
|
||||
{{end}}
|
||||
|
||||
{{define "listmonk-test"}}
|
||||
{{with .Error}}<ul class="checks"><li class="bad">{{.}}</li></ul>
|
||||
{{else}}<ul class="checks"><li class="ok">It works: {{len .Lists}} {{if eq (len .Lists) 1}}list{{else}}lists{{end}}.</li></ul>
|
||||
<ul class="small">{{range .Lists}}<li>{{.Name}} · {{.Type}}, {{if .Double}}double opt-in{{else}}single opt-in{{end}} · {{.Subscribers}} subscribers</li>{{end}}</ul>{{end}}
|
||||
{{end}}
|
||||
|
||||
@@ -130,6 +130,7 @@
|
||||
{{if not $p.CompanyID}}{{template "signal-tag" dict "Kind" "contact" "ID" $p.ID "SignalID" $p.SignalID "D" $d}}{{end}}
|
||||
{{with index .Matches (printf "contact:%d" $p.ID)}}{{template "crm-matches" .}}{{end}}
|
||||
{{template "outcomes" dict "Kind" "contact" "ID" $p.ID "Tab" $tab "D" $d}}
|
||||
{{template "mailing" dict "P" $p "Tab" $tab "D" $d}}
|
||||
</div>
|
||||
{{if eq $p.Status $tab}}
|
||||
<div class="row-actions">
|
||||
@@ -204,3 +205,19 @@
|
||||
</form>
|
||||
</details>
|
||||
{{end}}
|
||||
|
||||
{{define "mailing"}}{{$d := .D}}{{$p := .P}}
|
||||
{{with index $d.OnLists $p.ID}}<p class="small muted">On {{range $i, $m := .}}{{if $i}}, {{end}}<strong>{{$m.List}}</strong>{{if not $m.Confirmed}} (asked to confirm){{end}} since {{ago $m.At}}{{end}}.</p>{{end}}
|
||||
{{if and $d.MailingLists $p.Email}}
|
||||
<details class="outcome">
|
||||
<summary class="small">Add to a mailing list</summary>
|
||||
<form method="post" action="/prospects/contact/{{$p.ID}}/list" class="row wrap">
|
||||
<input type="hidden" name="tab" value="{{.Tab}}"><input type="hidden" name="filter" value="{{$d.Signal}}">
|
||||
<label class="small">List <select name="list">{{range $d.MailingLists}}<option value="{{.ID}}">{{.Name}}{{if .Double}} (double opt-in){{else}} (single opt-in){{end}}</option>{{end}}</select></label>
|
||||
<label class="check small"><input type="checkbox" name="agreed"> They agreed to receive it</label>
|
||||
<button type="submit" class="secondary small">Add</button>
|
||||
</form>
|
||||
<p class="small muted">Without their agreement, only a double opt-in list takes them: Listmonk asks them to confirm, and they get nothing until they do. A single opt-in list sends its mail straight away, so it needs their agreement.</p>
|
||||
</details>
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
@@ -254,6 +254,7 @@ func (s *Server) RunTracking(ctx context.Context) {
|
||||
defer tick.Stop()
|
||||
tried := map[string]time.Time{}
|
||||
for {
|
||||
s.hourlyOptOuts(ctx)
|
||||
yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
|
||||
for _, src := range []string{tracking.SourcePlausible, tracking.SourceStripe} {
|
||||
if (src == tracking.SourcePlausible && s.plausible() == nil) || (src == tracking.SourceStripe && s.stripe() == nil) {
|
||||
|
||||
Reference in New Issue
Block a user