From e85da1b1a1cda007a7e8ef39f63cda17109dece0 Mon Sep 17 00:00:00 2001 From: Bastian Beier Date: Wed, 22 Jan 2025 10:01:28 +0100 Subject: [PATCH] feat: Add email notification for share removed (cherry picked from commit e3876dba138ca7904110663b6f14dc7c13aef6c7) --- .../enhancement-share-removed-email.md | 4 + services/notifications/pkg/command/server.go | 1 + services/notifications/pkg/email/templates.go | 13 +++ services/notifications/pkg/service/job.go | 18 ++++ services/notifications/pkg/service/service.go | 2 + .../notifications/pkg/service/service_test.go | 93 +++++++++++++++++++ services/notifications/pkg/service/shares.go | 70 ++++++++++++++ .../settings/pkg/store/defaults/defaults.go | 2 +- 8 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 changelog/unreleased/enhancement-share-removed-email.md diff --git a/changelog/unreleased/enhancement-share-removed-email.md b/changelog/unreleased/enhancement-share-removed-email.md new file mode 100644 index 00000000000..b71b8ca99b1 --- /dev/null +++ b/changelog/unreleased/enhancement-share-removed-email.md @@ -0,0 +1,4 @@ +Enhancement: Add email notification for share removed event + +https://github.com/owncloud/ocis/pull/10915 +https://github.com/owncloud/ocis/issues/10904 diff --git a/services/notifications/pkg/command/server.go b/services/notifications/pkg/command/server.go index 309bc603f0d..9959e45c7b9 100644 --- a/services/notifications/pkg/command/server.go +++ b/services/notifications/pkg/command/server.go @@ -81,6 +81,7 @@ func Server(cfg *config.Config) *cli.Command { evs := []events.Unmarshaller{ events.ShareCreated{}, events.ShareExpired{}, + events.ShareRemoved{}, events.SpaceShared{}, events.SpaceUnshared{}, events.SpaceMembershipExpired{}, diff --git a/services/notifications/pkg/email/templates.go b/services/notifications/pkg/email/templates.go index c0d940f3a1d..db4de470ac9 100644 --- a/services/notifications/pkg/email/templates.go +++ b/services/notifications/pkg/email/templates.go @@ -33,6 +33,19 @@ var ( // ShareExpired email template, resolves via {{ .MessageBody }} MessageBody: l10n.Template(`Your share to {ShareFolder} has expired at {ExpiredAt} +Even though this share has been revoked you still might have access through other shares and/or space memberships.`), + } + + ShareRemoved = MessageTemplate{ + textTemplate: _textTemplate, + htmlTemplate: _htmlTemplate, + // ShareRemoved email template, Subject field (resolves directly) + Subject: l10n.Template(`{ShareSharer} unshared '{ShareFolder}' with you`), + // ShareRemoved email template, resolves via {{ .Greeting }} + Greeting: l10n.Template(`Hello {ShareGrantee},`), + // ShareRemoved email template, resolves via {{ .MessageBody }} + MessageBody: l10n.Template(`{ShareSharer} has unshared '{ShareFolder}' with you. + Even though this share has been revoked you still might have access through other shares and/or space memberships.`), } diff --git a/services/notifications/pkg/service/job.go b/services/notifications/pkg/service/job.go index f1acfcd2a31..5850944ca77 100644 --- a/services/notifications/pkg/service/job.go +++ b/services/notifications/pkg/service/job.go @@ -130,6 +130,24 @@ func (s eventsNotifier) createGroupedMail(ctx context.Context, logger zerolog.Lo "ShareFolder": shareFolder, "ExpiredAt": te.ExpiredAt.Format("2006-01-02 15:04:05"), }) + case events.ShareRemoved: + logger := logger.With(). + Str("event", "ShareRemoved"). + Str("eventId", te.ItemID.OpaqueId). + Logger() + executant, shareFolder, _, err := s.prepareShareRemoved(logger, te) + if err != nil { + logger.Error().Err(err).Msg("could not prepare vars for grouped email") + continue + } + + mts = append(mts, email.ShareRemoved) + mtsVars = append(mtsVars, map[string]string{ + "ShareSharer": executant.GetDisplayName(), + "ShareFolder": shareFolder, + }) + default: + logger.Error().Str("eventType", e.Type).Msg("unsupported event type for grouped email") } } if len(mts) == 0 && len(mtsVars) == 0 { diff --git a/services/notifications/pkg/service/service.go b/services/notifications/pkg/service/service.go index acf4545c523..e95f366b8a5 100644 --- a/services/notifications/pkg/service/service.go +++ b/services/notifications/pkg/service/service.go @@ -117,6 +117,8 @@ func (s eventsNotifier) Run() error { s.handleShareCreated(e, evt.ID) case events.ShareExpired: s.handleShareExpired(e, evt.ID) + case events.ShareRemoved: + s.handleShareRemoved(e, evt.ID) case events.ScienceMeshInviteTokenGenerated: s.handleScienceMeshInviteTokenGenerated(e) case events.SendEmailsEvent: diff --git a/services/notifications/pkg/service/service_test.go b/services/notifications/pkg/service/service_test.go index 614c6906028..30657c0e7e9 100644 --- a/services/notifications/pkg/service/service_test.go +++ b/services/notifications/pkg/service/service_test.go @@ -166,6 +166,30 @@ https://owncloud.com }, }), + Entry("Share Removed", testChannel{ + expectedReceipients: []string{sharee.GetMail()}, + expectedSubject: "Dr. S. Harer unshared 'secrets of the board' with you", + expectedTextBody: `Hello Eric Expireling, + +Dr. S. Harer has unshared 'secrets of the board' with you. + +Even though this share has been revoked you still might have access through other shares and/or space memberships. + + +--- +ownCloud - Store. Share. Work. +https://owncloud.com +`, + expectedSender: sharer.GetDisplayName(), + done: make(chan struct{}), + }, events.Event{ + Event: events.ShareRemoved{ + Executant: sharer.GetId(), + GranteeUserID: sharee.GetId(), + ItemID: resourceid, + }, + }), + Entry("Added to Space", testChannel{ expectedReceipients: []string{sharee.GetMail()}, expectedSubject: "Dr. S. Harer invited you to join secret space", @@ -643,6 +667,17 @@ var _ = Describe("Notifications grouped store", func() { }, "daily"), + Entry("Share Removed daily", testChannelGroupedStore{ + done: make(chan struct{}), + }, events.Event{ + Event: events.ShareRemoved{ + Executant: executant.GetId(), + GranteeGroupID: &group.GroupId{}, + ItemID: resourceid, + }, + }, + "daily"), + Entry("Added to Space daily", testChannelGroupedStore{ done: make(chan struct{}), }, events.Event{ @@ -703,6 +738,17 @@ var _ = Describe("Notifications grouped store", func() { }, "weekly"), + Entry("Share Removed weekly", testChannelGroupedStore{ + done: make(chan struct{}), + }, events.Event{ + Event: events.ShareRemoved{ + Executant: executant.GetId(), + GranteeGroupID: &group.GroupId{}, + ItemID: resourceid, + }, + }, + "weekly"), + Entry("Added to Space weekly", testChannelGroupedStore{ done: make(chan struct{}), }, events.Event{ @@ -840,6 +886,7 @@ var _ = Describe("Notifications grouped send", func() { evs := []events.Unmarshaller{ events.ShareCreated{}, events.ShareExpired{}, + events.ShareRemoved{}, events.SpaceShared{}, events.SpaceUnshared{}, events.SpaceMembershipExpired{}, @@ -886,6 +933,7 @@ var _ = Describe("Notifications grouped send", func() { ItemID: resourceid, }, }}, "daily"), + Entry("Share Expired daily", testChannel{ expectedReceipients: []string{receiver.GetMail()}, expectedSender: sender, @@ -904,6 +952,25 @@ Even though this share has been revoked you still might have access through othe ItemID: resourceid, }, }}, "daily"), + + Entry("Share Removed daily", testChannel{ + expectedReceipients: []string{receiver.GetMail()}, + expectedSender: sender, + expectedSubject: subject, + expectedTextBody: buildExpectedTextBodyGrouped(receiver.DisplayName, []string{`executant has unshared 'secrets of the board' with you. + +Even though this share has been revoked you still might have access through other shares and/or space memberships.`}), + expectedHTMLBody: buildExpectedHTMLBodyGrouped(receiver.DisplayName, []string{`executant has unshared 'secrets of the board' with you.

