2022-04-03 23:24:40 +08:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2022-09-03 15:48:02 +08:00
|
|
|
"time"
|
2022-04-03 23:24:40 +08:00
|
|
|
|
2022-10-19 00:14:57 +08:00
|
|
|
"github.com/knadh/listmonk/models"
|
2022-04-03 23:24:40 +08:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
2022-10-19 00:14:57 +08:00
|
|
|
// GetSubscriptions retrieves the subscriptions for a subscriber.
|
|
|
|
func (c *Core) GetSubscriptions(subID int, subUUID string, allLists bool) ([]models.Subscription, error) {
|
|
|
|
var out []models.Subscription
|
|
|
|
err := c.q.GetSubscriptions.Select(&out, subID, subUUID, allLists)
|
|
|
|
if err != nil {
|
|
|
|
c.log.Printf("error getting subscriptions: %v", err)
|
|
|
|
return nil, echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
c.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.subscribers}", "error", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
|
2022-04-03 23:24:40 +08:00
|
|
|
// AddSubscriptions adds list subscriptions to subscribers.
|
|
|
|
func (c *Core) AddSubscriptions(subIDs, listIDs []int, status string) error {
|
|
|
|
if _, err := c.q.AddSubscribersToLists.Exec(pq.Array(subIDs), pq.Array(listIDs), status); err != nil {
|
|
|
|
c.log.Printf("error adding subscriptions: %v", err)
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
c.i18n.Ts("globals.messages.errorUpdating", "name", "{globals.terms.subscribers}", "error", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddSubscriptionsByQuery adds list subscriptions to subscribers by a given arbitrary query expression.
|
|
|
|
// sourceListIDs is the list of list IDs to filter the subscriber query with.
|
2024-09-19 13:26:56 +08:00
|
|
|
func (c *Core) AddSubscriptionsByQuery(query string, sourceListIDs, targetListIDs []int, status string, subStatus string) error {
|
2022-04-03 23:24:40 +08:00
|
|
|
if sourceListIDs == nil {
|
|
|
|
sourceListIDs = []int{}
|
|
|
|
}
|
|
|
|
|
2024-09-19 13:26:56 +08:00
|
|
|
err := c.q.ExecSubQueryTpl(sanitizeSQLExp(query), c.q.AddSubscribersToListsByQuery, sourceListIDs, c.db, subStatus, pq.Array(targetListIDs), status)
|
2022-04-03 23:24:40 +08:00
|
|
|
if err != nil {
|
|
|
|
c.log.Printf("error adding subscriptions by query: %v", err)
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
c.i18n.Ts("globals.messages.errorUpdating", "name", "{globals.terms.subscribers}", "error", pqErrMsg(err)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSubscriptions delete list subscriptions from subscribers.
|
|
|
|
func (c *Core) DeleteSubscriptions(subIDs, listIDs []int) error {
|
|
|
|
if _, err := c.q.DeleteSubscriptions.Exec(pq.Array(subIDs), pq.Array(listIDs)); err != nil {
|
|
|
|
c.log.Printf("error deleting subscriptions: %v", err)
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
c.i18n.Ts("globals.messages.errorUpdating", "name", "{globals.terms.subscribers}", "error", err.Error()))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSubscriptionsByQuery deletes list subscriptions from subscribers by a given arbitrary query expression.
|
|
|
|
// sourceListIDs is the list of list IDs to filter the subscriber query with.
|
2024-09-19 13:26:56 +08:00
|
|
|
func (c *Core) DeleteSubscriptionsByQuery(query string, sourceListIDs, targetListIDs []int, subStatus string) error {
|
2022-04-03 23:24:40 +08:00
|
|
|
if sourceListIDs == nil {
|
|
|
|
sourceListIDs = []int{}
|
|
|
|
}
|
|
|
|
|
2024-09-19 13:26:56 +08:00
|
|
|
err := c.q.ExecSubQueryTpl(sanitizeSQLExp(query), c.q.DeleteSubscriptionsByQuery, sourceListIDs, c.db, subStatus, pq.Array(targetListIDs))
|
2022-04-03 23:24:40 +08:00
|
|
|
if err != nil {
|
|
|
|
c.log.Printf("error deleting subscriptions by query: %v", err)
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
c.i18n.Ts("globals.messages.errorUpdating", "name", "{globals.terms.subscribers}", "error", pqErrMsg(err)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnsubscribeLists sets list subscriptions to 'unsubscribed'.
|
2022-10-19 00:14:57 +08:00
|
|
|
func (c *Core) UnsubscribeLists(subIDs, listIDs []int, listUUIDs []string) error {
|
|
|
|
if _, err := c.q.UnsubscribeSubscribersFromLists.Exec(pq.Array(subIDs), pq.Array(listIDs), pq.StringArray(listUUIDs)); err != nil {
|
2022-04-03 23:24:40 +08:00
|
|
|
c.log.Printf("error unsubscribing from lists: %v", err)
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
c.i18n.Ts("globals.messages.errorUpdating", "name", "{globals.terms.subscribers}", "error", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-21 02:10:34 +08:00
|
|
|
// UnsubscribeListsByQuery sets list subscriptions to 'unsubscribed' by a given arbitrary query expression.
|
2022-04-03 23:24:40 +08:00
|
|
|
// sourceListIDs is the list of list IDs to filter the subscriber query with.
|
2024-09-19 13:26:56 +08:00
|
|
|
func (c *Core) UnsubscribeListsByQuery(query string, sourceListIDs, targetListIDs []int, subStatus string) error {
|
2022-04-03 23:24:40 +08:00
|
|
|
if sourceListIDs == nil {
|
|
|
|
sourceListIDs = []int{}
|
|
|
|
}
|
|
|
|
|
2024-09-19 13:26:56 +08:00
|
|
|
err := c.q.ExecSubQueryTpl(sanitizeSQLExp(query), c.q.UnsubscribeSubscribersFromListsByQuery, sourceListIDs, c.db, subStatus, pq.Array(targetListIDs))
|
2022-04-03 23:24:40 +08:00
|
|
|
if err != nil {
|
2023-06-21 02:10:34 +08:00
|
|
|
c.log.Printf("error unsubscribing from lists by query: %v", err)
|
2022-04-03 23:24:40 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
c.i18n.Ts("globals.messages.errorUpdating", "name", "{globals.terms.subscribers}", "error", pqErrMsg(err)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-03 15:48:02 +08:00
|
|
|
|
2023-06-21 02:10:34 +08:00
|
|
|
// DeleteUnconfirmedSubscriptions sets list subscriptions to 'unsubscribed' by a given arbitrary query expression.
|
2022-09-03 15:48:02 +08:00
|
|
|
// sourceListIDs is the list of list IDs to filter the subscriber query with.
|
|
|
|
func (c *Core) DeleteUnconfirmedSubscriptions(beforeDate time.Time) (int, error) {
|
|
|
|
res, err := c.q.DeleteUnconfirmedSubscriptions.Exec(beforeDate)
|
|
|
|
if err != nil {
|
|
|
|
c.log.Printf("error deleting unconfirmed subscribers: %v", err)
|
|
|
|
return 0, echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
c.i18n.Ts("globals.messages.errorDeleting", "name", "{globals.terms.subscribers}", "error", pqErrMsg(err)))
|
|
|
|
}
|
|
|
|
|
|
|
|
n, _ := res.RowsAffected()
|
|
|
|
return int(n), nil
|
|
|
|
}
|