Skip to content

Commit

Permalink
feat: messages previews in notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
glouvigny committed Feb 4, 2019
1 parent 796d483 commit f6e9514
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
23 changes: 22 additions & 1 deletion core/entity/conversation.go
@@ -1,6 +1,9 @@
package entity

import "berty.tech/core/pkg/errorcodes"
import (
"berty.tech/core/pkg/errorcodes"
"strings"
)

func (c Conversation) Validate() error {
if c.ID == "" {
Expand Down Expand Up @@ -43,3 +46,21 @@ func (m ConversationMember) Filtered() *ConversationMember {
func (c Conversation) IsNode() {} // required by gqlgen

func (m ConversationMember) IsNode() {} // required by gqlgen

func (c *Conversation) GetConversationTitle() string {
if c.Title != "" {
return c.Title
}

names := []string{}

for _, member := range c.Members {
if member.Contact.Status == Contact_Myself {
continue
}

names = append(names, member.Contact.DisplayName)
}

return strings.Join(names, ", ")
}
32 changes: 26 additions & 6 deletions core/node/event_handlers.go
Expand Up @@ -180,15 +180,35 @@ func (n *Node) handleConversationNewMessage(ctx context.Context, input *p2p.Even
return err
}

body := attrs.Message.Text
if n.config.NotificationsPreviews == false {
body = i18n.T("NewMessageBody", nil)
}

// say that conversation has not been read
n.sql(ctx).Save(&entity.Conversation{ID: input.ConversationID, ReadAt: time.Time{}})

sender := &entity.Contact{}
if err := n.sql(ctx).First(&sender, &entity.Contact{ID: input.SenderID}).Error; err != nil {
n.LogBackgroundWarn(ctx, errors.New("handleConversationNewMessage: Contact not found"))
sender = &entity.Contact{}
}

conversation := &entity.Conversation{}
if err := n.sql(ctx).First(&conversation, &entity.Conversation{ID: input.ConversationID}).Error; err != nil {
n.LogBackgroundWarn(ctx, errors.New("handleConversationNewMessage: Conversation not found"))
conversation = &entity.Conversation{}
}

title := i18n.T("NewMessageTitle", nil)
body := i18n.T("NewMessageBody", nil)

if n.config.NotificationsPreviews && conversation.ID != "" {
title = conversation.GetConversationTitle()
body = attrs.Message.Text

if sender.DisplayName != "" && len(conversation.Members) > 2 {
title = fmt.Sprintf("%s @ %s", sender.DisplayName, title)
}
}

n.DisplayNotification(&notification.Payload{
Title: i18n.T("NewMessageTitle", nil),
Title: title,
Body: body,
DeepLink: "berty://conversation#id=" + url.PathEscape(input.ConversationID),
})
Expand Down

0 comments on commit f6e9514

Please sign in to comment.