Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[server] Add admin endpoint to find mailing list subscribers with no account

Neeraj Guptae14d52255dbe
diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go
index 86986fca5a..27e8db6da9 100644
--- a/server/cmd/museum/main.go
+++ b/server/cmd/museum/main.go
@@ -860,6 +860,7 @@ func main() {
adminAPI.POST("/mail", adminHandler.SendMail)
adminAPI.POST("/mail/subscribe", adminHandler.SubscribeMail)
adminAPI.POST("/mail/unsubscribe", adminHandler.UnsubscribeMail)
+ adminAPI.GET("/listmonk/missing-subscribers/count", adminHandler.GetListmonkMissingSubscribersCount)
adminAPI.GET("/users", adminHandler.GetUsers)
adminAPI.GET("/user", adminHandler.GetUser)
adminAPI.POST("/user/disable-2fa", adminHandler.DisableTwoFactor)
diff --git a/server/pkg/api/admin_listmonk.go b/server/pkg/api/admin_listmonk.go
new file mode 100644
index 0000000000..2ad14b6e6b
--- /dev/null
+++ b/server/pkg/api/admin_listmonk.go
@@ -0,0 +1,368 @@
+package api
+
+import (
+ "fmt"
+ "net/http"
+ "strconv"
+ "strings"
+ "sync/atomic"
+
+ "github.com/ente-io/museum/ente"
+ "github.com/ente-io/museum/pkg/external/listmonk"
+ "github.com/ente-io/museum/pkg/utils/auth"
+ "github.com/ente-io/museum/pkg/utils/crypto"
+ "github.com/ente-io/museum/pkg/utils/handler"
+ "github.com/ente-io/museum/pkg/utils/time"
+ "github.com/ente-io/stacktrace"
+ "github.com/gin-contrib/requestid"
+ "github.com/gin-gonic/gin"
+ "github.com/sirupsen/logrus"
+ "github.com/spf13/viper"
+)
+
+const listmonkMissingSubscribersDefaultPageSize = 10000
+const listmonkMissingSubscribersMaxLoggedEmails = 10
+
+var listminkMissingSubscribersInFlight atomic.Bool
+
+type listminkMissingSubscribersJobConfig struct {
+ Endpoint string
+ Username string
+ Password string
+ PageSize int
+ HasUpperLimit bool
+ UpperLimit int
+ LogMissingEmails bool
+ TargetListIDSet map[int]struct{}
+}
+
+type listminkMissingSubscribersJobSummary struct {
+ StartedAt int64
+ CompletedAt int64
+ MissingCount int
+ MatchedCount int
+ ScannedCount int
+ FetchedCount int
+ PagesFetched int
+ ListmonkTotal int
+ StopReason string
+ Capped bool
+}
+
+func (h *AdminHandler) GetListmonkMissingSubscribersCount(c *gin.Context) {
+ baseURL := strings.TrimRight(viper.GetString("listmonk.server-url"), "/")
+ username := viper.GetString("listmonk.username")
+ password := viper.GetString("listmonk.password")
+ listIDs := viper.GetIntSlice("listmonk.list-ids")
+ pageSize := listmonkMissingSubscribersDefaultPageSize
+ if pageSizeParam := strings.TrimSpace(c.Query("pageSize")); pageSizeParam != "" {
+ parsedPageSize, err := strconv.Atoi(pageSizeParam)
+ if err != nil || parsedPageSize <= 0 {
+ handler.Error(c, stacktrace.Propagate(
+ ente.NewBadRequestWithMessage("invalid pageSize query param, expected positive integer"),
+ "",
+ ))
+ return
+ }
+ pageSize = parsedPageSize
+ }
+
+ hasUpperLimit := false
+ upperLimit := 0
+ if upperLimitParam := strings.TrimSpace(c.Query("upperLimit")); upperLimitParam != "" {
+ parsedUpperLimit, err := strconv.Atoi(upperLimitParam)
+ if err != nil || parsedUpperLimit <= 0 {
+ handler.Error(c, stacktrace.Propagate(
+ ente.NewBadRequestWithMessage("invalid upperLimit query param, expected positive integer"),
+ "",
+ ))
+ return
+ }
+ hasUpperLimit = true
+ upperLimit = parsedUpperLimit
+ }
+
+ logMissingEmails := false
+ if logMissingEmailsParam := strings.TrimSpace(c.Query("logMissingEmails")); logMissingEmailsParam != "" {
+ parsed, err := strconv.ParseBool(logMissingEmailsParam)
+ if err != nil {
+ handler.Error(c, stacktrace.Propagate(
+ ente.NewBadRequestWithMessage("invalid logMissingEmails query param, expected boolean"),
+ "",
+ ))
+ return
+ }
+ logMissingEmails = parsed
+ }
+
+ upperLimitLogValue := interface{}("none")
+ if hasUpperLimit {
+ upperLimitLogValue = upperLimit
+ }
+ upperLimitResponseValue := interface{}(nil)
+ if hasUpperLimit {
+ upperLimitResponseValue = upperLimit
+ }
+
+ adminID := auth.GetUserID(c.Request.Header)
+ reqID := requestid.Get(c)
+ logger := logrus.WithFields(logrus.Fields{
+ "admin_id": adminID,
+ "req_id": reqID,
+ "req_ctx": "listmonk_missing_subscribers_count",
+ "page_size": pageSize,
+ "upper_limit": upperLimitLogValue,
+ "log_missing": logMissingEmails,
+ "listmonk_user": username,
+ })
+
+ if baseURL == "" || username == "" || password == "" || len(listIDs) == 0 {
+ logger.Warn("listmonk missing-subscribers count requested without required listmonk config")
+ handler.Error(c, stacktrace.Propagate(
+ ente.NewBadRequestWithMessage("listmonk is not configured (server-url, username, password, list-ids are required)"),
+ "",
+ ))
+ return
+ }
+
+ if !listminkMissingSubscribersInFlight.CompareAndSwap(false, true) {
+ logger.Warn("listmonk missing-subscribers job already in progress on this host")
+ c.JSON(http.StatusConflict, gin.H{
+ "error": "listmonk missing-subscribers processing is already in progress on this host",
+ })
+ return
+ }
+
+ targetListSet := make(map[int]struct{}, len(listIDs))
+ for _, listID := range listIDs {
+ targetListSet[listID] = struct{}{}
+ }
+
+ jobConfig := listminkMissingSubscribersJobConfig{
+ Endpoint: baseURL + "/api/subscribers",
+ Username: username,
+ Password: password,
+ PageSize: pageSize,
+ HasUpperLimit: hasUpperLimit,
+ UpperLimit: upperLimit,
+ LogMissingEmails: logMissingEmails,
+ TargetListIDSet: targetListSet,
+ }
+
+ go h.runListminkMissingSubscribersJobAsync(logger, jobConfig)
+
+ responsePayload := gin.H{
+ "status": "started",
+ "requestID": reqID,
+ "pageSize": pageSize,
+ "upperLimit": upperLimitResponseValue,
+ "logMissingEmails": logMissingEmails,
+ "acceptedAt": time.Microseconds(),
+ }
+ logger.WithField("response_payload", responsePayload).Info("accepted listmonk missing-subscribers async job")
+ c.JSON(http.StatusAccepted, responsePayload)
+}
+
+func (h *AdminHandler) runListminkMissingSubscribersJobAsync(logger *logrus.Entry, cfg listminkMissingSubscribersJobConfig) {
+ defer func() {
+ if r := recover(); r != nil {
+ logger.WithField("panic", r).Error("listmonk missing-subscribers async job panicked")
+ if h.DiscordController != nil {
+ h.DiscordController.Notify(fmt.Sprintf("Listmonk missing-subscribers job panicked on `%s`", h.DiscordController.HostName))
+ }
+ }
+ listminkMissingSubscribersInFlight.Store(false)
+ }()
+
+ logger.Info("starting listmonk missing-subscribers async job")
+ summary, err := h.runListminkMissingSubscribersJob(cfg, logger)
+ if err != nil {
+ logger.WithError(err).Error("listmonk missing-subscribers async job failed")
+ if h.DiscordController != nil {
+ h.DiscordController.Notify(
+ fmt.Sprintf("Listmonk missing-subscribers job failed on `%s`", h.DiscordController.HostName),
+ )
+ }
+ return
+ }
+
+ durationMs := (summary.CompletedAt - summary.StartedAt) / 1000
+ logger.WithFields(logrus.Fields{
+ "pages_fetched": summary.PagesFetched,
+ "fetched_count": summary.FetchedCount,
+ "scanned_count": summary.ScannedCount,
+ "matched_count": summary.MatchedCount,
+ "missing_count": summary.MissingCount,
+ "listmonk_total": summary.ListmonkTotal,
+ "stop_reason": summary.StopReason,
+ "capped": summary.Capped,
+ "duration_ms": durationMs,
+ }).Info("completed listmonk missing-subscribers async job")
+
+ if h.DiscordController != nil {
+ // Summary intentionally contains no PII.
+ h.DiscordController.Notify(fmt.Sprintf(
+ "Listmonk missing-subscribers summary on `%s`: missing=%d, matched=%d, scanned=%d, fetched=%d, pages=%d, total=%d, capped=%t, stop_reason=%s, duration_ms=%d",
+ h.DiscordController.HostName,
+ summary.MissingCount,
+ summary.MatchedCount,
+ summary.ScannedCount,
+ summary.FetchedCount,
+ summary.PagesFetched,
+ summary.ListmonkTotal,
+ summary.Capped,
+ summary.StopReason,
+ durationMs,
+ ))
+ }
+}
+
+func (h *AdminHandler) runListminkMissingSubscribersJob(cfg listminkMissingSubscribersJobConfig, logger *logrus.Entry) (listminkMissingSubscribersJobSummary, error) {
+ summary := listminkMissingSubscribersJobSummary{
+ StartedAt: time.Microseconds(),
+ StopReason: "exhausted_pages",
+ }
+
+ pageNumber := 1
+ seenHashes := make(map[string]struct{})
+
+ for {
+ if cfg.HasUpperLimit && summary.FetchedCount >= cfg.UpperLimit {
+ summary.StopReason = "upper_limit_reached"
+ summary.Capped = true
+ logger.WithFields(logrus.Fields{
+ "fetched_count": summary.FetchedCount,
+ "stop_reason": summary.StopReason,
+ }).Info("stopping listmonk scan after upper limit")
+ break
+ }
+
+ page, err := listmonk.ListSubscribers(cfg.Endpoint, cfg.Username, cfg.Password, pageNumber, cfg.PageSize)
+ if err != nil {
+ return summary, stacktrace.Propagate(err, "failed to fetch listmonk subscribers page %d", pageNumber)
+ }
+
+ summary.PagesFetched++
+ if pageNumber == 1 {
+ summary.ListmonkTotal = page.Total
+ }
+ originalPageResultsCount := len(page.Results)
+ if originalPageResultsCount == 0 {
+ summary.StopReason = "empty_page"
+ break
+ }
+
+ pageResults := page.Results
+ if cfg.HasUpperLimit {
+ remaining := cfg.UpperLimit - summary.FetchedCount
+ if remaining < len(pageResults) {
+ pageResults = pageResults[:remaining]
+ summary.Capped = true
+ summary.StopReason = "upper_limit_reached"
+ }
+ }
+ summary.FetchedCount += len(pageResults)
+
+ pageHashes := make([]string, 0, len(pageResults))
+ pageHashToEmail := make(map[string]string, len(pageResults))
+ for _, subscriber := range pageResults {
+ if !subscriberInTargetLists(subscriber.ListIDs, cfg.TargetListIDSet) {
+ continue
+ }
+ email := normalizeEmailForHash(subscriber.Email)
+ if email == "" {
+ continue
+ }
+ emailHash, err := crypto.GetHash(email, h.HashingKey)
+ if err != nil {
+ return summary, stacktrace.Propagate(err, "failed to hash subscriber email")
+ }
+ if _, exists := seenHashes[emailHash]; exists {
+ continue
+ }
+ seenHashes[emailHash] = struct{}{}
+ pageHashes = append(pageHashes, emailHash)
+ pageHashToEmail[emailHash] = email
+ }
+
+ summary.ScannedCount += len(pageHashes)
+ matchedHashes, err := h.UserRepo.GetActiveUserEmailHashes(pageHashes)
+ if err != nil {
+ return summary, stacktrace.Propagate(err, "failed to count matched users")
+ }
+ pageMatchedCount := len(matchedHashes)
+ summary.MatchedCount += pageMatchedCount
+ matchedHashSet := make(map[string]struct{}, len(matchedHashes))
+ for _, hash := range matchedHashes {
+ matchedHashSet[hash] = struct{}{}
+ }
+
+ pageMissingCount := len(pageHashes) - pageMatchedCount
+ if cfg.LogMissingEmails && pageMissingCount > 0 {
+ missingEmailSamples := make([]string, 0, listmonkMissingSubscribersMaxLoggedEmails)
+ for _, hash := range pageHashes {
+ if _, exists := matchedHashSet[hash]; exists {
+ continue
+ }
+ missingEmailSamples = append(missingEmailSamples, pageHashToEmail[hash])
+ if len(missingEmailSamples) >= listmonkMissingSubscribersMaxLoggedEmails {
+ break
+ }
+ }
+ logger.WithFields(logrus.Fields{
+ "page": pageNumber,
+ "page_missing_count": pageMissingCount,
+ "missing_email_sample": missingEmailSamples,
+ "sample_count": len(missingEmailSamples),
+ }).Info("missing emails sample for listmonk page")
+ }
+
+ logger.WithFields(logrus.Fields{
+ "page": pageNumber,
+ "page_results": originalPageResultsCount,
+ "processed_results": len(pageResults),
+ "page_unique_hashes": len(pageHashes),
+ "page_matched": pageMatchedCount,
+ "page_missing": pageMissingCount,
+ "fetched_count": summary.FetchedCount,
+ "scanned_count": summary.ScannedCount,
+ "matched_count": summary.MatchedCount,
+ }).Info("processed listmonk subscribers page")
+
+ if summary.StopReason == "upper_limit_reached" {
+ logger.WithFields(logrus.Fields{
+ "fetched_count": summary.FetchedCount,
+ "stop_reason": summary.StopReason,
+ }).Info("stopping listmonk scan after upper limit")
+ break
+ }
+
+ effectivePerPage := cfg.PageSize
+ if page.PerPage > 0 {
+ effectivePerPage = page.PerPage
+ }
+ if originalPageResultsCount < effectivePerPage {
+ summary.StopReason = "last_partial_page"
+ break
+ }
+ pageNumber++
+ }
+
+ summary.CompletedAt = time.Microseconds()
+ summary.MissingCount = summary.ScannedCount - summary.MatchedCount
+ logger.WithField("missing_count", summary.MissingCount).Info("final missing subscribers count for async job")
+ return summary, nil
+}
+
+func normalizeEmailForHash(email string) string {
+ return strings.ToLower(email)
+}
+
+func subscriberInTargetLists(subscriberListIDs []int, targetListSet map[int]struct{}) bool {
+ for _, subscriberListID := range subscriberListIDs {
+ if _, ok := targetListSet[subscriberListID]; ok {
+ return true
+ }
+ }
+ return false
+}
diff --git a/server/pkg/external/listmonk/api.go b/server/pkg/external/listmonk/api.go
index 43bf548c77..fb0bb190c9 100644
--- a/server/pkg/external/listmonk/api.go
+++ b/server/pkg/external/listmonk/api.go
@@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/url"
+ "strconv"
"strings"
"github.com/ente-io/stacktrace"
@@ -24,6 +25,20 @@ type Credentials struct {
Password string
}
+// Subscriber captures a listmonk subscriber record required by museum.
+type Subscriber struct {
+ Email string
+ ListIDs []int
+}
+
+// SubscribersPage represents one paginated subscribers page from listmonk.
+type SubscribersPage struct {
+ Results []Subscriber
+ Total int
+ Page int
+ PerPage int
+}
+
// GetSubscriberID returns subscriber id of the provided email address,
// else returns an error if email was not found
func GetSubscriberID(endpoint string, username string, password string, subscriberEmail string) (int, error) {
@@ -89,6 +104,153 @@ func GetSubscriberID(endpoint string, username string, password string, subscrib
return id, nil
}
+// ListSubscribers returns one paginated subscribers page from listmonk.
+func ListSubscribers(endpoint string, username string, password string, page int, perPage int) (SubscribersPage, error) {
+ type subscriberResponse struct {
+ Data struct {
+ Results []map[string]json.RawMessage `json:"results"`
+ Total int `json:"total"`
+ Page int `json:"page"`
+ PerPage int `json:"per_page"`
+ } `json:"data"`
+ }
+
+ endpointURL, err := url.Parse(endpoint)
+ if err != nil {
+ return SubscribersPage{}, stacktrace.Propagate(err, "")
+ }
+
+ queryParams := endpointURL.Query()
+ queryParams.Set("page", strconv.Itoa(page))
+ queryParams.Set("per_page", strconv.Itoa(perPage))
+ endpointURL.RawQuery = queryParams.Encode()
+
+ req, err := http.NewRequest("GET", endpointURL.String(), nil)
+ if err != nil {
+ return SubscribersPage{}, stacktrace.Propagate(err, "")
+ }
+ req.SetBasicAuth(username, password)
+
+ client := &http.Client{}
+ resp, err := client.Do(req)
+ if err != nil {
+ return SubscribersPage{}, stacktrace.Propagate(err, "")
+ }
+ defer resp.Body.Close()
+
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return SubscribersPage{}, stacktrace.Propagate(err, "")
+ }
+ if resp.StatusCode != http.StatusOK {
+ return SubscribersPage{}, stacktrace.Propagate(
+ fmt.Errorf("listmonk request failed with status %d: %s", resp.StatusCode, strings.TrimSpace(string(body))),
+ "",
+ )
+ }
+
+ var parsed subscriberResponse
+ if err := json.Unmarshal(body, &parsed); err != nil {
+ return SubscribersPage{}, stacktrace.Propagate(err, "")
+ }
+
+ results := make([]Subscriber, 0, len(parsed.Data.Results))
+ for _, row := range parsed.Data.Results {
+ subscriber, parseErr := parseSubscriber(row)
+ if parseErr != nil {
+ return SubscribersPage{}, stacktrace.Propagate(parseErr, "")
+ }
+ results = append(results, subscriber)
+ }
+
+ return SubscribersPage{
+ Results: results,
+ Total: parsed.Data.Total,
+ Page: parsed.Data.Page,
+ PerPage: parsed.Data.PerPage,
+ }, nil
+}
+
+func parseSubscriber(data map[string]json.RawMessage) (Subscriber, error) {
+ emailRaw, ok := data["email"]
+ if !ok {
+ return Subscriber{}, stacktrace.Propagate(errors.New("missing email in listmonk subscriber payload"), "")
+ }
+ var email string
+ if err := json.Unmarshal(emailRaw, &email); err != nil {
+ return Subscriber{}, stacktrace.Propagate(err, "failed to parse listmonk subscriber email")
+ }
+
+ var listIDs []int
+ if listsRaw, ok := data["lists"]; ok {
+ ids, err := decodeListIDs(listsRaw)
+ if err != nil {
+ return Subscriber{}, stacktrace.Propagate(err, "failed to parse listmonk subscriber lists")
+ }
+ listIDs = ids
+ } else if listIDsRaw, ok := data["list_ids"]; ok {
+ ids, err := decodeListIDs(listIDsRaw)
+ if err != nil {
+ return Subscriber{}, stacktrace.Propagate(err, "failed to parse listmonk subscriber list_ids")
+ }
+ listIDs = ids
+ }
+
+ return Subscriber{
+ Email: email,
+ ListIDs: listIDs,
+ }, nil
+}
+
+func decodeListIDs(raw json.RawMessage) ([]int, error) {
+ type listWithID struct {
+ ID int `json:"id"`
+ }
+
+ if len(raw) == 0 || string(raw) == "null" {
+ return []int{}, nil
+ }
+
+ var directIntIDs []int
+ if err := json.Unmarshal(raw, &directIntIDs); err == nil {
+ return directIntIDs, nil
+ }
+
+ var objectIDs []listWithID
+ if err := json.Unmarshal(raw, &objectIDs); err == nil {
+ ids := make([]int, 0, len(objectIDs))
+ for _, item := range objectIDs {
+ ids = append(ids, item.ID)
+ }
+ return ids, nil
+ }
+
+ var generic []map[string]interface{}
+ if err := json.Unmarshal(raw, &generic); err == nil {
+ ids := make([]int, 0, len(generic))
+ for _, item := range generic {
+ value, ok := item["id"]
+ if !ok {
+ continue
+ }
+ switch typed := value.(type) {
+ case float64:
+ ids = append(ids, int(typed))
+ case int:
+ ids = append(ids, typed)
+ case string:
+ id, convErr := strconv.Atoi(typed)
+ if convErr == nil {
+ ids = append(ids, id)
+ }
+ }
+ }
+ return ids, nil
+ }
+
+ return nil, errors.New("unsupported list ids format")
+}
+
// SendRequest sends a request to the specified Listmonk API endpoint
// with the provided method and data
// after authentication with the provided credentials (username, password)
diff --git a/server/pkg/repo/user.go b/server/pkg/repo/user.go
index 7e9fbf0090..f797cc1f97 100644
--- a/server/pkg/repo/user.go
+++ b/server/pkg/repo/user.go
@@ -544,6 +544,56 @@ func (repo *UserRepository) GetEmailsFromHashes(hashes []string) ([]string, erro
return emails, nil
}
+// CountActiveUsersByEmailHashes returns count of active users matching any of
+// the provided hashes.
+func (repo *UserRepository) CountActiveUsersByEmailHashes(hashes []string) (int, error) {
+ if len(hashes) == 0 {
+ return 0, nil
+ }
+ var count int
+ err := repo.DB.QueryRow(`
+ SELECT COUNT(*)
+ FROM users
+ WHERE encrypted_email IS NOT NULL
+ AND email_hash = ANY($1);
+ `, pq.Array(hashes)).Scan(&count)
+ if err != nil {
+ return 0, stacktrace.Propagate(err, "")
+ }
+ return count, nil
+}
+
+// GetActiveUserEmailHashes returns active user email hashes present in the
+// provided list of hashes.
+func (repo *UserRepository) GetActiveUserEmailHashes(hashes []string) ([]string, error) {
+ if len(hashes) == 0 {
+ return []string{}, nil
+ }
+ rows, err := repo.DB.Query(`
+ SELECT email_hash
+ FROM users
+ WHERE encrypted_email IS NOT NULL
+ AND email_hash = ANY($1);
+ `, pq.Array(hashes))
+ if err != nil {
+ return nil, stacktrace.Propagate(err, "")
+ }
+ defer rows.Close()
+
+ matchedHashes := make([]string, 0, len(hashes))
+ for rows.Next() {
+ var emailHash string
+ if err := rows.Scan(&emailHash); err != nil {
+ return nil, stacktrace.Propagate(err, "")
+ }
+ matchedHashes = append(matchedHashes, emailHash)
+ }
+ if err := rows.Err(); err != nil {
+ return nil, stacktrace.Propagate(err, "")
+ }
+ return matchedHashes, nil
+}
+
// GetActiveUsersForIds returns a map of users by their IDs, similar to GetUserByID
func (repo *UserRepository) GetActiveUsersForIds(id []int64) (map[int64]*ente.User, error) {
result := make(map[int64]*ente.User)