Skip to content

Commit

Permalink
enhance templating
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Richter <[email protected]>
  • Loading branch information
dragonchaser committed Sep 20, 2022
1 parent 786af00 commit dbaef7f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 27 deletions.
17 changes: 11 additions & 6 deletions services/notifications/pkg/channels/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package channels
import (
"context"
"crypto/tls"
"fmt"
"strings"

gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
Expand All @@ -116,15 +117,19 @@ 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)

return email.Send(smtpClient)
}

// 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 {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
Hello {{ .ShareGrantee }},

{{ .ShareSharer }} has shared {{ .ShareFolder }} with you.

Link: {{ .ShareLink }}
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
19 changes: 17 additions & 2 deletions services/notifications/pkg/email/templates/sharedSpace.email.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
{{ .SpaceSharer }} has invited you to join {{ .SpaceName }}
Hello {{ .SpaceGrantee }},

Link: {{ .ShareLink }}
{{ .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
65 changes: 47 additions & 18 deletions services/notifications/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"context"
"fmt"
"net/url"
"os"
"os/signal"
Expand Down Expand Up @@ -72,18 +73,29 @@ 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").
Msg("Could not get user response from gatway client")
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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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().
Expand All @@ -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").
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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().
Expand Down

0 comments on commit dbaef7f

Please sign in to comment.