Skip to content

Commit

Permalink
add space notification
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Richter <[email protected]>
  • Loading branch information
dragonchaser committed Sep 15, 2022
1 parent a26bbf7 commit dfa6610
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
92 changes: 91 additions & 1 deletion services/notifications/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit dfa6610

Please sign in to comment.