Skip to content
Snippets Groups Projects
Commit 52be5f4d authored by Benjamin Wenger's avatar Benjamin Wenger
Browse files

implemented the handling for batched notifications.go

parent 88431cc7
Branches
Tags
2 merge requests!117Release,!94implemented the handling for batched notifications.go
...@@ -9,10 +9,13 @@ package bindings ...@@ -9,10 +9,13 @@ package bindings
import ( import (
"encoding/base64" "encoding/base64"
"encoding/csv"
"encoding/json" "encoding/json"
"github.com/pkg/errors" "github.com/pkg/errors"
"gitlab.com/elixxir/client/storage/edge" "gitlab.com/elixxir/client/storage/edge"
pb "gitlab.com/elixxir/comms/mixmessages"
"gitlab.com/elixxir/crypto/fingerprint" "gitlab.com/elixxir/crypto/fingerprint"
"strings"
) )
type NotificationForMeReport struct { type NotificationForMeReport struct {
...@@ -33,7 +36,24 @@ func (nfmr *NotificationForMeReport) Source() []byte { ...@@ -33,7 +36,24 @@ func (nfmr *NotificationForMeReport) Source() []byte {
return nfmr.source return nfmr.source
} }
// NotificationForMe Check if a notification received is for me type ManyNotificationForMeReport struct {
many []*NotificationForMeReport
}
func (mnfmr *ManyNotificationForMeReport) Get(i int) (*NotificationForMeReport, error) {
if len(mnfmr.many)>=i{
return nil, errors.New("Cannot get, too long")
}
return mnfmr.many[i], nil
}
func (mnfmr *ManyNotificationForMeReport) Len() int {
return len(mnfmr.many)
}
// NotificationsForMe Check if a notification received is for me
// It returns a NotificationForMeReport which contains a ForMe bool stating if it is for the caller, // It returns a NotificationForMeReport which contains a ForMe bool stating if it is for the caller,
// a Type, and a source. These are as follows: // a Type, and a source. These are as follows:
// TYPE SOURCE DESCRIPTION // TYPE SOURCE DESCRIPTION
...@@ -44,17 +64,7 @@ func (nfmr *NotificationForMeReport) Source() []byte { ...@@ -44,17 +64,7 @@ func (nfmr *NotificationForMeReport) Source() []byte {
// "e2e" sender user ID reception of an E2E message // "e2e" sender user ID reception of an E2E message
// "group" group ID reception of a group chat message // "group" group ID reception of a group chat message
// "endFT" sender user ID Last message sent confirming end of file transfer // "endFT" sender user ID Last message sent confirming end of file transfer
func NotificationForMe(messageHash, idFP string, preimages string) (*NotificationForMeReport, error) { func NotificationsForMe(notifCSV, preimages string) (*ManyNotificationForMeReport, error) {
//handle message hash and idFP
messageHashBytes, err := base64.StdEncoding.DecodeString(messageHash)
if err != nil {
return nil, errors.WithMessage(err, "Failed to decode message ID")
}
idFpBytes, err := base64.StdEncoding.DecodeString(idFP)
if err != nil {
return nil, errors.WithMessage(err, "Failed to decode identity fingerprint")
}
//handle deserialization of preimages //handle deserialization of preimages
var preimageList []edge.Preimage var preimageList []edge.Preimage
if err := json.Unmarshal([]byte(preimages), &preimageList); err != nil { if err := json.Unmarshal([]byte(preimages), &preimageList); err != nil {
...@@ -62,23 +72,37 @@ func NotificationForMe(messageHash, idFP string, preimages string) (*Notificatio ...@@ -62,23 +72,37 @@ func NotificationForMe(messageHash, idFP string, preimages string) (*Notificatio
"cannot check if notification is for me") "cannot check if notification is for me")
} }
list, err := DecodeNotificationsCSV(notifCSV)
if err != nil {
return nil, err
}
notifList := make( []*NotificationForMeReport, 0, len(list))
for _, notifData := range list {
n := &NotificationForMeReport{
forMe: false,
tYpe: "",
source: nil,
}
//check if any preimages match with the passed in data //check if any preimages match with the passed in data
for _, preimage := range preimageList { for _, preimage := range preimageList {
if fingerprint.CheckIdentityFpFromMessageHash(idFpBytes, messageHashBytes, preimage.Data) { if fingerprint.CheckIdentityFpFromMessageHash(notifData.IdentityFP, notifData.MessageHash, preimage.Data) {
return &NotificationForMeReport{ n = &NotificationForMeReport{
forMe: true, forMe: true,
tYpe: preimage.Type, tYpe: preimage.Type,
source: preimage.Source, source: preimage.Source,
}, nil
} }
} }
return &NotificationForMeReport{ break
forMe: false, }
tYpe: "", notifList = append(notifList, n)
source: nil,
}, nil
} }
return &ManyNotificationForMeReport{many: notifList}, nil
}
// RegisterForNotifications accepts firebase messaging token // RegisterForNotifications accepts firebase messaging token
func (c *Client) RegisterForNotifications(token string) error { func (c *Client) RegisterForNotifications(token string) error {
return c.api.RegisterForNotifications(token) return c.api.RegisterForNotifications(token)
...@@ -88,3 +112,30 @@ func (c *Client) RegisterForNotifications(token string) error { ...@@ -88,3 +112,30 @@ func (c *Client) RegisterForNotifications(token string) error {
func (c *Client) UnregisterForNotifications() error { func (c *Client) UnregisterForNotifications() error {
return c.api.UnregisterForNotifications() return c.api.UnregisterForNotifications()
} }
func DecodeNotificationsCSV(data string)([]*pb.NotificationData, error){
r := csv.NewReader(strings.NewReader(data))
read, err := r.ReadAll()
if err!=nil{
return nil, errors.WithMessage(err,"Failed to decode notifications CSV")
}
l := make([]*pb.NotificationData, len(read))
for i, touple := range read{
messageHash, err := base64.StdEncoding.DecodeString(touple[0])
if err!=nil{
return nil, errors.WithMessage(err,"Failed decode an element")
}
identityFP, err := base64.StdEncoding.DecodeString(touple[1])
if err!=nil{
return nil, errors.WithMessage(err,"Failed decode an element")
}
l[i] = &pb.NotificationData{
EphemeralID: 0,
IdentityFP: identityFP,
MessageHash: messageHash,
}
}
return l, nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment