Skip to main content
swift

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

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.

Sending Entries using Public API

Public submissions require using platformConnectPublic function, provided by the Connection class, to establish a public connection to the Platform. After connecting, create an InboxApi instance, which allows to operate on Inboxes.

guard var connection = try? Connection.platformConnectPublic(to: platformURL, on: contextId) as? Connection
else {return}

guard var storeApi = try? StoreApi.create(connection: &connection) else {return}
guard var threadApi = try? ThreadApi.create(connection: &connection) else {return}
guard let inboxApi = try? InboxApi.create(connection: &connection,
threadApi: &threadApi,
storeApi: &storeApi) else {return}

Assuming you need some kind of structure in entries, define a data struct InboxPublicEntry, and its instance. Define structure of data sending to Inbox entry:

struct InboxPublicEntry:Codable{
let name: String
let surname: String
let email: String
let comment: String
}

Now, having established public connection and inboxApi, you can send data to the Inbox:

let inboxPublicEntry = InboxPublicEntry(
name: "name",
surname: "surname",
email: "email",
comment: "comment")

guard let inboxPublicEntryData = try? JSONEncoder().encode(inboxPublicEntry) else {return}

var inboxID = "INBOX_ID"

guard let inboxHandle = try? inboxApi.prepareEntry(in: inboxID, containing: inboxPublicEntryData, attaching: [], as: nil) else {return}

try? 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

var inboxID = "INBOX_ID"
var startIndex:Int64 = 0
var pageSize:Int64 = 100
guard let privMXEndpoint = self.endpointContainer.getEndpoint(connectionId) else {return}
let inboxApi = privMXEndpoint.inboxApi

var entires = try? 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

var inboxID = "INBOX_ID"
var entryID = "ENTRY_ID"
var startIndex:Int64 = 0
var pageSize:Int64 = 100
guard let privMXEndpoint = self.endpointContainer.getEndpoint(connectionId) else {return}
let inboxApi = privMXEndpoint.inboxApi

let inboxEntry = try? inboxApi?.readEntry(entryID)

var files = inboxEntry?.files
var filesContents = files.map { file in
try? inboxApi?.openFile("\(file[0].info.fileId)")
}?.map{ fileHandle in
var content = Data()
var chunk : Data
repeat {
chunk = (try? inboxApi?.readFromFile(withHandle: fileHandle, length: PrivMXStoreFileHandler.RecommendedChunkSize)) ?? Data()
content.append(chunk)
} while chunk.count == PrivMXStoreFileHandler.RecommendedChunkSize
return content
}