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.
All policies in the following examples can be build with Policy Builders, according to Policies overview.
Working with Threads
To access Threads methods, get the field threadApi
from the active endpointSession
. Session should be initialized with [.thread]
and passed to PrivmxEndpoint
.
endpointSession?.threadApi? // instance of ThreadApi
Creating Threads
Creating a basic, unnamed Thread, which can act as an encrypted data container.
let users = [privmx.endpoint.core.UserWithPubKey]() //should be prepared by developer
let managers = [privmx.endpoint.core.UserWithPubKey]() //should be prepared by developer
let publicMeta = Data()
let privateMeta = Data()
_ = try? endpointSession?.threadApi?.createThread(
in: contextId,
for: users,
managedBy: managers,
withPublicMeta: publicMeta,
withPrivateMeta: privateMeta,
withPolicies: nil
)
Getting Threads
Fetching the most recent Threads in given Context:
var startIndex:Int64 = 0
var pageSize:Int64 = 100
guard let pagingList = try? endpointSession?.threadApi?
.listThreads(
from:contextId,
basedOn: .init(skip: startIndex, limit: pageSize, sortOrder: .desc)) else {return}
let threads = pagingList.readItems.map { $0 }
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
struct ThreadPublicMeta:Codable{
let tags: [String]
}
var threadID = "THREAD_ID"
let threadPublicMeta = ThreadPublicMeta(tags: ["TAG1","TAG2","TAG3"])
guard let publicMeta = try? JSONEncoder().encode(threadPublicMeta) else {return}
guard let thread = try? endpointSession?.threadApi?
.getThread(threadID) else {return}
//users list to be extracted from thread
let users = thread.users.map{
//Your application must provide a way,
//to get user's public key from their userId.
privmx.endpoint.core.UserWithPubKey(userId: $0, pubKey: "PUB")
}
//managers list to be extracted from thread
let managers = thread.managers.map{
//Your application must provide a way,
//to get user's public key from their userId.
privmx.endpoint.core.UserWithPubKey(userId: $0, pubKey: "PUB")
}
guard let newPrivateMeta = "New thread name".data(using: .utf8) else {return}
_ = try? endpointSession?.threadApi?.updateThread(
threadID,
atVersion: thread.version,
replacingUsers: users,
replacingManagers: managers,
replacingPublicMeta: thread.publicMeta.getData() ?? Data(),
replacingPrivateMeta: newPrivateMeta,
force:false,
forceGenerateNewKey: false,
replacingPolicies: nil)