Even though this share has been revoked you still might have access through other shares and/or space memberships.`}), + done: make(chan struct{}), + }, []events.Event{{ + Type: reflect.TypeOf(events.ShareRemoved{}).String(), + Event: events.ShareRemoved{ + Executant: executant.GetId(), + GranteeGroupID: &group.GroupId{}, + ItemID: resourceid, + }, + }}, "daily"), + Entry("Added to Space daily", testChannel{ expectedReceipients: []string{receiver.GetMail()}, expectedSender: sender, @@ -920,6 +987,7 @@ Even though this share has been revoked you still might have access through othe ID: &provider.StorageSpaceId{OpaqueId: "spaceid"}, }, }}, "daily"), + Entry("Removed from Space daily", testChannel{ expectedReceipients: []string{receiver.GetMail()}, expectedSender: sender, @@ -937,6 +1005,7 @@ You might still have access through your other groups or direct membership.`}), ID: &provider.StorageSpaceId{OpaqueId: "spaceid"}, }, }}, "daily"), + Entry("Space Expired daily", testChannel{ expectedReceipients: []string{receiver.GetMail()}, expectedSender: sender, @@ -956,6 +1025,7 @@ Even though this membership has expired you still might have access through othe ExpiredAt: time.Date(2023, 4, 17, 16, 42, 0, 0, time.UTC), }, }}, "daily"), + Entry("Share Created and Space Expired grouped daily", testChannel{ expectedReceipients: []string{receiver.GetMail()}, expectedSender: sender, @@ -1007,6 +1077,7 @@ Even though this membership has expired you still might have access through othe ItemID: resourceid, }, }}, "weekly"), + Entry("Share Expired weekly", testChannel{ expectedReceipients: []string{receiver.GetMail()}, expectedSender: sender, @@ -1025,6 +1096,25 @@ Even though this share has been revoked you still might have access through othe ItemID: resourceid, }, }}, "weekly"), + + Entry("Share Removed weekly", testChannel{ + expectedReceipients: []string{receiver.GetMail()}, + expectedSender: sender, + expectedSubject: subject, + expectedTextBody: buildExpectedTextBodyGrouped(receiver.DisplayName, []string{`executant has unshared 'secrets of the board' with you. + +Even though this share has been revoked you still might have access through other shares and/or space memberships.`}), + expectedHTMLBody: buildExpectedHTMLBodyGrouped(receiver.DisplayName, []string{`executant has unshared 'secrets of the board' with you.

Even though this share has been revoked you still might have access through other shares and/or space memberships.`}), + done: make(chan struct{}), + }, []events.Event{{ + Type: reflect.TypeOf(events.ShareRemoved{}).String(), + Event: events.ShareRemoved{ + Executant: executant.GetId(), + GranteeGroupID: &group.GroupId{}, + ItemID: resourceid, + }, + }}, "weekly"), + Entry("Added to Space weekly", testChannel{ expectedReceipients: []string{receiver.GetMail()}, expectedSender: sender, @@ -1041,6 +1131,7 @@ Even though this share has been revoked you still might have access through othe ID: &provider.StorageSpaceId{OpaqueId: "spaceid"}, }, }}, "weekly"), + Entry("Removed from Space weekly", testChannel{ expectedReceipients: []string{receiver.GetMail()}, expectedSender: sender, @@ -1058,6 +1149,7 @@ You might still have access through your other groups or direct membership.`}), ID: &provider.StorageSpaceId{OpaqueId: "spaceid"}, }, }}, "weekly"), + Entry("Space Expired weekly", testChannel{ expectedReceipients: []string{receiver.GetMail()}, expectedSender: sender, @@ -1077,6 +1169,7 @@ Even though this membership has expired you still might have access through othe ExpiredAt: time.Date(2023, 4, 17, 16, 42, 0, 0, time.UTC), }, }}, "weekly"), + Entry("Share Created and Space Expired grouped weekly", testChannel{ expectedReceipients: []string{receiver.GetMail()}, expectedSender: sender, diff --git a/services/notifications/pkg/service/shares.go b/services/notifications/pkg/service/shares.go index 1c3b53c0fb6..bcafac297e1 100644 --- a/services/notifications/pkg/service/shares.go +++ b/services/notifications/pkg/service/shares.go @@ -160,3 +160,73 @@ func (s eventsNotifier) prepareShareExpired(logger zerolog.Logger, e events.Shar return shareFolder, ctx, err } + +func (s eventsNotifier) handleShareRemoved(e events.ShareRemoved, eventId string) { + logger := s.logger.With(). + Str("event", "ShareRemoved"). + Str("itemid", e.ItemID.OpaqueId). + Logger() + + executant, shareFolder, ctx, err := s.prepareShareRemoved(logger, e) + if err != nil { + logger.Error().Err(err).Msg("could not prepare vars for email") + return + } + + granteeList := s.ensureGranteeList(ctx, executant.GetId(), e.GranteeUserID, e.GranteeGroupID) + filteredGrantees := s.filter.execute(ctx, granteeList, defaults.SettingUUIDProfileEventShareRemoved) + + recipientsInstant, recipientsDaily, recipientsInstantWeekly := s.splitter.execute(ctx, filteredGrantees) + recipientsInstant = append(recipientsInstant, s.userEventStore.persist(_intervalDaily, eventId, recipientsDaily)...) + recipientsInstant = append(recipientsInstant, s.userEventStore.persist(_intervalWeekly, eventId, recipientsInstantWeekly)...) + if recipientsInstant == nil { + return + } + + sharerDisplayName := executant.GetDisplayName() + + emails, err := s.render(ctx, email.ShareRemoved, + "ShareGrantee", + map[string]string{ + "ShareSharer": sharerDisplayName, + "ShareFolder": shareFolder, + }, recipientsInstant, sharerDisplayName) + if err != nil { + logger.Error().Err(err).Msg("could not get render the email") + return + } + s.send(ctx, emails) +} + +func (s eventsNotifier) prepareShareRemoved(logger zerolog.Logger, e events.ShareRemoved) (executant *user.User, shareFolder string, ctx context.Context, err error) { + gatewayClient, err := s.gatewaySelector.Next() + if err != nil { + logger.Error().Err(err).Msg("could not select next gateway client") + return executant, shareFolder, ctx, err + } + + ctx, err = utils.GetServiceUserContextWithContext(context.Background(), gatewayClient, s.serviceAccountID, s.serviceAccountSecret) + if err != nil { + logger.Error().Err(err).Msg("could not get service user context") + return executant, shareFolder, ctx, err + } + + resourceInfo, err := s.getResourceInfo(ctx, e.ItemID, &fieldmaskpb.FieldMask{Paths: []string{"name"}}) + if err != nil { + logger.Error(). + Err(err). + Msg("could not stat resource") + return executant, shareFolder, ctx, err + } + shareFolder = resourceInfo.Name + + executant, err = utils.GetUserWithContext(ctx, e.Executant, gatewayClient) + if err != nil { + logger.Error(). + Err(err). + Msg("could not get user") + return executant, shareFolder, ctx, err + } + + return executant, shareFolder, ctx, err +} diff --git a/services/settings/pkg/store/defaults/defaults.go b/services/settings/pkg/store/defaults/defaults.go index 1cbaeb4a1fb..0404f3588d1 100644 --- a/services/settings/pkg/store/defaults/defaults.go +++ b/services/settings/pkg/store/defaults/defaults.go @@ -328,7 +328,7 @@ func generateBundleProfileRequest() *settingsmsg.Bundle { MultiChoiceCollectionValue: &settingsmsg.MultiChoiceCollection{ Options: []*settingsmsg.MultiChoiceCollectionOption{ &optionInAppTrue, - &optionMailFalseDisabled, + &optionMailTrue, }, }, },