Skip to content

armgit / ente

Public Imported from GitHub
0

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

Remove Zoho Campaigns support

Manav Rathi3ed0b8dc275d
diff --git a/server/configurations/local.yaml b/server/configurations/local.yaml
index 8e4b391db0..4457831af6 100644
--- a/server/configurations/local.yaml
+++ b/server/configurations/local.yaml
@@ -357,15 +357,6 @@ discord:
token:
channel:
-# Zoho Campaigns config (optional)
-# Use case: Sending emails
-zoho:
- client-id:
- client-secret:
- refresh-token:
- list-key:
- topic-ids:
-
# Listmonk Campaigns config (optional)
# Use case: Sending emails
listmonk:
diff --git a/server/pkg/controller/mailing_lists.go b/server/pkg/controller/mailing_lists.go
index e027e57a53..1c6f3004e8 100644
--- a/server/pkg/controller/mailing_lists.go
+++ b/server/pkg/controller/mailing_lists.go
@@ -3,31 +3,16 @@ package controller
import (
"errors"
"fmt"
- "net/url"
"strconv"
- "strings"
"github.com/ente/museum/pkg/controller/discord"
"github.com/ente/museum/pkg/external/listmonk"
- "github.com/ente/museum/pkg/external/zoho"
"github.com/ente/stacktrace"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
-// MailingListsController is used to keeping the external mailing lists in sync
-// with customer email changes.
-//
-// MailingListsController contains methods for keeping external mailing lists in
-// sync when new users sign up, or update their email, or delete their account.
-// Currently, these mailing lists are hosted on Zoho Campaigns.
-//
-// See also: Syncing emails with Zoho Campaigns
type MailingListsController struct {
- zohoAccessToken string
- zohoListKey string
- zohoTopicIds string
- zohoCredentials zoho.Credentials
listmonkListIDs []int
listmonkCredentials listmonk.Credentials
discordController *discord.DiscordController
@@ -35,34 +20,6 @@ type MailingListsController struct {
// Return a new instance of MailingListsController
func NewMailingListsController(discordController *discord.DiscordController) *MailingListsController {
- zohoCredentials := zoho.Credentials{
- ClientID: viper.GetString("zoho.client-id"),
- ClientSecret: viper.GetString("zoho.client-secret"),
- RefreshToken: viper.GetString("zoho.refresh-token"),
- }
-
- // The Zoho "List Key" identifies a particular list of email IDs that are
- // stored in Zoho. All the actions that we perform (adding, removing and
- // updating emails) are done on this list.
- //
- // https://www.zoho.com/campaigns/help/developers/list-management.html
- zohoListKey := viper.GetString("zoho.list-key")
-
- // List of topics to which emails are sent.
- //
- // Ostensibly, we can get them from their API
- // https://www.zoho.com/campaigns/oldhelp/api/get-topics.html
- //
- // But that doesn't currently work, luckily we can get these IDs by looking
- // at the HTML source of the topic update dashboard page.
- zohoTopicIds := viper.GetString("zoho.topic-ids")
-
- // Zoho has a rate limit on the number of access tokens that can created
- // within a given time period. So as an aid in debugging, allow the access
- // token to be passed in. This will not be present in production - there
- // we'll use the refresh token to create an access token on demand.
- zohoAccessToken := viper.GetString("zoho.access_token")
-
listmonkCredentials := listmonk.Credentials{
BaseURL: viper.GetString("listmonk.server-url"),
Username: viper.GetString("listmonk.username"),
@@ -74,43 +31,13 @@ func NewMailingListsController(discordController *discord.DiscordController) *Ma
listmonkListIDs := viper.GetIntSlice("listmonk.list-ids")
return &MailingListsController{
- zohoCredentials: zohoCredentials,
- zohoListKey: zohoListKey,
- zohoTopicIds: zohoTopicIds,
- zohoAccessToken: zohoAccessToken,
listmonkCredentials: listmonkCredentials,
listmonkListIDs: listmonkListIDs,
discordController: discordController,
}
}
-// Add the given email address to our default Zoho Campaigns list
-// or Listmonk Campaigns List
-//
-// It is valid to resubscribe an email that has previously been unsubscribe.
-//
-// # Syncing emails with Zoho Campaigns
-//
-// Zoho Campaigns does not support maintaining a list of raw email addresses
-// that can be later updated or deleted via their API. So instead, we maintain
-// the email addresses of our customers in a Zoho Campaign "list", and subscribe
-// or unsubscribe them to this list.
func (c *MailingListsController) Subscribe(email string) error {
- if !(c.shouldSkipZoho()) {
- // Need to set "Signup Form Disabled" in the list settings since we use this
- // list to keep track of emails that have already been verified.
- //
- // > You can use this API to add contacts to your mailing lists. For signup
- // form enabled mailing lists, the contacts will receive a confirmation
- // email. For signup form disabled lists, contacts will be added without
- // any confirmations.
- //
- // https://www.zoho.com/campaigns/help/developers/contact-subscribe.html
- err := c.doListActionZoho("listsubscribe", email)
- if err != nil {
- return stacktrace.Propagate(err, "")
- }
- }
if !(c.shouldSkipListmonk()) {
err := c.listmonkSubscribe(email)
if err != nil {
@@ -120,18 +47,7 @@ func (c *MailingListsController) Subscribe(email string) error {
return nil
}
-// Unsubscribe the given email address to our default Zoho Campaigns list
-// or Listmonk Campaigns List
-//
-// See: [Note: Syncing emails with Zoho Campaigns]
func (c *MailingListsController) Unsubscribe(email string) error {
- if !(c.shouldSkipZoho()) {
- // https://www.zoho.com/campaigns/help/developers/contact-unsubscribe.html
- err := c.doListActionZoho("listunsubscribe", email)
- if err != nil {
- return stacktrace.Propagate(err, "")
- }
- }
if !(c.shouldSkipListmonk()) {
err := c.listmonkUnsubscribe(email)
if err != nil {
@@ -141,12 +57,6 @@ func (c *MailingListsController) Unsubscribe(email string) error {
return nil
}
-// shouldSkipZoho() checks if the MailingListsController
-// should be skipped due to missing credentials.
-func (c *MailingListsController) shouldSkipZoho() bool {
- return c.zohoCredentials.RefreshToken == ""
-}
-
// shouldSkipListmonk() checks if the Listmonk mailing list
// should be skipped due to missing credentials
// listmonklistIDs value.
@@ -163,53 +73,6 @@ func (c *MailingListsController) shouldSkipListmonk() bool {
return false
}
-// Both the listsubscribe and listunsubscribe Zoho Campaigns API endpoints work
-// similarly, so use this function to keep the common code.
-func (c *MailingListsController) doListActionZoho(action string, email string) error {
- // Query escape the email so that any pluses get converted to %2B.
- escapedEmail := url.QueryEscape(email)
- contactInfo := fmt.Sprintf("{Contact+Email: \"%s\"}", escapedEmail)
- // Instead of using QueryEscape, use PathEscape. QueryEscape escapes the "+"
- // character, which causes Zoho API to not recognize the parameter.
- escapedContactInfo := url.PathEscape(contactInfo)
-
- url := fmt.Sprintf(
- "https://campaigns.zoho.com/api/v1.1/json/%s?resfmt=JSON&listkey=%s&contactinfo=%s&topic_id=%s",
- action, c.zohoListKey, escapedContactInfo, c.zohoTopicIds)
-
- zohoAccessToken, err := zoho.DoRequest("POST", url, c.zohoAccessToken, c.zohoCredentials)
- c.zohoAccessToken = zohoAccessToken
-
- if err != nil {
- // This is not necessarily an error, and can happen when the customer
- // had earlier unsubscribed from our organization emails in Zoho,
- // selecting the "Erase my data" option. This causes Zoho to remove the
- // customer's entire record from their database.
- //
- // Then later, say if the customer deletes their account from ente, we
- // would try to unsubscribe their email but it wouldn't be present in
- // Zoho, and this API call would've failed.
- //
- // In such a case, Zoho will return the following response:
- //
- // { code":"2103",
- // "message":"Contact does not exist.",
- // "version":"1.1",
- // "uri":"/api/v1.1/json/listunsubscribe",
- // "status":"error"}
- //
- // Special case these to reduce the severity level so as to not cause
- // error log spam.
- if strings.Contains(err.Error(), "Contact does not exist") {
- log.Warnf("Zoho - Could not %s '%s': %s", action, email, err)
- } else {
- log.Errorf("Zoho - Could not %s '%s': %s", action, email, err)
- }
- }
-
- return stacktrace.Propagate(err, "")
-}
-
// Subscribes an email address to a particular listmonk campaign mailing list
func (c *MailingListsController) listmonkSubscribe(email string) error {
data := map[string]interface{}{
diff --git a/server/pkg/controller/user/user.go b/server/pkg/controller/user/user.go
index b31a7f8090..a2c63e440e 100644
--- a/server/pkg/controller/user/user.go
+++ b/server/pkg/controller/user/user.go
@@ -338,7 +338,7 @@ func (c *UserController) handleAccountDeletion(
}
email := user.Email
- // See also: Do not block on mailing list errors
+ // Mailing list failures must not block account deletion.
go func() {
if err := c.MailingListsController.Unsubscribe(email); err != nil {
logger.WithError(err).WithFields(logrus.Fields{
@@ -432,12 +432,7 @@ func (c *UserController) createUser(email string, source *string) (int64, ente.S
if err != nil {
return -1, ente.Subscription{}, stacktrace.Propagate(err, "")
}
- // Do not block on mailing list errors
- //
- // The mailing lists are hosted on a third party (Zoho), so we do not wish
- // to fail user creation in case Zoho is having temporary issues. So we
- // perform these actions async, and ignore errors that happen with them (a
- // notification will be sent to Discord for those).
+ // Mailing list failures must not block user creation.
go func() {
if err := c.MailingListsController.Subscribe(email); err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
diff --git a/server/pkg/controller/user/userauth.go b/server/pkg/controller/user/userauth.go
index b9e2597b26..8b513bcfa2 100644
--- a/server/pkg/controller/user/userauth.go
+++ b/server/pkg/controller/user/userauth.go
@@ -380,12 +380,7 @@ func (c *UserController) UpdateEmail(ctx *gin.Context, userID int64, email strin
}).Error("stripe update email failed")
}
- // Unsubscribe the old email, subscribe the new one.
- //
- // Note that resubscribing the same email after it has been unsubscribed
- // once works fine.
- //
- // See also: Do not block on mailing list errors
+ // Mailing list failures must not block email changes.
go func() {
if err := c.MailingListsController.Unsubscribe(oldEmail); err != nil {
log.WithError(err).WithFields(log.Fields{
diff --git a/server/pkg/external/zoho/api.go b/server/pkg/external/zoho/api.go
deleted file mode 100644
index 1b08d7799c..0000000000
--- a/server/pkg/external/zoho/api.go
+++ /dev/null
@@ -1,155 +0,0 @@
-// The zoho package contains wrappers for the (generic) Zoho API.
-//
-// These are stateless functions that wrap over the HTTP calls that need to be
-// made to obtain data from the Zoho HTTP API. In particular, they contain the
-// code for dealing with access tokens and their renewal.
-package zoho
-
-import (
- "encoding/json"
- "fmt"
- "io"
- "net/http"
-
- "github.com/ente/stacktrace"
- log "github.com/sirupsen/logrus"
-)
-
-// The minimum credentials we need to obtain valid access tokens.
-//
-// To generate these credentials:
-//
-// Create new client in https://api-console.zoho.com/. Use client type
-// "Self-client". This gives us the client id and client secret.
-//
-// Generate an (authorization) code with scope "ZohoCampaigns.contact.WRITE" and
-// portal "Campaigns".
-//
-// Use this authorization code to obtain a refresh/access token pair. Note that
-// we don't have a redirect_uri, so we just use the dummy one that is given in
-// their documentation examples elsewhere.
-// (https://www.zoho.com/accounts/protocol/oauth/web-apps/access-token.html)
-//
-// curl -X POST \
-// 'https://accounts.zoho.com/oauth/v2/token? \
-// client_id=xxx&grant_type=authorization_code&client_secret=yyy \
-// &redirect_uri=https://www.zylker.com/oauthredirect&code=zzz'
-//
-// Save the refresh token. We can later use it to regenerate the access token
-// (Zoho access tokens have a short, 1 hour validity anyway).
-type Credentials struct {
- ClientID string
- ClientSecret string
- RefreshToken string
-}
-
-// Do an HTTP `method` request to `url` using the given accessToken.
-//
-// If the accessToken has expired, use the given credentials to renew it.
-//
-// Return the accessToken (renewed or original) that gets used, and any errors
-// that occurred. If the API returns `status` "success", then error will be nil.
-func DoRequest(method string, url string, accessToken string, credentials Credentials) (string, error) {
- ar, err := doRequestNoRetry(method, url, accessToken, credentials)
- if err != nil {
- return accessToken, stacktrace.Propagate(err, "")
- }
-
- // Code 1007 indicates that the access token has expired
- // ("message":"Unauthorized request.")
- if ar.Status == "error" && ar.Code == "1007" {
- accessToken, err = renewAccessToken(credentials)
- if err != nil {
- return accessToken, stacktrace.Propagate(err, "")
- }
-
- // Try again
- ar, err = doRequestNoRetry(method, url, accessToken, credentials)
- if err != nil {
- return accessToken, stacktrace.Propagate(err, "")
- }
- }
-
- if ar.Status == "success" {
- return accessToken, nil
- }
-
- // Something else went wrong
- return accessToken, stacktrace.NewError(
- "Zoho API returned an non-success status %s (code %s: %s)",
- ar.Status, ar.Code, ar.Message)
-}
-
-// The basic generic fields that we expect in a response from Zoho APIs
-type genericAPIResponse struct {
- Status string `json:"status"`
- Code string `json:"Code"`
- Message string `json:"message"`
-}
-
-func doRequestNoRetry(method string, url string, accessToken string, credentials Credentials) (genericAPIResponse, error) {
- var ar genericAPIResponse
-
- client := &http.Client{}
- req, err := http.NewRequest(method, url, nil)
- if err != nil {
- return ar, stacktrace.Propagate(err, "")
- }
-
- req.Header.Set("Accept", "application/json")
- req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", accessToken))
- res, err := client.Do(req)
- if err != nil {
- return ar, stacktrace.Propagate(err, "")
- }
-
- if res.Body != nil {
- defer res.Body.Close()
- }
-
- body, err := io.ReadAll(res.Body)
- if err != nil {
- return ar, stacktrace.Propagate(err, "")
- }
-
- log.Infof("Zoho %s %s response: %s", method, url, body)
-
- err = json.Unmarshal(body, &ar)
- return ar, stacktrace.Propagate(err, "")
-}
-
-// Obtain a new access token using the given credentials
-func renewAccessToken(credentials Credentials) (string, error) {
- // https://www.zoho.com/crm/developer/docs/api/v3/refresh.html
- url := fmt.Sprintf(
- "https://accounts.zoho.com/oauth/v2/token?refresh_token=%s&client_id=%s&client_secret=%s&grant_type=refresh_token",
- credentials.RefreshToken, credentials.ClientID, credentials.ClientSecret)
-
- type jsonResponse struct {
- AccessToken string `json:"access_token"`
- }
-
- res, err := http.Post(url, "application/json", nil)
- if err != nil {
- return "", stacktrace.Propagate(err, "")
- }
-
- if res.Body != nil {
- defer res.Body.Close()
- }
-
- body, err := io.ReadAll(res.Body)
- if err != nil {
- return "", stacktrace.Propagate(err, "")
- }
-
- var jr jsonResponse
- err = json.Unmarshal(body, &jr)
- if err != nil {
- return "", stacktrace.Propagate(err, "")
- }
-
- log.Infof("Renewed Zoho access token")
-
- return jr.AccessToken, nil
-}