2021-01-26 11:49:37 +00:00
|
|
|
package protocol
|
|
|
|
|
2021-03-31 16:23:45 +00:00
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"encoding/json"
|
2021-01-26 11:49:37 +00:00
|
|
|
|
2021-03-31 16:23:45 +00:00
|
|
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
2022-03-23 18:47:00 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/settings"
|
2021-03-31 16:23:45 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/protocol/communities"
|
|
|
|
localnotifications "github.com/status-im/status-go/services/local-notifications"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NotificationBody struct {
|
|
|
|
Message *common.Message `json:"message"`
|
|
|
|
Contact *Contact `json:"contact"`
|
|
|
|
Chat *Chat `json:"chat"`
|
|
|
|
Community *communities.Community `json:"community"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func showMessageNotification(publicKey ecdsa.PublicKey, message *common.Message, chat *Chat, responseTo *common.Message) bool {
|
2021-04-21 11:46:53 +00:00
|
|
|
if chat != nil && !chat.Active {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-31 16:23:45 +00:00
|
|
|
if chat != nil && (chat.OneToOne() || chat.PrivateGroupChat()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-05-25 09:40:02 +00:00
|
|
|
if message.Mentioned {
|
2021-03-31 16:23:45 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if responseTo != nil {
|
2021-05-25 09:40:02 +00:00
|
|
|
publicKeyString := common.PubkeyToHex(&publicKey)
|
2021-03-31 16:23:45 +00:00
|
|
|
return responseTo.From == publicKeyString
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n NotificationBody) MarshalJSON() ([]byte, error) {
|
|
|
|
type Alias NotificationBody
|
|
|
|
item := struct{ *Alias }{Alias: (*Alias)(&n)}
|
|
|
|
return json.Marshal(item)
|
|
|
|
}
|
|
|
|
|
2023-10-03 16:29:27 +00:00
|
|
|
func NewMessageNotification(id string, message *common.Message, chat *Chat, contact *Contact, resolvePrimaryName func(string) (string, error), profilePicturesVisibility int) (*localnotifications.Notification, error) {
|
2021-03-31 16:23:45 +00:00
|
|
|
body := &NotificationBody{
|
|
|
|
Message: message,
|
|
|
|
Chat: chat,
|
|
|
|
Contact: contact,
|
|
|
|
}
|
|
|
|
|
2023-10-03 16:29:27 +00:00
|
|
|
return body.toMessageNotification(id, resolvePrimaryName, profilePicturesVisibility)
|
2021-03-31 16:23:45 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 13:16:45 +00:00
|
|
|
func DeletedMessageNotification(id string, chat *Chat) *localnotifications.Notification {
|
|
|
|
return &localnotifications.Notification{
|
|
|
|
BodyType: localnotifications.TypeMessage,
|
|
|
|
ID: gethcommon.HexToHash(id),
|
|
|
|
IsConversation: true,
|
|
|
|
ConversationID: chat.ID,
|
|
|
|
Deeplink: chat.DeepLink(),
|
|
|
|
Deleted: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 16:23:45 +00:00
|
|
|
func NewCommunityRequestToJoinNotification(id string, community *communities.Community, contact *Contact) *localnotifications.Notification {
|
|
|
|
body := &NotificationBody{
|
|
|
|
Community: community,
|
|
|
|
Contact: contact,
|
|
|
|
}
|
|
|
|
|
|
|
|
return body.toCommunityRequestToJoinNotification(id)
|
|
|
|
}
|
|
|
|
|
2021-10-07 04:42:27 +00:00
|
|
|
func NewPrivateGroupInviteNotification(id string, chat *Chat, contact *Contact, profilePicturesVisibility int) *localnotifications.Notification {
|
2021-09-27 14:02:25 +00:00
|
|
|
body := &NotificationBody{
|
|
|
|
Chat: chat,
|
|
|
|
Contact: contact,
|
|
|
|
}
|
|
|
|
|
2021-10-07 04:42:27 +00:00
|
|
|
return body.toPrivateGroupInviteNotification(id, profilePicturesVisibility)
|
2021-09-27 14:02:25 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 16:29:27 +00:00
|
|
|
func (n NotificationBody) toMessageNotification(id string, resolvePrimaryName func(string) (string, error), profilePicturesVisibility int) (*localnotifications.Notification, error) {
|
2021-03-31 16:23:45 +00:00
|
|
|
var title string
|
|
|
|
if n.Chat.PrivateGroupChat() || n.Chat.Public() || n.Chat.CommunityChat() {
|
|
|
|
title = n.Chat.Name
|
|
|
|
} else if n.Chat.OneToOne() {
|
2023-02-22 16:57:33 +00:00
|
|
|
title = n.Contact.PrimaryName()
|
2021-03-31 16:23:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
canonicalNames := make(map[string]string)
|
2021-03-29 15:41:30 +00:00
|
|
|
for _, mentionID := range n.Message.Mentions {
|
2023-10-03 16:29:27 +00:00
|
|
|
name, err := resolvePrimaryName(mentionID)
|
|
|
|
if err != nil {
|
|
|
|
canonicalNames[mentionID] = mentionID
|
|
|
|
} else {
|
|
|
|
canonicalNames[mentionID] = name
|
2021-03-31 16:23:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 09:40:02 +00:00
|
|
|
// We don't pass idenity as only interested in the simplified text
|
|
|
|
simplifiedText, err := n.Message.GetSimplifiedText("", canonicalNames)
|
2021-03-31 16:23:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &localnotifications.Notification{
|
|
|
|
Body: n,
|
|
|
|
ID: gethcommon.HexToHash(id),
|
|
|
|
BodyType: localnotifications.TypeMessage,
|
|
|
|
Category: localnotifications.CategoryMessage,
|
|
|
|
Deeplink: n.Chat.DeepLink(),
|
|
|
|
Title: title,
|
|
|
|
Message: simplifiedText,
|
|
|
|
IsConversation: true,
|
|
|
|
IsGroupConversation: true,
|
|
|
|
Author: localnotifications.NotificationAuthor{
|
2023-02-22 16:57:33 +00:00
|
|
|
Name: n.Contact.PrimaryName(),
|
2022-03-23 18:47:00 +00:00
|
|
|
Icon: n.Contact.CanonicalImage(settings.ProfilePicturesVisibilityType(profilePicturesVisibility)),
|
2021-03-31 16:23:45 +00:00
|
|
|
ID: n.Contact.ID,
|
|
|
|
},
|
|
|
|
Timestamp: n.Message.WhisperTimestamp,
|
|
|
|
ConversationID: n.Chat.ID,
|
|
|
|
Image: "",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-10-07 04:42:27 +00:00
|
|
|
func (n NotificationBody) toPrivateGroupInviteNotification(id string, profilePicturesVisibility int) *localnotifications.Notification {
|
2021-09-27 14:02:25 +00:00
|
|
|
return &localnotifications.Notification{
|
|
|
|
ID: gethcommon.HexToHash(id),
|
|
|
|
Body: n,
|
2023-02-22 16:57:33 +00:00
|
|
|
Title: n.Contact.PrimaryName() + " invited you to " + n.Chat.Name,
|
|
|
|
Message: n.Contact.PrimaryName() + " wants you to join group " + n.Chat.Name,
|
2021-09-27 14:02:25 +00:00
|
|
|
BodyType: localnotifications.TypeMessage,
|
|
|
|
Category: localnotifications.CategoryGroupInvite,
|
|
|
|
Deeplink: n.Chat.DeepLink(),
|
|
|
|
Author: localnotifications.NotificationAuthor{
|
2023-02-22 16:57:33 +00:00
|
|
|
Name: n.Contact.PrimaryName(),
|
2022-03-23 18:47:00 +00:00
|
|
|
Icon: n.Contact.CanonicalImage(settings.ProfilePicturesVisibilityType(profilePicturesVisibility)),
|
2021-09-27 14:02:25 +00:00
|
|
|
ID: n.Contact.ID,
|
|
|
|
},
|
|
|
|
Image: "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 16:23:45 +00:00
|
|
|
func (n NotificationBody) toCommunityRequestToJoinNotification(id string) *localnotifications.Notification {
|
|
|
|
return &localnotifications.Notification{
|
|
|
|
ID: gethcommon.HexToHash(id),
|
|
|
|
Body: n,
|
2023-02-22 16:57:33 +00:00
|
|
|
Title: n.Contact.PrimaryName() + " wants to join " + n.Community.Name(),
|
|
|
|
Message: n.Contact.PrimaryName() + " wants to join message " + n.Community.Name(),
|
2021-03-31 16:23:45 +00:00
|
|
|
BodyType: localnotifications.TypeMessage,
|
|
|
|
Category: localnotifications.CategoryCommunityRequestToJoin,
|
2023-11-02 05:56:06 +00:00
|
|
|
Deeplink: "status-app://cr/" + n.Community.IDString(),
|
2021-03-31 16:23:45 +00:00
|
|
|
Image: "",
|
|
|
|
}
|
2021-01-26 11:49:37 +00:00
|
|
|
}
|