Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add email notification for share removed #10915

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/unreleased/enhancement-share-removed-email.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions services/notifications/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down
13 changes: 13 additions & 0 deletions services/notifications/pkg/email/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
bastianbeier marked this conversation as resolved.
Show resolved Hide resolved

Even though this share has been revoked you still might have access through other shares and/or space memberships.`),
}

Expand Down
18 changes: 18 additions & 0 deletions services/notifications/pkg/service/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions services/notifications/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
93 changes: 93 additions & 0 deletions services/notifications/pkg/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{},
Expand Down Expand Up @@ -886,6 +933,7 @@ var _ = Describe("Notifications grouped send", func() {
ItemID: resourceid,
},
}}, "daily"),

Entry("Share Expired daily", testChannel{
expectedReceipients: []string{receiver.GetMail()},
expectedSender: sender,
Expand All @@ -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.<br><br>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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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.<br><br>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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
70 changes: 70 additions & 0 deletions services/notifications/pkg/service/shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion services/settings/pkg/store/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func generateBundleProfileRequest() *settingsmsg.Bundle {
MultiChoiceCollectionValue: &settingsmsg.MultiChoiceCollection{
Options: []*settingsmsg.MultiChoiceCollectionOption{
&optionInAppTrue,
&optionMailFalseDisabled,
&optionMailTrue,
},
},
},
Expand Down