Overview
Threads are a secure way for assigned members to exchange encrypted messages.
Info
Before working with Threads, follow our Getting Started Guide. It will show you how to set up your project to work with PrivMX Bridge.
The sample code on this page is based on the initial assumptions.
Info
Thread publicMeta
and privateMeta
fields support any kind of data formats encoded to byte arrays.
Examples in this section use kotlinx-serialization-json-jvm
dependency for Kotlin and com.google.code.gson:gson
for Java to serialize object to JSON format.
Working with Threads
To access Threads methods, get the field threadApi
from active connection. Connection should be initialized with Modules.THREAD
passed to PrivmxEndpoint()
.
val threadApi: ThreadApi = endpointSession.threadApi
Creating Threads
Creating a basic, unnamed Thread, which can act as an encrypted data container.
val users : List<UserWithPubKey> = listOf(
UserWithPubKey(user1Id, user1PublicKey),
UserWithPubKey(user2Id, user2PublicKey)
)
val managers : List<UserWithPubKey> = listOf(
UserWithPubKey(user1Id, user1PublicKey)
)
val publicMeta = ByteArray(0)
val privateMeta = ByteArray(0)
val threadId = threadApi.createThread(
contextId,
users,
managers,
publicMeta,
privateMeta
)
Getting Threads
Define Thread item class with decoded publicMeta
and privateMeta
:
data class ThreadItem(
val thread: com.simplito.java.privmx_endpoint.model.Thread,
val decodedPrivateMeta: String,
val decodedPublicMeta: ThreadPublicMeta
)
Fetching the most recent Threads in given Context:
val startIndex = 0L
val pageSize = 100L
val threadsPagingList = threadApi.listThreads(
contextId,
startIndex,
pageSize,
SortOrder.DESC
)
val threads = threadsPagingList.readItems.map {
ThreadItem(
it,
it.privateMeta.decodeToString(),
Json.decodeFromString(it.publicMeta.decodeToString())
)
}
Managing Threads
To update a Thread you must always provide its current version, as well as:
- list of users
- list of managers
- new private and public meta (even if it didn't change)
- Thread's current version
true
if update action should be forced
val threadID = "THREAD_ID"
val thread: Thread = threadApi.getThread(threadID)
val users = thread
.users
.map { userId ->
//Your application must provide a way,
//to get user's public key from their userId.
UserWithPubKey(
userId,
"USER_PUBLIC_KEY"
)
}
val managers = thread
.managers
.map { userId ->
//Your application must provide a way,
//to get user's public key from their userId.
UserWithPubKey(
userId,
"USER_PUBLIC_KEY"
)
}
val newThreadNameAsPrivateMeta = "New thread name"
threadApi.updateThread(
thread.threadId,
users,
managers,
thread.publicMeta,
newThreadNameAsPrivateMeta.encodeToByteArray(),
thread.version,
false
)