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

     1  package protocol
     2  
     3  import (
     4  	"crypto/ecdsa"
     5  	"encoding/json"
     6  
     7  	gethcommon "github.com/ethereum/go-ethereum/common"
     8  	"github.com/status-im/status-go/multiaccounts/settings"
     9  	"github.com/status-im/status-go/protocol/common"
    10  	"github.com/status-im/status-go/protocol/communities"
    11  	localnotifications "github.com/status-im/status-go/services/local-notifications"
    12  )
    13  
    14  type NotificationBody struct {
    15  	Message   *common.Message        `json:"message"`
    16  	Contact   *Contact               `json:"contact"`
    17  	Chat      *Chat                  `json:"chat"`
    18  	Community *communities.Community `json:"community"`
    19  }
    20  
    21  func showMessageNotification(publicKey ecdsa.PublicKey, message *common.Message, chat *Chat, responseTo *common.Message) bool {
    22  	if chat != nil && !chat.Active {
    23  		return false
    24  	}
    25  
    26  	if chat != nil && (chat.OneToOne() || chat.PrivateGroupChat()) {
    27  		return true
    28  	}
    29  
    30  	if message.Mentioned {
    31  		return true
    32  	}
    33  
    34  	if responseTo != nil {
    35  		publicKeyString := common.PubkeyToHex(&publicKey)
    36  		return responseTo.From == publicKeyString
    37  	}
    38  
    39  	return false
    40  }
    41  
    42  func (n NotificationBody) MarshalJSON() ([]byte, error) {
    43  	type Alias NotificationBody
    44  	item := struct{ *Alias }{Alias: (*Alias)(&n)}
    45  	return json.Marshal(item)
    46  }
    47  
    48  func NewMessageNotification(id string, message *common.Message, chat *Chat, contact *Contact, resolvePrimaryName func(string) (string, error), profilePicturesVisibility int) (*localnotifications.Notification, error) {
    49  	body := &NotificationBody{
    50  		Message: message,
    51  		Chat:    chat,
    52  		Contact: contact,
    53  	}
    54  
    55  	return body.toMessageNotification(id, resolvePrimaryName, profilePicturesVisibility)
    56  }
    57  
    58  func DeletedMessageNotification(id string, chat *Chat) *localnotifications.Notification {
    59  	return &localnotifications.Notification{
    60  		BodyType:       localnotifications.TypeMessage,
    61  		ID:             gethcommon.HexToHash(id),
    62  		IsConversation: true,
    63  		ConversationID: chat.ID,
    64  		Deeplink:       chat.DeepLink(),
    65  		Deleted:        true,
    66  	}
    67  }
    68  
    69  func NewCommunityRequestToJoinNotification(id string, community *communities.Community, contact *Contact) *localnotifications.Notification {
    70  	body := &NotificationBody{
    71  		Community: community,
    72  		Contact:   contact,
    73  	}
    74  
    75  	return body.toCommunityRequestToJoinNotification(id)
    76  }
    77  
    78  func NewPrivateGroupInviteNotification(id string, chat *Chat, contact *Contact, profilePicturesVisibility int) *localnotifications.Notification {
    79  	body := &NotificationBody{
    80  		Chat:    chat,
    81  		Contact: contact,
    82  	}
    83  
    84  	return body.toPrivateGroupInviteNotification(id, profilePicturesVisibility)
    85  }
    86  
    87  func (n NotificationBody) toMessageNotification(id string, resolvePrimaryName func(string) (string, error), profilePicturesVisibility int) (*localnotifications.Notification, error) {
    88  	var title string
    89  	if n.Chat.PrivateGroupChat() || n.Chat.Public() || n.Chat.CommunityChat() {
    90  		title = n.Chat.Name
    91  	} else if n.Chat.OneToOne() {
    92  		title = n.Contact.PrimaryName()
    93  
    94  	}
    95  
    96  	canonicalNames := make(map[string]string)
    97  	for _, mentionID := range n.Message.Mentions {
    98  		name, err := resolvePrimaryName(mentionID)
    99  		if err != nil {
   100  			canonicalNames[mentionID] = mentionID
   101  		} else {
   102  			canonicalNames[mentionID] = name
   103  		}
   104  	}
   105  
   106  	// We don't pass idenity as only interested in the simplified text
   107  	simplifiedText, err := n.Message.GetSimplifiedText("", canonicalNames)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	return &localnotifications.Notification{
   113  		Body:                n,
   114  		ID:                  gethcommon.HexToHash(id),
   115  		BodyType:            localnotifications.TypeMessage,
   116  		Category:            localnotifications.CategoryMessage,
   117  		Deeplink:            n.Chat.DeepLink(),
   118  		Title:               title,
   119  		Message:             simplifiedText,
   120  		IsConversation:      true,
   121  		IsGroupConversation: true,
   122  		Author: localnotifications.NotificationAuthor{
   123  			Name: n.Contact.PrimaryName(),
   124  			Icon: n.Contact.CanonicalImage(settings.ProfilePicturesVisibilityType(profilePicturesVisibility)),
   125  			ID:   n.Contact.ID,
   126  		},
   127  		Timestamp:      n.Message.WhisperTimestamp,
   128  		ConversationID: n.Chat.ID,
   129  		Image:          "",
   130  	}, nil
   131  }
   132  
   133  func (n NotificationBody) toPrivateGroupInviteNotification(id string, profilePicturesVisibility int) *localnotifications.Notification {
   134  	return &localnotifications.Notification{
   135  		ID:       gethcommon.HexToHash(id),
   136  		Body:     n,
   137  		Title:    n.Contact.PrimaryName() + " invited you to " + n.Chat.Name,
   138  		Message:  n.Contact.PrimaryName() + " wants you to join group " + n.Chat.Name,
   139  		BodyType: localnotifications.TypeMessage,
   140  		Category: localnotifications.CategoryGroupInvite,
   141  		Deeplink: n.Chat.DeepLink(),
   142  		Author: localnotifications.NotificationAuthor{
   143  			Name: n.Contact.PrimaryName(),
   144  			Icon: n.Contact.CanonicalImage(settings.ProfilePicturesVisibilityType(profilePicturesVisibility)),
   145  			ID:   n.Contact.ID,
   146  		},
   147  		Image: "",
   148  	}
   149  }
   150  
   151  func (n NotificationBody) toCommunityRequestToJoinNotification(id string) *localnotifications.Notification {
   152  	return &localnotifications.Notification{
   153  		ID:       gethcommon.HexToHash(id),
   154  		Body:     n,
   155  		Title:    n.Contact.PrimaryName() + " wants to join " + n.Community.Name(),
   156  		Message:  n.Contact.PrimaryName() + " wants to join  message " + n.Community.Name(),
   157  		BodyType: localnotifications.TypeMessage,
   158  		Category: localnotifications.CategoryCommunityRequestToJoin,
   159  		Deeplink: "status-app://cr/" + n.Community.IDString(),
   160  		Image:    "",
   161  	}
   162  }