-
Notifications
You must be signed in to change notification settings - Fork 243
/
local_notifications.go
137 lines (117 loc) · 4 KB
/
local_notifications.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package protocol
import (
"crypto/ecdsa"
"encoding/json"
gethcommon "github.com/ethereum/go-ethereum/common"
"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, contacts *contactMap) (*localnotifications.Notification, error) {
body := &NotificationBody{
Message: message,
Chat: chat,
Contact: contact,
}
return body.toMessageNotification(id, contacts)
}
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 (n NotificationBody) toMessageNotification(id string, contacts *contactMap) (*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() {
title = n.Contact.CanonicalName()
}
canonicalNames := make(map[string]string)
for _, mentionID := range n.Message.Mentions {
contact, ok := contacts.Load(mentionID)
if !ok {
var err error
contact, err = buildContactFromPkString(mentionID)
if err != nil {
return nil, err
}
}
canonicalNames[mentionID] = contact.CanonicalName()
}
// 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{
Name: n.Contact.CanonicalName(),
Icon: n.Contact.CanonicalImage(),
ID: n.Contact.ID,
},
Timestamp: n.Message.WhisperTimestamp,
ConversationID: n.Chat.ID,
Image: "",
}, nil
}
func (n NotificationBody) toCommunityRequestToJoinNotification(id string) *localnotifications.Notification {
return &localnotifications.Notification{
ID: gethcommon.HexToHash(id),
Body: n,
Title: n.Contact.CanonicalName() + " wants to join " + n.Community.Name(),
Message: n.Contact.CanonicalName() + " wants to join message " + n.Community.Name(),
BodyType: localnotifications.TypeMessage,
Category: localnotifications.CategoryCommunityRequestToJoin,
Deeplink: "status-im://cr/" + n.Community.IDString(),
Image: "",
}
}