Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package Tools.Inboxes.UsingInboxes

import com.simplito.kotlin.privmx_endpoint.model.File
import com.simplito.kotlin.privmx_endpoint.model.InboxEntry
import com.simplito.kotlin.privmx_endpoint_extra.inboxFileStream.InboxFileStreamWriter
import com.simplito.kotlin.privmx_endpoint_extra.model.SortOrder
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive

fun sendingInboxEntryBasic() {
// 1. Preparing Entry
val inboxID = "INBOX_ID"

val entryDataAnswer = "USER_PROVIDED_TEXT"
val entryDataVersion = 1L
val entryDataType = "TEXT_ANSWER"

val entryData: ByteArray = """
{
"content": {
"answer": "$entryDataAnswer"
},
"version": $entryDataVersion,
"type": "$entryDataType"
}

""".trimIndent().encodeToByteArray()

val entryHandle = endpointSession.inboxApi?.prepareEntry(
inboxID,
entryData,
emptyList()
)!!

// 2. Sending Entry
endpointSession.inboxApi?.sendEntry(entryHandle)
}

fun attachingFiles(fileID: String?) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this snippet is not the same as in docs

// 1. Preparing Entry & sending File Contents
val inboxID = "INBOX_ID"
val publicMeta: ByteArray = "My public data".encodeToByteArray()

val fileName = "FILE_NAME"
val fileType = "FILE_TYPE"
val fileContent: ByteArray = "FILE_CONTENT".encodeToByteArray()
val fileSize = fileContent.size.toLong()

val entryDataAnswer = "USER_PROVIDED_TEXT"
val entryDataVersion = 1L
val entryDataType = "TEXT_ANSWER"

val entryData: ByteArray = """
{
"content": {
"answer": "$entryDataAnswer"
},
"version": $entryDataVersion,
"type": "$entryDataType"
}

""".trimIndent().encodeToByteArray()

val filePrivateMeta: ByteArray = """
{
"name": "$fileName",
"mimetype": "$fileType"
}

""".trimIndent().encodeToByteArray()

val inboxFileStreamWriter: InboxFileStreamWriter = InboxFileStreamWriter.createFile(
endpointSession.inboxApi!!,
publicMeta,
filePrivateMeta,
fileSize
)

val fileHandle = inboxFileStreamWriter.fileHandle
val inboxHandle = endpointSession.inboxApi?.prepareEntry(
inboxID,
entryData,
listOf(fileHandle)
)!!

// 2. Sending File Contents
inboxFileStreamWriter.write(
inboxHandle,
fileContent
)

// 3. Sending Entry
endpointSession.inboxApi?.sendEntry(inboxHandle)
}

fun fetchingEntriesBasic() {
val inboxID = "INBOX_ID"
val startIndex = 0L
val pageSize = 100L

val entriesPagingList = endpointSession.inboxApi?.listEntries(
inboxID,
startIndex,
pageSize,
SortOrder.DESC
)
}

fun fetchingEntriesWithFiles() {
val inboxID = "INBOX_ID"
val startIndex = 0L
val pageSize = 1L

// getting last entry
val entriesPagingList = endpointSession.inboxApi?.listEntries(
inboxID,
startIndex,
pageSize,
SortOrder.DESC
)!!

val entry: InboxEntry = entriesPagingList.readItems[0]
val entryFile: File = entry.files[0]

// decoded privateMeta
val privateMeta = entryFile.privateMeta.decodeToString()
val privateMetaJson = Json.parseToJsonElement(privateMeta).jsonObject
val fileName = privateMetaJson["name"]?.jsonPrimitive?.content
val fileType = privateMetaJson["mimetype"]?.jsonPrimitive?.content

// decoded data
val entryData = entry.data.decodeToString()
val entryDataJson = Json.parseToJsonElement(entryData).jsonObject
val answer = entryDataJson["content"]!!
.jsonObject["answer"]!!
.jsonPrimitive.content
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package Tools.Inboxes.UsingInboxes

import com.simplito.kotlin.privmx_endpoint.model.events.eventSelectorTypes.InboxEventSelectorType
import com.simplito.kotlin.privmx_endpoint_extra.events.CallbackRegistration
import com.simplito.kotlin.privmx_endpoint_extra.events.EventType

suspend fun handlingInboxEvents() {
val inboxCallbacksGroup = "INBOX_CALLBACKS_GROUP"
val entryCallbacksGroup = "ENTRY_CALLBACKS_GROUP"
val inboxID = "INBOX_ID"

// Starting the Event Loop
endpointContainer.startListening()

endpointSession.registerManyCallbacks(

// Handling Inbox Events
CallbackRegistration(
inboxCallbacksGroup,
EventType.InboxUpdatedEvent(
InboxEventSelectorType.CONTEXT_ID,
contextId
)
) { updatedInbox ->
println(updatedInbox.lastModifier)
},

// Handling Inbox Entry Events
CallbackRegistration(
entryCallbacksGroup,
EventType.InboxEntryCreatedEvent(
InboxEventSelectorType.INBOX_ID,
inboxID
)
) { newEntry ->
println(newEntry.inboxId)
}
)

endpointSession.unregisterCallbacks(inboxCallbacksGroup, entryCallbacksGroup)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package Tools.Inboxes.UsingInboxes

import com.simplito.kotlin.privmx_endpoint.model.Inbox
import com.simplito.kotlin.privmx_endpoint.model.InboxPublicView
import com.simplito.kotlin.privmx_endpoint.model.UserWithPubKey
import com.simplito.kotlin.privmx_endpoint_extra.lib.PrivmxEndpoint
import com.simplito.kotlin.privmx_endpoint_extra.lib.PrivmxEndpointContainer
import com.simplito.kotlin.privmx_endpoint_extra.model.Modules
import com.simplito.kotlin.privmx_endpoint_extra.model.SortOrder

// START: Initial Assumptions Snippets
/*
All the values below like BRIDGE_URL, SOLUTION_ID, CONTEXT_ID
should be replaced by the ones corresponding to your Bridge Server instance.

The private keys here are for demonstration purposes only.
Normally, they should be kept separately by each user and stored in a safe place,
or generated from a password (see the derivePrivateKey2() method in the Crypto API)
*/
var bridgeUrl: String = "YOUR_BRIDGE_URL"
var solutionId: String = "YOUR_SOLUTION_ID"
var contextId: String = "YOUR_CONTEXT_ID"

var user1Id: String = "USER_ID_1"
var user1PublicKey: String = "PUBLIC_KEY_1"
var user1PrivateKey: String = "PRIVATE_KEY_1"

var user2Id: String = "USER_ID_2"
var user2PublicKey: String = "PUBLIC_KEY_2"

var user3Id: String = "USER_ID_3"
var user3PublicKey: String = "PUBLIC_KEY_3"

var endpointContainer: PrivmxEndpointContainer = PrivmxEndpointContainer()

var initModules = setOf(Modules.INBOX)
var endpointSession: PrivmxEndpoint = endpointContainer.connect(
initModules,
user1PrivateKey,
solutionId,
bridgeUrl
)
// END: Initial Assumptions Snippets

fun creatingInboxes() {
val privateMeta: ByteArray = "My private data".encodeToByteArray()
val publicMeta: ByteArray = "My public data".encodeToByteArray()
val users: List<UserWithPubKey> = listOf(
UserWithPubKey(user1Id, user1PublicKey),
UserWithPubKey(user2Id, user2PublicKey)
)
val managers: List<UserWithPubKey> = listOf(
UserWithPubKey(user1Id, user1PublicKey)
)

val inboxID = endpointSession.inboxApi?.createInbox(
contextId,
users,
managers,
publicMeta,
privateMeta
)
}

fun listingInboxes() {
val limit = 30L
val skip = 0L

val inboxes: List<Inbox> = endpointSession.inboxApi!!.listInboxes(
contextId,
skip,
limit,
SortOrder.DESC
).readItems
}

fun modifyingInboxes() {
val inboxID = "INBOX_ID"
val newUsers: List<UserWithPubKey> = listOf(
UserWithPubKey(user1Id, user1PublicKey),
UserWithPubKey(user2Id, user2PublicKey),
UserWithPubKey(user3Id, user3PublicKey)
)
val newManagers: List<UserWithPubKey> = listOf(
UserWithPubKey(user1Id, user1PublicKey),
UserWithPubKey(user2Id, user2PublicKey)
)
val newPrivateMeta: ByteArray = "New inbox name".encodeToByteArray()

endpointSession.inboxApi?.let { inboxApi ->
val inbox = inboxApi.getInbox(inboxID)

inboxApi.updateInbox(
inboxID,
newUsers,
newManagers,
inbox.publicMeta,
newPrivateMeta,
filesConfig = null,
version = inbox.version!!,
force = false,
forceGenerateNewKey = false,
policies = inbox.policy
)
}
}

fun deletingInboxes() {
val inboxID = "INBOX_ID"
endpointSession.inboxApi!!.deleteInbox(inboxID)
}

fun usingPublicView() {
// Retrieve and print the public view of the Inbox
val inboxID = "INBOX_ID"

val inboxPublicView: InboxPublicView = endpointSession.inboxApi!!.getInboxPublicView(inboxID)
val publicMeta: String = inboxPublicView.publicMeta.decodeToString()
println(publicMeta)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package Tools.Kvdbs.UsingKvdbs

import com.simplito.kotlin.privmx_endpoint.model.events.eventSelectorTypes.KvdbEventSelectorType
import com.simplito.kotlin.privmx_endpoint_extra.events.CallbackRegistration
import com.simplito.kotlin.privmx_endpoint_extra.events.EventType

suspend fun handlingKvdbEvents() {
val kvdbCallbacksGroup = "KVDB_CALLBACKS_GROUP"
val entryCallbacksGroup = "ENTRY_CALLBACKS_GROUP"
val kvdbID = "KVDB_ID"

// Starting the Event Loop
endpointContainer.startListening()

endpointSession.registerManyCallbacks(

// Handling KVDB Events
CallbackRegistration(
kvdbCallbacksGroup,
EventType.KvdbStatsChangedEvent(
KvdbEventSelectorType.CONTEXT_ID,
contextId
)
) { kvdbStats ->
println(kvdbStats.lastEntryDate)
},

// Handling KVDB Entry Events
CallbackRegistration(
entryCallbacksGroup,
EventType.KvdbNewEntryEvent(
KvdbEventSelectorType.KVDB_ID,
kvdbID
)
) { newEntry ->
println(newEntry.info.key)
}
)

endpointSession.unregisterCallbacks(kvdbCallbacksGroup, entryCallbacksGroup)
}
Loading