diff --git a/go.sum b/go.sum index 1a87bda1533..3e31b3b46e5 100644 --- a/go.sum +++ b/go.sum @@ -1074,8 +1074,6 @@ github.com/prometheus/statsd_exporter v0.22.4/go.mod h1:N4Z1+iSqc9rnxlT1N8Qn3l65 github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= github.com/rainycape/memcache v0.0.0-20150622160815-1031fa0ce2f2/go.mod h1:7tZKcyumwBO6qip7RNQ5r77yrssm9bfCowcLEBcU5IA= github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= -github.com/rhafer/reva/v2 v2.0.0-20220913142514-cf5b93129713 h1:CLaGrpVJLptZk417YiW4jRHfTKiLMl9RljrxZd5u4l0= -github.com/rhafer/reva/v2 v2.0.0-20220913142514-cf5b93129713/go.mod h1:+BYVpRV8g1hL8wF3+3BunL9BKPsXVyJYmH8COxq/V7Y= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= diff --git a/services/notifications/pkg/service/service.go b/services/notifications/pkg/service/service.go index dab60010001..8ae372788d8 100644 --- a/services/notifications/pkg/service/service.go +++ b/services/notifications/pkg/service/service.go @@ -71,7 +71,97 @@ func (s eventsNotifier) Run() error { } func (s eventsNotifier) handleSpaceCreated(e events.SpaceCreated) { - // TODO: implement me + userResponse, err := s.gwClient.GetUser(context.Background(), &userv1beta1.GetUserRequest{ + UserId: e.Executant, + }) + if err != nil || userResponse.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) + authRes, err := s.gwClient.Authenticate(ownerCtx, &gateway.AuthenticateRequest{ + Type: "machine", + ClientId: "userid:" + e.Executant.OpaqueId, + ClientSecret: s.machineAuthAPIKey, + }) + if err != nil || authRes.GetStatus().GetCode() != rpcv1beta1.Code_CODE_OK { + s.logger.Error(). + Err(err). + Str("event", "SpaceCreated"). + Msg("Could not impersonate sharer") + return + } + + if authRes.GetStatus().GetCode() != rpcv1beta1.Code_CODE_OK { + s.logger.Error(). + Err(err). + Str("event", "SpaceCreated"). + Msg("could not get authenticated context for user") + return + } + ownerCtx = metadata.AppendToOutgoingContext(ownerCtx, ctxpkg.TokenHeader, authRes.Token) + + resourceID, err := storagespace.ParseID(e.ID.OpaqueId) + if err != nil { + s.logger.Error(). + Err(err). + Str("event", "SpaceCreated"). + Str("itemid", e.ID.OpaqueId). + Msg("could not parse resourceid from ItemID ") + return + } + // TODO: maybe cache this stat to reduce storage iops + md, err := s.gwClient.Stat(ownerCtx, &providerv1beta1.StatRequest{ + Ref: &providerv1beta1.Reference{ + ResourceId: &resourceID, + }, + FieldMask: &fieldmaskpb.FieldMask{Paths: []string{"name"}}, + }) + + if err != nil || md.Status.Code != rpcv1beta1.Code_CODE_OK { + s.logger.Error(). + Err(err). + Str("event", "ShareCreated"). + Str("itemid", e.ID.OpaqueId). + Msg("could not stat resource") + return + } + + if md.Status.Code != rpcv1beta1.Code_CODE_OK { + s.logger.Error(). + Err(err). + Str("event", "ShareCreated"). + Str("itemid", e.ID.OpaqueId). + Str("rpc status", md.Status.Code.String()). + Msg("could not stat resource") + return + } + + // old code + msg, err := email.RenderEmailTemplate("sharedSpace.email.tmpl", map[string]string{ + "SpaceSharer": "spacesharer", + "SpaceName": "spacename", + }, s.emailTemplatePath) + + if err != nil { + s.logger.Error(). + Err(err). + Str("event", "SpaceCreated"). + Msg("Could not render E-Mail template for spaces") + } + if e.Executant != nil { + err = s.channel.SendMessage([]string{e.Executant.OpaqueId}, msg) + } + if err != nil { + s.logger.Error(). + Err(err). + Str("event", "SpaceCreated"). + Msg("failed to send a message") + } } func (s eventsNotifier) handleShareCreated(e events.ShareCreated) {