feat: the room says who became an admin, and who stopped (member-admin-promotion, Phase 2)
TYPE_ADMIN_ADDED and TYPE_ADMIN_REMOVED, derived like the membership lines: processGroupMembershipChanges compares the group's admin set before the commit with the one after, on every device that applies it. The prior set comes from the group's state -- captured before the engine moves the group on the inbound paths, and from the obligation's prior state on a confirmed own commit -- never from the adminAt flags. An account that left the tree gets its departure line and no admin line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
53acae68a4
commit
19f0fc0f1e
6 files changed
+171
-2
No files matched your search
@@ -5,6 +5,7 @@ import androidx.room3.Transaction
|
||||
import co.touchlab.kermit.Logger
|
||||
import com.vitorpamplona.marmot.GroupEventResult
|
||||
import com.vitorpamplona.marmot.groups.MarmotGroupPolicy
|
||||
import com.vitorpamplona.marmot.groups.currentAdminIdentities
|
||||
import com.vitorpamplona.marmot.groups.currentGroupState
|
||||
import com.vitorpamplona.marmot.groups.currentMarmotData
|
||||
import com.vitorpamplona.marmot.groups.currentNostrGroupId
|
||||
@@ -17,6 +18,7 @@ import com.vitorpamplona.quartz.marmot.mip00KeyPackages.KeyPackageRelayListEvent
|
||||
import com.vitorpamplona.quartz.marmot.mip02Welcome.WelcomeEvent
|
||||
import com.vitorpamplona.quartz.marmot.mip03GroupMessages.GroupEvent
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair
|
||||
import com.vitorpamplona.quartz.nip01Core.metadata.MetadataEvent
|
||||
@@ -1406,6 +1408,10 @@ abstract class NostrDao(
|
||||
|
||||
upsertMarmotGroupEvent(groupEvent, chatRoomId, activeKeyPair)
|
||||
|
||||
// Who administered the room before this event, for the captions a commit's
|
||||
// change to it earns. A copy, taken before the engine moves the group.
|
||||
val priorAdmins = mlsGroup.currentAdminIdentities()
|
||||
|
||||
// Through the engine, not the processor: an application message is the one
|
||||
// result that moves state the library writes nothing for, and the engine is
|
||||
// where that write is. The processor names the group by its address; every
|
||||
@@ -1420,6 +1426,7 @@ abstract class NostrDao(
|
||||
activeKeyPair = activeKeyPair,
|
||||
// The live group the manager holds, which the commit just moved.
|
||||
mlsGroup = engine.groupManager.getGroup(routingId) ?: mlsGroup,
|
||||
priorAdmins = priorAdmins,
|
||||
)
|
||||
|
||||
// A commit that forked the group opened a pass, and a quiet group has
|
||||
@@ -1464,6 +1471,8 @@ abstract class NostrDao(
|
||||
|
||||
if (memberPubkeys.contains(activeKeyPair.pubKey.toHex())) {
|
||||
upsertMarmotGroupEvent(groupEvent, chatRoomId, activeKeyPair)
|
||||
// Before processing moves this very instance.
|
||||
val priorAdmins = mlsGroup.currentAdminIdentities()
|
||||
MarmotInboundManager.processGroupEvent(
|
||||
database = database,
|
||||
activeKeyPair = activeKeyPair,
|
||||
@@ -1479,6 +1488,7 @@ abstract class NostrDao(
|
||||
groupEventResult = groupEventResult,
|
||||
activeKeyPair = activeKeyPair,
|
||||
mlsGroup = mlsGroup,
|
||||
priorAdmins = priorAdmins,
|
||||
)
|
||||
}
|
||||
} else {
|
||||
@@ -1527,13 +1537,15 @@ abstract class NostrDao(
|
||||
groupEventResult: GroupEventResult,
|
||||
activeKeyPair: KeyPair,
|
||||
mlsGroup: MlsGroup,
|
||||
priorAdmins: Set<HexKey>? = null,
|
||||
) {
|
||||
when(groupEventResult) {
|
||||
is GroupEventResult.CommitProcessed -> {
|
||||
MarmotInboundManager.processGroupMembershipChanges(
|
||||
database = database,
|
||||
mlsGroup = mlsGroup,
|
||||
localChatRoom = localChatRoom
|
||||
localChatRoom = localChatRoom,
|
||||
priorAdmins = priorAdmins,
|
||||
)
|
||||
}
|
||||
// A member's standalone proposal -- their SelfRemove -- sits in the
|
||||
|
||||
@@ -570,6 +570,14 @@ data class ChatMessage(
|
||||
const val TYPE_MEMBER_LEFT = "memberLeft"
|
||||
const val TYPE_GROUP_DISBANDED = "groupDisbanded"
|
||||
|
||||
/**
|
||||
* A member became an admin, or stopped being one -- derived like the lines
|
||||
* above, from the group's admin set before and after a commit, on every
|
||||
* device that applies it. docs/member-admin-promotion.md, Phase 2.
|
||||
*/
|
||||
const val TYPE_ADMIN_ADDED = "adminAdded"
|
||||
const val TYPE_ADMIN_REMOVED = "adminRemoved"
|
||||
|
||||
/**
|
||||
* A change this device asked for that every relay refused, so the line
|
||||
* written when it was asked for -- a removal, a disband -- did not come
|
||||
@@ -590,6 +598,8 @@ data class ChatMessage(
|
||||
TYPE_MEMBER_ADDED,
|
||||
TYPE_MEMBER_LEFT,
|
||||
TYPE_GROUP_DISBANDED,
|
||||
TYPE_ADMIN_ADDED,
|
||||
TYPE_ADMIN_REMOVED,
|
||||
TYPE_CHANGE_REJECTED,
|
||||
)
|
||||
|
||||
|
||||
@@ -224,7 +224,10 @@ class MarmotEngine(
|
||||
val chatRoomId = database.marmotRoutingDao().chatRoomIdFor(obligation.groupId)
|
||||
val localChatRoom = database.chatRoomDao().findChatRoomById(chatRoomId) ?: return
|
||||
val group = groupManager.getGroup(obligation.groupId) ?: return
|
||||
MarmotInboundManager.processGroupMembershipChanges(database, localChatRoom, group, announceAdditions = false)
|
||||
MarmotInboundManager.processGroupMembershipChanges(
|
||||
database, localChatRoom, group, announceAdditions = false,
|
||||
priorAdmins = MarmotGroupPolicy.adminIdentitiesIn(obligation.priorState.groupContext.extensions),
|
||||
)
|
||||
database.marmotOutboundDao().continueDisband(localChatRoom)
|
||||
}
|
||||
|
||||
|
||||
@@ -128,12 +128,18 @@ object MarmotInboundManager {
|
||||
* the room out. Both are a terminal state on the other side too -- a removed
|
||||
* member cannot decrypt the next epoch, and a disbanded group refuses every
|
||||
* later event -- so nothing is lost by closing here.
|
||||
*
|
||||
* [priorAdmins] is the group's admin set before the commit, read off the
|
||||
* group's state by the caller. With it, promotions and demotions are captioned
|
||||
* too; without it, the flags are corrected and nothing is said about them. See
|
||||
* docs/member-admin-promotion.md, Phase 2.
|
||||
*/
|
||||
suspend fun processGroupMembershipChanges(
|
||||
database: GizaDatabase,
|
||||
localChatRoom: LocalChatRoom,
|
||||
mlsGroup: MlsGroup,
|
||||
announceAdditions: Boolean = true,
|
||||
priorAdmins: Set<HexKey>? = null,
|
||||
) {
|
||||
logger.d("processGroupMembershipChanges")
|
||||
val adminPublicKeys = mlsGroup.currentAdminIdentities()
|
||||
@@ -254,6 +260,24 @@ object MarmotInboundManager {
|
||||
"${memberName(database, participant.participantPublicKey)} left the group")
|
||||
}
|
||||
|
||||
// Who governs the room, from the group's admin set before and after this
|
||||
// commit -- never from the `adminAt` flags, which a room made before
|
||||
// Phase 1 could have wrong. Only for accounts still in the tree: one that
|
||||
// left took its admin rights with it, and "left the group" says so already.
|
||||
if (priorAdmins != null && !disbanded && !removedUs) {
|
||||
val members = committedGroupMembersPublicKeys.toSet()
|
||||
(priorAdmins - adminPublicKeys).filter { it in members }.sorted().forEach { demoted ->
|
||||
announceLine(database, localChatRoom, ChatMessage.TYPE_ADMIN_REMOVED,
|
||||
if (demoted == userPublicKey) "You are no longer an admin"
|
||||
else "${memberName(database, demoted)} is no longer an admin")
|
||||
}
|
||||
(adminPublicKeys - priorAdmins).filter { it in members }.sorted().forEach { promoted ->
|
||||
announceLine(database, localChatRoom, ChatMessage.TYPE_ADMIN_ADDED,
|
||||
if (promoted == userPublicKey) "You are now an admin"
|
||||
else "${memberName(database, promoted)} is now an admin")
|
||||
}
|
||||
}
|
||||
|
||||
when {
|
||||
disbanded -> announceLine(database, localChatRoom, ChatMessage.TYPE_GROUP_DISBANDED, "The group was disbanded")
|
||||
removedUs -> announceLine(database, localChatRoom, ChatMessage.TYPE_MEMBER_LEFT, "You were removed from the group")
|
||||
|
||||
+4
@@ -34,6 +34,8 @@ import androidx.compose.material.icons.filled.Check
|
||||
import androidx.compose.material.icons.filled.CheckCircle
|
||||
import androidx.compose.material.icons.filled.ChevronRight
|
||||
import androidx.compose.material.icons.filled.ErrorOutline
|
||||
import androidx.compose.material.icons.filled.AdminPanelSettings
|
||||
import androidx.compose.material.icons.filled.RemoveModerator
|
||||
import androidx.compose.material.icons.filled.CallMerge
|
||||
import androidx.compose.material.icons.filled.Campaign
|
||||
import androidx.compose.material.icons.filled.FactCheck
|
||||
@@ -880,6 +882,8 @@ private fun RitualNotice(
|
||||
ChatMessage.TYPE_MEMBER_INVITE_SENT -> Icons.Default.Upload
|
||||
ChatMessage.TYPE_MEMBER_INVITE_FAILED -> Icons.Default.ErrorOutline
|
||||
ChatMessage.TYPE_CHANGE_REJECTED -> Icons.Default.ErrorOutline
|
||||
ChatMessage.TYPE_ADMIN_ADDED -> Icons.Default.AdminPanelSettings
|
||||
ChatMessage.TYPE_ADMIN_REMOVED -> Icons.Default.RemoveModerator
|
||||
|
||||
|
||||
else -> Icons.Default.PanTool
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
package mobi.giza.compose.managers
|
||||
|
||||
import androidx.room3.Room
|
||||
import com.vitorpamplona.marmot.appComponents.AdminPolicyV1
|
||||
import com.vitorpamplona.marmot.appComponents.CurrentProfileGroupFactory
|
||||
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair
|
||||
import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerInternal
|
||||
import kotlin.test.AfterTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import mobi.giza.compose.database.GizaDatabase
|
||||
import mobi.giza.compose.database.builder.getRoomDatabase
|
||||
import mobi.giza.compose.database.model.ChatMessage
|
||||
import mobi.giza.compose.database.repository.DatabaseMarmotRepository
|
||||
|
||||
/**
|
||||
* The lines a room gets when who governs it changes. docs/member-admin-promotion.md,
|
||||
* Phase 2.
|
||||
*
|
||||
* Another member administers the room and changes its admin list over the wire;
|
||||
* the app applies each commit and is checked for what it says. The lines come from
|
||||
* the group's admin set before and after the commit -- not from the participants'
|
||||
* `adminAt` flags -- so they are the same on every device that applies it.
|
||||
*/
|
||||
class AdminCaptionsJvmTest {
|
||||
|
||||
private val db: GizaDatabase = getRoomDatabase(Room.inMemoryDatabaseBuilder<GizaDatabase>())
|
||||
private val marmotRepository = DatabaseMarmotRepository(db, CoroutineScope(Dispatchers.Default))
|
||||
|
||||
@AfterTest
|
||||
fun closeDb() = db.close()
|
||||
|
||||
private val user = KeyPair()
|
||||
private val userHex = user.pubKey.toHexKey()
|
||||
private val dana = MarmotStandIn()
|
||||
|
||||
/** Dana's room with the app in it; Dana is its only admin. Returns the room id. */
|
||||
private suspend fun danasRoom(): String {
|
||||
MarmotStandIn.seedProfile(db, userHex, "us")
|
||||
MarmotStandIn.seedProfile(db, dana.hex, "Dana")
|
||||
val routingId = dana.createGroup("Dana's")
|
||||
return dana.inviteApp(db, marmotRepository, user, routingId)
|
||||
}
|
||||
|
||||
/** Dana commits [admins] as the room's admin list, and the app receives it. */
|
||||
private suspend fun danaSetsAdmins(routingId: String, admins: List<String>) {
|
||||
val commit = dana.commitLocally(routingId) {
|
||||
val staged = dana.manager.stageAppDataUpdate(routingId, AdminPolicyV1.COMPONENT_ID, AdminPolicyV1.ofHex(admins).encode())
|
||||
dana.manager.installState(routingId, staged.pendingState)
|
||||
staged.result
|
||||
}
|
||||
MarmotStandIn.storeInApp(db, user, dana.outbound.buildCommitEvent(routingId, commit.framedCommitBytes, commit.preCommitExporterSecret).signedEvent)
|
||||
}
|
||||
|
||||
/**
|
||||
* The admin lines, in no particular order: two written in the same second
|
||||
* cannot be told apart by time, so the tests look for each by its type.
|
||||
*/
|
||||
private suspend fun adminLines(roomId: String) =
|
||||
db.chatMessageDao().getChatMessagesByChatRoomId(roomId).map { it.chatMessage }
|
||||
.filter { it.messageType == ChatMessage.TYPE_ADMIN_ADDED || it.messageType == ChatMessage.TYPE_ADMIN_REMOVED }
|
||||
|
||||
@Test
|
||||
fun `made an admin, and then not, the app says so in its own words`() = runBlocking {
|
||||
val roomId = danasRoom()
|
||||
|
||||
danaSetsAdmins(roomId, listOf(dana.hex, userHex))
|
||||
assertEquals(listOf(ChatMessage.TYPE_ADMIN_ADDED to "You are now an admin"), adminLines(roomId).map { it.messageType to it.content })
|
||||
assertNotNull(db.chatRoomDao().findChatRoomById(roomId)!!.localParticipants.single { it.participant.participantPublicKey == userHex }.participant.adminAt)
|
||||
|
||||
danaSetsAdmins(roomId, listOf(dana.hex))
|
||||
assertEquals(
|
||||
listOf("You are no longer an admin"),
|
||||
adminLines(roomId).filter { it.messageType == ChatMessage.TYPE_ADMIN_REMOVED }.map { it.content },
|
||||
)
|
||||
assertEquals(2, adminLines(roomId).size, "the demotion repeated the promotion's line")
|
||||
assertNull(db.chatRoomDao().findChatRoomById(roomId)!!.localParticipants.single { it.participant.participantPublicKey == userHex }.participant.adminAt)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `another member made an admin is named`() = runBlocking {
|
||||
val roomId = danasRoom()
|
||||
val erin = KeyPair()
|
||||
MarmotStandIn.seedProfile(db, erin.pubKey.toHexKey(), "Erin")
|
||||
val add = dana.commitLocally(roomId) {
|
||||
dana.manager.addMember(roomId, CurrentProfileGroupFactory.createKeyPackage(NostrSignerInternal(erin)).keyPackage.toTlsBytes())
|
||||
}
|
||||
MarmotStandIn.storeInApp(db, user, dana.outbound.buildCommitEvent(roomId, add.framedCommitBytes, add.preCommitExporterSecret).signedEvent)
|
||||
|
||||
danaSetsAdmins(roomId, listOf(dana.hex, erin.pubKey.toHexKey()))
|
||||
|
||||
assertEquals(listOf("Erin is now an admin"), adminLines(roomId).map { it.content })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a commit that changes nobody's rights says nothing about them`() = runBlocking {
|
||||
val roomId = danasRoom()
|
||||
val erin = KeyPair()
|
||||
MarmotStandIn.seedProfile(db, erin.pubKey.toHexKey(), "Erin")
|
||||
|
||||
val add = dana.commitLocally(roomId) {
|
||||
dana.manager.addMember(roomId, CurrentProfileGroupFactory.createKeyPackage(NostrSignerInternal(erin)).keyPackage.toTlsBytes())
|
||||
}
|
||||
MarmotStandIn.storeInApp(db, user, dana.outbound.buildCommitEvent(roomId, add.framedCommitBytes, add.preCommitExporterSecret).signedEvent)
|
||||
|
||||
assertTrue(db.chatMessageDao().getChatMessagesByChatRoomId(roomId).any { it.chatMessage.messageType == ChatMessage.TYPE_MEMBER_ADDED })
|
||||
assertTrue(adminLines(roomId).isEmpty(), "an add wrote an admin line")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user