Skip to content
Snippets Groups Projects
Commit 470a2eea authored by Jono Wenger's avatar Jono Wenger
Browse files

XX-4712 / Add wrapper for bindings.DmCallbacks

parent 38e5c12f
No related branches found
No related tags found
2 merge requests!135XX-4712 / Add wrapper for bindings.DmCallbacks,!109Project/haven beta
......@@ -11,7 +11,7 @@ require (
github.com/spf13/cobra v1.7.0
github.com/spf13/jwalterweatherman v1.1.0
github.com/stretchr/testify v1.8.2
gitlab.com/elixxir/client/v4 v4.6.4-0.20230629182558-519cc3467eae
gitlab.com/elixxir/client/v4 v4.6.4-0.20230629183830-d8f41f8d0d1e
gitlab.com/elixxir/crypto v0.0.7-0.20230614183801-387e0cb8e76f
gitlab.com/elixxir/primitives v0.0.3-0.20230613193928-8cf8bdd777ef
gitlab.com/elixxir/wasm-utils v0.0.0-20230615222914-185dd3a6fa08
......@@ -31,10 +31,12 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
github.com/elliotchance/orderedmap v1.4.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/gobwas/ws v1.1.0 // indirect
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/improbable-eng/grpc-web v0.15.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
......@@ -43,18 +45,27 @@ require (
github.com/klauspost/compress v1.15.9 // indirect
github.com/klauspost/cpuid/v2 v2.1.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-sqlite3 v1.14.15 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
github.com/oasisprotocol/curve25519-voi v0.0.0-20221003100820-41fad3beba17 // indirect
github.com/oasisprotocol/deoxysii v0.0.0-20220228165953-2091330c22b7 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/pkg/profile v1.6.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/cors v1.8.2 // indirect
github.com/sethvargo/go-diceware v0.3.0 // indirect
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
github.com/soheilhy/cmux v0.1.5 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.12.0 // indirect
github.com/subosito/gotenv v1.4.0 // indirect
github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 // indirect
github.com/ttacon/libphonenumber v1.2.1 // indirect
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
......@@ -75,6 +86,8 @@ require (
google.golang.org/genproto v0.0.0-20220822174746-9e6da59bd2fc // indirect
google.golang.org/grpc v1.49.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/sqlite v1.4.4 // indirect
gorm.io/gorm v1.24.3 // indirect
......
This diff is collapsed.
......@@ -12,6 +12,7 @@ package main
import (
"crypto/ed25519"
"encoding/json"
"gitlab.com/elixxir/client/v4/bindings"
"strconv"
"strings"
"syscall/js"
......@@ -168,8 +169,8 @@ func (w *wasmModel) ReceiveMessage(channelID *id.ID, messageID message.ID,
return 0
}
go w.eventUpdate(channels.MessageReceived, channels.MessageReceivedJson{
Uuid: int64(uuid),
go w.eventUpdate(bindings.MessageReceived, bindings.MessageReceivedJSON{
UUID: int64(uuid),
ChannelID: channelID,
Update: false,
})
......@@ -210,8 +211,8 @@ func (w *wasmModel) ReceiveReply(channelID *id.ID, messageID,
return 0
}
go w.eventUpdate(channels.MessageReceived, channels.MessageReceivedJson{
Uuid: int64(uuid),
go w.eventUpdate(bindings.MessageReceived, bindings.MessageReceivedJSON{
UUID: int64(uuid),
ChannelID: channelID,
Update: false,
})
......@@ -252,8 +253,8 @@ func (w *wasmModel) ReceiveReaction(channelID *id.ID, messageID,
return 0
}
go w.eventUpdate(channels.MessageReceived, channels.MessageReceivedJson{
Uuid: int64(uuid),
go w.eventUpdate(bindings.MessageReceived, bindings.MessageReceivedJSON{
UUID: int64(uuid),
ChannelID: channelID,
Update: false,
})
......@@ -409,8 +410,8 @@ func (w *wasmModel) updateMessage(currentMsg *Message, messageID *message.ID,
return 0, err
}
go w.eventUpdate(channels.MessageReceived, channels.MessageReceivedJson{
Uuid: int64(uuid),
go w.eventUpdate(bindings.MessageReceived, bindings.MessageReceivedJSON{
UUID: int64(uuid),
ChannelID: channelID,
Update: true,
})
......@@ -531,8 +532,8 @@ func (w *wasmModel) DeleteMessage(messageID message.ID) error {
return err
}
go w.eventUpdate(channels.MessageDeleted,
channels.MessageDeletedJson{MessageID: messageID})
go w.eventUpdate(bindings.MessageDeleted,
bindings.MessageDeletedJSON{MessageID: messageID})
return nil
}
......@@ -541,7 +542,7 @@ func (w *wasmModel) DeleteMessage(messageID message.ID) error {
func (w *wasmModel) MuteUser(
channelID *id.ID, pubKey ed25519.PublicKey, unmute bool) {
go w.eventUpdate(channels.UserMuted, channels.UserMutedJson{
go w.eventUpdate(bindings.UserMuted, bindings.UserMutedJSON{
ChannelID: channelID,
PubKey: pubKey,
Unmute: unmute,
......
......@@ -16,6 +16,7 @@ import (
"github.com/hack-pad/go-indexeddb/idb"
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/client/v4/bindings"
"gitlab.com/elixxir/client/v4/channels"
idbCrypto "gitlab.com/elixxir/crypto/indexedDb"
"gitlab.com/elixxir/xxdk-wasm/indexedDb/impl"
......@@ -29,13 +30,13 @@ const currentVersion uint = 1
// The name should be a base64 encoding of the users public key. Returns the
// EventModel based on IndexedDb and the database name as reported by IndexedDb.
func NewWASMEventModel(databaseName string, encryption idbCrypto.Cipher,
uiCallbacks channels.ChannelUICallbacks) (channels.EventModel, error) {
uiCallbacks bindings.ChannelUICallbacks) (channels.EventModel, error) {
return newWASMModel(databaseName, encryption, uiCallbacks)
}
// newWASMModel creates the given [idb.Database] and returns a wasmModel.
func newWASMModel(databaseName string, encryption idbCrypto.Cipher,
uiCallbacks channels.ChannelUICallbacks) (*wasmModel, error) {
uiCallbacks bindings.ChannelUICallbacks) (*wasmModel, error) {
// Attempt to open database object
ctx, cancel := impl.NewContext()
defer cancel()
......
......@@ -15,6 +15,7 @@ import (
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/client/v4/bindings"
"gitlab.com/elixxir/client/v4/channels"
idbCrypto "gitlab.com/elixxir/crypto/indexedDb"
"gitlab.com/elixxir/xxdk-wasm/logging"
......@@ -33,9 +34,8 @@ type eventUpdateCallback func(eventType int64, jsonData []byte)
// NewWASMEventModelBuilder returns an EventModelBuilder which allows
// the channel manager to define the path but the callback is the same
// across the board.
func NewWASMEventModelBuilder(wasmJsPath string,
encryption idbCrypto.Cipher,
channelCbs channels.ChannelUICallbacks) channels.EventModelBuilder {
func NewWASMEventModelBuilder(wasmJsPath string, encryption idbCrypto.Cipher,
channelCbs bindings.ChannelUICallbacks) channels.EventModelBuilder {
fn := func(path string) (channels.EventModel, error) {
return NewWASMEventModel(path, wasmJsPath, encryption,
channelCbs)
......@@ -53,8 +53,7 @@ type NewWASMEventModelMessage struct {
// NewWASMEventModel returns a [channels.EventModel] backed by a wasmModel.
// The name should be a base64 encoding of the users public key.
func NewWASMEventModel(path, wasmJsPath string, encryption idbCrypto.Cipher,
channelCbs channels.ChannelUICallbacks) (
channels.EventModel, error) {
channelCbs bindings.ChannelUICallbacks) (channels.EventModel, error) {
databaseName := path + databaseSuffix
wm, err := worker.NewManager(wasmJsPath, "channelsIndexedDb", true)
......
......@@ -97,8 +97,9 @@ const (
// - Internal password (Uint8Array).
// - Throws TypeError on failure.
func GetOrInitPassword(_ js.Value, args []js.Value) any {
externalPassword := args[0].String()
promiseFn := func(resolve, reject func(args ...any) js.Value) {
internalPassword, err := getOrInit(args[0].String())
internalPassword, err := getOrInit(externalPassword)
if err != nil {
reject(exception.NewTrace(err))
} else {
......
......@@ -77,65 +77,20 @@ func newDMClientJS(api *bindings.DMClient) map[string]any {
return dmClientMap
}
// newDmNotificationUpdate adds the callbacks from the Javascript object.
func newDmNotificationUpdate(value js.Value) *dmNotificationUpdate {
return &dmNotificationUpdate{callback: utils.WrapCB(value, "Callback")}
// dmCallbacks wraps Javascript callbacks to adhere to the
// [bindings.DmCallbacks] interface.
type dmCallbacks struct {
eventUpdate func(args ...any) js.Value
}
// dmNotificationUpdate wraps Javascript callbacks to adhere to the
// [bindings.DmNotificationUpdate] interface.
type dmNotificationUpdate struct {
callback func(args ...any) js.Value
// newDmCallbacks adds the callbacks from the Javascript object.
func newDmCallbacks(value js.Value) *dmCallbacks {
return &dmCallbacks{eventUpdate: utils.WrapCB(value, "EventUpdate")}
}
// Callback is called everytime there is an update to the notification filter
// or notification status for DMs.
//
// Parameters:
// - nfJSON - JSON of [dm.NotificationFilter], which is passed into
// [GetDmNotificationReportsForMe] to filter DM notifications for the user.
// - changedStateListJSON - JSON of a slice of [dm.NotificationState]. It
// includes all added or changed notification states for DM conversations.
// - deletedListJSON - JSON of a slice of [ed25519.PublicKey]. It includes
// conversation that were deleted.
//
// Example nfJSON:
//
// {
// "identifier": "MWL6mvtZ9UUm7jP3ainyI4erbRl+wyVaO5MOWboP0rA=",
// "myID": "AqDqg6Tcs359dBNRBCX7XHaotRDhz1ZRQNXIsGaubvID",
// "tags": [
// "61334HtH85DPIifvrM+JzRmLqfV5R4AMEmcPelTmFX0=",
// "zc/EPwtx5OKTVdwLcI15bghjJ7suNhu59PcarXE+m9o=",
// "FvArzVJ/082UEpMDCWJsopCLeLnxJV6NXINNkJTk3k8="
// ],
// "PublicKeys": {
// "61334HtH85DPIifvrM+JzRmLqfV5R4AMEmcPelTmFX0=": "b3HygDv8gjteune9wgBm3YtVuAo2foOusRmj0m5nl6E=",
// "FvArzVJ/082UEpMDCWJsopCLeLnxJV6NXINNkJTk3k8=": "uOLitBZcCh2TEW406jXHJ+Rsi6LybsH8R1u4Mxv/7hA=",
// "zc/EPwtx5OKTVdwLcI15bghjJ7suNhu59PcarXE+m9o=": "lqLD1EzZBxB8PbILUJIfFq4JI0RKThpUQuNlTNgZAWk="
// },
// "allowedTypes": {"1": {}, "2": {}}
// }
//
// Example changedStateListJSON:
//
// [
// {"pubKey": "lqLD1EzZBxB8PbILUJIfFq4JI0RKThpUQuNlTNgZAWk=", "level": 40},
// {"pubKey": "uOLitBZcCh2TEW406jXHJ+Rsi6LybsH8R1u4Mxv/7hA=", "level": 10},
// {"pubKey": "b3HygDv8gjteune9wgBm3YtVuAo2foOusRmj0m5nl6E=", "level": 10}
// ]
//
// Example deletedListJSON:
//
// [
// "lqLD1EzZBxB8PbILUJIfFq4JI0RKThpUQuNlTNgZAWk=",
// "lqLD1EzZBxB8PbILUJIfFq4JI0RKThpUQuNlTNgZAWk="
// ]
func (dmNU *dmNotificationUpdate) Callback(
nfJSON, changedStateListJSON, deletedListJSON []byte) {
dmNU.callback(utils.CopyBytesToJS(nfJSON),
utils.CopyBytesToJS(changedStateListJSON),
utils.CopyBytesToJS(deletedListJSON))
// EventUpdate implements [bindings.DmCallbacks.EventUpdate].
func (dmCBS *dmCallbacks) EventUpdate(eventType int64, jsonData []byte) {
dmCBS.eventUpdate(eventType, utils.CopyBytesToJS(jsonData))
}
// NewDMClient creates a new [DMClient] from a private identity
......@@ -156,9 +111,10 @@ func (dmNU *dmNotificationUpdate) Callback(
// - args[3] - A function that initialises and returns a Javascript object
// that matches the [bindings.EventModel] interface. The function must match
// the Build function in [bindings.EventModelBuilder].
// - args[4] - A callback that is triggered everytime there is a change to the
// notification status of a DM conversation It must be a Javascript object
// that implements the callback in [bindings.DmNotificationUpdate].
// - args[4] - A Javascript object that implements the function on
// [bindings.DmCallbacks]. It is a callback that informs the UI about
// updates relating to DM conversations. The interface may be null, but if
// one is provided, each method must be implemented.
//
// Returns:
// - Javascript representation of the [DMClient] object.
......@@ -168,10 +124,10 @@ func NewDMClient(_ js.Value, args []js.Value) any {
notificationsID := args[1].Int()
privateIdentity := utils.CopyBytesToGo(args[2])
em := newDMReceiverBuilder(args[3])
nu := newDmNotificationUpdate(args[4])
cbs := newDmCallbacks(args[4])
cm, err :=
bindings.NewDMClient(cmixID, notificationsID, privateIdentity, em, nu)
bindings.NewDMClient(cmixID, notificationsID, privateIdentity, em, cbs)
if err != nil {
exception.ThrowTrace(err)
return nil
......@@ -207,9 +163,10 @@ func NewDMClient(_ js.Value, args []js.Value) any {
// The row in the database that was updated can be found using the UUID.
// messageUpdate is true if the message already exists and was edited.
// conversationUpdate is true if the Conversation was created or modified.
// - args[6] - A callback that is triggered everytime there is a change to the
// notification status of a DM conversation It must be a Javascript object
// that implements the callback in [bindings.DmNotificationUpdate].
// - args[6] - A Javascript object that implements the function on
// [bindings.DmCallbacks]. It is a callback that informs the UI about
// updates relating to DM conversations. The interface may be null, but if
// one is provided, each method must be implemented.
//
// Returns:
// - Resolves to a Javascript representation of the [DMClient] object.
......@@ -222,7 +179,7 @@ func NewDMClientWithIndexedDb(_ js.Value, args []js.Value) any {
wasmJsPath := args[3].String()
privateIdentity := utils.CopyBytesToGo(args[4])
messageReceivedCB := args[5]
nu := newDmNotificationUpdate(args[6])
cbs := newDmCallbacks(args[6])
cipher, err := dbCipherTrackerSingleton.get(cipherID)
if err != nil {
......@@ -230,7 +187,7 @@ func NewDMClientWithIndexedDb(_ js.Value, args []js.Value) any {
}
return newDMClientWithIndexedDb(cmixID, notificationsID, wasmJsPath,
privateIdentity, messageReceivedCB, cipher, nu)
privateIdentity, messageReceivedCB, cipher, cbs)
}
// NewDMClientWithIndexedDbUnsafe creates a new [DMClient] from a private
......@@ -259,9 +216,10 @@ func NewDMClientWithIndexedDb(_ js.Value, args []js.Value) any {
// The row in the database that was updated can be found using the UUID.
// messageUpdate is true if the message already exists and was edited.
// conversationUpdate is true if the Conversation was created or modified.
// - args[5] - A callback that is triggered everytime there is a change to the
// notification status of a DM conversation It must be a Javascript object
// that implements the callback in [bindings.DmNotificationUpdate].
// - args[5] - A Javascript object that implements the function on
// [bindings.DmCallbacks]. It is a callback that informs the UI about
// updates relating to DM conversations. The interface may be null, but if
// one is provided, each method must be implemented.
//
// Returns a promise:
// - Resolves to a Javascript representation of the [DMClient] object.
......@@ -272,14 +230,14 @@ func NewDMClientWithIndexedDbUnsafe(_ js.Value, args []js.Value) any {
wasmJsPath := args[2].String()
privateIdentity := utils.CopyBytesToGo(args[3])
messageReceivedCB := args[4]
nu := newDmNotificationUpdate(args[5])
cbs := newDmCallbacks(args[5])
return newDMClientWithIndexedDb(cmixID, notificationsID, wasmJsPath,
privateIdentity, messageReceivedCB, nil, nu)
privateIdentity, messageReceivedCB, nil, cbs)
}
func newDMClientWithIndexedDb(cmixID, notificationsID int, wasmJsPath string,
privateIdentity []byte, cb js.Value, cipher *DbCipher, nuCB bindings.DmNotificationUpdate) any {
privateIdentity []byte, cb js.Value, cipher *DbCipher, cbs *dmCallbacks) any {
messageReceivedCB := func(uuid uint64, pubKey ed25519.PublicKey,
messageUpdate, conversationUpdate bool) {
......@@ -301,7 +259,7 @@ func newDMClientWithIndexedDb(cmixID, notificationsID int, wasmJsPath string,
}
cm, err := bindings.NewDMClientWithGoEventModel(
cmixID, notificationsID, privateIdentity, model, nuCB)
cmixID, notificationsID, privateIdentity, model, cbs)
if err != nil {
reject(exception.NewTrace(err))
} else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment