Entries
All the data sent by someone to an Inbox is called an Entry. In Threads and Stores, a user must be assigned to the container to send data. In Inboxes, however, anyone who has Inbox ID can send a reply (assuming they have the Bridge URL and Solution ID).
Info
Before working with Inboxes, 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.
Entry's publicMeta
and privateMeta
fields support any kind of data formats encoded to byte arrays. Examples in this section use Swift serialization to JSON format.
Assumptions
In further examples, the following structures are used:
struct InboxPublicEntry:Codable{
let name: String
let surname: String
let email: String
let comment: String
}
Having established public connection and inboxApi
, you can send data to the Inbox:
let inboxID = "INBOX_ID"
let inboxPublicEntry = InboxPublicEntry(
name: "name",
surname: "surname",
email: "email",
comment: "comment")
guard let inboxPublicEntryData = try? JSONEncoder().encode(inboxPublicEntry) else {return}
guard let inboxHandle = try? publicEndpointSession?.inboxApi?.prepareEntry(in: inboxID, containing: inboxPublicEntryData, attaching: [], as: nil) else {return}
try? publicEndpointSession?.inboxApi?.sendEntry(to: inboxHandle)
Getting Entries
Created entries can be listed by non-public connections created using PrivmxEndpoint
.
Fetching the most recent Entries in given Inbox
let inboxID = "INBOX_ID"
let startIndex:Int64 = 0
let pageSize:Int64 = 100
var entires = try? endpointSession?.inboxApi?.listEntries(
from: inboxID,
basedOn: privmx.endpoint.core.PagingQuery(skip: startIndex, limit: pageSize, sortOrder: .desc)
)
Reading Entries Files
Files from Entries can be read by non-public connections created using PrivmxEndpoint
class
let inboxID = "INBOX_ID"
let entryID = "ENTRY_ID"
let startIndex:Int64 = 0
let pageSize:Int64 = 100
let inboxEntry = try? endpointSession?.inboxApi?.readEntry(entryID)
var files = inboxEntry?.files
var filesContents = files.map { file in
try? endpointSession?.inboxApi?.openFile("\(file[0].info.fileId)")
}?.map{ fileHandle in
var content = Data()
var chunk : Data
repeat {
chunk = (try? endpointSession?.inboxApi?.readFromFile(withHandle: fileHandle, length: PrivMXStoreFileHandler.RecommendedChunkSize)) ?? Data()
content.append(chunk)
} while chunk.count == PrivMXStoreFileHandler.RecommendedChunkSize
return content
}