On the server and multi-tenant editions a team's admin enters its Core's domain and account under Settings → Core & Hermes, instead of core.conf; Hermes connects from the team's side with an agent's key. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
149 lines
4.6 KiB
Go
149 lines
4.6 KiB
Go
package web
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"supermarket/internal/hugorepo"
|
|
"supermarket/internal/sites"
|
|
"supermarket/internal/tracking"
|
|
)
|
|
|
|
// captureClient is the HTTP client for testing a Lead Capture (a test's
|
|
// TLS server's in tests).
|
|
var captureClient = sites.TestClient
|
|
|
|
func captureKey(repo string) string { return "sites:lead-capture:" + repo }
|
|
func formsPRKey(repo string) string { return "sites:lead-forms-pr:" + repo }
|
|
|
|
// formsPR is the pull request that connects a site's forms.
|
|
type formsPR struct {
|
|
Number int64 `json:"number"`
|
|
URL string `json:"url"`
|
|
Capture string `json:"capture"`
|
|
At time.Time `json:"at"`
|
|
State string `json:"state"` // open, merged, closed
|
|
}
|
|
|
|
func (s *Server) sitesFormsPage(w http.ResponseWriter, r *http.Request) {
|
|
s.renderForms(w, r, "", "")
|
|
}
|
|
|
|
func (s *Server) renderForms(w http.ResponseWriter, r *http.Request, errMsg, flash string) {
|
|
all, cur := s.currentSite(r)
|
|
data := map[string]any{"Sites": all, "Current": cur, "Gitea": s.gitea() != nil, "Payload": sites.LeadPayloadFields,
|
|
"Goal": tracking.LeadGoal, "Plausible": s.plausible() != nil}
|
|
if s.core() != nil && s.core().Domain != "" {
|
|
data["CRM"] = "https://espocrm." + s.core().Domain
|
|
}
|
|
if cur != nil && cur.Repo != "" {
|
|
var capture string
|
|
s.DB.GetJSON(captureKey(cur.Repo), &capture)
|
|
data["Capture"] = capture
|
|
var pr formsPR
|
|
if ok, _ := s.DB.GetJSON(formsPRKey(cur.Repo), &pr); ok {
|
|
if pr.State == "open" && s.gitea() != nil {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
|
if pull, err := s.gitea().GetPull(ctx, cur.Repo, pr.Number); err == nil {
|
|
switch {
|
|
case pull.Merged:
|
|
pr.State = "merged"
|
|
case pull.State == "closed":
|
|
pr.State = "closed"
|
|
}
|
|
_ = s.DB.PutJSON(formsPRKey(cur.Repo), pr)
|
|
}
|
|
cancel()
|
|
}
|
|
data["PR"] = pr
|
|
}
|
|
var synced siteSynced
|
|
if ok, _ := s.DB.GetJSON(siteSyncKey(cur.Repo), &synced); ok && !synced.At.IsZero() {
|
|
data["HasSynced"] = true
|
|
wc := &hugorepo.Repo{Dir: s.siteDir(cur.Repo), Branch: synced.Branch}
|
|
wc.Acquire()
|
|
cat, _ := sites.Read(wc.Dir, wc.PathOf)
|
|
st := sites.ReadForms(wc, cat)
|
|
wc.Release()
|
|
data["State"] = st
|
|
data["Connected"] = st.Connected(capture)
|
|
}
|
|
}
|
|
data["Leads"], _ = tracking.FormLeads(s.DB, 20)
|
|
s.render(w, r, "sites-forms", page{Title: "Lead forms", Nav: "sites/forms", Error: errMsg, Flash: flash, Data: data})
|
|
}
|
|
|
|
func (s *Server) sitesFormsAction(w http.ResponseWriter, r *http.Request) {
|
|
if err := r.ParseForm(); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
_, cur := s.currentSite(r)
|
|
if cur == nil || cur.Repo == "" {
|
|
s.renderForms(w, r, "Set this website's Hugo repository in Settings → Profile first.", "")
|
|
return
|
|
}
|
|
capture := strings.TrimSpace(r.PostFormValue("capture"))
|
|
if _, has := r.PostForm["capture"]; !has {
|
|
s.DB.GetJSON(captureKey(cur.Repo), &capture)
|
|
}
|
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Minute)
|
|
defer cancel()
|
|
switch r.PathValue("action") {
|
|
case "save":
|
|
if capture != "" {
|
|
if err := sites.CheckCaptureURL(capture); err != nil {
|
|
s.renderForms(w, r, err.Error(), "")
|
|
return
|
|
}
|
|
}
|
|
if err := s.DB.PutJSON(captureKey(cur.Repo), capture); err != nil {
|
|
s.serverError(w, err)
|
|
return
|
|
}
|
|
if capture == "" {
|
|
s.renderForms(w, r, "", "The Lead Capture address is removed.")
|
|
return
|
|
}
|
|
if err := sites.TestCapture(ctx, captureClient(), capture, cur.URL); err != nil {
|
|
s.renderForms(w, r, "Saved, but the test failed: "+err.Error(), "")
|
|
return
|
|
}
|
|
s.renderForms(w, r, "", "Saved and tested: "+cur.URL+" may post to it. Nothing was created in the CRM.")
|
|
case "connect":
|
|
if capture == "" {
|
|
s.renderForms(w, r, "Save the Lead Capture address first.", "")
|
|
return
|
|
}
|
|
if s.gitea() == nil {
|
|
s.renderForms(w, r, "Save a Gitea token under Settings → Search engines first.", "")
|
|
return
|
|
}
|
|
cat, err := s.catalog(ctx, *cur, true)
|
|
if err != nil {
|
|
s.renderForms(w, r, err.Error(), "")
|
|
return
|
|
}
|
|
formPage := ""
|
|
if t := cat.Type("form"); t != nil && len(t.Pages) > 0 {
|
|
formPage = t.Pages[0]
|
|
}
|
|
pull, err := sites.ConnectForms(ctx, s.applier(seoSite{Index: cur.Index, URL: cur.URL}), capture, formPage)
|
|
if err != nil {
|
|
s.renderForms(w, r, err.Error(), "")
|
|
return
|
|
}
|
|
pr := formsPR{Number: pull.Number, URL: pull.HTMLURL, Capture: capture, At: time.Now(), State: "open"}
|
|
if err := s.DB.PutJSON(formsPRKey(cur.Repo), pr); err != nil {
|
|
s.serverError(w, err)
|
|
return
|
|
}
|
|
http.Redirect(w, r, "/sites/forms?site="+strconv.Itoa(cur.Index), http.StatusSeeOther)
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}
|