listmonk/cmd/manager_store.go

124 lines
3.2 KiB
Go
Raw Normal View History

2018-10-25 13:51:47 +00:00
package main
import (
"net/http"
2020-03-07 15:07:48 +00:00
"github.com/gofrs/uuid"
"github.com/knadh/listmonk/internal/core"
"github.com/knadh/listmonk/internal/manager"
"github.com/knadh/listmonk/internal/media"
2018-10-25 13:51:47 +00:00
"github.com/knadh/listmonk/models"
"github.com/lib/pq"
)
// store implements DataSource over the primary
2018-10-25 13:51:47 +00:00
// database.
type store struct {
queries *models.Queries
core *core.Core
media media.Store
h *http.Client
2018-10-25 13:51:47 +00:00
}
func newManagerStore(q *models.Queries, c *core.Core, m media.Store) *store {
return &store{
2018-10-25 13:51:47 +00:00
queries: q,
core: c,
media: m,
2018-10-25 13:51:47 +00:00
}
}
// NextCampaigns retrieves active campaigns ready to be processed.
func (s *store) NextCampaigns(excludeIDs []int64) ([]*models.Campaign, error) {
2018-10-25 13:51:47 +00:00
var out []*models.Campaign
err := s.queries.NextCampaigns.Select(&out, pq.Int64Array(excludeIDs))
2018-10-25 13:51:47 +00:00
return out, err
}
// NextSubscribers retrieves a subset of subscribers of a given campaign.
// Since batches are processed sequentially, the retrieval is ordered by ID,
// and every batch takes the last ID of the last batch and fetches the next
// batch above that.
func (s *store) NextSubscribers(campID, limit int) ([]models.Subscriber, error) {
2020-03-08 05:37:24 +00:00
var out []models.Subscriber
err := s.queries.NextCampaignSubscribers.Select(&out, campID, limit)
2018-10-25 13:51:47 +00:00
return out, err
}
// GetCampaign fetches a campaign from the database.
func (s *store) GetCampaign(campID int) (*models.Campaign, error) {
2018-10-25 13:51:47 +00:00
var out = &models.Campaign{}
err := s.queries.GetCampaign.Get(out, campID, nil, "default")
2018-10-25 13:51:47 +00:00
return out, err
}
// UpdateCampaignStatus updates a campaign's status.
func (s *store) UpdateCampaignStatus(campID int, status string) error {
_, err := s.queries.UpdateCampaignStatus.Exec(campID, status)
2018-10-25 13:51:47 +00:00
return err
}
// GetAttachment fetches a media attachment blob.
func (s *store) GetAttachment(mediaID int) (models.Attachment, error) {
m, err := s.core.GetMedia(mediaID, "", s.media)
if err != nil {
return models.Attachment{}, err
}
b, err := s.media.GetBlob(m.URL)
if err != nil {
return models.Attachment{}, err
}
return models.Attachment{
Name: m.Filename,
Content: b,
Header: manager.MakeAttachmentHeader(m.Filename, "base64", m.ContentType),
}, nil
}
// CreateLink registers a URL with a UUID for tracking clicks and returns the UUID.
func (s *store) CreateLink(url string) (string, error) {
// Create a new UUID for the URL. If the URL already exists in the DB
// the UUID in the database is returned.
2020-03-07 15:07:48 +00:00
uu, err := uuid.NewV4()
if err != nil {
return "", err
}
2020-03-07 15:07:48 +00:00
var out string
if err := s.queries.CreateLink.Get(&out, uu, url); err != nil {
2020-03-07 15:07:48 +00:00
return "", err
}
return out, nil
}
// RecordBounce records a bounce event and returns the bounce count.
func (s *store) RecordBounce(b models.Bounce) (int64, int, error) {
var res = struct {
SubscriberID int64 `db:"subscriber_id"`
Num int `db:"num"`
}{}
err := s.queries.UpdateCampaignStatus.Select(&res,
b.SubscriberUUID,
b.Email,
b.CampaignUUID,
b.Type,
b.Source,
b.Meta)
return res.SubscriberID, res.Num, err
}
func (s *store) BlocklistSubscriber(id int64) error {
_, err := s.queries.BlocklistSubscribers.Exec(pq.Int64Array{id})
return err
}
func (s *store) DeleteSubscriber(id int64) error {
_, err := s.queries.DeleteSubscribers.Exec(pq.Int64Array{id})
return err
}