first commit
This commit is contained in:
35
internal/dm/service.go
Normal file
35
internal/dm/service.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package dm
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/noderunners/nip05api/internal/messages"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
repo *Repo
|
||||
templates *messages.Templates
|
||||
enabled bool
|
||||
}
|
||||
|
||||
func NewService(repo *Repo, t *messages.Templates, enabled bool) *Service {
|
||||
return &Service{repo: repo, templates: t, enabled: enabled}
|
||||
}
|
||||
|
||||
func (s *Service) Enabled() bool { return s.enabled }
|
||||
|
||||
// Send renders the template for the given event and enqueues a DM. Empty rendered
|
||||
// content short-circuits and returns nil.
|
||||
func (s *Service) Send(ctx context.Context, event EventType, pubkey string, vars map[string]string) error {
|
||||
if !s.enabled {
|
||||
return nil
|
||||
}
|
||||
content, err := s.templates.Render(string(event), vars)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if content == "" {
|
||||
return nil
|
||||
}
|
||||
return s.repo.Insert(ctx, event, pubkey, content)
|
||||
}
|
||||
Reference in New Issue
Block a user