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

[#457] Include sender, post content and action in group post notification body #472

Merged
merged 1 commit into from
Jun 24, 2024
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.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added
- Include sender, post content and action in group post notification body [#457](https://github.com/rokwire/groups-building-block/issues/457)
- Send post notification only to the creator of the post [#372](https://github.com/rokwire/groups-building-block/issues/372)

## [1.43.0] - 2024-06-19
### Added
- Provide Replies when loading single Post [#468](https://github.com/rokwire/groups-building-block/issues/468)
Expand Down
124 changes: 80 additions & 44 deletions core/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,59 +490,95 @@ func (app *Application) createPost(clientID string, current *model.User, post *m
}
go handleRewardsAsync(clientID, current.ID)

go app.sendGroupNotificationForNewPost(clientID, &current.ID, group, post)
go app.sendGroupNotificationForNewPost(clientID, &current.ID, &current.Name, group, post)

return post, nil
}

func (app *Application) sendGroupNotificationForNewPost(clientID string, currentUserID *string, group *model.Group, post *model.Post) error {
recipientsUserIDs, _ := app.getPostNotificationRecipientsAsUserIDs(clientID, post, currentUserID)
func (app *Application) sendGroupNotificationForNewPost(clientID string, currentUserID *string, currentUserName *string, group *model.Group, post *model.Post) error {
now := time.Now()
if post.DateScheduled == nil || now.After(*post.DateScheduled) {

result, _ := app.storage.FindGroupMemberships(clientID, model.MembershipFilter{
GroupIDs: []string{group.ID},
UserIDs: recipientsUserIDs,
Statuses: []string{"member", "admin"},
})
recipients := result.GetMembersAsNotificationRecipients(func(member model.GroupMembership) (bool, bool) {
return member.IsAdminOrMember() && (*currentUserID != member.UserID),
member.NotificationsPreferences.OverridePreferences &&
(member.NotificationsPreferences.PostsMuted || member.NotificationsPreferences.AllMute)
})
recipientsUserIDs, _ := app.getPostNotificationRecipientsAsUserIDs(clientID, post, currentUserID)

if len(recipients) > 0 {
groupStr := "Group"
if group.ResearchGroup {
groupStr = "Research Project"
}
title := fmt.Sprintf("%s - %s", groupStr, group.Title)
body := fmt.Sprintf("New post has been published in '%s' %s", group.Title, strings.ToLower(groupStr))
if post.UseAsNotification {
title = post.Subject
body = post.Body
result, _ := app.storage.FindGroupMemberships(clientID, model.MembershipFilter{
GroupIDs: []string{group.ID},
UserIDs: recipientsUserIDs,
Statuses: []string{"member", "admin"},
})
var recipients []notifications.Recipient
if post.ParentID == nil {
recipients = result.GetMembersAsNotificationRecipients(func(member model.GroupMembership) (bool, bool) {
return member.IsAdminOrMember() && (*currentUserID != member.UserID),
member.NotificationsPreferences.OverridePreferences &&
(member.NotificationsPreferences.PostsMuted || member.NotificationsPreferences.AllMute)
})
} else {
parentPost, err := app.storage.FindPost(nil, clientID, nil, group.ID, *post.ParentID, true, false)
if err != nil {
log.Printf("error app.sendGroupNotificationForNewPost() - %s", err)
return fmt.Errorf("error app.sendGroupNotificationForNewPost() - %s", err)
}
if parentPost != nil {
recipients = append(recipients, notifications.Recipient{
UserID: parentPost.Creator.UserID,
})
}
}

topic := "group.posts"
return app.notifications.SendNotification(
recipients,
&topic,
title,
body,
map[string]string{
"type": "group",
"operation": "post_created",
"entity_type": "group",
"entity_id": group.ID,
"entity_name": group.Title,
"post_id": post.ID,
"post_subject": post.Subject,
"post_body": post.Body,
},
app.config.AppID,
app.config.OrgID,
nil,
)
}
if len(recipients) > 0 {
groupStr := "Group"
if group.ResearchGroup {
groupStr = "Research Project"
}
title := fmt.Sprintf("%s - %s", groupStr, group.Title)
operation := "posted"
if post.ParentID != nil {
operation = "replied"
}
if currentUserName == nil && currentUserID != nil {
coreUsers, err := app.corebb.GetAccountsWithIDs([]string{*currentUserID}, nil, nil, nil, nil)
if err != nil {
log.Printf("error app.sendGroupNotificationForNewPost() - %s", err)
}
if len(coreUsers) > 0 {
val := coreUsers[0].GetFullName()
currentUserName = &val
}
}
if currentUserName == nil || *currentUserName == "" {
val := "User"
currentUserName = &val
}

body := fmt.Sprintf("%s %s '%s'", *currentUserName, operation, post.Body)
if post.UseAsNotification {
title = post.Subject
body = post.Body
}

topic := "group.posts"
return app.notifications.SendNotification(
recipients,
&topic,
title,
body,
map[string]string{
"type": "group",
"operation": "post_created",
"entity_type": "group",
"entity_id": group.ID,
"entity_name": group.Title,
"post_id": post.ID,
"post_subject": post.Subject,
"post_body": post.Body,
},
app.config.AppID,
app.config.OrgID,
nil,
)
}
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion core/services_scheduled_posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (app Application) processScheduledPosts() error {
return err
}
if group != nil {
err = app.sendGroupNotificationForNewPost(post.ClientID, &post.Creator.UserID, group, &post)
err = app.sendGroupNotificationForNewPost(post.ClientID, &post.Creator.UserID, &post.Creator.Name, group, &post)
if err != nil {
return nil
}
Expand Down
Loading