From dbaef7f1bf87c382d1e11fa6fef4c6e960543c47 Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Tue, 20 Sep 2022 13:22:11 +0200 Subject: [PATCH] enhance templating Signed-off-by: Christian Richter --- .../notifications/pkg/channels/channels.go | 17 +++-- .../email/templates/shareCreated.email.tmpl | 17 ++++- .../email/templates/sharedSpace.email.tmpl | 19 +++++- services/notifications/pkg/service/service.go | 65 ++++++++++++++----- 4 files changed, 91 insertions(+), 27 deletions(-) diff --git a/services/notifications/pkg/channels/channels.go b/services/notifications/pkg/channels/channels.go index 3929d30c068..a86fff60d9b 100644 --- a/services/notifications/pkg/channels/channels.go +++ b/services/notifications/pkg/channels/channels.go @@ -4,6 +4,7 @@ package channels import ( "context" "crypto/tls" + "fmt" "strings" gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" @@ -19,9 +20,9 @@ import ( // Channel defines the methods of a communication channel. type Channel interface { // SendMessage sends a message to users. - SendMessage(userIDs []string, msg, subject string) error + SendMessage(userIDs []string, msg, subject, senderDisplayName string) error // SendMessageToGroup sends a message to a group. - SendMessageToGroup(groupdID *groups.GroupId, msg, subject string) error + SendMessageToGroup(groupdID *groups.GroupId, msg, subject, senderDisplayName string) error } // NewMailChannel instantiates a new mail communication channel. @@ -100,7 +101,7 @@ func (m Mail) getMailClient() (*mail.SMTPClient, error) { } // SendMessage sends a message to all given users. -func (m Mail) SendMessage(userIDs []string, msg, subject string) error { +func (m Mail) SendMessage(userIDs []string, msg, subject, senderDisplayName string) error { if m.conf.Notifications.SMTP.Host == "" { return nil } @@ -116,7 +117,11 @@ func (m Mail) SendMessage(userIDs []string, msg, subject string) error { } email := mail.NewMSG() - email.SetFrom(m.conf.Notifications.SMTP.Sender).AddTo(to...) + if senderDisplayName != "" { + email.SetFrom(fmt.Sprintf("%s via owncloud <%s>", senderDisplayName, m.conf.Notifications.SMTP.Sender)).AddTo(to...) + } else { + email.SetFrom(m.conf.Notifications.SMTP.Sender).AddTo(to...) + } email.SetBody(mail.TextPlain, msg) email.SetSubject(subject) @@ -124,7 +129,7 @@ func (m Mail) SendMessage(userIDs []string, msg, subject string) error { } // SendMessageToGroup sends a message to all members of the given group. -func (m Mail) SendMessageToGroup(groupID *groups.GroupId, msg, subject string) error { +func (m Mail) SendMessageToGroup(groupID *groups.GroupId, msg, subject, senderDisplayName string) error { // TODO We need an authenticated context here... res, err := m.gatewayClient.GetGroup(context.Background(), &groups.GetGroupRequest{GroupId: groupID}) if err != nil { @@ -139,7 +144,7 @@ func (m Mail) SendMessageToGroup(groupID *groups.GroupId, msg, subject string) e members = append(members, id.OpaqueId) } - return m.SendMessage(members, msg, subject) + return m.SendMessage(members, msg, subject, senderDisplayName) } func (m Mail) getReceiverAddresses(receivers []string) ([]string, error) { diff --git a/services/notifications/pkg/email/templates/shareCreated.email.tmpl b/services/notifications/pkg/email/templates/shareCreated.email.tmpl index 376775dfad5..5c0b3fca0bf 100644 --- a/services/notifications/pkg/email/templates/shareCreated.email.tmpl +++ b/services/notifications/pkg/email/templates/shareCreated.email.tmpl @@ -1,3 +1,18 @@ +Hello {{ .ShareGrantee }}, + {{ .ShareSharer }} has shared {{ .ShareFolder }} with you. -Link: {{ .ShareLink }} \ No newline at end of file +Click here to view it: {{ .ShareLink }} + +---------------------------------------------------------- + +Hallo {{ .Grantee }}, + +{{ .ShareSharer }} hat dich zu {{ .ShareFolder }} eingeladen. + +Klicke hier zum Anzeigen: {{ .ShareLink }} + + +--- +ownCloud - Store. Share. Work. +https://owncloud.com \ No newline at end of file diff --git a/services/notifications/pkg/email/templates/sharedSpace.email.tmpl b/services/notifications/pkg/email/templates/sharedSpace.email.tmpl index 6ded0d6773b..16bef45f609 100644 --- a/services/notifications/pkg/email/templates/sharedSpace.email.tmpl +++ b/services/notifications/pkg/email/templates/sharedSpace.email.tmpl @@ -1,3 +1,18 @@ -{{ .SpaceSharer }} has invited you to join {{ .SpaceName }} +Hello {{ .SpaceGrantee }}, -Link: {{ .ShareLink }} \ No newline at end of file +{{ .SpaceSharer }} has invited you to join {{ .SpaceName }}. + +Click here to view it: {{ .ShareLink }} + +---------------------------------------------------------- + +Hallo {{ .SpaceGrantee }}, + +{{ .SpaceSharer }} hat dich in den Space {{ .SpaceName }} eingeladen. + +Klicke hier zum Anzeigen: {{ .ShareLink }} + + +--- +ownCloud - Store. Share. Work. +https://owncloud.com \ No newline at end of file diff --git a/services/notifications/pkg/service/service.go b/services/notifications/pkg/service/service.go index f52dd9c9861..1065c78f624 100644 --- a/services/notifications/pkg/service/service.go +++ b/services/notifications/pkg/service/service.go @@ -2,6 +2,7 @@ package service import ( "context" + "fmt" "net/url" "os" "os/signal" @@ -72,10 +73,21 @@ func (s eventsNotifier) Run() error { } func (s eventsNotifier) handleSpaceShared(e events.SpaceShared) { - userResponse, err := s.gwClient.GetUser(context.Background(), &userv1beta1.GetUserRequest{ + sharerUserResponse, err := s.gwClient.GetUser(context.Background(), &userv1beta1.GetUserRequest{ UserId: e.Creator, }) - if err != nil || userResponse.Status.Code != rpcv1beta1.Code_CODE_OK { + if err != nil || sharerUserResponse.Status.Code != rpcv1beta1.Code_CODE_OK { + s.logger.Error(). + Err(err). + Str("event", "SpaceCreated"). + Msg("Could not get user response from gatway client") + return + } + + granteeUserResponse, err := s.gwClient.GetUser(context.Background(), &userv1beta1.GetUserRequest{ + UserId: e.Creator, + }) + if err != nil || sharerUserResponse.Status.Code != rpcv1beta1.Code_CODE_OK { s.logger.Error(). Err(err). Str("event", "SpaceCreated"). @@ -83,7 +95,7 @@ func (s eventsNotifier) handleSpaceShared(e events.SpaceShared) { return } // Get auth context - ownerCtx := ctxpkg.ContextSetUser(context.Background(), userResponse.User) + ownerCtx := ctxpkg.ContextSetUser(context.Background(), sharerUserResponse.User) authRes, err := s.gwClient.Authenticate(ownerCtx, &gateway.AuthenticateRequest{ Type: "machine", ClientId: "userid:" + e.Executant.OpaqueId, @@ -153,11 +165,12 @@ func (s eventsNotifier) handleSpaceShared(e events.SpaceShared) { return } + sharerDisplayName := sharerUserResponse.GetUser().DisplayName msg, err := email.RenderEmailTemplate("sharedSpace.email.tmpl", map[string]string{ - // TODO: add additional fields here (like link etc.) - "SpaceSharer": userResponse.GetUser().DisplayName, - "SpaceName": md.GetInfo().GetSpace().Name, - "ShareLink": shareLink, + "SpaceGrantee": granteeUserResponse.GetUser().DisplayName, + "SpaceSharer": sharerDisplayName, + "SpaceName": md.GetInfo().GetSpace().Name, + "ShareLink": shareLink, }, s.emailTemplatePath) if err != nil { @@ -166,10 +179,12 @@ func (s eventsNotifier) handleSpaceShared(e events.SpaceShared) { Str("event", "SpaceCreated"). Msg("Could not render E-Mail template for spaces") } + + emailSubject := fmt.Sprintf("%s invited you to join %s", sharerUserResponse.GetUser().DisplayName, md.GetInfo().GetSpace().Name) if e.GranteeUserID != nil { - err = s.channel.SendMessage([]string{e.GranteeUserID.OpaqueId}, msg, "You have received a share.") + err = s.channel.SendMessage([]string{e.GranteeUserID.OpaqueId}, msg, emailSubject, sharerDisplayName) } else if e.GranteeGroupID != nil { - err = s.channel.SendMessageToGroup(e.GranteeGroupID, msg, "You have received a share.") + err = s.channel.SendMessageToGroup(e.GranteeGroupID, msg, emailSubject, sharerDisplayName) } if err != nil { s.logger.Error(). @@ -180,10 +195,21 @@ func (s eventsNotifier) handleSpaceShared(e events.SpaceShared) { } func (s eventsNotifier) handleShareCreated(e events.ShareCreated) { - userResponse, err := s.gwClient.GetUser(context.Background(), &userv1beta1.GetUserRequest{ + sharerUserResponse, err := s.gwClient.GetUser(context.Background(), &userv1beta1.GetUserRequest{ UserId: e.Sharer, }) - if err != nil || userResponse.Status.Code != rpcv1beta1.Code_CODE_OK { + if err != nil || sharerUserResponse.Status.Code != rpcv1beta1.Code_CODE_OK { + s.logger.Error(). + Err(err). + Str("event", "ShareCreated"). + Msg("Could not get user response from gatway client") + return + } + + granteeUserResponse, err := s.gwClient.GetUser(context.Background(), &userv1beta1.GetUserRequest{ + UserId: e.GranteeUserID, + }) + if err != nil || sharerUserResponse.Status.Code != rpcv1beta1.Code_CODE_OK { s.logger.Error(). Err(err). Str("event", "ShareCreated"). @@ -192,7 +218,7 @@ func (s eventsNotifier) handleShareCreated(e events.ShareCreated) { } // Get auth context - ownerCtx := ctxpkg.ContextSetUser(context.Background(), userResponse.User) + ownerCtx := ctxpkg.ContextSetUser(context.Background(), sharerUserResponse.User) authRes, err := s.gwClient.Authenticate(ownerCtx, &gateway.AuthenticateRequest{ Type: "machine", ClientId: "userid:" + e.Sharer.OpaqueId, @@ -252,11 +278,12 @@ func (s eventsNotifier) handleShareCreated(e events.ShareCreated) { return } + sharerDisplayName := sharerUserResponse.GetUser().DisplayName msg, err := email.RenderEmailTemplate("shareCreated.email.tmpl", map[string]string{ - // TODO: add additional fields here (like link etc.) - "ShareSharer": userResponse.GetUser().DisplayName, - "ShareFolder": md.GetInfo().Name, - "ShareLink": shareLink, + "ShareGrantee": granteeUserResponse.GetUser().DisplayName, + "ShareSharer": sharerDisplayName, + "ShareFolder": md.GetInfo().Name, + "ShareLink": shareLink, }, s.emailTemplatePath) if err != nil { @@ -265,10 +292,12 @@ func (s eventsNotifier) handleShareCreated(e events.ShareCreated) { Str("event", "ShareCreated"). Msg("Could not render E-Mail template for shares") } + + emailSubject := fmt.Sprintf("%s shared %s with you", sharerUserResponse.GetUser().DisplayName, md.GetInfo().Name) if e.GranteeUserID != nil { - err = s.channel.SendMessage([]string{e.GranteeUserID.OpaqueId}, msg, "You have received a share.") + err = s.channel.SendMessage([]string{e.GranteeUserID.OpaqueId}, msg, emailSubject, sharerDisplayName) } else if e.GranteeGroupID != nil { - err = s.channel.SendMessageToGroup(e.GranteeGroupID, msg, "You have received a share.") + err = s.channel.SendMessageToGroup(e.GranteeGroupID, msg, emailSubject, sharerDisplayName) } if err != nil { s.logger.Error().