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:
Kgothatso NgakoandClaude Opus 5.5 committed 2026-11-20 01:13:37 +02:00
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")
@@ -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