Radiks - How to do private group messaging using UserGroups


#1

@hank ,

What is the syntax to implement a more advanced use case with Radiks for a private group messaging app? I want the messages encrypted and only available to the users in the group. Not sure if I missed this in the documentation but I was searching through Lettermesh and this was the closest example I could find:

Not sure if the Message model is supposed to extend Model or UserGroup. Also not sure how to associate a new message to a group.

My best first guess is a model that looks somethin like this???

import { Model, UserGroup } from 'radiks';

export default class PrivateGroupMessage extends UserGroup {
    static className = 'PrivateGroupMessage';
    static schema = {
      ...UserGroup.schema,
      content: {
        type: String
      },
      createdBy: {
        type: String,
      }
  };
}

Also how do I add a private message for the user in the group after he has accepted his invitation?


#2

Hey good question, and seems like I could clarify these docs a bit.

You can extend UserGroup to make a model like MessageGroup, and you only need to do this if you need to add some attributes to the group, like description. It already has a name attribute.

Then you have a Message model that has a userGroupId attribute (unencrypted), and that actually has fields like content (encrypted). As long as you attach the right userGroupId to the message, it’ll be encrypted for everyone in the group.


#3

Awesome thanks! Do you have syntax or code samples of this?


#4

Not right now, unfortunately. I have been working on a new Kanstack with Radiks but it needs some polishing before releasing. It does follow this model, though. Your code might look like this:

export default class PrivateGroupMessage extends UserGroup {
    static schema = {
      ...UserGroup.schema,
      description: {
        type: String
      },
  };
}

export default class Message extends Model {
    static className = 'Message';
    static schema = {
      content: {
        type: String
      },
     userGroupId: {
       type: String,
       decrypted: true,
     }
      createdBy: {
        type: String,
      }
  };
}

const message = new Message({ content: 'asdf', userGroupId: 'asdf' });
await message.save(); 
// the message will be encrypted using this user group's private key,
// and anyone in the group will be able to read it.

#5

Thanks @hank works like a charm! I really appreciate the ease of use this framework provides. I almost went down the path of rolling my own Group Key signing system but this solution is sooo elegant!

Here is the code to query all the messages in that group:

const messages = await Message.fetchList({
  userGroupId: 'b1b478cf842d-4c6b-bf60-ec0f75aa5dac'
}, {decrypt: true});

It creates this network request:

https:/radiks-url.com/radiks/models/find?userGroupId=b1b478cf842d-4c6b-bf60-ec0f75aa5dac&radiksType=Message

P.S I’m soo stoked to have this working! Gunna take my Dapp to the next level! :partying_face::partying_face::partying_face:


#6

yay, glad its working!!

P.S.: fetchList has { decrypt: true } by default for the second argument, so you don’t have to actually pass it. You only need to pass that option if you want decrypt: false.