- 
                Notifications
    You must be signed in to change notification settings 
- Fork 0
[DOCS]: Update start page snippets to 2.6 #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            Doominika
  wants to merge
  8
  commits into
  feat/implement-2.6
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
feat/update-start-page-snippets-to-2.6
  
      
      
   
  
    
  
  
  
 
  
      
    base: feat/implement-2.6
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            8 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      ece43b9
              
                chore: add snippets for inboxes
              
              
                Doominika eb3f8e1
              
                chore: add snippets for kvdbs
              
              
                Doominika ab77c45
              
                chore: updat snippets for stores
              
              
                Doominika 7bd35bb
              
                chore: update snippets for threads
              
              
                Doominika a5e392c
              
                chore: set all possible params when updating containers and name them
              
              
                Doominika 7013287
              
                chore: remove unnecessary comment when unregistering callback in snippet
              
              
                Doominika 6670cd8
              
                chore: reformat files, remove classes
              
              
                Doominika a0c1490
              
                chore: rename variable
              
              
                Doominika File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
          
            138 changes: 138 additions & 0 deletions
          
          138 
        
  examples/snippets/src/commonMain/kotlin/Tools/Inboxes/UsingInboxes/InboxEntries.kt
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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?) { | ||
| // 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 | ||
| } | ||
        
          
          
            41 changes: 41 additions & 0 deletions
          
          41 
        
  examples/snippets/src/commonMain/kotlin/Tools/Inboxes/UsingInboxes/InboxUpdates.kt
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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) | ||
| } | 
        
          
          
            120 changes: 120 additions & 0 deletions
          
          120 
        
  examples/snippets/src/commonMain/kotlin/Tools/Inboxes/UsingInboxes/WorkingWithInboxes.kt
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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) | ||
| } | 
        
          
          
            41 changes: 41 additions & 0 deletions
          
          41 
        
  examples/snippets/src/commonMain/kotlin/Tools/Kvdbs/UsingKvdbs/ListeningForChanges.kt
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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) | ||
| } | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
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