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 May 1, 2024
1 parent 5f8ef2f commit 12f3a60
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 43 deletions.
8 changes: 4 additions & 4 deletions src/app/modules/main/chat_section/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,12 @@ proc init*(self: Controller) =

self.events.on(SIGNAL_CHECK_PERMISSIONS_TO_JOIN_RESPONSE) do(e: Args):
let args = CheckPermissionsToJoinResponseArgs(e)
if (args.communityId == self.sectionId):
if (args.communityId == self.sectionId and args.requestId == CommunityPermissionsCheckRequestID.GeneralCommunity):
self.delegate.onCommunityCheckPermissionsToJoinResponse(args.checkPermissionsToJoinResponse)

self.events.on(SIGNAL_CHECK_PERMISSIONS_TO_JOIN_FAILED) do(e: Args):
let args = CheckPermissionsToJoinFailedArgs(e)
if (args.communityId == self.sectionId):
if (args.communityId == self.sectionId and args.requestId == CommunityPermissionsCheckRequestID.GeneralCommunity):
self.delegate.setPermissionsToJoinCheckOngoing(false)

self.events.on(SIGNAL_CHECK_CHANNEL_PERMISSIONS_RESPONSE) do(e: Args):
Expand All @@ -318,13 +318,13 @@ proc init*(self: Controller) =

self.events.on(SIGNAL_CHECK_ALL_CHANNELS_PERMISSIONS_RESPONSE) do(e: Args):
let args = CheckAllChannelsPermissionsResponseArgs(e)
if args.communityId == self.sectionId:
if args.communityId == self.sectionId and args.requestId == CommunityPermissionsCheckRequestID.GeneralCommunity:
self.allChannelsPermissionCheckOngoing = false
self.delegate.onCommunityCheckAllChannelsPermissionsResponse(args.checkAllChannelsPermissionsResponse)

self.events.on(SIGNAL_CHECK_ALL_CHANNELS_PERMISSIONS_FAILED) do(e: Args):
let args = CheckChannelsPermissionsErrorArgs(e)
if args.communityId == self.sectionId:
if args.communityId == self.sectionId and args.requestId == CommunityPermissionsCheckRequestID.GeneralCommunity:
self.allChannelsPermissionCheckOngoing = false
self.delegate.setPermissionsToJoinCheckOngoing(false)

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
31 changes: 20 additions & 11 deletions src/app/modules/main/communities/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -865,8 +865,11 @@ 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) =
let requestId = if temporary: CommunityPermissionsCheckRequestID.SharedAddressesCheck else: CommunityPermissionsCheckRequestID.GeneralCommunity
method checkPermissions*(self: Module, communityId: string, sharedAddresses: seq[string], dedicated: bool) =
let requestId = if dedicated:
CommunityPermissionsCheckRequestID.SharedAddressesCheck
else:
CommunityPermissionsCheckRequestID.GeneralCommunity

self.joiningCommunityDetails.communityIdForPermissions = communityId

