Skip to content
Closed
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@verida/client-rn": "^2.2.0",
"@verida/encryption-utils": "^2.2.0",
"@verida/types": "^2.2.0",
"@verida/vda-sbt-client": "^2.2.1",
"@verida/verifiable-credentials": "^2.2.0",
"@verida/wallet-utils": "^1.7.4",
"@walletconnect/client": "^1.7.7",
Expand Down
75 changes: 48 additions & 27 deletions src/api/DataConnectorsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Linking } from 'react-native'

import CONFIG from '../config/environment'
import AccountManager from './AccountManager'
import { SBTManager } from './SBTManager'

const DATA_CONNECTION_SCHEMA =
'https://vault.schemas.verida.io/data-connections/connection/v0.1.0/schema.json'
Expand All @@ -20,24 +21,9 @@ const delay = async (ms: number) => {
await new Promise((resolve: any) => setTimeout(() => resolve(), ms))
}

// possible states for status: syncing, disabled, active
let CONNECTION_CACHE: any

// @todo: Pull this from the server
const FacebookIcon = require('assets/social_icons/facebook.png')
const TwitterIcon = require('assets/social_icons/twitter.png')

const CONNECTIONS: any = {
facebook: {
name: 'facebook',
label: 'Facebook',
icon: FacebookIcon,
},
twitter: {
name: 'twitter',
label: 'Twitter',
icon: TwitterIcon,
},
}
// possible states for status: syncing, disabled, active

class DataConnectorsEvents extends EventEmitter {
private static instance: DataConnectorsEvents
Expand Down Expand Up @@ -85,7 +71,19 @@ export default class DataConnectorsManager {
return DataConnectorsManager.datastore
}

static getConnectionInfo(connectorName: string) {
static async getConnections(): Promise<Record<string, any>> {
if (CONNECTION_CACHE) {
return CONNECTION_CACHE
}

// @todo cache
const response = await axios.get(`${CONFIG.DATA_CONNECTOR_URL}/providers`)
CONNECTION_CACHE = response.data
return CONNECTION_CACHE
}

static async getConnectionInfo(connectorName: string) {
const CONNECTIONS = await DataConnectorsManager.getConnections()
return CONNECTIONS[connectorName]
}

Expand All @@ -94,15 +92,25 @@ export default class DataConnectorsManager {
return DataConnectorsManager._connections[connectorName]
}

const connector = new DataConnection(connectorName)
const connectionInfo = await DataConnectorsManager.getConnectionInfo(
connectorName
)

const connector = new DataConnection(
connectorName,
connectionInfo.icon,
connectionInfo.label
)
await connector.init()

DataConnectorsManager._connections[connectorName] = connector
return connector
}

static async getConnectors(): Promise<any> {
const connections: any = Object.values(CONNECTIONS)
const connections: any = Object.values(
await DataConnectorsManager.getConnections()
)
const connectors: any = {}
for (let i = 0; i < connections.length; i++) {
const connection = await DataConnectorsManager.getConnection(
Expand All @@ -115,7 +123,9 @@ export default class DataConnectorsManager {
}

static async resetConnector() {
const connections: any = Object.values(CONNECTIONS)
const connections: any = Object.values(
await DataConnectorsManager.getConnections()
)
for (let i = 0; i < connections.length; i++) {
if (
DataConnectorsManager._connections[connections[i].name].syncStatus !==
Expand Down Expand Up @@ -174,12 +184,12 @@ class DataConnection extends EventEmitter {
public metadata?: any
public icon?: any

constructor(name: string) {
constructor(name: string, icon: string, label: string) {
super()
this.name = this.source = name
this.syncStatus = 'disabled'
this.icon = CONNECTIONS[this.name].icon
this.label = CONNECTIONS[this.name].label
this.icon = icon
this.label = label
this.syncFrequency = 'hour'
}

Expand Down Expand Up @@ -369,9 +379,20 @@ class DataConnection extends EventEmitter {
const syncRequest = await externalDatastore.get(syncRequestId)

if (syncRequest.status === 'complete') {
// Sync has completed on the server, so complete the sync
// by replicating data from the server
this.syncReplication(serverDid, contextName, syncRequest)
// Sync has completed on the server

// Save a SBT credential if it was provided
if (syncRequest.syncInfo.profile.credential) {
const sbtManager = new SBTManager()
const profileCredentialId = `${syncRequest.source}-${syncRequest.syncInfo.profile.id}-profile`
await sbtManager.saveCredential(
profileCredentialId,
syncRequest.syncInfo.profile.credential
)
}

// Complete the sync by replicating data from the server
await this.syncReplication(serverDid, contextName, syncRequest)
} else {
if (retryCount === 0) {
// Retry count limit hit
Expand Down
Loading