Skip to content

DROID-3634 Notifications | Handle pushKeyUpdates and store #2379

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

Merged
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
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package com.anytypeio.anytype.di.main

import android.content.Context
import android.content.SharedPreferences
import com.anytypeio.anytype.app.AnytypeNotificationService
import com.anytypeio.anytype.data.auth.event.NotificationsDateChannel
import com.anytypeio.anytype.data.auth.event.NotificationsRemoteChannel
import com.anytypeio.anytype.data.auth.event.PushKeyDataChannel
import com.anytypeio.anytype.data.auth.event.PushKeyRemoteChannel
import com.anytypeio.anytype.domain.account.AwaitAccountStartManager
import com.anytypeio.anytype.domain.base.AppCoroutineDispatchers
import com.anytypeio.anytype.domain.chats.PushKeyChannel
import com.anytypeio.anytype.domain.notifications.SystemNotificationService
import com.anytypeio.anytype.domain.workspace.NotificationsChannel
import com.anytypeio.anytype.middleware.EventProxy
import com.anytypeio.anytype.middleware.interactor.EventHandlerChannel
import com.anytypeio.anytype.middleware.interactor.NotificationsMiddlewareChannel
import com.anytypeio.anytype.middleware.interactor.events.PushKeyMiddlewareChannel
import com.anytypeio.anytype.presentation.notifications.NotificationsProvider
import com.anytypeio.anytype.presentation.notifications.PushKeyProvider
import com.anytypeio.anytype.presentation.notifications.PushKeyProviderImpl
import dagger.Module
import dagger.Provides
import javax.inject.Named
Expand Down Expand Up @@ -59,4 +67,43 @@ object NotificationsModule {
notificationsChannel = notificationsChannel,
awaitAccountStartManager = awaitAccountStartManager,
)

@JvmStatic
@Provides
@Singleton
fun providePushKeyProvider(
@Named("encrypted") sharedPreferences: SharedPreferences,
dispatchers: AppCoroutineDispatchers,
@Named(ConfigModule.DEFAULT_APP_COROUTINE_SCOPE) scope: CoroutineScope,
channel: PushKeyChannel
): PushKeyProvider {
return PushKeyProviderImpl(
sharedPreferences = sharedPreferences,
dispatchers = dispatchers,
scope = scope,
channel = channel
)
}

@JvmStatic
@Provides
@Singleton
fun providePushKeyRemoteChannel(
channel: EventHandlerChannel,
@Named(ConfigModule.DEFAULT_APP_COROUTINE_SCOPE) scope: CoroutineScope,
dispatchers: AppCoroutineDispatchers
): PushKeyRemoteChannel = PushKeyMiddlewareChannel(
channel = channel,
scope = scope,
dispatcher = dispatchers.io
)

@JvmStatic
@Provides
@Singleton
fun providePushKeyChannel(
channel: PushKeyRemoteChannel
): PushKeyChannel = PushKeyDataChannel(
channel = channel
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.anytypeio.anytype.core_models.chats

data class PushKeyUpdate(
val encryptionKeyId: String,
val encryptionKey: String
) {
companion object {
val EMPTY = PushKeyUpdate(
encryptionKeyId = "",
encryptionKey = ""
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.anytypeio.anytype.data.auth.event

import com.anytypeio.anytype.core_models.chats.PushKeyUpdate
import com.anytypeio.anytype.domain.chats.PushKeyChannel
import kotlinx.coroutines.flow.Flow

interface PushKeyRemoteChannel {
fun start()
fun stop()
fun observe(): Flow<PushKeyUpdate>
}

class PushKeyDataChannel(
private val channel: PushKeyRemoteChannel
) : PushKeyChannel {

override fun observe(): Flow<PushKeyUpdate> {
return channel.observe()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.anytypeio.anytype.domain.chats

import com.anytypeio.anytype.core_models.chats.PushKeyUpdate
import kotlinx.coroutines.flow.Flow

interface PushKeyChannel {
fun observe(): Flow<PushKeyUpdate>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.anytypeio.anytype.middleware.interactor.events

import com.anytypeio.anytype.core_models.chats.PushKeyUpdate
import com.anytypeio.anytype.core_utils.ext.cancel
import com.anytypeio.anytype.data.auth.event.PushKeyRemoteChannel
import com.anytypeio.anytype.middleware.interactor.EventHandlerChannel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.launch
import timber.log.Timber

class PushKeyMiddlewareChannel(
private val scope: CoroutineScope,
private val channel: EventHandlerChannel,
private val dispatcher: CoroutineDispatcher
) : PushKeyRemoteChannel {

private val jobs = mutableListOf<Job>()

private val _pushKeyStatus = MutableStateFlow<PushKeyUpdate>(PushKeyUpdate.EMPTY)
val pushKeyStatus: Flow<PushKeyUpdate> = _pushKeyStatus.asStateFlow()

override fun start() {
Timber.i("PushKeyMiddlewareChannel start")
jobs.cancel()
jobs += scope.launch(dispatcher) {
channel.flow()
.catch {
Timber.w(it, "Error collecting push key updates")
}
.collect { emission ->
emission.messages.forEach { message ->
message.pushEncryptionKeyUpdate?.let {
val pushKeyUpdate = PushKeyUpdate(
encryptionKeyId = it.encryptionKeyId,
encryptionKey = it.encryptionKey
)
_pushKeyStatus.value = pushKeyUpdate
}
}
}
}
}

override fun stop() {
Timber.i("PushKeyMiddlewareChannel stop")
jobs.cancel()
}

override fun observe(): Flow<PushKeyUpdate> {
return pushKeyStatus
}
}
Loading