Expand All @@ -880,7 +883,7 @@ method checkPermissions*(self: Module, communityId: string, sharedAddresses: seq
self.view.setCheckingPermissionsInProgress(inProgress = true)
self.view.setChannelsPermissionsCheckSuccessful(false)

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 @@ -892,17 +895,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 @@ -943,7 +950,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 @@ -962,17 +969,17 @@ 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.view.setJoinPermissionsCheckSuccessful(true)
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 @@ -983,10 +990,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 @@ -67,6 +69,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 @@ -97,6 +101,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 @@ -344,8 +350,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 @@ -356,10 +365,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 @@ -369,6 +378,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
4 changes: 2 additions & 2 deletions src/app_service/service/chat/async_tasks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ const asyncCheckAllChannelsPermissionsTask: Task = proc(argEncoded: string) {.gc
arg.finish(%* {
"response": response,
"communityId": arg.communityId,
"requestId": arg.requestId,
"requestId": arg.requestId.int,
"error": "",
})
except Exception as e:
arg.finish(%* {
"communityId": arg.communityId,
"requestId": arg.requestId,
"requestId": arg.requestId.int,
"error": e.msg,
})
2 changes: 2 additions & 0 deletions src/app_service/service/chat/service.nim
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ type
checkAllChannelsPermissionsResponse*: CheckAllChannelsPermissionsResponseDto

CheckChannelsPermissionsErrorArgs* = ref object of Args
requestId*: CommunityPermissionsCheckRequestID
communityId*: string
error*: string

Expand Down Expand Up @@ -892,4 +893,5 @@ QtObject:
self.events.emit(SIGNAL_CHECK_ALL_CHANNELS_PERMISSIONS_FAILED, CheckChannelsPermissionsErrorArgs(
communityId: communityId,
error: errMsg,
requestId: requestId
))
4 changes: 2 additions & 2 deletions src/app_service/service/community/async_tasks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ const asyncCheckPermissionsToJoinTask: Task = proc(argEncoded: string) {.gcsafe,
arg.finish(%* {
"response": response,
"communityId": arg.communityId,
"requestId": arg.requestId,
"requestId": arg.requestId.int,
"error": "",
})
except Exception as e:
arg.finish(%* {
"communityId": arg.communityId,
"requestId": arg.requestId,
"requestId": arg.requestId.int,
"error": e.msg,
})

Expand Down
2 changes: 2 additions & 0 deletions src/app_service/service/community/service.nim
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ type
checkPermissionsToJoinResponse*: CheckPermissionsToJoinResponseDto

CheckPermissionsToJoinFailedArgs* = ref object of Args
requestId*: CommunityPermissionsCheckRequestID
communityId*: string
error*: string

Expand Down Expand Up @@ -1661,6 +1662,7 @@ QtObject:
self.events.emit(SIGNAL_CHECK_PERMISSIONS_TO_JOIN_FAILED, CheckPermissionsToJoinFailedArgs(
communityId: communityId,
error: errMsg,
requestId: requestId
))

proc generateJoiningCommunityRequestsForSigning*(self: Service, memberPubKey: string, communityId: string,
Expand Down
10 changes: 6 additions & 4 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 Expand Up @@ -700,7 +702,7 @@ QtObject {
}
}

function updatePermissionsModel(communityId, sharedAddresses) {
communitiesModuleInst.checkPermissions(communityId, JSON.stringify(sharedAddresses), false)
function updatePermissionsModel(communityId, sharedAddresses, dedicated) {
communitiesModuleInst.checkPermissions(communityId, JSON.stringify(sharedAddresses), dedicated)
}
}
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
8 changes: 4 additions & 4 deletions ui/app/AppLayouts/stores/RootStore.qml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,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 Expand Up @@ -270,8 +270,8 @@ QtObject {
communitiesModuleInst.cleanJoinEditCommunityData()
}

function updatePermissionsModel(communityId, sharedAddresses) {
communitiesModuleInst.checkPermissions(communityId, JSON.stringify(sharedAddresses), false)
function updatePermissionsModel(communityId, sharedAddresses, dedicated) {
communitiesModuleInst.checkPermissions(communityId, JSON.stringify(sharedAddresses), dedicated)
}

function promoteSelfToControlNode(communityId) {
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 @@ -714,8 +714,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 @@ -762,7 +762,7 @@ QtObject {
}

onSharedAddressesUpdated: {
root.rootStore.updatePermissionsModel(dialogRoot.communityId, sharedAddresses)
root.rootStore.updatePermissionsModel(dialogRoot.communityId, sharedAddresses, true)
}

onAboutToShow: { root.rootStore.communityKeyToImport = dialogRoot.communityId; }
Expand Down Expand Up @@ -978,7 +978,7 @@ QtObject {
}

onSharedAddressesUpdated: {
root.rootStore.updatePermissionsModel(editSharedAddressesPopup.communityId, sharedAddresses)
root.rootStore.updatePermissionsModel(editSharedAddressesPopup.communityId, sharedAddresses, false)
}

onPrepareForSigning: {
Expand Down

0 comments on commit 12f3a60

Please sign in to comment.