status-go/protocol/local_notifications.go

163 lines
5.2 KiB
Go
Raw Normal View History

package protocol
import (
"crypto/ecdsa"
"encoding/json"
gethcommon "github.com/ethereum/go-ethereum/common"
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
"github.com/status-im/status-go/multiaccounts/settings"
"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 {
if chat != nil && !chat.Active {
return false
}
if chat != nil && (chat.OneToOne() || chat.PrivateGroupChat()) {
return true
}
if message.Mentioned {
return true
}
if responseTo != nil {
publicKeyString := common.PubkeyToHex(&publicKey)
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)
}
func NewMessageNotification(id string, message *common.Message, chat *Chat, contact *Contact, resolvePrimaryName func(string) (string, error), profilePicturesVisibility int) (*localnotifications.Notification, error) {
body := &NotificationBody{
Message: message,
Chat: chat,
Contact: contact,
}
return body.toMessageNotification(id, resolvePrimaryName, profilePicturesVisibility)
}
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,
}
}
func NewCommunityRequestToJoinNotification(id string, community *communities.Community, contact *Contact) *localnotifications.Notification {
body := &NotificationBody{
Community: community,
Contact: contact,
}
return body.toCommunityRequestToJoinNotification(id)
}
func NewPrivateGroupInviteNotification(id string, chat *Chat, contact *Contact, profilePicturesVisibility int) *localnotifications.Notification {
body := &NotificationBody{
Chat: chat,
Contact: contact,
}
return body.toPrivateGroupInviteNotification(id, profilePicturesVisibility)
}
func (n NotificationBody) toMessageNotification(id string, resolvePrimaryName func(string) (string, error), profilePicturesVisibility int) (*localnotifications.Notification, error) {
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()
}
canonicalNames := make(map[string]string)
2021-03-29 15:41:30 +00:00
for _, mentionID := range n.Message.Mentions {
name, err := resolvePrimaryName(mentionID)
if err != nil {
canonicalNames[mentionID] = mentionID
} else {
canonicalNames[mentionID] = name
}
}
// We don't pass idenity as only interested in the simplified text
simplifiedText, err := n.Message.GetSimplifiedText("", canonicalNames)
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(),
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
Icon: n.Contact.CanonicalImage(settings.ProfilePicturesVisibilityType(profilePicturesVisibility)),
ID: n.Contact.ID,
},
Timestamp: n.Message.WhisperTimestamp,
ConversationID: n.Chat.ID,
Image: "",
}, nil
}
func (n NotificationBody) toPrivateGroupInviteNotification(id string, profilePicturesVisibility int) *localnotifications.Notification {
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,
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(),
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
Icon: n.Contact.CanonicalImage(settings.ProfilePicturesVisibilityType(profilePicturesVisibility)),
ID: n.Contact.ID,
},
Image: "",
}
}
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(),
BodyType: localnotifications.TypeMessage,
Category: localnotifications.CategoryCommunityRequestToJoin,
Deeplink: "status-app://cr/" + n.Community.IDString(),
Image: "",
}
}