Skip to content

Commit

Permalink
Inject Hedwig
Browse files Browse the repository at this point in the history
  • Loading branch information
SeniorZhai committed Dec 2, 2024
1 parent b8803cb commit 29544ec
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class BlazeMessageService : LifecycleService(), NetworkEventProvider.Listener, C
lateinit var circleService: CircleService

fun database(): MixinDatabase = databaseProvider.getMixinDatabase()

fun pendingDatabase(): PendingDatabase = databaseProvider.getPendingDatabase()
fun ftsDatabase(): FtsDatabase = databaseProvider.getFtsDatabase()

Expand All @@ -164,7 +165,7 @@ class BlazeMessageService : LifecycleService(), NetworkEventProvider.Listener, C
private val destroyScope = scope(Lifecycle.Event.ON_DESTROY)

private val hedwig: Hedwig by lazy {
HedwigImp(database(), pendingDatabase(), conversationService, circleService, jobManager, callState, lifecycleScope)
HedwigImp(databaseProvider, conversationService, circleService, jobManager, callState, lifecycleScope)
}

override fun onBind(intent: Intent): IBinder? {
Expand Down
81 changes: 40 additions & 41 deletions app/src/main/java/one/mixin/android/messenger/HedwigImp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import one.mixin.android.api.service.CircleService
import one.mixin.android.api.service.ConversationService
import one.mixin.android.db.CircleConversationDao
import one.mixin.android.db.CircleDao
import one.mixin.android.db.ConversationDao
import one.mixin.android.db.ConversationExtDao
import one.mixin.android.db.DatabaseProvider
import one.mixin.android.db.JobDao
import one.mixin.android.db.MessageDao
import one.mixin.android.db.MixinDatabase
import one.mixin.android.db.ParticipantDao
import one.mixin.android.db.ParticipantSessionDao
import one.mixin.android.db.RemoteMessageStatusDao
import one.mixin.android.db.flow.MessageFlow
import one.mixin.android.db.insertNoReplace

Expand Down Expand Up @@ -41,8 +51,7 @@ import timber.log.Timber
import java.io.IOException

class HedwigImp(
private val mixinDatabase: MixinDatabase,
private val pendingDatabase: PendingDatabase,
private val databaseProvider: DatabaseProvider,
private val conversationService: ConversationService,
private val circleService: CircleService,
private val jobManager: MixinJobManager,
Expand All @@ -61,37 +70,27 @@ class HedwigImp(
pendingJob?.cancel()
}

private val messageDao by lazy {
mixinDatabase.messageDao()
}

private val conversationDao by lazy {
mixinDatabase.conversationDao()
}
private val participantDao by lazy {
mixinDatabase.participantDao()
}
private val participantSessionDao by lazy {
mixinDatabase.participantSessionDao()
}
private val circleConversationDao by lazy {
mixinDatabase.circleConversationDao()
}
private val circleDao by lazy {
mixinDatabase.circleDao()
}
private val messageDao: MessageDao
get() = databaseProvider.getMixinDatabase().messageDao()

private val conversationExtDao by lazy {
mixinDatabase.conversationExtDao()
}
private val conversationDao: ConversationDao
get() = databaseProvider.getMixinDatabase().conversationDao()
private val conversationExtDao: ConversationExtDao
get() = databaseProvider.getMixinDatabase().conversationExtDao()
private val participantDao: ParticipantDao
get() = databaseProvider.getMixinDatabase().participantDao()
private val participantSessionDao: ParticipantSessionDao
get() = databaseProvider.getMixinDatabase().participantSessionDao()
private val circleConversationDao: CircleConversationDao
get() = databaseProvider.getMixinDatabase().circleConversationDao()
private val circleDao: CircleDao
get() = databaseProvider.getMixinDatabase().circleDao()

private val remoteMessageStatusDao by lazy {
mixinDatabase.remoteMessageStatusDao()
}
private val remoteMessageStatusDao: RemoteMessageStatusDao
get() = databaseProvider.getMixinDatabase().remoteMessageStatusDao()

private val jobDao by lazy {
pendingDatabase.jobDao()
}
private val jobDao: JobDao
get() = databaseProvider.getPendingDatabase().jobDao()

private var floodJob: Job? = null
private val floodObserver =
Expand All @@ -103,11 +102,11 @@ class HedwigImp(

private fun startObserveFlood() {
runFloodJob()
pendingDatabase.addObserver(floodObserver)
databaseProvider.getPendingDatabase().addObserver(floodObserver)
}

private fun stopObserveFlood() {
pendingDatabase.removeObserver(floodObserver)
databaseProvider.getPendingDatabase().removeObserver(floodObserver)
}

@Synchronized
Expand All @@ -133,10 +132,10 @@ class HedwigImp(
}

private suspend fun handleBlobTooBigError(e: Exception) {
val messageIds = pendingDatabase.findMessageIdsLimit10()
val maxLengthId = pendingDatabase.findMaxLengthMessageId(messageIds)
val messageIds = databaseProvider.getPendingDatabase().findMessageIdsLimit10()
val maxLengthId = databaseProvider.getPendingDatabase().findMaxLengthMessageId(messageIds)
if (maxLengthId != null) {
pendingDatabase.deleteFloodMessageById(maxLengthId)
databaseProvider.getPendingDatabase().deleteFloodMessageById (maxLengthId)
jobDao.insertNoReplace(createAckJob(ACKNOWLEDGE_MESSAGE_RECEIPTS, BlazeAckMessage(maxLengthId, MessageStatus.DELIVERED.name)))
}
Timber.e(e)
Expand All @@ -151,7 +150,7 @@ class HedwigImp(
private val callMessageDecrypt by lazy { DecryptCallMessage(callState, lifecycleScope) }

private tailrec suspend fun processFloodMessage(): Boolean {
val messages = pendingDatabase.findFloodMessages()
val messages = databaseProvider.getPendingDatabase().findFloodMessages()
return if (messages.isNotEmpty()) {
messages.forEach { message ->
val data = gson.fromJson(message.data, BlazeMessageData::class.java)
Expand All @@ -160,7 +159,7 @@ class HedwigImp(
} else {
messageDecrypt.onRun(data)
}
pendingDatabase.deleteFloodMessage(message)
databaseProvider.getPendingDatabase().deleteFloodMessage(message)
pendingMessageStatusLruCache.remove(data.messageId)
}
processFloodMessage()
Expand All @@ -179,11 +178,11 @@ class HedwigImp(

private fun startObservePending() {
runPendingJob()
pendingDatabase.addObserver(pendingObserver)
databaseProvider.getPendingDatabase().addObserver(pendingObserver)
}

private fun stopObservePending() {
pendingDatabase.removeObserver(pendingObserver)
databaseProvider.getPendingDatabase().removeObserver(pendingObserver)
}

@Synchronized
Expand All @@ -194,12 +193,12 @@ class HedwigImp(
pendingJob =
lifecycleScope.launch(PENDING_DB_THREAD) {
try {
val list = pendingDatabase.getPendingMessages()
val list = databaseProvider.getPendingDatabase().getPendingMessages()
list.groupBy { it.conversationId }.filter { (conversationId, _) ->
conversationId != SYSTEM_USER && conversationId != Session.getAccountId() && checkConversation(conversationId) != null
}.forEach { (conversationId, messages) ->
messageDao.insertList(messages)
pendingDatabase.deletePendingMessageByIds(messages.map { it.messageId })
databaseProvider.getPendingDatabase().deletePendingMessageByIds(messages.map { it.messageId })
conversationExtDao.increment(conversationId, messages.size)
messages.filter { message ->
!message.isMine() && message.status != MessageStatus.READ.name && (pendingMessageStatusLruCache[message.messageId] != MessageStatus.READ.name)
Expand Down

0 comments on commit 29544ec

Please sign in to comment.