Skip to content

Commit

Permalink
Merge pull request #467 from jorgeblacio/multiple_websocket
Browse files Browse the repository at this point in the history
Now the web socket works with multiple accounts.
  • Loading branch information
danieltigse authored Apr 29, 2019
2 parents bc545b9 + 663f465 commit 9d2657b
Show file tree
Hide file tree
Showing 48 changed files with 327 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class UpdateMailboxWorkerTest {

private fun newWorker(loadedThreadsCount: Int, label: Label): UpdateMailboxWorker =
UpdateMailboxWorker(label = label,
activeAccount = activeAccount, loadedThreadsCount = loadedThreadsCount,
recipientId = activeAccount.recipientId, loadedThreadsCount = loadedThreadsCount,
publishFn = {}, httpClient = httpClient, dbEvents = eventDB, storage = storage,
pendingEventDao = db.pendingEventDao(), accountDao = db.accountDao(), isActiveAccount = true,
db = db)
Expand Down
6 changes: 5 additions & 1 deletion src/main/kotlin/com/criptext/mail/CriptextApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ class CriptextApplication : MultiDexApplication() {
lockManager.enableAppLock(this, LockScreenActivity::class.java)
configureAppLock(lockManager)
}
WebSocketSingleton.getInstance(activeAccount)
val jwts = storage.getString(KeyValueStorage.StringKey.JWTS, "")
if(jwts.isNotEmpty())
WebSocketSingleton.getInstance(jwts)
else
WebSocketSingleton.getInstance(activeAccount.jwt)
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/com/criptext/mail/api/models/DeviceInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import com.criptext.mail.utils.DeviceUtils
import org.json.JSONObject

sealed class DeviceInfo{
data class TrustedDeviceInfo (val deviceId: Int, val deviceFriendlyName: String,
data class TrustedDeviceInfo (val recipientId: String, val deviceId: Int, val deviceFriendlyName: String,
val deviceType: DeviceUtils.DeviceType, val randomId: String, val syncFileVersion: Int): DeviceInfo()
{
companion object {

fun fromJSON(jsonString: String): TrustedDeviceInfo {
fun fromJSON(jsonString: String, recipientId: String): TrustedDeviceInfo {
val json = JSONObject(jsonString).getJSONObject("requestingDeviceInfo")
return TrustedDeviceInfo(
recipientId = recipientId,
randomId = JSONObject(jsonString).getString("randomId"),
deviceId = json.getInt("deviceId"),
deviceFriendlyName = json.getString("deviceFriendlyName"),
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/criptext/mail/api/models/Event.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.json.JSONObject
/**
* Created by gabriel on 4/26/18.
*/
data class Event(val rowid: Long, val cmd: Int, val params: String) {
data class Event(val rowid: Long, val recipientId: String, val cmd: Int, val params: String) {
companion object {
fun fromJSON(jsonEventString: String): Event {
val json = JSONObject(jsonEventString)
Expand All @@ -14,6 +14,7 @@ data class Event(val rowid: Long, val cmd: Int, val params: String) {
else
-1
return Event(rowid = rowId,
recipientId = if(json.has("recipientId")) json.getString("recipientId") else "",
cmd = json.getInt("cmd"),
params = json.getString("params"))
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/criptext/mail/db/KeyValueStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ interface KeyValueStorage {
HasTimestampForCacheReset("HasTimestampForCacheReset"),
CacheResetTimestamp("CacheResetTimestamp"),
UseWifiOnlyForBackup("UseWifiOnlyForBackup"),
SavedBackupData("SavedBackupData")
SavedBackupData("SavedBackupData"),
JWTS("JWTS")
}

class SharedPrefs(private val ctx: Context) : KeyValueStorage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,18 @@ class EmailDetailActivity: BaseActivity() {

val emailDetailSceneView = EmailDetailScene.EmailDetailSceneView(
findViewById(R.id.include_emails_detail), this)
val storage = KeyValueStorage.SharedPrefs(this)


val webSocketEvents = WebSocketSingleton.getInstance(
activeAccount = activeAccount)
val jwts = storage.getString(KeyValueStorage.StringKey.JWTS, "")
val webSocketEvents = if(jwts.isNotEmpty())
WebSocketSingleton.getInstance(jwts)
else
WebSocketSingleton.getInstance(activeAccount.jwt)
val downloadDir = AndroidFs.getDownloadsCacheDir(this).absolutePath
val remoteChangeDataSource = GeneralDataSource(
signalClient = signalClient,
eventLocalDB = EventLocalDB(appDB, this.filesDir, this.cacheDir),
storage = KeyValueStorage.SharedPrefs(this),
storage = storage,
db = appDB,
runner = AsyncTaskWorkRunner(),
activeAccount = activeAccount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,16 +230,24 @@ class EmailDetailSceneController(private val storage: KeyValueStorage,
private fun onMailboxUpdate(result: GeneralResult.UpdateMailbox){
when (result) {
is GeneralResult.UpdateMailbox.Success -> {
dataSource.submitRequest(EmailDetailRequest.LoadFullEmailsFromThreadId(
model.threadId, model.currentLabel, null))
if(result.isActiveAccount) {
dataSource.submitRequest(EmailDetailRequest.LoadFullEmailsFromThreadId(
model.threadId, model.currentLabel, null))
}
}
is GeneralResult.UpdateMailbox.SuccessAndRepeat -> {
generalDataSource.submitRequest(GeneralRequest.UpdateMailbox(true, null, model.currentLabel, 1))
generalDataSource.submitRequest(GeneralRequest.UpdateMailbox(
result.isActiveAccount, result.recipientId, model.currentLabel, 1
))
}
is GeneralResult.UpdateMailbox.Unauthorized -> {
if (result.isActiveAccount)
generalDataSource.submitRequest(GeneralRequest.DeviceRemoved(false))
}
is GeneralResult.UpdateMailbox.Forbidden -> {
if (result.isActiveAccount)
scene.showConfirmPasswordDialog(emailDetailUIObserver)
}
is GeneralResult.UpdateMailbox.Unauthorized ->
generalDataSource.submitRequest(GeneralRequest.DeviceRemoved(false))
is GeneralResult.UpdateMailbox.Forbidden ->
scene.showConfirmPasswordDialog(emailDetailUIObserver)
}
}

Expand Down Expand Up @@ -539,7 +547,7 @@ class EmailDetailSceneController(private val storage: KeyValueStorage,
override fun onSourceOptionSelected(fullEmail: FullEmail) {
if(fullEmail.headers != null && fullEmail.email.boundary != null)
host.goToScene(EmailSourceParams(EmailUtils.getEmailSource(
headers = fullEmail.headers!!,
headers = fullEmail.headers,
boundary = fullEmail.email.boundary!!,
content = fullEmail.email.content
)), true)
Expand Down Expand Up @@ -980,9 +988,11 @@ class EmailDetailSceneController(private val storage: KeyValueStorage,

private val webSocketEventListener = object : WebSocketEventListener {
override fun onSyncBeginRequest(trustedDeviceInfo: DeviceInfo.TrustedDeviceInfo) {
host.runOnUiThread(Runnable {
scene.showSyncDeviceAuthConfirmation(trustedDeviceInfo)
})
if(activeAccount.recipientId == trustedDeviceInfo.recipientId) {
host.runOnUiThread(Runnable {
scene.showSyncDeviceAuthConfirmation(trustedDeviceInfo)
})
}
}

override fun onSyncRequestAccept(syncStatusData: SyncStatusData) {
Expand All @@ -1002,9 +1012,11 @@ class EmailDetailSceneController(private val storage: KeyValueStorage,
}

override fun onDeviceLinkAuthRequest(untrustedDeviceInfo: DeviceInfo.UntrustedDeviceInfo) {
host.runOnUiThread(Runnable {
scene.showLinkDeviceAuthConfirmation(untrustedDeviceInfo)
})
if(activeAccount.recipientId == untrustedDeviceInfo.recipientId) {
host.runOnUiThread(Runnable {
scene.showLinkDeviceAuthConfirmation(untrustedDeviceInfo)
})
}
}

override fun onDeviceLinkAuthAccept(linkStatusData: LinkStatusData) {
Expand All @@ -1015,8 +1027,12 @@ class EmailDetailSceneController(private val storage: KeyValueStorage,

}

override fun onNewEvent() {
generalDataSource.submitRequest(GeneralRequest.UpdateMailbox(true, null, model.currentLabel, 1))
override fun onNewEvent(recipientId: String) {
generalDataSource.submitRequest(
GeneralRequest.UpdateMailbox(
recipientId == activeAccount.recipientId, recipientId,
model.currentLabel, 1
))
}

override fun onRecoveryEmailChanged(newEmail: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,25 @@ class LinkingActivity: BaseActivity(){
val appDB = AppDatabase.getAppDatabase(this)
val activeAccount = ActiveAccount.loadFromStorage(this)!!
val signalClient = SignalClient.Default(SignalStoreCriptext(appDB, activeAccount))
val webSocketEvents = WebSocketSingleton.getInstance(
activeAccount = activeAccount)

val storage = KeyValueStorage.SharedPrefs(this)

val jwts = storage.getString(KeyValueStorage.StringKey.JWTS, "")
val webSocketEvents = if(jwts.isNotEmpty())
WebSocketSingleton.getInstance(jwts)
else
WebSocketSingleton.getInstance(activeAccount.jwt)

val dataSource = LinkingDataSource(
httpClient = HttpClient.Default(),
activeAccount = activeAccount,
runner = AsyncTaskWorkRunner(),
accountDao = appDB.accountDao(),
storage = KeyValueStorage.SharedPrefs(this))
storage = storage)
val generalDataSource = GeneralDataSource(
signalClient = signalClient,
eventLocalDB = EventLocalDB(appDB, this.filesDir, this.cacheDir),
storage = KeyValueStorage.SharedPrefs(this),
storage = storage,
db = appDB,
runner = AsyncTaskWorkRunner(),
activeAccount = ActiveAccount.loadFromStorage(this)!!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class LinkingController(

}

override fun onNewEvent() {
override fun onNewEvent(recipientId: String) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,12 @@ class MailboxActivity : BaseActivity() {
val db: MailboxLocalDB.Default = MailboxLocalDB.Default(appDB, activity.filesDir)
val activeAccount = ActiveAccount.loadFromStorage(activity)!!
val signalClient = SignalClient.Default(SignalStoreCriptext(appDB, activeAccount))
val webSocketEvents = WebSocketSingleton.getInstance(
activeAccount = activeAccount)

val jwts = storage.getString(KeyValueStorage.StringKey.JWTS, "")
val webSocketEvents = if(jwts.isNotEmpty())
WebSocketSingleton.getInstance(jwts)
else
WebSocketSingleton.getInstance(activeAccount.jwt)

val mailboxDataSource = MailboxDataSource(
filesDir = activity.filesDir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ class MailboxSceneController(private val scene: MailboxScene,
}
SyncDeviceActionService.APPROVE -> {
val extrasDevice = extras as IntentExtrasData.IntentExtrasSyncDevice
val trustedDeviceInfo = DeviceInfo.TrustedDeviceInfo(extrasDevice.deviceId, extrasDevice.deviceName,
val trustedDeviceInfo = DeviceInfo.TrustedDeviceInfo(extrasDevice.account, extrasDevice.deviceId, extrasDevice.deviceName,
extrasDevice.deviceType, extrasDevice.randomId, extrasDevice.syncFileVersion)
if(trustedDeviceInfo.syncFileVersion == UserDataWriter.FILE_SYNC_VERSION)
generalDataSource.submitRequest(GeneralRequest.SyncAccept(trustedDeviceInfo))
Expand Down Expand Up @@ -771,7 +771,7 @@ class MailboxSceneController(private val scene: MailboxScene,
label = mailboxLabel,
loadedThreadsCount = model.threads.size,
isActiveAccount = true,
activeAccount = null
recipientId = activeAccount.recipientId
)
generalDataSource.submitRequest(req)
}
Expand Down Expand Up @@ -820,7 +820,13 @@ class MailboxSceneController(private val scene: MailboxScene,
activeAccount = resultData.activeAccount
generalDataSource.activeAccount = activeAccount
dataSource.activeAccount = activeAccount
websocketEvents = WebSocketSingleton.getInstance(activeAccount)

val jwts = storage.getString(KeyValueStorage.StringKey.JWTS, "")
websocketEvents = if(jwts.isNotEmpty())
WebSocketSingleton.getInstance(jwts)
else
WebSocketSingleton.getInstance(activeAccount.jwt)

websocketEvents.setListener(webSocketEventListener)

scene.initMailboxAvatar(activeAccount.name, activeAccount.userEmail)
Expand Down Expand Up @@ -1221,7 +1227,7 @@ class MailboxSceneController(private val scene: MailboxScene,
label = model.selectedLabel,
loadedThreadsCount = model.threads.size,
isActiveAccount = false,
activeAccount = ActiveAccount.loadFromDB(it)
recipientId = it.recipientId
))
}
}
Expand Down Expand Up @@ -1260,9 +1266,11 @@ class MailboxSceneController(private val scene: MailboxScene,

private val webSocketEventListener = object : WebSocketEventListener {
override fun onSyncBeginRequest(trustedDeviceInfo: DeviceInfo.TrustedDeviceInfo) {
host.runOnUiThread(Runnable {
scene.showSyncDeviceAuthConfirmation(trustedDeviceInfo)
})
if(trustedDeviceInfo.recipientId == activeAccount.recipientId) {
host.runOnUiThread(Runnable {
scene.showSyncDeviceAuthConfirmation(trustedDeviceInfo)
})
}
}

override fun onSyncRequestAccept(syncStatusData: SyncStatusData) {
Expand Down Expand Up @@ -1290,13 +1298,27 @@ class MailboxSceneController(private val scene: MailboxScene,
}

override fun onDeviceLinkAuthRequest(untrustedDeviceInfo: DeviceInfo.UntrustedDeviceInfo) {
host.runOnUiThread(Runnable {
scene.showLinkDeviceAuthConfirmation(untrustedDeviceInfo)
})
}

override fun onNewEvent() {
reloadViewAfterSocketEvent()
if(untrustedDeviceInfo.recipientId == activeAccount.recipientId) {
host.runOnUiThread(Runnable {
scene.showLinkDeviceAuthConfirmation(untrustedDeviceInfo)
})
}
}

override fun onNewEvent(recipientId: String) {
if(recipientId == activeAccount.recipientId)
reloadViewAfterSocketEvent()
else{
val account = model.extraAccounts.find { it.recipientId == recipientId }
if(account != null){
generalDataSource.submitRequest(GeneralRequest.UpdateMailbox(
label = model.selectedLabel,
loadedThreadsCount = model.threads.size,
isActiveAccount = false,
recipientId = account.recipientId
))
}
}
}

override fun onRecoveryEmailChanged(newEmail: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class MailboxDataSource(
is MailboxRequest.GetMenuInformation -> GetMenuInformationWorker(
db = mailboxLocalDB,
activeAccount = activeAccount,
storage = storage,
publishFn = { result ->
flushResults(result)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ class EmailSourceActivity: BaseActivity(){
val appDB = AppDatabase.getAppDatabase(this)
val activeAccount = ActiveAccount.loadFromStorage(this)!!
val signalClient = SignalClient.Default(SignalStoreCriptext(appDB, activeAccount))
val webSocketEvents = WebSocketSingleton.getInstance(
activeAccount = activeAccount)
val storage = KeyValueStorage.SharedPrefs(this)

val jwts = storage.getString(KeyValueStorage.StringKey.JWTS, "")
val webSocketEvents = if(jwts.isNotEmpty())
WebSocketSingleton.getInstance(jwts)
else
WebSocketSingleton.getInstance(activeAccount.jwt)

val generalDataSource = GeneralDataSource(
signalClient = signalClient,
eventLocalDB = EventLocalDB(appDB, this.filesDir, this.cacheDir),
storage = KeyValueStorage.SharedPrefs(this),
storage = storage,
db = appDB,
runner = AsyncTaskWorkRunner(),
activeAccount = activeAccount,
Expand All @@ -46,7 +51,7 @@ class EmailSourceActivity: BaseActivity(){
activeAccount = activeAccount,
model = model,
scene = scene,
storage = KeyValueStorage.SharedPrefs(this),
storage = storage,
websocketEvents = webSocketEvents,
generalDataSource = generalDataSource,
keyboardManager = KeyboardManager(this),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,11 @@ class EmailSourceController(
}

override fun onDeviceLinkAuthRequest(untrustedDeviceInfo: DeviceInfo.UntrustedDeviceInfo) {
host.runOnUiThread(Runnable {
scene.showLinkDeviceAuthConfirmation(untrustedDeviceInfo)
})
if(activeAccount.recipientId == untrustedDeviceInfo.recipientId) {
host.runOnUiThread(Runnable {
scene.showLinkDeviceAuthConfirmation(untrustedDeviceInfo)
})
}
}

override fun onDeviceLinkAuthAccept(linkStatusData: LinkStatusData) {
Expand All @@ -203,7 +205,7 @@ class EmailSourceController(

}

override fun onNewEvent() {
override fun onNewEvent(recipientId: String) {

}

Expand Down
Loading

0 comments on commit 9d2657b

Please sign in to comment.