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 19, 2024
1 parent 85735a4 commit 57b21a0
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 46 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
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
33 changes: 22 additions & 11 deletions src/app/modules/main/communities/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -863,8 +863,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) =
self.joiningCommunityDetails.communityIdForPermissions = communityId

echo "---------------------> checkPermissions: ", dedicated
let requestId = if dedicated: CommunityPermissionsCheckRequestID.SharedAddressesCheck else: CommunityPermissionsCheckRequestID.GeneralCommunity

self.joiningCommunityDetails.communityIdForPermissions = communityId

Expand All @@ -878,7 +881,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 @@ -890,17 +893,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 @@ -941,7 +948,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 @@ -960,17 +967,19 @@ method onCommunityCheckAllChannelPermissionsFailed*(self: Module, communityId: s
self.updateCheckingPermissionsInProgressIfNeeded(inProgress = false)

method onCommunityCheckPermissionsToJoinResponse*(self: Module, communityId: string,
checkPermissionsToJoinResponse: CheckPermissionsToJoinResponseDto) =
checkPermissionsToJoinResponse: CheckPermissionsToJoinResponseDto, dedicated: bool) =
echo "-----------------------> onCommunityCheckPermissionsToJoinResponse: ", dedicated
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) =
echo "-----------------------> onCommunityCheckAllChannelsPermissionsResponse: ", dedicated
if not self.checkingAllChannelPermissionsInProgress and
self.joiningCommunityDetails.communityIdForPermissions != communityId:
return
Expand All @@ -981,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,
})
7 changes: 7 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 @@ -850,6 +851,7 @@ QtObject:
error "error checking all channel permissions: ", errMsg

proc asyncCheckAllChannelsPermissions*(self: Service, communityId: string, addresses: seq[string], requestId: CommunityPermissionsCheckRequestID) =
echo "---------------------> asyncCheckAllChannelsPermissions: ", requestId
let arg = AsyncCheckAllChannelsPermissionsTaskArg(
tptr: cast[ByteAddress](asyncCheckAllChannelsPermissionsTask),
vptr: cast[ByteAddress](self.vptr),
Expand All @@ -870,6 +872,10 @@ QtObject:
(requestIdInt >= ord(low(CommunityPermissionsCheckRequestID)) and
requestIdInt <= ord(high(CommunityPermissionsCheckRequestID)))):
requestId = CommunityPermissionsCheckRequestID(requestIdInt)
else:
echo "---------------------> ERROR:: can't parse", requestIdInt

echo "---------------------> onAsyncCheckAllChannelsPermissionsDone: ", requestId

try:
if rpcResponseObj{"error"}.kind != JNull and rpcResponseObj{"error"}.getStr != "":
Expand All @@ -892,4 +898,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
7 changes: 7 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 @@ -1620,6 +1621,7 @@ QtObject:
self.events.emit(SIGNAL_COMMUNITIES_UPDATE, CommunitiesArgs(communities: @[community]))

proc asyncCheckPermissionsToJoin*(self: Service, communityId: string, addresses: seq[string], requestId: CommunityPermissionsCheckRequestID) =
echo "---------------------> asyncCheckPermissionsToJoin: ", requestId
let arg = AsyncCheckPermissionsToJoinTaskArg(
tptr: cast[ByteAddress](asyncCheckPermissionsToJoinTask),
vptr: cast[ByteAddress](self.vptr),
Expand All @@ -1640,6 +1642,8 @@ QtObject:
(requestIdInt >= ord(low(CommunityPermissionsCheckRequestID)) and
requestIdInt <= ord(high(CommunityPermissionsCheckRequestID)))):
requestId = CommunityPermissionsCheckRequestID(requestIdInt)
else:
echo "---------------------> ERROR:: can't parse", requestIdInt

try:
if rpcResponseObj{"error"}.kind != JNull and rpcResponseObj{"error"}.getStr != "":
Expand All @@ -1649,6 +1653,8 @@ QtObject:
let error = Json.decode(rpcResponseObj["response"]["error"].getStr, RpcError)
raise newException(RpcException, error.message)

echo "---------------------> onAsyncCheckPermissionsToJoinDone: ", requestId

let checkPermissionsToJoinResponse = rpcResponseObj["response"]["result"].toCheckPermissionsToJoinResponseDto
self.events.emit(SIGNAL_CHECK_PERMISSIONS_TO_JOIN_RESPONSE, CheckPermissionsToJoinResponseArgs(
communityId: communityId,
Expand All @@ -1661,6 +1667,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

0 comments on commit 57b21a0

Please sign in to comment.