2018-10-25 13:51:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-03-07 15:07:48 +00:00
|
|
|
"github.com/gofrs/uuid"
|
2018-10-25 13:51:47 +00:00
|
|
|
"github.com/knadh/listmonk/models"
|
|
|
|
"github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
// runnerDB implements runner.DataSource over the primary
|
|
|
|
// database.
|
|
|
|
type runnerDB struct {
|
|
|
|
queries *Queries
|
|
|
|
}
|
|
|
|
|
2021-05-24 17:11:48 +00:00
|
|
|
func newManagerStore(q *Queries) *runnerDB {
|
2018-10-25 13:51:47 +00:00
|
|
|
return &runnerDB{
|
|
|
|
queries: q,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NextCampaigns retrieves active campaigns ready to be processed.
|
|
|
|
func (r *runnerDB) NextCampaigns(excludeIDs []int64) ([]*models.Campaign, error) {
|
|
|
|
var out []*models.Campaign
|
|
|
|
err := r.queries.NextCampaigns.Select(&out, pq.Int64Array(excludeIDs))
|
|
|
|
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.
|
2020-03-08 05:37:24 +00:00
|
|
|
func (r *runnerDB) NextSubscribers(campID, limit int) ([]models.Subscriber, error) {
|
|
|
|
var out []models.Subscriber
|
2018-10-25 13:51:47 +00:00
|
|
|
err := r.queries.NextCampaignSubscribers.Select(&out, campID, limit)
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCampaign fetches a campaign from the database.
|
|
|
|
func (r *runnerDB) GetCampaign(campID int) (*models.Campaign, error) {
|
|
|
|
var out = &models.Campaign{}
|
2020-04-26 10:21:26 +00:00
|
|
|
err := r.queries.GetCampaign.Get(out, campID, nil)
|
2018-10-25 13:51:47 +00:00
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
|
2018-11-26 11:10:51 +00:00
|
|
|
// UpdateCampaignStatus updates a campaign's status.
|
|
|
|
func (r *runnerDB) UpdateCampaignStatus(campID int, status string) error {
|
|
|
|
_, err := r.queries.UpdateCampaignStatus.Exec(campID, status)
|
2018-10-25 13:51:47 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-10-31 12:54:21 +00:00
|
|
|
|
|
|
|
// CreateLink registers a URL with a UUID for tracking clicks and returns the UUID.
|
|
|
|
func (r *runnerDB) 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 {
|
2018-10-31 12:54:21 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-03-07 15:07:48 +00:00
|
|
|
var out string
|
|
|
|
if err := r.queries.CreateLink.Get(&out, uu, url); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
2018-10-31 12:54:21 +00:00
|
|
|
}
|
2021-05-24 17:11:48 +00:00
|
|
|
|
|
|
|
// RecordBounce records a bounce event and returns the bounce count.
|
|
|
|
func (r *runnerDB) RecordBounce(b models.Bounce) (int64, int, error) {
|
|
|
|
var res = struct {
|
|
|
|
SubscriberID int64 `db:"subscriber_id"`
|
|
|
|
Num int `db:"num"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
err := r.queries.UpdateCampaignStatus.Select(&res,
|
|
|
|
b.SubscriberUUID,
|
|
|
|
b.Email,
|
|
|
|
b.CampaignUUID,
|
|
|
|
b.Type,
|
|
|
|
b.Source,
|
|
|
|
b.Meta)
|
|
|
|
|
|
|
|
return res.SubscriberID, res.Num, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *runnerDB) BlocklistSubscriber(id int64) error {
|
|
|
|
_, err := r.queries.BlocklistSubscribers.Exec(pq.Int64Array{id})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *runnerDB) DeleteSubscriber(id int64) error {
|
|
|
|
_, err := r.queries.DeleteSubscribers.Exec(pq.Int64Array{id})
|
|
|
|
return err
|
|
|
|
}
|