Skip to content

armgit / posta

Public Imported from GitHub
0

Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.

fix(webhooks): scope webhook dispatch to workspace so deliveries are recorded fixes #93

Jonas Kanindaa439c7bb1c3d
diff --git a/internal/services/email/service.go b/internal/services/email/service.go
index 84e228f..2a5e277 100644
--- a/internal/services/email/service.go
+++ b/internal/services/email/service.go
@@ -710,7 +710,7 @@ func (s *Service) sendSync(em *models.Email, userID uint, workspaceID *uint, req
em.Status = models.EmailStatusFailed
em.ErrorMessage = "no SMTP server configured"
_ = s.emailRepo.Update(em)
- s.dispatcher.Dispatch(userID, "email.failed", em.UUID, req.From)
+ s.dispatcher.Dispatch(userID, workspaceID, "email.failed", em.UUID, req.From)
if s.onFailed != nil {
s.onFailed()
}
@@ -730,7 +730,7 @@ func (s *Service) sendSync(em *models.Email, userID uint, workspaceID *uint, req
em.Status = models.EmailStatusFailed
em.ErrorMessage = fmt.Sprintf("sender %q is not in the allowed emails list", req.From)
_ = s.emailRepo.Update(em)
- s.dispatcher.Dispatch(userID, "email.failed", em.UUID, req.From)
+ s.dispatcher.Dispatch(userID, workspaceID, "email.failed", em.UUID, req.From)
if s.onFailed != nil {
s.onFailed()
}
@@ -745,7 +745,7 @@ func (s *Service) sendSync(em *models.Email, userID uint, workspaceID *uint, req
em.Status = models.EmailStatusFailed
em.ErrorMessage = err.Error()
_ = s.emailRepo.Update(em)
- s.dispatcher.Dispatch(userID, "email.failed", em.UUID, req.From)
+ s.dispatcher.Dispatch(userID, workspaceID, "email.failed", em.UUID, req.From)
if s.onFailed != nil {
s.onFailed()
}
@@ -759,7 +759,7 @@ func (s *Service) sendSync(em *models.Email, userID uint, workspaceID *uint, req
em.Status = models.EmailStatusSent
em.SentAt = &now
_ = s.emailRepo.Update(em)
- s.dispatcher.Dispatch(userID, "email.sent", em.UUID, req.From)
+ s.dispatcher.Dispatch(userID, workspaceID, "email.sent", em.UUID, req.From)
if s.onSent != nil {
s.onSent()
}
diff --git a/internal/services/retry/worker.go b/internal/services/retry/worker.go
index f8bde38..b0563b8 100644
--- a/internal/services/retry/worker.go
+++ b/internal/services/retry/worker.go
@@ -170,7 +170,7 @@ func (w *Worker) retryEmail(em *models.Email, server *models.SMTPServer) {
em.SentAt = &now
em.ErrorMessage = ""
_ = w.emailRepo.Update(em)
- w.dispatcher.Dispatch(em.UserID, "email.sent", em.UUID, em.Sender)
+ w.dispatcher.Dispatch(em.UserID, em.WorkspaceID, "email.sent", em.UUID, em.Sender)
logger.Info("retry worker: email sent successfully", "id", em.ID, "attempt", em.RetryCount)
if w.onSent != nil {
w.onSent()
diff --git a/internal/services/webhook/dispatcher.go b/internal/services/webhook/dispatcher.go
index 28e4387..1dfc4f8 100644
--- a/internal/services/webhook/dispatcher.go
+++ b/internal/services/webhook/dispatcher.go
@@ -157,7 +157,9 @@ type Payload struct {
// Dispatch sends webhook notifications for the given event using the default
// outbound payload shape ({event, email_id, timestamp}). Runs asynchronously.
-func (d *Dispatcher) Dispatch(userID uint, event string, emailID string, from string) {
+// workspaceID scopes which webhooks fire: nil targets the user's personal
+// webhooks, a value targets that workspace's webhooks.
+func (d *Dispatcher) Dispatch(userID uint, workspaceID *uint, event string, emailID string, from string) {
payload := Payload{
Event: event,
EmailID: emailID,
@@ -170,16 +172,18 @@ func (d *Dispatcher) Dispatch(userID uint, event string, emailID string, from st
return
}
- d.DispatchJSON(userID, event, body, from)
+ d.DispatchJSON(userID, workspaceID, event, body, from)
}
// DispatchJSON sends a pre-serialized JSON body to all webhooks subscribed to event
-// for the given user that pass the sender-address filter. Runs asynchronously.
+// within the given scope (personal when workspaceID is nil, otherwise the
+// workspace) that pass the sender-address filter. Runs asynchronously.
// Callers own the payload shape — this is the escape hatch used by the inbound
// pipeline (which has a richer payload than the outbound Payload struct).
-func (d *Dispatcher) DispatchJSON(userID uint, event string, body []byte, fromAddr string) {
+func (d *Dispatcher) DispatchJSON(userID uint, workspaceID *uint, event string, body []byte, fromAddr string) {
go func() {
- webhooks, err := d.repo.FindByUserIDAndEvent(userID, event)
+ scope := repositories.ResourceScope{UserID: userID, WorkspaceID: workspaceID}
+ webhooks, err := d.repo.FindByScopeAndEvent(scope, event)
if err != nil {
logger.Error("failed to find webhooks", "error", err)
return
diff --git a/internal/storage/repositories/webhook_repo.go b/internal/storage/repositories/webhook_repo.go
index 946ad30..c10dca6 100644
--- a/internal/storage/repositories/webhook_repo.go
+++ b/internal/storage/repositories/webhook_repo.go
@@ -96,9 +96,11 @@ func (r *WebhookRepository) FindByScope(scope ResourceScope, limit, offset int)
return items, total, nil
}
-func (r *WebhookRepository) FindByUserIDAndEvent(userID uint, event string) ([]models.Webhook, error) {
+func (r *WebhookRepository) FindByScopeAndEvent(scope ResourceScope, event string) ([]models.Webhook, error) {
var webhooks []models.Webhook
- if err := r.db.Where("user_id = ? AND ? = ANY(events)", userID, event).Find(&webhooks).Error; err != nil {
+ if err := ApplyScope(r.db.Model(&models.Webhook{}), scope).
+ Where("? = ANY(events)", event).
+ Find(&webhooks).Error; err != nil {
return nil, err
}
return webhooks, nil
diff --git a/internal/worker/campaign_processor.go b/internal/worker/campaign_processor.go
index dee5fe9..9ccfc20 100644
--- a/internal/worker/campaign_processor.go
+++ b/internal/worker/campaign_processor.go
@@ -206,7 +206,7 @@ func (p *CampaignProcessor) HandleCampaignStart(_ context.Context, t *asynq.Task
return err
}
if p.dispatcher != nil {
- p.dispatcher.Dispatch(campaign.UserID, "campaign.started", fmt.Sprint(campaign.ID), campaign.FromEmail)
+ p.dispatcher.Dispatch(campaign.UserID, campaign.WorkspaceID, "campaign.started", fmt.Sprint(campaign.ID), campaign.FromEmail)
}
return nil
}
@@ -242,7 +242,7 @@ func (p *CampaignProcessor) HandleCampaignBatch(_ context.Context, t *asynq.Task
return err
}
if p.dispatcher != nil {
- p.dispatcher.Dispatch(campaign.UserID, "campaign.completed", fmt.Sprint(campaign.ID), campaign.FromEmail)
+ p.dispatcher.Dispatch(campaign.UserID, campaign.WorkspaceID, "campaign.completed", fmt.Sprint(campaign.ID), campaign.FromEmail)
}
return nil
}
@@ -379,7 +379,7 @@ func (p *CampaignProcessor) HandleCampaignBatch(_ context.Context, t *asynq.Task
return err
}
if p.dispatcher != nil {
- p.dispatcher.Dispatch(campaign.UserID, "campaign.completed", fmt.Sprint(campaign.ID), campaign.FromEmail)
+ p.dispatcher.Dispatch(campaign.UserID, campaign.WorkspaceID, "campaign.completed", fmt.Sprint(campaign.ID), campaign.FromEmail)
}
return nil
}
diff --git a/internal/worker/handler.go b/internal/worker/handler.go
index a760839..7086293 100644
--- a/internal/worker/handler.go
+++ b/internal/worker/handler.go
@@ -344,7 +344,7 @@ func (h *EmailSendHandler) ProcessTask(ctx context.Context, t *asynq.Task) error
if sharedServerID != 0 && h.serverRepo != nil {
go h.serverRepo.IncrementSentCount(sharedServerID)
}
- h.dispatcher.Dispatch(em.UserID, "email.sent", em.UUID, em.Sender)
+ h.dispatcher.Dispatch(em.UserID, em.WorkspaceID, "email.sent", em.UUID, em.Sender)
if h.onSent != nil {
h.onSent()
}
@@ -365,7 +365,7 @@ func (h *EmailSendHandler) markFailed(em *models.Email, reason string) {
em.Status = models.EmailStatusFailed
em.ErrorMessage = reason
_ = h.emailRepo.Update(em)
- h.dispatcher.Dispatch(em.UserID, "email.failed", em.UUID, em.Sender)
+ h.dispatcher.Dispatch(em.UserID, em.WorkspaceID, "email.failed", em.UUID, em.Sender)
if h.onFailed != nil {
h.onFailed()
}
@@ -433,7 +433,7 @@ func (h *EmailSendHandler) handlePermanentRejection(em *models.Email, se *email.
em.ErrorMessage = se.Error()
_ = h.emailRepo.Update(em)
- h.dispatcher.Dispatch(em.UserID, "email.failed", em.UUID, em.Sender)
+ h.dispatcher.Dispatch(em.UserID, em.WorkspaceID, "email.failed", em.UUID, em.Sender)
if h.onFailed != nil {
h.onFailed()
}
@@ -488,7 +488,7 @@ func (e *ExhaustedErrorHandler) HandleError(_ context.Context, t *asynq.Task, er
em.Status = models.EmailStatusFailed
em.ErrorMessage = fmt.Sprintf("permanently failed after retries: %v", err)
_ = e.emailRepo.Update(em)
- e.dispatcher.Dispatch(em.UserID, "email.failed", em.UUID, em.Sender)
+ e.dispatcher.Dispatch(em.UserID, em.WorkspaceID, "email.failed", em.UUID, em.Sender)
if e.onFailed != nil {
e.onFailed()
}
diff --git a/internal/worker/inbound_handler.go b/internal/worker/inbound_handler.go
index 6ca1070..dabdb0e 100644
--- a/internal/worker/inbound_handler.go
+++ b/internal/worker/inbound_handler.go
@@ -216,7 +216,7 @@ func (h *InboundProcessHandler) ProcessTask(_ context.Context, t *asynq.Task) er
return fmt.Errorf("marshal inbound webhook body: %w", err)
}
- h.dispatcher.DispatchJSON(rec.UserID, "email.inbound", encoded, rec.Sender)
+ h.dispatcher.DispatchJSON(rec.UserID, rec.WorkspaceID, "email.inbound", encoded, rec.Sender)
now := time.Now().UTC()
rec.Status = models.InboundStatusForwarded