From 9e901c78ef317c0adfafda18ddeb495751626657 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Tue, 7 Jan 2025 11:44:22 +0530 Subject: [PATCH] [server] Send email when link is joined --- server/cmd/museum/main.go | 12 +- server/mail-templates/base.html | 139 ++++++++++++++++++ server/mail-templates/on_link_joined.html | 9 ++ server/pkg/controller/collection.go | 12 +- .../controller/email/email_notification.go | 60 +++++--- 5 files changed, 206 insertions(+), 26 deletions(-) create mode 100644 server/mail-templates/base.html create mode 100644 server/mail-templates/on_link_joined.html diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go index 50c507bcbc..5503879099 100644 --- a/server/cmd/museum/main.go +++ b/server/cmd/museum/main.go @@ -5,11 +5,6 @@ import ( "database/sql" b64 "encoding/base64" "fmt" - "github.com/ente-io/museum/ente/base" - "github.com/ente-io/museum/pkg/controller/emergency" - "github.com/ente-io/museum/pkg/controller/file_copy" - "github.com/ente-io/museum/pkg/controller/filedata" - emergencyRepo "github.com/ente-io/museum/pkg/repo/emergency" "net/http" "os" "os/signal" @@ -19,6 +14,12 @@ import ( "syscall" "time" + "github.com/ente-io/museum/ente/base" + "github.com/ente-io/museum/pkg/controller/emergency" + "github.com/ente-io/museum/pkg/controller/file_copy" + "github.com/ente-io/museum/pkg/controller/filedata" + emergencyRepo "github.com/ente-io/museum/pkg/repo/emergency" + "github.com/ente-io/museum/pkg/repo/two_factor_recovery" "github.com/ente-io/museum/pkg/controller/cast" @@ -305,6 +306,7 @@ func main() { collectionController := &controller.CollectionController{ CollectionRepo: collectionRepo, + EmailCtrl: emailNotificationCtrl, AccessCtrl: accessCtrl, PublicCollectionCtrl: publicCollectionCtrl, UserRepo: userRepo, diff --git a/server/mail-templates/base.html b/server/mail-templates/base.html new file mode 100644 index 0000000000..c74ca0c633 --- /dev/null +++ b/server/mail-templates/base.html @@ -0,0 +1,139 @@ +{{define "base"}} + + + + + + +
+Hello,
+ +{{.OtherUserEmail}} has joined your album via a public link.
+ +You can revoke access or change permissions for {{.OtherUserEmail}} from the Ente app.
+ +For any assistance or questions, please reply to this email.
+{{end}} diff --git a/server/pkg/controller/collection.go b/server/pkg/controller/collection.go index 7476de1c39..52c6a32418 100644 --- a/server/pkg/controller/collection.go +++ b/server/pkg/controller/collection.go @@ -4,11 +4,13 @@ import ( "context" "encoding/json" "fmt" - "github.com/ente-io/museum/pkg/repo/cast" "runtime/debug" "strings" + "github.com/ente-io/museum/pkg/repo/cast" + "github.com/ente-io/museum/pkg/controller/access" + "github.com/ente-io/museum/pkg/controller/email" "github.com/gin-contrib/requestid" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" @@ -31,6 +33,7 @@ const ( // CollectionController encapsulates logic that deals with collections type CollectionController struct { PublicCollectionCtrl *PublicCollectionController + EmailCtrl *email.EmailNotificationController AccessCtrl access.Controller BillingCtrl *BillingController CollectionRepo *repo.CollectionRepository @@ -219,7 +222,12 @@ func (c *CollectionController) JoinViaLink(ctx *gin.Context, req ente.JoinCollec if publicCollectionToken.EnableCollect { role = ente.COLLABORATOR } - return c.CollectionRepo.Share(req.CollectionID, collection.Owner.ID, userID, req.EncryptedKey, role, time.Microseconds()) + joinErr := c.CollectionRepo.Share(req.CollectionID, collection.Owner.ID, userID, req.EncryptedKey, role, time.Microseconds()) + if joinErr != nil { + return stacktrace.Propagate(joinErr, "") + } + go c.EmailCtrl.OnLinkJoined(collection.Owner.ID, userID, role) + return nil } // UnShare unshares a collection with a user diff --git a/server/pkg/controller/email/email_notification.go b/server/pkg/controller/email/email_notification.go index 1f67f1a916..328a094ce4 100644 --- a/server/pkg/controller/email/email_notification.go +++ b/server/pkg/controller/email/email_notification.go @@ -5,6 +5,7 @@ import ( "strconv" "github.com/avct/uasurfer" + "github.com/ente-io/museum/ente" "github.com/ente-io/museum/pkg/controller/lock" "github.com/ente-io/museum/pkg/repo" "github.com/ente-io/museum/pkg/utils/email" @@ -13,31 +14,31 @@ import ( ) const ( - WebAppFirstUploadTemplate = "web_app_first_upload.html" - MobileAppFirstUploadTemplate = "mobile_app_first_upload.html" - FirstUploadEmailSubject = "Congratulations! 🎉" - - StorageLimitExceededMailLock = "storage_limit_exceeded_mail_lock" - StorageLimitExceededTemplateID = "storage_limit_exceeded" - StorageLimitExceededTemplate = "storage_limit_exceeded.html" - - FilesCollectedTemplate = "files_collected.html" - FilesCollectedTemplateID = "files_collected" - FilesCollectedSubject = "You've got photos!" + WebAppFirstUploadTemplate = "web_app_first_upload.html" + MobileAppFirstUploadTemplate = "mobile_app_first_upload.html" + FirstUploadEmailSubject = "Congratulations! 🎉" + + StorageLimitExceededMailLock = "storage_limit_exceeded_mail_lock" + StorageLimitExceededTemplateID = "storage_limit_exceeded" + StorageLimitExceededTemplate = "storage_limit_exceeded.html" + + FilesCollectedTemplate = "files_collected.html" + FilesCollectedTemplateID = "files_collected" + FilesCollectedSubject = "You've got photos!" + + SubscriptionUpgradedTemplate = "subscription_upgraded.html" + SubscriptionUpgradedSubject = "Thank you for choosing Ente!" - SubscriptionUpgradedTemplate = "subscription_upgraded.html" - SubscriptionUpgradedSubject = "Thank you for choosing Ente!" - SubscriptionCancelledSubject = "Good bye (?) from Ente" SubscriptionCancelledTemplate = "subscription_cancelled.html" FilesCollectedMuteDurationInMinutes = 10 - StorageLimitExceededSubject = "[Alert] You have exceeded your storage limit" - ReferralSuccessfulTemplate = "successful_referral.html" - ReferralSuccessfulSubject = "You've earned 10 GB on Ente! 🎁" + StorageLimitExceededSubject = "[Alert] You have exceeded your storage limit" + ReferralSuccessfulTemplate = "successful_referral.html" + ReferralSuccessfulSubject = "You've earned 10 GB on Ente! 🎁" - LoginSuccessSubject = "New login to your Ente account" - LoginSuccessTemplate = "on_login.html" + LoginSuccessSubject = "New login to your Ente account" + LoginSuccessTemplate = "on_login.html" ) type EmailNotificationController struct { @@ -78,6 +79,27 @@ func (c *EmailNotificationController) OnSuccessfulReferral(userID int64) { } } +func (c *EmailNotificationController) OnLinkJoined(ownerID int64, otherUserID int64, role ente.CollectionParticipantRole) { + user, err := c.UserRepo.Get(ownerID) + if err != nil { + return + } + otherUser, err := c.UserRepo.Get(otherUserID) + if err != nil { + return + } + data := map[string]interface{}{ + "OtherUserEmail": otherUser.Email, + "Role": role, + } + err = email.SendTemplatedEmailV2( + []string{user.Email}, "Ente", "team@ente.io", + fmt.Sprintf("%s has joined your album", otherUser.Email), "base.html", "on_link_joined.html", data, nil) + if err != nil { + log.Error("Error sending link joined email ", err) + } +} + func (c *EmailNotificationController) OnFilesCollected(userID int64) { user, err := c.UserRepo.Get(userID) if err != nil {