2021-04-07 12:57:14 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2021-05-29 17:05:25 +00:00
|
|
|
"crypto/ecdsa"
|
2021-04-07 12:57:14 +00:00
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/protocol/common"
|
2022-08-31 14:41:58 +00:00
|
|
|
"github.com/status-im/status-go/protocol/verification"
|
2021-04-07 12:57:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// The activity center is a place where we store incoming notifications before
|
|
|
|
// they are shown to the users as new chats, in order to mitigate the impact of spam
|
|
|
|
// on the messenger
|
|
|
|
|
|
|
|
type ActivityCenterType int
|
|
|
|
|
|
|
|
const (
|
2021-06-11 20:14:05 +00:00
|
|
|
ActivityCenterNotificationNoType ActivityCenterType = iota
|
|
|
|
ActivityCenterNotificationTypeNewOneToOne
|
2021-04-07 12:57:14 +00:00
|
|
|
ActivityCenterNotificationTypeNewPrivateGroupChat
|
2021-05-29 17:05:25 +00:00
|
|
|
ActivityCenterNotificationTypeMention
|
2021-06-11 20:14:05 +00:00
|
|
|
ActivityCenterNotificationTypeReply
|
2022-01-18 16:31:34 +00:00
|
|
|
ActivityCenterNotificationTypeContactRequest
|
2022-10-25 22:06:20 +00:00
|
|
|
ActivityCenterNotificationTypeCommunityInvitation
|
|
|
|
ActivityCenterNotificationTypeCommunityRequest
|
|
|
|
ActivityCenterNotificationTypeCommunityMembershipRequest
|
|
|
|
ActivityCenterNotificationTypeCommunityKicked
|
2022-08-31 14:41:58 +00:00
|
|
|
ActivityCenterNotificationTypeContactVerification
|
2022-10-25 22:06:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ActivityCenterMembershipStatus int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ActivityCenterMembershipStatusIdle ActivityCenterMembershipStatus = iota
|
|
|
|
ActivityCenterMembershipStatusPending
|
|
|
|
ActivityCenterMembershipStatusAccepted
|
|
|
|
ActivityCenterMembershipStatusDeclined
|
2021-04-07 12:57:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var ErrInvalidActivityCenterNotification = errors.New("invalid activity center notification")
|
|
|
|
|
|
|
|
type ActivityCenterNotification struct {
|
2022-10-24 11:33:47 +00:00
|
|
|
ID types.HexBytes `json:"id"`
|
|
|
|
ChatID string `json:"chatId"`
|
|
|
|
CommunityID string `json:"communityId"`
|
|
|
|
MembershipStatus ActivityCenterMembershipStatus `json:"membershipStatus"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Author string `json:"author"`
|
|
|
|
Type ActivityCenterType `json:"type"`
|
|
|
|
LastMessage *common.Message `json:"lastMessage"`
|
|
|
|
Message *common.Message `json:"message"`
|
|
|
|
ReplyMessage *common.Message `json:"replyMessage"`
|
|
|
|
Timestamp uint64 `json:"timestamp"`
|
|
|
|
Read bool `json:"read"`
|
|
|
|
Dismissed bool `json:"dismissed"`
|
|
|
|
Accepted bool `json:"accepted"`
|
|
|
|
ContactVerificationStatus verification.RequestStatus `json:"contactVerificationStatus"`
|
2021-04-07 12:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ActivityCenterPaginationResponse struct {
|
|
|
|
Cursor string `json:"cursor"`
|
|
|
|
Notifications []*ActivityCenterNotification `json:"notifications"`
|
|
|
|
}
|
|
|
|
|
2023-02-17 10:08:08 +00:00
|
|
|
type ActivityCenterState struct {
|
|
|
|
HasSeen bool `json:"hasSeen"`
|
|
|
|
}
|
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
func (n *ActivityCenterNotification) Valid() error {
|
|
|
|
if len(n.ID) == 0 || n.Type == 0 || n.Timestamp == 0 {
|
|
|
|
return ErrInvalidActivityCenterNotification
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-29 17:05:25 +00:00
|
|
|
|
2021-06-11 20:14:05 +00:00
|
|
|
func showMentionOrReplyActivityCenterNotification(publicKey ecdsa.PublicKey, message *common.Message, chat *Chat, responseTo *common.Message) (bool, ActivityCenterType) {
|
|
|
|
if chat == nil || !chat.Active || (!chat.CommunityChat() && !chat.PrivateGroupChat()) {
|
|
|
|
return false, ActivityCenterNotificationNoType
|
2021-05-29 17:05:25 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 20:14:05 +00:00
|
|
|
if message.Mentioned {
|
|
|
|
return true, ActivityCenterNotificationTypeMention
|
2021-05-29 17:05:25 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 20:14:05 +00:00
|
|
|
publicKeyString := common.PubkeyToHex(&publicKey)
|
|
|
|
if responseTo != nil && responseTo.From == publicKeyString {
|
|
|
|
return true, ActivityCenterNotificationTypeReply
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, ActivityCenterNotificationNoType
|
2021-05-29 17:05:25 +00:00
|
|
|
}
|