Skip to content

Commit

Permalink
fix(Communities): add dedicated permissions model for addresses check…
Browse files Browse the repository at this point in the history
… popup
  • Loading branch information
MishkaRogachev committed Apr 18, 2024
1 parent 09948a6 commit 15ba842
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/app/modules/main/chat_section/io_interface.nim
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ method onAcceptRequestToJoinFailedNoPermission*(self: AccessInterface, community
method onDeactivateChatLoader*(self: AccessInterface, chatId: string) {.base.} =
raise newException(ValueError, "No implementation available")

method onCommunityCheckPermissionsToJoinResponse*(self: AccessInterface, checkPermissionsToJoinResponse: CheckPermissionsToJoinResponseDto) {.base.} =
method onCommunityCheckPermissionsToJoinResponse*(self: AccessInterface, checkPermissionsToJoinResponse: CheckPermissionsToJoinResponseDto, ) {.base.} =
raise newException(ValueError, "No implementation available")

method onCommunityCheckChannelPermissionsResponse*(self: AccessInterface, chatId: string, checkChannelPermissionsResponse: CheckChannelPermissionsResponseDto) {.base.} =
Expand Down
7 changes: 6 additions & 1 deletion src/app/modules/main/communities/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,18 @@ proc init*(self: Controller) =

self.events.on(SIGNAL_CHECK_PERMISSIONS_TO_JOIN_RESPONSE) do(e: Args):
let args = CheckPermissionsToJoinResponseArgs(e)
self.delegate.onCommunityCheckPermissionsToJoinResponse(args.communityId, args.checkPermissionsToJoinResponse)
self.delegate.onCommunityCheckPermissionsToJoinResponse(
args.communityId,
args.checkPermissionsToJoinResponse,
args.requestId == CommunityPermissionsCheckRequestID.SharedAddressesCheck
)

self.events.on(SIGNAL_CHECK_ALL_CHANNELS_PERMISSIONS_RESPONSE) do(e: Args):
let args = CheckAllChannelsPermissionsResponseArgs(e)
self.delegate.onCommunityCheckAllChannelsPermissionsResponse(
args.communityId,
args.checkAllChannelsPermissionsResponse,
args.requestId == CommunityPermissionsCheckRequestID.SharedAddressesCheck
)

self.events.on(SIGNAL_COMMUNITY_MEMBER_REVEALED_ACCOUNTS_LOADED) do(e: Args):
Expand Down
9 changes: 5 additions & 4 deletions src/app/modules/main/communities/io_interface.nim
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,21 @@ method signSharedAddressesForKeypair*(self: AccessInterface, keyUid: string, pin
method joinCommunityOrEditSharedAddresses*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")

method prepareTokenModelForCommunity*(self: AccessInterface, communityId: string) {.base.} =
method prepareTokenModelForCommunity*(self: AccessInterface, communityId: string, dedicated: bool) {.base.} =
raise newException(ValueError, "No implementation available")

method getCommunityPublicKeyFromPrivateKey*(self: AccessInterface, communityPrivateKey: string): string {.base.} =
raise newException(ValueError, "No implementation available")

method checkPermissions*(self: AccessInterface, communityId: string, sharedAddresses: seq[string], temporary: bool) {.base.} =
method checkPermissions*(self: AccessInterface, communityId: string, sharedAddresses: seq[string], dedicated: bool) {.base.} =
raise newException(ValueError, "No implementation available")

method onCommunityCheckPermissionsToJoinResponse*(self: AccessInterface, communityId: string, checkPermissionsToJoinResponse: CheckPermissionsToJoinResponseDto) {.base.} =
method onCommunityCheckPermissionsToJoinResponse*(self: AccessInterface, communityId: string,
checkPermissionsToJoinResponse: CheckPermissionsToJoinResponseDto, dedicated: bool) {.base.} =
raise newException(ValueError, "No implementation available")

method onCommunityCheckAllChannelsPermissionsResponse*(self: AccessInterface, communityId: string,
checkChannelPermissionsResponse: CheckAllChannelsPermissionsResponseDto) {.base.} =
checkChannelPermissionsResponse: CheckAllChannelsPermissionsResponseDto, dedicated: bool) {.base.} =
raise newException(ValueError, "No implementation available")

method onCommunityCheckPermissionsToJoinFailed*(self: AccessInterface, communityId: string, ValueErrorerror: string) {.base.} =
Expand Down
28 changes: 17 additions & 11 deletions src/app/modules/main/communities/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -863,10 +863,10 @@ method joinCommunityOrEditSharedAddresses*(self: Module) =
method getCommunityPublicKeyFromPrivateKey*(self: Module, communityPrivateKey: string): string =
result = self.controller.getCommunityPublicKeyFromPrivateKey(communityPrivateKey)

method checkPermissions*(self: Module, communityId: string, sharedAddresses: seq[string], temporary: bool) =
method checkPermissions*(self: Module, communityId: string, sharedAddresses: seq[string], dedicated: bool) =
self.joiningCommunityDetails.communityIdForPermissions = communityId

let requestId = if temporary: CommunityPermissionsCheckRequestID.SharedAddressesCheck else: CommunityPermissionsCheckRequestID.GeneralCommunity
let requestId = if dedicated: CommunityPermissionsCheckRequestID.SharedAddressesCheck else: CommunityPermissionsCheckRequestID.GeneralCommunity

self.controller.asyncCheckPermissionsToJoin(communityId, sharedAddresses, requestId)
self.checkingPermissionToJoinInProgress = true
Expand All @@ -876,7 +876,7 @@ method checkPermissions*(self: Module, communityId: string, sharedAddresses: seq

self.view.setCheckingPermissionsInProgress(inProgress = true)

method prepareTokenModelForCommunity*(self: Module, communityId: string) =
method prepareTokenModelForCommunity*(self: Module, communityId: string, dedicated: bool) =
self.joiningCommunityDetails.communityIdForRevealedAccounts = communityId
self.controller.asyncGetRevealedAccountsForMember(communityId, singletonInstance.userProfile.getPubKey())

Expand All @@ -888,17 +888,21 @@ method prepareTokenModelForCommunity*(self: Module, communityId: string) =
let tokenPermissionItem = buildTokenPermissionItem(tokenPermission, chats)
tokenPermissionsItems.add(tokenPermissionItem)

self.view.spectatedCommunityPermissionModel.setItems(tokenPermissionsItems)
self.checkPermissions(communityId, @[], false)
if dedicated:
self.view.dedicatedCommunityPermissionModel.setItems(tokenPermissionsItems)
else:
self.view.spectatedCommunityPermissionModel.setItems(tokenPermissionsItems)
self.checkPermissions(communityId, @[], dedicated)

proc applyPermissionResponse*(self: Module, communityId: string, permissions: Table[string, CheckPermissionsResultDto]) =
proc applyPermissionResponse*(self: Module, communityId: string, permissions: Table[string, CheckPermissionsResultDto], dedicated: bool) =
let community = self.controller.getCommunityById(communityId)
let permissionsModel = if dedicated: self.view.dedicatedCommunityPermissionModel else: self.view.spectatedCommunityPermissionModel
for id, criteriaResult in permissions:
if not community.tokenPermissions.hasKey(id):
warn "unknown permission", id
continue

let tokenPermissionItem = self.view.spectatedCommunityPermissionModel.getItemById(id)
let tokenPermissionItem = permissionsModel.getItemById(id)
if tokenPermissionItem.id == "":
warn "no permission in model", id
continue
Expand Down Expand Up @@ -938,7 +942,7 @@ proc applyPermissionResponse*(self: Module, communityId: string, permissions: Ta
permissionSatisfied,
tokenPermissionItem.state
)
self.view.spectatedCommunityPermissionModel.updateItem(id, updatedTokenPermissionItem)
permissionsModel.updateItem(id, updatedTokenPermissionItem)

proc updateCheckingPermissionsInProgressIfNeeded(self: Module, inProgress = false) =
if self.checkingPermissionToJoinInProgress != self.checkingAllChannelPermissionsInProgress:
Expand All @@ -957,16 +961,16 @@ method onCommunityCheckAllChannelPermissionsFailed*(self: Module, communityId: s
self.updateCheckingPermissionsInProgressIfNeeded(inProgress = false)

method onCommunityCheckPermissionsToJoinResponse*(self: Module, communityId: string,
checkPermissionsToJoinResponse: CheckPermissionsToJoinResponseDto) =
checkPermissionsToJoinResponse: CheckPermissionsToJoinResponseDto, dedicated: bool) =
if not self.checkingPermissionToJoinInProgress and
self.joiningCommunityDetails.communityIdForPermissions != communityId:
return
self.applyPermissionResponse(communityId, checkPermissionsToJoinResponse.permissions)
self.applyPermissionResponse(communityId, checkPermissionsToJoinResponse.permissions, dedicated)
self.checkingPermissionToJoinInProgress = false
self.updateCheckingPermissionsInProgressIfNeeded(inProgress = false)

method onCommunityCheckAllChannelsPermissionsResponse*(self: Module, communityId: string,
checkChannelPermissionsResponse: CheckAllChannelsPermissionsResponseDto) =
checkChannelPermissionsResponse: CheckAllChannelsPermissionsResponseDto, dedicated: bool) =
if not self.checkingAllChannelPermissionsInProgress and
self.joiningCommunityDetails.communityIdForPermissions != communityId:
return
Expand All @@ -976,10 +980,12 @@ method onCommunityCheckAllChannelsPermissionsResponse*(self: Module, communityId
self.applyPermissionResponse(
communityId,
channelPermissionResponse.viewOnlyPermissions.permissions,
dedicated,
)
self.applyPermissionResponse(
communityId,
channelPermissionResponse.viewAndPostPermissions.permissions,
dedicated,
)

method onCommunityMemberRevealedAccountsLoaded*(self: Module, communityId, memberPubkey: string,
Expand Down
23 changes: 19 additions & 4 deletions src/app/modules/main/communities/view.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ QtObject:
modelVariant: QVariant
spectatedCommunityPermissionModel: TokenPermissionsModel
spectatedCommunityPermissionModelVariant: QVariant
dedicatedCommunityPermissionModel: TokenPermissionsModel
dedicatedCommunityPermissionModelVariant: QVariant
curatedCommunitiesModel: CuratedCommunityModel
curatedCommunitiesModelVariant: QVariant
curatedCommunitiesLoading: bool
Expand Down Expand Up @@ -65,6 +67,8 @@ QtObject:
self.modelVariant.delete
self.spectatedCommunityPermissionModel.delete
self.spectatedCommunityPermissionModelVariant.delete
self.dedicatedCommunityPermissionModel.delete
self.dedicatedCommunityPermissionModelVariant.delete
self.curatedCommunitiesModel.delete
self.curatedCommunitiesModelVariant.delete
self.discordFileListModel.delete
Expand Down Expand Up @@ -95,6 +99,8 @@ QtObject:
result.modelVariant = newQVariant(result.model)
result.spectatedCommunityPermissionModel = newTokenPermissionsModel()
result.spectatedCommunityPermissionModelVariant = newQVariant(result.spectatedCommunityPermissionModel)
result.dedicatedCommunityPermissionModel = newTokenPermissionsModel()
result.dedicatedCommunityPermissionModelVariant = newQVariant(result.dedicatedCommunityPermissionModel)
result.curatedCommunitiesModel = newCuratedCommunityModel()
result.curatedCommunitiesModelVariant = newQVariant(result.curatedCommunitiesModel)
result.curatedCommunitiesLoading = false
Expand Down Expand Up @@ -339,8 +345,11 @@ QtObject:
proc spectatedCommunityPermissionModel*(self: View): TokenPermissionsModel =
result = self.spectatedCommunityPermissionModel

proc prepareTokenModelForCommunity(self: View, communityId: string) {.slot.} =
self.delegate.prepareTokenModelForCommunity(communityId)
proc dedicatedCommunityPermissionModel*(self: View): TokenPermissionsModel =
result = self.dedicatedCommunityPermissionModel

proc prepareTokenModelForCommunity(self: View, communityId: string, dedicated: bool) {.slot.} =
self.delegate.prepareTokenModelForCommunity(communityId, dedicated)

proc signProfileKeypairAndAllNonKeycardKeypairs*(self: View) {.slot.} =
self.delegate.signProfileKeypairAndAllNonKeycardKeypairs()
Expand All @@ -351,10 +360,10 @@ QtObject:
proc joinCommunityOrEditSharedAddresses*(self: View) {.slot.} =
self.delegate.joinCommunityOrEditSharedAddresses()

proc checkPermissions*(self: View, communityId: string, addressesToShare: string, temporary: bool) {.slot.} =
proc checkPermissions*(self: View, communityId: string, addressesToShare: string, dedicated: bool) {.slot.} =
try:
let sharedAddresses = map(parseJson(addressesToShare).getElems(), proc(x:JsonNode):string = x.getStr())
self.delegate.checkPermissions(communityId, sharedAddresses, temporary)
self.delegate.checkPermissions(communityId, sharedAddresses, dedicated)
except Exception as e:
echo "Error updating token model with addresses: ", e.msg

Expand All @@ -364,6 +373,12 @@ QtObject:
QtProperty[QVariant] spectatedCommunityPermissionModel:
read = getSpectatedCommunityPermissionModel

proc getDedicatedCommunityPermissionModel(self: View): QVariant {.slot.} =
return self.dedicatedCommunityPermissionModelVariant

QtProperty[QVariant] dedicatedCommunityPermissionModel:
read = getDedicatedCommunityPermissionModel

proc curatedCommunitiesModel*(self: View): CuratedCommunityModel =
result = self.curatedCommunitiesModel

Expand Down
6 changes: 4 additions & 2 deletions ui/app/AppLayouts/Chat/stores/RootStore.qml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ QtObject {
}
}

function prepareTokenModelForCommunity(publicKey) {
root.communitiesModuleInst.prepareTokenModelForCommunity(publicKey)
function prepareTokenModelForCommunity(publicKey, dedicated) {
root.communitiesModuleInst.prepareTokenModelForCommunity(publicKey, dedicated)
}

readonly property bool allChannelsAreHiddenBecauseNotPermitted: root.chatCommunitySectionModule.allChannelsAreHiddenBecauseNotPermitted &&
Expand All @@ -69,6 +69,8 @@ QtObject {
readonly property var permissionsModel: !!root.communitiesModuleInst.spectatedCommunityPermissionModel ?
root.communitiesModuleInst.spectatedCommunityPermissionModel : null

readonly property var dedicatedPermissionsModel: root.communitiesModuleInst.dedicatedPermissionsModel

readonly property string overviewChartData: chatCommunitySectionModule.overviewChartData

readonly property bool isUserAllowedToSendMessage: _d.isUserAllowedToSendMessage
Expand Down
4 changes: 2 additions & 2 deletions ui/app/AppLayouts/Communities/stores/CommunitiesStore.qml
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ QtObject {
root.communitiesModuleInst.spectateCommunity(publicKey, "");
}

function prepareTokenModelForCommunity(publicKey) {
root.communitiesModuleInst.prepareTokenModelForCommunity(publicKey);
function prepareTokenModelForCommunity(publicKey, dedicated) {
root.communitiesModuleInst.prepareTokenModelForCommunity(publicKey, dedicated);
}

function getCommunityDetails(communityId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ Item {
assetsModel: root.store.assetsModel
collectiblesModel: root.store.collectiblesModel
permissionsModel: {
root.store.prepareTokenModelForCommunity(communityData.id)
root.store.prepareTokenModelForCommunity(communityData.id, false)
return root.store.permissionsModel
}
channelsModel: root.store.chatCommunitySectionModule.model
Expand Down
4 changes: 2 additions & 2 deletions ui/app/AppLayouts/stores/RootStore.qml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ QtObject {
return Constants.LoginType.Password
}

function prepareTokenModelForCommunity(publicKey) {
root.communitiesModuleInst.prepareTokenModelForCommunity(publicKey)
function prepareTokenModelForCommunity(publicKey, dedicated) {
root.communitiesModuleInst.prepareTokenModelForCommunity(publicKey, dedicated)
}

property string communityKeyToImport
Expand Down
8 changes: 4 additions & 4 deletions ui/app/mainui/Popups.qml
Original file line number Diff line number Diff line change
Expand Up @@ -713,8 +713,8 @@ QtObject {

walletAssetsModel: walletAssetsStore.groupedAccountAssetsModel
permissionsModel: {
root.rootStore.prepareTokenModelForCommunity(dialogRoot.communityId)
return root.rootStore.permissionsModel
root.rootStore.prepareTokenModelForCommunity(dialogRoot.communityId, true)
return root.rootStore.dedicatedPermissionsModel
}
assetsModel: root.rootStore.assetsModel
collectiblesModel: root.rootStore.collectiblesModel
Expand Down Expand Up @@ -964,8 +964,8 @@ QtObject {
walletCollectiblesModel: WalletStore.RootStore.collectiblesStore.allCollectiblesModel

permissionsModel: {
root.rootStore.prepareTokenModelForCommunity(editSharedAddressesPopup.communityId)
return root.rootStore.permissionsModel
root.rootStore.prepareTokenModelForCommunity(editSharedAddressesPopup.communityId, true)
return root.rootStore.dedicatedPermissionsModel
}
assetsModel: chatStore.assetsModel
collectiblesModel: chatStore.collectiblesModel
Expand Down

0 comments on commit 15ba842

Please sign in to comment.