feat: read marks, mute and the notification ledger, schema 46 to 47 (notifications, Phase 3)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1ac519059f
commit
c248ab529c
10 files changed
+7324
-4
No files matched your search
@@ -26,6 +26,8 @@ import mobi.giza.compose.database.dao.GiftWrapSealDao
|
||||
import mobi.giza.compose.database.dao.GroupKeyStateDao
|
||||
import mobi.giza.compose.database.dao.GroupSignedEventDao
|
||||
import mobi.giza.compose.database.dao.GroupInboxDao
|
||||
import mobi.giza.compose.database.dao.NotificationDao
|
||||
import mobi.giza.compose.database.migrations.ReadMarkMigrationSpec
|
||||
import mobi.giza.compose.database.dao.InboxKeyDao
|
||||
import mobi.giza.compose.database.dao.InReplyToRelationDao
|
||||
import mobi.giza.compose.database.dao.KeyShareDao
|
||||
@@ -108,6 +110,7 @@ import mobi.giza.compose.database.model.NegentropySynchronizeRequest
|
||||
import mobi.giza.compose.database.model.NegentropySynchronizeResult
|
||||
import mobi.giza.compose.database.model.NostrEvent
|
||||
import mobi.giza.compose.database.model.NostrEventRelay
|
||||
import mobi.giza.compose.database.model.NotificationLedger
|
||||
import mobi.giza.compose.database.model.Participant
|
||||
import mobi.giza.compose.database.model.Post
|
||||
import mobi.giza.compose.database.model.Profile
|
||||
@@ -196,6 +199,7 @@ val GENESIS_AT = Instant.fromEpochMilliseconds(1231006505000L)
|
||||
NegentropySynchronizeResult::class,
|
||||
NostrEvent::class,
|
||||
NostrEventRelay::class,
|
||||
NotificationLedger::class,
|
||||
Participant::class,
|
||||
Post::class,
|
||||
Profile::class,
|
||||
@@ -209,7 +213,7 @@ val GENESIS_AT = Instant.fromEpochMilliseconds(1231006505000L)
|
||||
UnsignedNostrEvent::class,
|
||||
Zap::class
|
||||
],
|
||||
version = 46,
|
||||
version = 47,
|
||||
autoMigrations = [
|
||||
// v2 only adds the DkgSession/DkgParticipantMessage tables, so Room can
|
||||
// generate the migration itself — nothing existing changes shape.
|
||||
@@ -489,6 +493,11 @@ val GENESIS_AT = Instant.fromEpochMilliseconds(1231006505000L)
|
||||
// can find a threshold-1 key, and GroupInboxWrap, GroupInboxMessage and
|
||||
// GroupInboxPublication, and InboxSigning (Phase 5). New tables only, so Room generates it.
|
||||
AutoMigration(from = 45, to = 46),
|
||||
// v47 adds notifications' storage (docs/notifications-and-background-sync.md, Phase 3):
|
||||
// ChatRoom.lastReadMessageId, ChatRoom.notifications and the NotificationLedger table.
|
||||
// Room generates the shape; ReadMarkMigrationSpec then marks every existing room read up
|
||||
// to its newest line, so an upgrade does not turn the user's whole history unread.
|
||||
AutoMigration(from = 46, to = 47, spec = ReadMarkMigrationSpec::class),
|
||||
]
|
||||
)
|
||||
@ConstructedBy(GizaDatabaseConstructor::class)
|
||||
@@ -538,6 +547,8 @@ abstract class GizaDatabase: RoomDatabase() {
|
||||
|
||||
abstract fun groupInboxDao(): GroupInboxDao
|
||||
|
||||
abstract fun notificationDao(): NotificationDao
|
||||
|
||||
|
||||
|
||||
abstract fun connectionDao(): ConnectionDao
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
package mobi.giza.compose.database.dao
|
||||
|
||||
import androidx.room3.Dao
|
||||
import androidx.room3.Insert
|
||||
import androidx.room3.OnConflictStrategy
|
||||
import androidx.room3.Query
|
||||
import androidx.room3.Transaction
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
import kotlin.time.Instant
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import mobi.giza.compose.database.model.ChatMessage
|
||||
import mobi.giza.compose.database.model.NotificationLedger
|
||||
|
||||
/**
|
||||
* Read marks, the notification cursor and the background sync's record.
|
||||
* docs/notifications-and-background-sync.md, Phase 3.
|
||||
*
|
||||
* Every mark here only moves forward. A read mark moved back would raise an unread count the
|
||||
* user already cleared; a cursor moved back would post again what was already posted.
|
||||
*/
|
||||
@Dao
|
||||
abstract class NotificationDao {
|
||||
|
||||
// -- read marks (D8) ---------------------------------------------------------------------
|
||||
|
||||
/** Marks [chatRoomId] read up to [upTo], unless it is already read further. */
|
||||
@Query(
|
||||
"UPDATE ChatRoom SET lastReadMessageId = :upTo " +
|
||||
"WHERE id = :chatRoomId AND (lastReadMessageId IS NULL OR lastReadMessageId < :upTo)"
|
||||
)
|
||||
abstract suspend fun markRead(chatRoomId: String, upTo: Long): Int
|
||||
|
||||
/** Marks [chatRoomId] read up to its newest line. */
|
||||
@Query(
|
||||
"UPDATE ChatRoom SET lastReadMessageId = (SELECT MAX(id) FROM ChatMessage WHERE chatRoomId = ChatRoom.id) " +
|
||||
"WHERE id = :chatRoomId AND EXISTS (SELECT 1 FROM ChatMessage WHERE chatRoomId = ChatRoom.id " +
|
||||
"AND (ChatRoom.lastReadMessageId IS NULL OR id > ChatRoom.lastReadMessageId))"
|
||||
)
|
||||
abstract suspend fun markAllRead(chatRoomId: String): Int
|
||||
|
||||
/** The newest line in [chatRoomId], read or not. */
|
||||
@Query("SELECT MAX(id) FROM ChatMessage WHERE chatRoomId = :chatRoomId")
|
||||
abstract suspend fun newestMessageId(chatRoomId: String): Long?
|
||||
|
||||
/**
|
||||
* Lines in [chatRoomId] the user has not been shown: said by somebody else, above the read
|
||||
* mark, and in [kinds] -- the conversation, not the transcript of ceremonies around it.
|
||||
*/
|
||||
@Query(
|
||||
"SELECT COUNT(*) FROM ChatMessage JOIN ChatRoom ON ChatRoom.id = ChatMessage.chatRoomId " +
|
||||
"WHERE ChatMessage.chatRoomId = :chatRoomId AND ChatMessage.isUserMessage = 0 " +
|
||||
"AND ChatMessage.deletedAt IS NULL AND ChatMessage.messageType IN (:kinds) " +
|
||||
"AND ChatMessage.id > COALESCE(ChatRoom.lastReadMessageId, 0)"
|
||||
)
|
||||
abstract suspend fun unreadCount(chatRoomId: String, kinds: List<String> = UNREAD_KINDS): Int
|
||||
|
||||
/** Unread lines per room, for every room [userPublicKey] is in and has not left or deleted. */
|
||||
@Query(
|
||||
"SELECT ChatMessage.chatRoomId AS chatRoomId, COUNT(*) AS unread FROM ChatMessage " +
|
||||
"JOIN ChatRoom ON ChatRoom.id = ChatMessage.chatRoomId " +
|
||||
"WHERE ChatRoom.userPublicKey = :userPublicKey AND ChatRoom.deletedAt IS NULL " +
|
||||
"AND ChatRoom.leftGroupAt IS NULL AND ChatMessage.isUserMessage = 0 " +
|
||||
"AND ChatMessage.deletedAt IS NULL AND ChatMessage.messageType IN (:kinds) " +
|
||||
"AND ChatMessage.id > COALESCE(ChatRoom.lastReadMessageId, 0) " +
|
||||
"GROUP BY ChatMessage.chatRoomId"
|
||||
)
|
||||
abstract fun observeUnreadCounts(userPublicKey: HexKey, kinds: List<String> = UNREAD_KINDS): Flow<List<RoomUnread>>
|
||||
|
||||
@Query("UPDATE ChatRoom SET notifications = :notifications WHERE id = :chatRoomId")
|
||||
abstract suspend fun setNotifications(chatRoomId: String, notifications: String)
|
||||
|
||||
// -- the cursor (D2, D3) -----------------------------------------------------------------
|
||||
|
||||
@Query("SELECT * FROM NotificationLedger WHERE userPublicKey = :userPublicKey")
|
||||
abstract suspend fun getLedger(userPublicKey: HexKey): NotificationLedger?
|
||||
|
||||
@Query("SELECT * FROM NotificationLedger WHERE userPublicKey = :userPublicKey")
|
||||
abstract fun observeLedger(userPublicKey: HexKey): Flow<NotificationLedger?>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||
protected abstract suspend fun insertLedger(ledger: NotificationLedger): Long
|
||||
|
||||
@Query(
|
||||
"SELECT MAX(ChatMessage.id) FROM ChatMessage JOIN ChatRoom ON ChatRoom.id = ChatMessage.chatRoomId " +
|
||||
"WHERE ChatRoom.userPublicKey = :userPublicKey"
|
||||
)
|
||||
protected abstract suspend fun newestMessageIdFor(userPublicKey: HexKey): Long?
|
||||
|
||||
@Query(
|
||||
"UPDATE ChatRoom SET lastReadMessageId = (SELECT MAX(id) FROM ChatMessage WHERE chatRoomId = ChatRoom.id) " +
|
||||
"WHERE userPublicKey = :userPublicKey"
|
||||
)
|
||||
protected abstract suspend fun markEverythingReadFor(userPublicKey: HexKey)
|
||||
|
||||
/**
|
||||
* Starts notifying for [userPublicKey], once: the cursor at the newest line it holds, and every
|
||||
* room read up to its newest line -- what a device had when it started counting is not news.
|
||||
* Returns false, and changes nothing, when it was already armed.
|
||||
*/
|
||||
@Transaction
|
||||
open suspend fun arm(userPublicKey: HexKey, at: Instant): Boolean {
|
||||
if (getLedger(userPublicKey) != null) return false
|
||||
markEverythingReadFor(userPublicKey)
|
||||
insertLedger(
|
||||
NotificationLedger(
|
||||
userPublicKey = userPublicKey,
|
||||
armedAt = at,
|
||||
cursorMessageId = newestMessageIdFor(userPublicKey) ?: 0,
|
||||
)
|
||||
)
|
||||
return true
|
||||
}
|
||||
|
||||
/** Moves the cursor to [cursorMessageId], unless it is already past it. */
|
||||
@Query(
|
||||
"UPDATE NotificationLedger SET cursorMessageId = :cursorMessageId " +
|
||||
"WHERE userPublicKey = :userPublicKey AND cursorMessageId < :cursorMessageId"
|
||||
)
|
||||
abstract suspend fun advanceCursor(userPublicKey: HexKey, cursorMessageId: Long): Int
|
||||
|
||||
/**
|
||||
* Lines above [cursor] in [userPublicKey]'s rooms that somebody else wrote, oldest first, at most
|
||||
* [limit] of them. The dispatcher decides about these and moves the cursor past them.
|
||||
*/
|
||||
@Query(
|
||||
"SELECT ChatMessage.* FROM ChatMessage JOIN ChatRoom ON ChatRoom.id = ChatMessage.chatRoomId " +
|
||||
"WHERE ChatRoom.userPublicKey = :userPublicKey AND ChatMessage.id > :cursor " +
|
||||
"ORDER BY ChatMessage.id ASC LIMIT :limit"
|
||||
)
|
||||
abstract fun observeAfter(userPublicKey: HexKey, cursor: Long, limit: Int = 200): Flow<List<ChatMessage>>
|
||||
|
||||
@Query(
|
||||
"SELECT ChatMessage.* FROM ChatMessage JOIN ChatRoom ON ChatRoom.id = ChatMessage.chatRoomId " +
|
||||
"WHERE ChatRoom.userPublicKey = :userPublicKey AND ChatMessage.id > :cursor " +
|
||||
"ORDER BY ChatMessage.id ASC LIMIT :limit"
|
||||
)
|
||||
abstract suspend fun getAfter(userPublicKey: HexKey, cursor: Long, limit: Int = 200): List<ChatMessage>
|
||||
|
||||
// -- the background sync's record (D5) ---------------------------------------------------
|
||||
|
||||
@Query(
|
||||
"UPDATE NotificationLedger SET lastBackgroundSyncAt = :at, lastBackgroundSyncOutcome = :outcome, " +
|
||||
"lastBackgroundSyncKind = :kind WHERE userPublicKey = :userPublicKey"
|
||||
)
|
||||
abstract suspend fun recordBackgroundSync(userPublicKey: HexKey, at: Instant, outcome: String, kind: String): Int
|
||||
|
||||
companion object {
|
||||
/** What an unread count counts: the conversation. */
|
||||
val UNREAD_KINDS = listOf(ChatMessage.TYPE_MESSAGE, ChatMessage.TYPE_DIRECT_MESSAGE)
|
||||
}
|
||||
}
|
||||
|
||||
/** One room's unread count. */
|
||||
data class RoomUnread(val chatRoomId: String, val unread: Int)
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package mobi.giza.compose.database.migrations
|
||||
|
||||
import androidx.room3.migration.AutoMigrationSpec
|
||||
import androidx.sqlite.SQLiteConnection
|
||||
import androidx.sqlite.execSQL
|
||||
|
||||
/**
|
||||
* v47's data half: every room an upgraded install already has is read up to its newest line.
|
||||
*
|
||||
* Room generates the shape -- `ChatRoom.lastReadMessageId`, `ChatRoom.notifications` and the
|
||||
* `NotificationLedger` table -- and runs this after. Without it the column is null for every
|
||||
* existing room, null reads as "nothing read", and the first launch of this build would show the
|
||||
* user's entire history as unread. A user who had not read something before the upgrade has no
|
||||
* way to have told us so, and "everything you had is read" is the only answer that does not
|
||||
* invent one. docs/notifications-and-background-sync.md, Phase 3.
|
||||
*/
|
||||
class ReadMarkMigrationSpec : AutoMigrationSpec {
|
||||
override suspend fun onPostMigrate(connection: SQLiteConnection) {
|
||||
connection.execSQL(
|
||||
"UPDATE `ChatRoom` SET `lastReadMessageId` = " +
|
||||
"(SELECT MAX(`id`) FROM `ChatMessage` WHERE `ChatMessage`.`chatRoomId` = `ChatRoom`.`id`)"
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -167,6 +167,9 @@ data class ChatMessage(
|
||||
* who can read it, because naming the recipient is the point of the line the
|
||||
* bystanders get. See docs/marmot-direct-messages.md.
|
||||
*/
|
||||
/** An ordinary line of conversation: the default [messageType]. */
|
||||
const val TYPE_MESSAGE = "message"
|
||||
|
||||
const val TYPE_DIRECT_MESSAGE = "directMessage"
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package mobi.giza.compose.database.model
|
||||
|
||||
import androidx.room3.ColumnInfo
|
||||
import androidx.room3.Entity
|
||||
import androidx.room3.ForeignKey
|
||||
import androidx.room3.Ignore
|
||||
@@ -138,6 +139,22 @@ data class ChatRoom(
|
||||
*/
|
||||
val parentChatRoomId: HexKey? = null,
|
||||
|
||||
/**
|
||||
* The newest line the user has been shown in this room, by [ChatMessage.id], or null for
|
||||
* none. A row id rather than a time because a line's time is the sender's claim and lines
|
||||
* are written out of order -- "load older messages" files old lines now. Only ever moves
|
||||
* forward. Local: no NIP says how to share it between devices.
|
||||
* docs/notifications-and-background-sync.md, D8.
|
||||
*/
|
||||
val lastReadMessageId: Long? = null,
|
||||
|
||||
/**
|
||||
* Whether this room may notify: [NOTIFICATIONS_ALL] or [NOTIFICATIONS_MUTED]. A muted room
|
||||
* still counts what is unread; it only stays quiet about it.
|
||||
*/
|
||||
@ColumnInfo(defaultValue = NOTIFICATIONS_ALL)
|
||||
val notifications: String = NOTIFICATIONS_ALL,
|
||||
|
||||
override val createdAt: Instant = Clock.System.now(),
|
||||
override val updatedAt: Instant = createdAt,
|
||||
override val savedAt: Instant = createdAt,
|
||||
@@ -149,6 +166,9 @@ data class ChatRoom(
|
||||
companion object {
|
||||
const val TAG = "ChatRoom"
|
||||
|
||||
const val NOTIFICATIONS_ALL = "all"
|
||||
const val NOTIFICATIONS_MUTED = "muted"
|
||||
|
||||
@Ignore
|
||||
val logger = Logger.withTag(TAG)
|
||||
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package mobi.giza.compose.database.model
|
||||
|
||||
import androidx.room3.Entity
|
||||
import androidx.room3.PrimaryKey
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* What one identity's notifications have got to, and how its last background sync went.
|
||||
* docs/notifications-and-background-sync.md, D2 and D3.
|
||||
*
|
||||
* No row means not armed: nothing for that identity notifies until its first sync has settled
|
||||
* and a row is written with the cursor at the newest line it then held. Otherwise signing in on
|
||||
* a new phone would post one notification per message of history.
|
||||
*
|
||||
* Keyed by public key rather than by wallet id because every row it is compared against --
|
||||
* `ChatRoom.userPublicKey` -- is.
|
||||
*/
|
||||
@Entity
|
||||
data class NotificationLedger(
|
||||
@PrimaryKey
|
||||
val userPublicKey: HexKey,
|
||||
|
||||
/** When this identity started notifying. */
|
||||
val armedAt: Instant,
|
||||
|
||||
/**
|
||||
* The newest [ChatMessage.id] the dispatcher has decided about. A line above it is new to
|
||||
* this device; one at or below it has been posted or dropped already. Only moves forward.
|
||||
*/
|
||||
val cursorMessageId: Long,
|
||||
|
||||
/** When a background pass last finished for this identity, however it finished. */
|
||||
val lastBackgroundSyncAt: Instant? = null,
|
||||
|
||||
/** How it finished: `completed`, `deadline`, `noNetwork`, `visible`, `failed`. */
|
||||
val lastBackgroundSyncOutcome: String? = null,
|
||||
|
||||
/** `quick` (to EOSE) or `full` (negentropy). */
|
||||
val lastBackgroundSyncKind: String? = null,
|
||||
)
|
||||
Reference in new issue
Block a user