diff --git a/wasm/channels.go b/wasm/channels.go index f30fce121d955f2eed21b6598deb46f0bfcbe1e2..09d3b1b9e578879c1c5e8f47ab0df2f1cadc79f5 100644 --- a/wasm/channels.go +++ b/wasm/channels.go @@ -59,6 +59,7 @@ func newChannelsManagerJS(api *bindings.ChannelsManager) map[string]any { "SendReply": js.FuncOf(cm.SendReply), "SendReaction": js.FuncOf(cm.SendReaction), "SendSilent": js.FuncOf(cm.SendSilent), + "SendInvite": js.FuncOf(cm.SendInvite), "DeleteMessage": js.FuncOf(cm.DeleteMessage), "PinMessage": js.FuncOf(cm.PinMessage), "MuteUser": js.FuncOf(cm.MuteUser), @@ -622,6 +623,32 @@ func DecodePrivateURL(_ js.Value, args []js.Value) any { return c } +// DecodeInviteURL decodes the channel URL, using the password, into a channel +// pretty print. This function can only be used for URLs from invitations. +// +// Parameters: +// - args[0] - The channel's share URL (string). Should be received from +// another user via invitation. +// - args[1] - The password needed to decrypt the secret data in the URL +// (string). +// +// Returns: +// - The channel pretty print (string) +func DecodeInviteURL(_ js.Value, args []js.Value) any { + var ( + url = args[0].String() + password = args[1].String() + ) + + c, err := bindings.DecodeInviteURL(url, password) + if err != nil { + exception.ThrowTrace(err) + return nil + } + + return c +} + // GetChannelJSON returns the JSON of the channel for the given pretty print. // // Parameters: @@ -1232,6 +1259,64 @@ func (cm *ChannelsManager) SendSilent(_ js.Value, args []js.Value) any { return utils.CreatePromise(promiseFn) } +// SendInvite is used to send to a channel (invited) an invitation to another +// channel (invitee). +// +// If the channel ID for the invitee channel is not recognized by the Manager, +// then an error will be returned. +// +// Parameters: +// - args[0] - Marshalled bytes of the invited channel [id.ID] (Uint8Array). +// - args[1] - JSON of the invitee channel [id.ID]. +// This can be retrieved from [GetChannelJSON]. (Uint8Array). +// - args[2] - The contents of the message (string). +// - args[3] - The URL to append the channel info to (string). +// - args[4] - The lease of the message. This will be how long the +// message is available from the network, in milliseconds (int). As per the +// [channels.Manager] documentation, this has different meanings depending +// on the use case. These use cases may be generic enough that they will not +// be enumerated here. Use [ValidForever] to last the max message life. +// - args[5] - JSON of [xxdk.CMIXParams]. If left empty +// [bindings.GetDefaultCMixParams] will be used internally (Uint8Array). +// - args[6] - JSON of a slice of public keys of users that should receive +// mobile notifications for the message. +// +// Example slice of public keys: +// +// [ +// "FgJMvgSsY4rrKkS/jSe+vFOJOs5qSSyOUSW7UtF9/KU=", +// "fPqcHtrJ398PAC35QyWXEU9PHzz8Z4BKQTCxSvpSygw=", +// "JnjCgh7g/+hNiI9VPKW01aRSxGOFmNulNCymy3ImXAo=" +// ] +// +// Returns a promise: +// - Resolves to the JSON of [bindings.ChannelSendReport] (Uint8Array). +// - Rejected with an error if sending fails. +func (cm *ChannelsManager) SendInvite(_ js.Value, args []js.Value) any { + var ( + marshalledChanId = utils.CopyBytesToGo(args[0]) + inviteToJSON = utils.CopyBytesToGo(args[1]) + msg = args[2].String() + host = args[3].String() + leaseTimeMS = int64(args[4].Int()) + cmixParamsJSON = utils.CopyBytesToGo(args[5]) + pingsJSON = utils.CopyBytesToGo(args[6]) + ) + + promiseFn := func(resolve, reject func(args ...any) js.Value) { + sendReport, err := cm.api.SendInvite(marshalledChanId, + inviteToJSON, msg, host, leaseTimeMS, + cmixParamsJSON, pingsJSON) + if err != nil { + reject(exception.NewTrace(err)) + } else { + resolve(utils.CopyBytesToJS(sendReport)) + } + } + + return utils.CreatePromise(promiseFn) +} + //////////////////////////////////////////////////////////////////////////////// // Admin Sending // //////////////////////////////////////////////////////////////////////////////// diff --git a/wasm/dm.go b/wasm/dm.go index 0cfa1af849141152374a70b46ac59432a311215a..92f47125aa1dddd9ecc4f6258e12751277108f1a 100644 --- a/wasm/dm.go +++ b/wasm/dm.go @@ -63,6 +63,7 @@ func newDMClientJS(api *bindings.DMClient) map[string]any { "SendText": js.FuncOf(cm.SendText), "SendReply": js.FuncOf(cm.SendReply), "SendReaction": js.FuncOf(cm.SendReaction), + "SendInvite": js.FuncOf(cm.SendInvite), "SendSilent": js.FuncOf(cm.SendSilent), "Send": js.FuncOf(cm.Send), @@ -553,7 +554,8 @@ func (dmc *DMClient) SendReply(_ js.Value, args []js.Value) any { // Users will drop the reaction if they do not recognize the reactTo message. // // Parameters: -// - args[0] - Marshalled bytes of the channel [id.ID] (Uint8Array). +// - args[0] - The bytes of the public key of the partner's ED25519 signing +// key (Uint8Array). // - args[1] - The token used to derive the reception ID for the partner (int). // - args[2] - The user's reaction. This should be a single emoji with no // other characters. As such, a Unicode string is expected (string). @@ -630,6 +632,52 @@ func (dmc *DMClient) SendSilent(_ js.Value, args []js.Value) any { return utils.CreatePromise(promiseFn) } +// SendInvite is used to send to a DM partner an invitation to another +// channel. +// +// If the channel ID for the invitee channel is not recognized by the Manager, +// then an error will be returned. +// +// Parameters: +// - args[0] - The bytes of the public key of the partner's ED25519 signing +// key (Uint8Array). +// - args[1] - The token used to derive the reception ID for the partner (int). +// - args[2] - JSON of the invitee channel [id.ID]. +// This can be retrieved from [GetChannelJSON]. (Uint8Array). +// - args[3] - The contents of the message. The message should be at most 510 +// bytes. This is expected to be Unicode, and thus a string data type is +// expected. +// - args[4] - The URL to append the channel info to. +// - args[5] - A JSON marshalled [xxdk.CMIXParams]. This may be empty, +// and GetDefaultCMixParams will be used internally. +// +// Returns a promise: +// - Resolves to the JSON of [bindings.ChannelSendReport] (Uint8Array). +// - Rejected with an error if sending fails. +func (dmc *DMClient) SendInvite(_ js.Value, args []js.Value) any { + var ( + partnerPubKeyBytes = utils.CopyBytesToGo(args[0]) + partnerToken = int32(args[1].Int()) + inviteToJSON = utils.CopyBytesToGo(args[2]) + msg = args[3].String() + host = args[4].String() + cmixParamsJSON = utils.CopyBytesToGo(args[5]) + ) + + promiseFn := func(resolve, reject func(args ...any) js.Value) { + sendReport, err := dmc.api.SendInvite( + partnerPubKeyBytes, partnerToken, inviteToJSON, msg, host, + cmixParamsJSON) + if err != nil { + reject(exception.NewTrace(err)) + } else { + resolve(utils.CopyBytesToJS(sendReport)) + } + } + + return utils.CreatePromise(promiseFn) +} + // Send is used to send a raw message. In general, it // should be wrapped in a function that defines the wire protocol. //