Skip to content
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
Expand Up @@ -10,7 +10,6 @@ import org.session.libsession.utilities.Phrase
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.merge
import network.loki.messenger.R
import org.session.libsession.utilities.Address
import org.session.libsession.utilities.StringSubstitutionConstants
Expand Down Expand Up @@ -69,6 +68,7 @@ class FullAndNameOnlyNotificationHandler @Inject constructor(
notificationManager: NotificationManagerCompat,
prefs: PreferenceStorage,
appVisibilityManager: AppVisibilityManager,
private val missedMessageEvents: MissedMessageEvents,
) : ThreadBasedNotificationHandler(
context = context,
currentActivityObserver = currentActivityObserver,
Expand All @@ -90,15 +90,23 @@ class FullAndNameOnlyNotificationHandler @Inject constructor(


suspend fun process() {
merge(
threadDb.changeNotification.map(::ThreadUpdated),
mmsDatabase.changeNotification.map(::MessageUpdated),
smsDatabase.changeNotification.map(::MessageUpdated),
reactionDatabase.changeNotification.map(::ReactionUpdated),
missedMessageEvents.withMissedMessages(
live = listOf(
threadDb.changeNotification.map(::ThreadUpdated),
mmsDatabase.changeNotification.map(::MessageUpdated),
smsDatabase.changeNotification.map(::MessageUpdated),
reactionDatabase.changeNotification.map(::ReactionUpdated),
),
toEvent = ::MessageUpdated,
).collect(object : FlowCollector<Event> {
private val lastPostedMessageTimestampByThreadId = MutableLongLongMap()

override suspend fun emit(value: Event) {
handle(value)
if (value is MessageUpdated) missedMessageEvents.markSeen(value.change)
}

private suspend fun handle(value: Event) {
// Whether this event can only update an existing notification (i.e. no action
// if there's no existing notification, and no loud notify on the updated contents)
var updateOnly: Boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package org.thoughtcrime.securesms.notifications

import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.sqlite.db.SupportSQLiteOpenHelper
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.launch
import org.session.libsignal.utilities.Log
import org.thoughtcrime.securesms.database.MmsDatabase
import org.thoughtcrime.securesms.database.MmsSmsColumns
import org.thoughtcrime.securesms.database.SmsDatabase
import org.thoughtcrime.securesms.database.model.MessageChanges
import org.thoughtcrime.securesms.database.model.MessageId
import org.thoughtcrime.securesms.preferences.PreferenceKey
import org.thoughtcrime.securesms.preferences.PreferenceStorage
import javax.inject.Inject
import javax.inject.Provider
import javax.inject.Singleton

/**
* Replays the incoming messages that no notification handler has seen.
*
* The handlers learn about new messages from the databases' change flows, which keep no replay. A
* push or background poll that cold-starts the process stores its message before
* [NotificationProcessor] subscribes, so the message's Added event is dropped and it never notifies.
* Each message a handler deals with is marked in the `notified` column, and each subscription starts
* by replaying the unread incoming messages still unmarked. Messages a handler already saw aren't
* replayed, so an unread message the user dismissed doesn't notify again on every cold start.
*
* The mark is per row rather than a high-water id because sms has no AUTOINCREMENT: deleting its
* newest rows frees their ids for the next inserts.
*/
@Singleton
class MissedMessageEvents @Inject constructor(
private val prefs: PreferenceStorage,
private val openHelper: Provider<SupportSQLiteOpenHelper>,
) {
/**
* The events from [live], plus one Added event for each thread with messages no handler has
* seen. [live] is subscribed before the database is read, so a message stored in between
* arrives at least once; the handlers already ignore a message they've notified.
*/
fun <T> withMissedMessages(live: List<Flow<T>>, toEvent: (MessageChanges) -> T): Flow<T> = channelFlow {
for (flow in live) {
// Undispatched, so the change flow has this subscriber before the read below.
launch(start = CoroutineStart.UNDISPATCHED) {
flow.collect { send(it) }
}
}

for (change in findMissed()) {
send(toEvent(change))
}
}

/**
* Records that a handler has dealt with [change], so it isn't replayed. Call it once the
* handler has finished with the event: a process that dies before then replays it next time.
*/
fun markSeen(change: MessageChanges) {
if (change.changeType != MessageChanges.ChangeType.Added) return

val db = openHelper.get().writableDatabase
for ((isMms, ids) in change.ids.groupBy { it.mms }) {
db.execSQL("""
UPDATE ${tableName(isMms)} SET ${MmsSmsColumns.NOTIFIED} = 1
WHERE ${MmsSmsColumns.ID} IN (${ids.joinToString(",") { it.id.toString() }})
AND ${MmsSmsColumns.NOTIFIED} = 0
""")
}
}

private fun findMissed(): List<MessageChanges> {
val db = openHelper.get().writableDatabase

if (!prefs[TRACKING_STARTED]) {
// Nothing has been marked yet, e.g. the first run of this version. Mark what's stored
// now rather than notifying every unread message.
db.markAllUnreadNotified()
prefs[TRACKING_STARTED] = true
return emptyList()
}

val missed = db.getUnnotifiedIncomingMessages()
if (missed.isNotEmpty()) {
Log.d(TAG, "Replaying ${missed.size} message(s) stored while no handler was listening")
}

return missed.groupBy(keySelector = { it.first }, valueTransform = { it.second })
.map { (threadId, ids) -> MessageChanges(MessageChanges.ChangeType.Added, ids, threadId) }
}

private fun SupportSQLiteDatabase.markAllUnreadNotified() {
for (isMms in listOf(false, true)) {
execSQL("""
UPDATE ${tableName(isMms)} SET ${MmsSmsColumns.NOTIFIED} = 1
WHERE ${MmsSmsColumns.READ} = 0 AND ${MmsSmsColumns.NOTIFIED} = 0
""")
}
}

/**
* Incoming messages that are unread and unmarked, with their threads. A message read on another
* device needs no notification. The read and notified tests match the existing
* (read, notified, thread_id) index on both tables.
*/
private fun SupportSQLiteDatabase.getUnnotifiedIncomingMessages(): List<Pair<Long, MessageId>> {
val select = { isMms: Boolean ->
"""
SELECT ${MmsSmsColumns.THREAD_ID}, ${MmsSmsColumns.ID}, ${if (isMms) 1 else 0}
FROM ${tableName(isMms)}
WHERE ${MmsSmsColumns.READ} = 0 AND ${MmsSmsColumns.NOTIFIED} = 0
AND NOT ${MmsSmsColumns.IS_OUTGOING} AND NOT ${MmsSmsColumns.IS_DELETED}
"""
}

return query("${select(false)} UNION ALL ${select(true)}").use { cursor ->
val result = ArrayList<Pair<Long, MessageId>>(cursor.count)
while (cursor.moveToNext()) {
result += cursor.getLong(0) to MessageId(cursor.getLong(1), mms = cursor.getInt(2) == 1)
}
result
}
}

private fun tableName(isMms: Boolean): String =
if (isMms) MmsDatabase.TABLE_NAME else SmsDatabase.TABLE_NAME

companion object {
private const val TAG = "MissedMessageEvents"

private val TRACKING_STARTED = PreferenceKey.boolean("notification.missed_message_tracking_started")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.merge
import network.loki.messenger.R
import org.session.libsession.utilities.Address
import org.session.libsignal.utilities.Log
Expand Down Expand Up @@ -67,21 +66,30 @@ class NoNameOrContentNotificationHandler @Inject constructor(
private val prefs: PreferenceStorage,
private val appVisibilityManager: AppVisibilityManager,
private val loginStateRepository: LoginStateRepository,
private val missedMessageEvents: MissedMessageEvents,
): BaseNotificationHandler(
currentActivityObserver = currentActivityObserver,
threadDb = threadDb,
recipientRepository = recipientRepository,
) {
suspend fun process() {
merge(
threadDb.changeNotification,
mmsDatabase.changeNotification,
smsDatabase.changeNotification,
reactionDatabase.changeNotification,
missedMessageEvents.withMissedMessages<Any>(
live = listOf(
threadDb.changeNotification,
mmsDatabase.changeNotification,
smsDatabase.changeNotification,
reactionDatabase.changeNotification,
),
toEvent = { it },
).collect(object : FlowCollector<Any> {
private val lastNotifiedByThreadId = MutableLongLongMap()

override suspend fun emit(value: Any) {
handle(value)
if (value is MessageChanges) missedMessageEvents.markSeen(value)
}

private suspend fun handle(value: Any) {
when (value) {
is ThreadChanges -> {
val newLastSeen =
Expand Down
Loading
Loading