diff --git a/indexedDb/init.go b/indexedDb/init.go index 694e073e46389fd0a486529790309950d21201ef..7421e888304b178593e6f7a80913d7ffabe55836 100644 --- a/indexedDb/init.go +++ b/indexedDb/init.go @@ -17,16 +17,19 @@ import ( "syscall/js" "gitlab.com/elixxir/client/channels" - "gitlab.com/xx_network/primitives/id" ) -// currentVersion of the IndexDb runtime. Used for migration purposes. -const currentVersion uint = 1 +const ( + // databaseSuffix to be appended to the name of the database + databaseSuffix = "_messenger" + // currentVersion of the IndexDb runtime. Used for migration purposes. + currentVersion uint = 1 +) // NewWasmEventModel returns a [channels.EventModel] backed by a wasmModel -func NewWasmEventModel(receptionId *id.ID) (channels.EventModel, error) { +func NewWasmEventModel(username string) (channels.EventModel, error) { ctx := context.Background() - databaseName := receptionId.String() + databaseName := username + databaseSuffix // Attempt to open database object openRequest, _ := idb.Global().Open(ctx, databaseName, currentVersion, @@ -47,6 +50,7 @@ func NewWasmEventModel(receptionId *id.ID) (channels.EventModel, error) { return errors.Errorf("Invalid version upgrade path: v%d -> v%d", oldVersion, newVersion) }) + // Wait for database open to finish db, err := openRequest.Await(ctx) diff --git a/indexedDb/model.go b/indexedDb/model.go new file mode 100644 index 0000000000000000000000000000000000000000..fbed220ff14e548f197712e8949f220f7084dfc3 --- /dev/null +++ b/indexedDb/model.go @@ -0,0 +1,65 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2022 Privategrity Corporation / +// / +// All rights reserved. / +//////////////////////////////////////////////////////////////////////////////// + +//go:build js && wasm +// +build js,wasm + +package indexedDb + +import ( + "time" +) + +const ( + // Text representation of primary key value (keyPath). + pkeyName = "id" + + // Text representation of the names of the various [idb.ObjectStore]. + messageStoreName = "messages" + userStoreName = "users" + channelsStoreName = "channels" + + // Message index names. + messageStoreChannelIndex = "channel_id_index" + messageStoreParentIndex = "parent_message_id_index" + messageStoreTimestampIndex = "timestamp_index" + messageStorePinnedIndex = "pinned_index" + + // Message keyPath names (must match json struct tags). + messageStoreChannel = "channel_id" + messageStoreParent = "parent_message_id" + messageStoreTimestamp = "timestamp" + messageStorePinned = "pinned" +) + +// Message defines the IndexedDb representation of a single Message. +// A Message belongs to one User (Sender). +// A Message belongs to one Channel. +// A Message may belong to one Message (Parent). +type Message struct { + Id []byte `json:"id"` // Matches pkeyName + SenderId []byte `json:"sender_id"` + ChannelId []byte `json:"channel_id"` // Index + ParentMessageId []byte `json:"parent_message_id"` // Index + Timestamp time.Time `json:"timestamp"` // Index + Hidden bool `json:"hidden"` + Pinned bool `json:"pinned"` // Index +} + +// User defines the IndexedDb representation of a single User. +// A User sends many Message. +type User struct { + Id []byte `json:"id"` // Matches pkeyName + Username string `json:"username"` +} + +// Channel defines the IndexedDb representation of a single Channel +// A Channel has many Message. +type Channel struct { + Id []byte `json:"id"` // Matches pkeyName + Name string `json:"name"` + Description string `json:"description"` +}