Skip to content

Commit

Permalink
enhancement(notifications): process ocm invites and sent out a mail n…
Browse files Browse the repository at this point in the history
…otification
  • Loading branch information
fschade committed Sep 3, 2024
1 parent 9935fe8 commit 4213981
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 12 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ require (
sigs.k8s.io/yaml v1.4.0 // indirect
)

replace github.com/cs3org/reva/v2 => github.com/fschade/reva/v2 v2.0.0-20240903161137-e67e3bedc495

replace github.com/studio-b12/gowebdav => github.com/aduffeck/gowebdav v0.0.0-20231215102054-212d4a4374f6

replace github.com/egirna/icap-client => github.com/fschade/icap-client v0.0.0-20240802074440-aade4a234387
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,6 @@ github.com/crewjam/saml v0.4.14 h1:g9FBNx62osKusnFzs3QTN5L9CVA/Egfgm+stJShzw/c=
github.com/crewjam/saml v0.4.14/go.mod h1:UVSZCf18jJkk6GpWNVqcyQJMD5HsRugBPf4I1nl2mME=
github.com/cs3org/go-cs3apis v0.0.0-20240724121416-062c4e3046cb h1:KmYZDReplv/yfwc1LNYpDcVhVujC3Pasv6WjXx1haSU=
github.com/cs3org/go-cs3apis v0.0.0-20240724121416-062c4e3046cb/go.mod h1:yyP8PRo0EZou3nSH7H4qjlzQwaydPeIRNgX50npQHpE=
github.com/cs3org/reva/v2 v2.23.1-0.20240829104718-86f39ecc9f89 h1:fHQzCRgnsullqFcX9Equ/MKJdbBRSDfn2FtdSpbkdaw=
github.com/cs3org/reva/v2 v2.23.1-0.20240829104718-86f39ecc9f89/go.mod h1:p7CHBXcg6sSqB+0JMNDfC1S7TSh9FghXkw1kTV3KcJI=
github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4=
github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=
github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
Expand Down Expand Up @@ -331,6 +329,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fschade/icap-client v0.0.0-20240802074440-aade4a234387 h1:Y3wZgTr29sLxWSMz4KF91o0x87EaJF6FIPNJFepRIiw=
github.com/fschade/icap-client v0.0.0-20240802074440-aade4a234387/go.mod h1:HpntrRsQA6RKNXy2Nbr4kVj+NO3OYWpAQUVxeya+3sU=
github.com/fschade/reva/v2 v2.0.0-20240903161137-e67e3bedc495 h1:bpG+T3hy86xhqEVmJ3xNe5m/zz+CmQJ1eQIG2q7K9V4=
github.com/fschade/reva/v2 v2.0.0-20240903161137-e67e3bedc495/go.mod h1:p7CHBXcg6sSqB+0JMNDfC1S7TSh9FghXkw1kTV3KcJI=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
Expand Down
2 changes: 1 addition & 1 deletion services/notifications/pkg/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
imgDir = filepath.Join("templates", "html", "img")
)

// RenderEmailTemplate renders the email template for a new share
// RenderEmailTemplate is responsible to prepare a message which than can be used to notify the user via email.
func RenderEmailTemplate(mt MessageTemplate, locale, defaultLocale string, emailTemplatePath string, translationPath string, vars map[string]string) (*channels.Message, error) {
textMt, err := NewTextTemplate(mt, locale, defaultLocale, translationPath, vars)
if err != nil {
Expand Down
37 changes: 29 additions & 8 deletions services/notifications/pkg/email/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,39 @@ You might still have access through your other groups or direct membership.`),
Even though this membership has expired you still might have access through other shares and/or space memberships`),
}

ScienceMeshInviteTokenGenerated = MessageTemplate{
textTemplate: "templates/text/email.text.tmpl",
htmlTemplate: "templates/html/email.html.tmpl",
// ScienceMeshInviteTokenGenerated email template, Subject field (resolves directly)
Subject: l10n.Template(`ScienceMesh: {InitiatorName} wants to collaborate with you`),
// ScienceMeshInviteTokenGenerated email template, resolves via {{ .Greeting }}
Greeting: l10n.Template(`Hi,`),
// ScienceMeshInviteTokenGenerated email template, resolves via {{ .MessageBody }}
MessageBody: l10n.Template(`{ShareSharer} ({ShareSharerMail}) wants to start sharing OCM resources with you.
{{if .ShareLink }}To accept the invite, please visit the following URL:
{ShareLink}
Alternatively, you can{{else}}
Please{{end}} visit your mesh provider and use the following details:
Token: {Token}
ProviderDomain: {ProviderDomain}`),
}
)

// holds the information to turn the raw template into a parseable go template
var _placeholders = map[string]string{
"{ShareSharer}": "{{ .ShareSharer }}",
"{ShareFolder}": "{{ .ShareFolder }}",
"{ShareGrantee}": "{{ .ShareGrantee }}",
"{ShareLink}": "{{ .ShareLink }}",
"{SpaceName}": "{{ .SpaceName }}",
"{SpaceGrantee}": "{{ .SpaceGrantee }}",
"{SpaceSharer}": "{{ .SpaceSharer }}",
"{ExpiredAt}": "{{ .ExpiredAt }}",
"{ShareSharer}": "{{ .ShareSharer }}",
"{ShareFolder}": "{{ .ShareFolder }}",
"{ShareGrantee}": "{{ .ShareGrantee }}",
"{ShareLink}": "{{ .ShareLink }}",
"{SpaceName}": "{{ .SpaceName }}",
"{SpaceGrantee}": "{{ .SpaceGrantee }}",
"{SpaceSharer}": "{{ .SpaceSharer }}",
"{ExpiredAt}": "{{ .ExpiredAt }}",
"{ShareSharerMail}": "{{ .ShareSharerMail }}",
"{ProviderDomain}": "{{ .ProviderDomain }}",
"{Token}": "{{ .Token }}",
}

// MessageTemplate is the data structure for the email
Expand Down
70 changes: 69 additions & 1 deletion services/notifications/pkg/service/sciencemesh.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,77 @@
package service

import (
"context"

"github.com/cs3org/reva/v2/pkg/events"
"github.com/cs3org/reva/v2/pkg/utils"
"github.com/owncloud/ocis/v2/services/notifications/pkg/channels"
"github.com/owncloud/ocis/v2/services/notifications/pkg/email"
)

func (s eventsNotifier) handleScienceMeshInviteTokenGenerated(e events.ScienceMeshInviteTokenGenerated) {
// fixMe: add implementation
logger := s.logger.With().
Str("event", "ScienceMeshInviteTokenGenerated").
Logger()

gatewayClient, err := s.gatewaySelector.Next()
if err != nil {
logger.Error().Err(err).Msg("could not select next gateway client")
return
}

ctx, err := utils.GetServiceUserContextWithContext(context.Background(), gatewayClient, s.serviceAccountID, s.serviceAccountSecret)
if err != nil {
logger.Error().Err(err).Msg("Could not impersonate service user")
return
}

owner, err := utils.GetUserWithContext(ctx, e.Sharer, gatewayClient)
if err != nil {
logger.Error().Err(err).Msg("unable to get user")
return
}

msgENV := map[string]string{
"ShareSharer": owner.GetDisplayName(),
"ShareSharerMail": owner.GetMail(),
"ShareLink": e.InviteLink,
"Token": e.Token,
"ProviderDomain": owner.GetId().GetIdp(),
"RecipientMail": e.RecipientMail,
}

// validate the message, we only need recipient mail at the moment,
// event that is optional when the event got triggered...
// this means if we get a validation error, we can't send the message and skip it
{
validationEnv := make(map[string]interface{}, len(msgENV))
for k, v := range msgENV {
validationEnv[k] = v
}
if errs := validate.ValidateMap(validationEnv,
map[string]interface{}{
"RecipientMail": "required,email", // only recipient mail is required to send the message
}); len(errs) > 0 {
return // no mail, no message
}
}

msg, err := email.RenderEmailTemplate(
email.ScienceMeshInviteTokenGenerated,
s.defaultLanguage, // fixMe: the recipient is unknown, should it be the defaultLocale?,
s.defaultLanguage, // fixMe: the defaultLocale is not set by default, shouldn't it be?,
s.emailTemplatePath,
s.translationPath,
msgENV,
)
if err != nil {
s.logger.Error().Err(err).Msg("building the message has failed")
return
}

msg.Sender = owner.GetDisplayName()
msg.Recipient = []string{e.RecipientMail}

s.send(ctx, []*channels.Message{msg})
}
8 changes: 8 additions & 0 deletions services/notifications/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/go-playground/validator/v10"
"go-micro.dev/v4/metadata"
"google.golang.org/protobuf/types/known/fieldmaskpb"

Expand All @@ -30,6 +31,13 @@ import (
"github.com/owncloud/ocis/v2/services/settings/pkg/store/defaults"
)

// validate is the package level validator instance
var validate *validator.Validate

func init() {
validate = validator.New()
}

// Service should be named `Runner`
type Service interface {
Run() error
Expand Down

0 comments on commit 4213981

Please sign in to comment.