Skip to content

Commit

Permalink
feat: add extra time in events (#1)
Browse files Browse the repository at this point in the history
### Purpose of this PR
This PR introduces a new config field to add delay to the timestamp
  • Loading branch information
enac21 authored Jan 8, 2025
1 parent 2c1944f commit 687f2d3
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ There are some customization possibles, all of them are defined by using the sys
* **MAIL_HISTORY_DURATION**: duration, using Golang pattern, which is used to delete messages older than that duration;
* **STORAGE_FILE**: when defined, instead of creating a randomized temporary file, the given file name will be used;
* **BLOCK_DELETE_ALL**: when value is `true`, deleting all messages will not be allowed;
* **EVENT_TIMESTAMP_DELAY**_ duration, using Golang pattern, which is used to add extra time to the events;

## Container (Docker)

Expand Down
22 changes: 20 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package config

import (
"github.com/rapatao/go-injector"
"github.com/rs/zerolog/log"
"os"
"time"

"github.com/rapatao/go-injector"
"github.com/rs/zerolog/log"
)

const (
Expand All @@ -20,6 +21,7 @@ type Config struct {
WebStaticFiles string
StorageFile string
BlockDeleteAll bool
MessageDelay time.Duration
}

func (c *Config) Initialize(_ *injector.Container) error {
Expand All @@ -29,6 +31,7 @@ func (c *Config) Initialize(_ *injector.Container) error {
c.webStaticFiles()
c.storageDir()
c.blockDeleteAll()
c.messageDelay()

return nil
}
Expand Down Expand Up @@ -105,6 +108,21 @@ func (c *Config) storageDir() {
c.StorageFile = storage
}

func (c *Config) messageDelay() {
duration, err := time.ParseDuration(os.Getenv("EVENT_TIMESTAMP_DELAY"))
if err != nil {
log.Error().Err(err).Msgf("failed to parse EVENT_TIMESTAMP_DELAY, using default 0")

c.MessageDelay = 0

return
}

c.MessageDelay = duration

return
}

func (c *Config) blockDeleteAll() {
env := os.Getenv("BLOCK_DELETE_ALL")
if env == "true" {
Expand Down
2 changes: 1 addition & 1 deletion internal/eventsender/click.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (s *Service) TriggerClick(ctx context.Context, message *model.Message, user
return
}

event := baseEvent(message)
event := s.baseEvent(message)

event["event"] = "click"
event["useragent"] = userAgent
Expand Down
2 changes: 1 addition & 1 deletion internal/eventsender/delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (s *Service) TriggerDeliveryEvent(ctx context.Context, message *model.Messa
return
}

event := baseEvent(message)
event := s.baseEvent(message)

var (
eventName string
Expand Down
4 changes: 2 additions & 2 deletions internal/eventsender/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"time"
)

func baseEvent(message *model.Message) map[string]any {
func (s *Service) baseEvent(message *model.Message) map[string]any {
event := map[string]any{}

for key, value := range message.CustomArgs {
event[key] = value
}

event["email"] = message.To.Address
event["timestamp"] = time.Now().Unix()
event["timestamp"] = time.Now().Add(s.config.MessageDelay).Unix()
event["smtp-id"] = message.EventID + "." + message.MessageID + "@mock"
event["sg_event_id"] = message.EventID
event["sg_message_id"] = message.MessageID
Expand Down
2 changes: 1 addition & 1 deletion internal/eventsender/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (s *Service) TriggerOpen(ctx context.Context, message *model.Message, userA
return
}

event := baseEvent(message)
event := s.baseEvent(message)

event["event"] = "open"
event["useragent"] = userAgent
Expand Down

0 comments on commit 687f2d3

Please sign in to comment.