github.com/status-im/status-go@v1.1.0/protocol/activity_center.go (about)

     1  package protocol
     2  
     3  import (
     4  	"crypto/ecdsa"
     5  	"errors"
     6  
     7  	"github.com/status-im/status-go/eth-node/types"
     8  	"github.com/status-im/status-go/protocol/common"
     9  	"github.com/status-im/status-go/protocol/verification"
    10  	"github.com/status-im/status-go/services/wallet/thirdparty"
    11  )
    12  
    13  // The activity center is a place where we store incoming notifications before
    14  // they are shown to the users as new chats, in order to mitigate the impact of spam
    15  // on the messenger
    16  
    17  type ActivityCenterType int
    18  
    19  const (
    20  	ActivityCenterNotificationNoType ActivityCenterType = iota
    21  	ActivityCenterNotificationTypeNewOneToOne
    22  	ActivityCenterNotificationTypeNewPrivateGroupChat
    23  	ActivityCenterNotificationTypeMention
    24  	ActivityCenterNotificationTypeReply
    25  	ActivityCenterNotificationTypeContactRequest
    26  	ActivityCenterNotificationTypeCommunityInvitation
    27  	ActivityCenterNotificationTypeCommunityRequest
    28  	ActivityCenterNotificationTypeCommunityMembershipRequest
    29  	ActivityCenterNotificationTypeCommunityKicked
    30  	ActivityCenterNotificationTypeContactVerification
    31  	ActivityCenterNotificationTypeContactRemoved
    32  	ActivityCenterNotificationTypeNewKeypairAddedToPairedDevice
    33  	ActivityCenterNotificationTypeOwnerTokenReceived
    34  	ActivityCenterNotificationTypeOwnershipReceived
    35  	ActivityCenterNotificationTypeOwnershipLost
    36  	ActivityCenterNotificationTypeSetSignerFailed
    37  	ActivityCenterNotificationTypeSetSignerDeclined
    38  	ActivityCenterNotificationTypeShareAccounts
    39  	ActivityCenterNotificationTypeCommunityTokenReceived
    40  	ActivityCenterNotificationTypeFirstCommunityTokenReceived
    41  	ActivityCenterNotificationTypeCommunityBanned
    42  	ActivityCenterNotificationTypeCommunityUnbanned
    43  )
    44  
    45  type ActivityCenterMembershipStatus int
    46  
    47  const (
    48  	ActivityCenterMembershipStatusIdle ActivityCenterMembershipStatus = iota
    49  	ActivityCenterMembershipStatusPending
    50  	ActivityCenterMembershipStatusAccepted
    51  	ActivityCenterMembershipStatusDeclined
    52  	ActivityCenterMembershipStatusAcceptedPending
    53  	ActivityCenterMembershipStatusDeclinedPending
    54  	ActivityCenterMembershipOwnershipChanged
    55  )
    56  
    57  type ActivityCenterQueryParamsRead uint
    58  
    59  const (
    60  	ActivityCenterQueryParamsReadRead = iota + 1
    61  	ActivityCenterQueryParamsReadUnread
    62  	ActivityCenterQueryParamsReadAll
    63  )
    64  
    65  var ErrInvalidActivityCenterNotification = errors.New("invalid activity center notification")
    66  
    67  type ActivityTokenData struct {
    68  	ChainID       uint64                         `json:"chainId,omitempty"`
    69  	CollectibleID thirdparty.CollectibleUniqueID `json:"collectibleId,omitempty"`
    70  	TxHash        string                         `json:"txHash,omitempty"`
    71  	WalletAddress string                         `json:"walletAddress,omitempty"`
    72  	IsFirst       bool                           `json:"isFirst,omitempty"`
    73  	// Community data
    74  	CommunityID string `json:"communityId,omitempty"`
    75  	// Token data
    76  	Amount    string `json:"amount,omitempty"`
    77  	Name      string `json:"name,omitempty"`
    78  	Symbol    string `json:"symbol,omitempty"`
    79  	ImageURL  string `json:"imageUrl,omitempty"`
    80  	TokenType int    `json:"tokenType,omitempty"`
    81  }
    82  
    83  type ActivityCenterNotification struct {
    84  	ID                        types.HexBytes                 `json:"id"`
    85  	ChatID                    string                         `json:"chatId"`
    86  	CommunityID               string                         `json:"communityId"`
    87  	MembershipStatus          ActivityCenterMembershipStatus `json:"membershipStatus"`
    88  	Name                      string                         `json:"name"`
    89  	Author                    string                         `json:"author"`
    90  	Type                      ActivityCenterType             `json:"type"`
    91  	LastMessage               *common.Message                `json:"lastMessage"`
    92  	Message                   *common.Message                `json:"message"`
    93  	ReplyMessage              *common.Message                `json:"replyMessage"`
    94  	Timestamp                 uint64                         `json:"timestamp"`
    95  	Read                      bool                           `json:"read"`
    96  	Dismissed                 bool                           `json:"dismissed"`
    97  	Deleted                   bool                           `json:"deleted"`
    98  	Accepted                  bool                           `json:"accepted"`
    99  	ContactVerificationStatus verification.RequestStatus     `json:"contactVerificationStatus"`
   100  	TokenData                 *ActivityTokenData             `json:"tokenData"`
   101  	//Used for synchronization. Each update should increment the UpdatedAt.
   102  	//The value should represent the time when the update occurred.
   103  	UpdatedAt     uint64            `json:"updatedAt"`
   104  	AlbumMessages []*common.Message `json:"albumMessages"`
   105  }
   106  
   107  func (n *ActivityCenterNotification) IncrementUpdatedAt(timesource common.TimeSource) {
   108  	tNow := timesource.GetCurrentTime()
   109  	// If updatead at is greater or equal than time now, we bump it
   110  	if n.UpdatedAt >= tNow {
   111  		n.UpdatedAt++
   112  	} else {
   113  		n.UpdatedAt = tNow
   114  	}
   115  }
   116  
   117  type ActivityCenterNotificationsRequest struct {
   118  	Cursor        string                        `json:"cursor"`
   119  	Limit         uint64                        `json:"limit"`
   120  	ActivityTypes []ActivityCenterType          `json:"activityTypes"`
   121  	ReadType      ActivityCenterQueryParamsRead `json:"readType"`
   122  }
   123  
   124  type ActivityCenterCountRequest struct {
   125  	ActivityTypes []ActivityCenterType          `json:"activityTypes"`
   126  	ReadType      ActivityCenterQueryParamsRead `json:"readType"`
   127  }
   128  
   129  type ActivityCenterPaginationResponse struct {
   130  	Cursor        string                        `json:"cursor"`
   131  	Notifications []*ActivityCenterNotification `json:"notifications"`
   132  }
   133  
   134  type ActivityCenterCountResponse = map[ActivityCenterType]uint64
   135  
   136  type ActivityCenterState struct {
   137  	HasSeen   bool   `json:"hasSeen"`
   138  	UpdatedAt uint64 `json:"updatedAt"`
   139  }
   140  
   141  func (n *ActivityCenterNotification) Valid() error {
   142  	if len(n.ID) == 0 || n.Type == 0 || n.Timestamp == 0 {
   143  		return ErrInvalidActivityCenterNotification
   144  	}
   145  	return nil
   146  }
   147  
   148  func showMentionOrReplyActivityCenterNotification(publicKey ecdsa.PublicKey, message *common.Message, chat *Chat, responseTo *common.Message) (bool, ActivityCenterType) {
   149  	if chat == nil || !chat.Active || (!chat.CommunityChat() && !chat.PrivateGroupChat()) || chat.Muted {
   150  		return false, ActivityCenterNotificationNoType
   151  	}
   152  
   153  	if message.Mentioned {
   154  		return true, ActivityCenterNotificationTypeMention
   155  	}
   156  
   157  	publicKeyString := common.PubkeyToHex(&publicKey)
   158  	if responseTo != nil && responseTo.From == publicKeyString {
   159  		return true, ActivityCenterNotificationTypeReply
   160  	}
   161  
   162  	return false, ActivityCenterNotificationNoType
   163  }