Managing Threads
At their core, Threads provide a secure way for assigned members to exchange encrypted messages.
- Threads allow users to communicate using topic-specific communication channels.
- Each Context can contain any number of Threads with a unique identifier (
threadId
) used to distinguish them. - Threads do not need to have unique names or assigned public keys.
Permissions
Threads differentiate two types of users - Managers and Regular Users. The table below shows the differences in their permissions:
Activity | User | Manager |
---|---|---|
Sending messages | yes | yes |
Editing Thread | no | yes |
Deleting messages | only their own | all messages |
Creating Threads
To create a Thread, you need a name and a list of public key - userID pairs. Due to the fact that each Thread is inside a Context, all the public keys have to be registered inside the given Context. You can do it using Platform REST API.
After creating a Thread, all the users with management rights will be able to edit the Thread. Skip to Modifying Threads for more info.
Below you can see some examples of creating Threads that function as a chat feature in your application, with access management capabilities similar to those found in other popular collaboration tools.
const users = [{ userId: 'MY_USER', pubKey: 'PUB_KEY' }, { userId: 'MY_USER2', pubKey: 'PUB_KEY2' }];
const managers = [{ userId: 'MY_USER', pubKey: 'PUB_KEY' }];
const threadId = await Endpoint.connection().threads.new({
contextId: 'CONTEXT_ID',
users,
managers
});
For JavaScript reference to here.
Important note: Managers declared while creating the Thread, also have to be included in the regular users list.
The user list in Threads is designed to be flexible, accommodating a wide range of use cases. Adding only two users creates a secure, encrypted one-on-one channel. Granting all users management rights results in a chat where everyone can edit the Threads.
Listing Threads
Your application may include multiple Threads, each associated with different Contexts. You can retrieve a list of all Threads within a given Context. This list will include useful metadata about the Threads, such as the creation date, last message date, user list, and information about the last modification. However, to optimize performance, the list will only include the total number of messages in each Thread, not the full message content.
Here's an example of how to download the last 30 Threads created within a Context:
import { Endpoint } from '@simplito/privmx-endpoint-web-sdk'
const PAGE_SIZE = 30;
const pageIndex = 0;
const sortOrder = 'desc';
const threadList = Endpoint.connection().threads.list({
contextId: 'CONTEXT_ID',
pageIndex,
options: {
pageSize: PAGE_SIZE,
sort: sortOrder
}
});
To limit collecting too much data when downloading Threads, specify the page index (starting from 0) and the number of items to be included on each page.
Modifying Threads
Depending on your project's specification, it may be necessary to modify a Thread. It could be e.g. changing the name or adding/removing users. Each user with management rights is able to modify Threads, delete them as a whole or particular messages.
Updating a Thread means overwriting it with the provided data.
To successfully update a Thread, you must specify its current version
.
The version
field is mandatory to handle multiple updates on the server and it is incremented by 1 with each update.
Below there is an example of modifying a Thread:
const thread = Endpoint.connection().thread('THREAD_ID');
const threadInfo = await thread.info();
const newUsers = threadInfo.users.map(user => ({
//Your application must provide a way,
//to get user's public key from their userId.
userId: user,
pubKey: 'USER_PUBLIC_KEY'
})
);
const newManagers = newUsers.filter(user =>
threadInfo.managers.find(manager => manager == user.userId));
const newPrivateMeta = {
title: 'New thread name'
};
await thread.update({
users: newUsers,
managers: newManagers,
privateMeta: serializeObject(newPrivateMeta),
version: threadInfo.version
});
Three additional options are available when changing the list of users inside a Thread:
accessToOldDataForNewUsers
- if markedtrue
, the newly added users will be able to access messages sent before they joined the Thread;force
- applies an update, without checking the current version;generateNewKey
- re-encrypts messages in the Thread. It's useful when a user is removed and we want to prevent them from accessing the Thread.