Skip to content

Commit

Permalink
fix(Communities): Introduce request id for checking community permiss…
Browse files Browse the repository at this point in the history
…ions

Close 14376
  • Loading branch information
MishkaRogachev committed Apr 11, 2024
1 parent da7f419 commit c99e106
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/app/modules/main/chat_section/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ proc getMySectionId*(self: Controller): string =
proc asyncCheckPermissionsToJoin*(self: Controller) =
if self.delegate.getPermissionsToJoinCheckOngoing():
return
self.communityService.asyncCheckPermissionsToJoin(self.getMySectionId(), addresses = @[])
self.communityService.asyncCheckPermissionsToJoin(self.getMySectionId(), addresses = @[], CommunityPermissionsCheckRequestID.GeneralCommunity)
self.delegate.setPermissionsToJoinCheckOngoing(true)

proc asyncCheckAllChannelsPermissions*(self: Controller) =
if self.allChannelsPermissionCheckOngoing:
return
self.allChannelsPermissionCheckOngoing = true
self.chatService.asyncCheckAllChannelsPermissions(self.getMySectionId(), addresses = @[])
self.chatService.asyncCheckAllChannelsPermissions(self.getMySectionId(), addresses = @[], CommunityPermissionsCheckRequestID.GeneralCommunity)
self.delegate.setChannelsPermissionsCheckOngoing(true)

proc asyncCheckChannelPermissions*(self: Controller, communityId: string, chatId: string) =
Expand Down
8 changes: 4 additions & 4 deletions src/app/modules/main/communities/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,11 @@ proc authenticate*(self: Controller) =
proc getCommunityPublicKeyFromPrivateKey*(self: Controller, communityPrivateKey: string): string =
result = self.communityService.getCommunityPublicKeyFromPrivateKey(communityPrivateKey)

proc asyncCheckPermissionsToJoin*(self: Controller, communityId: string, addressesToShare: seq[string]) =
self.communityService.asyncCheckPermissionsToJoin(communityId, addressesToShare)
proc asyncCheckPermissionsToJoin*(self: Controller, communityId: string, addressesToShare: seq[string], requestId: CommunityPermissionsCheckRequestID) =
self.communityService.asyncCheckPermissionsToJoin(communityId, addressesToShare, requestId)

proc asyncCheckAllChannelsPermissions*(self: Controller, communityId: string, sharedAddresses: seq[string]) =
self.chatService.asyncCheckAllChannelsPermissions(communityId, sharedAddresses)
proc asyncCheckAllChannelsPermissions*(self: Controller, communityId: string, sharedAddresses: seq[string], requestId: CommunityPermissionsCheckRequestID) =
self.chatService.asyncCheckAllChannelsPermissions(communityId, sharedAddresses, requestId)

proc asyncGetRevealedAccountsForMember*(self: Controller, communityId, memberPubkey: string) =
self.communityService.asyncGetRevealedAccountsForMember(communityId, memberPubkey)
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/main/communities/io_interface.nim
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ method prepareTokenModelForCommunity*(self: AccessInterface, communityId: string
method getCommunityPublicKeyFromPrivateKey*(self: AccessInterface, communityPrivateKey: string): string {.base.} =
raise newException(ValueError, "No implementation available")

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

method onCommunityCheckPermissionsToJoinResponse*(self: AccessInterface, communityId: string, checkPermissionsToJoinResponse: CheckPermissionsToJoinResponseDto) {.base.} =
Expand Down
10 changes: 6 additions & 4 deletions src/app/modules/main/communities/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -863,13 +863,15 @@ 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]) =
method checkPermissions*(self: Module, communityId: string, sharedAddresses: seq[string], temporary: bool) =
self.joiningCommunityDetails.communityIdForPermissions = communityId

self.controller.asyncCheckPermissionsToJoin(communityId, sharedAddresses)
let requestId = if temporary: CommunityPermissionsCheckRequestID.SharedAddressesCheck else: CommunityPermissionsCheckRequestID.GeneralCommunity

self.controller.asyncCheckPermissionsToJoin(communityId, sharedAddresses, requestId)
self.checkingPermissionToJoinInProgress = true

self.controller.asyncCheckAllChannelsPermissions(communityId, sharedAddresses)
self.controller.asyncCheckAllChannelsPermissions(communityId, sharedAddresses, requestId)
self.checkingAllChannelPermissionsInProgress = true

self.view.setCheckingPermissionsInProgress(inProgress = true)
Expand All @@ -887,7 +889,7 @@ method prepareTokenModelForCommunity*(self: Module, communityId: string) =
tokenPermissionsItems.add(tokenPermissionItem)

self.view.spectatedCommunityPermissionModel.setItems(tokenPermissionsItems)
self.checkPermissions(communityId, @[])
self.checkPermissions(communityId, @[], false)

proc applyPermissionResponse*(self: Module, communityId: string, permissions: Table[string, CheckPermissionsResultDto]) =
let community = self.controller.getCommunityById(communityId)
Expand Down
4 changes: 2 additions & 2 deletions src/app/modules/main/communities/view.nim
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,10 @@ QtObject:
proc joinCommunityOrEditSharedAddresses*(self: View) {.slot.} =
self.delegate.joinCommunityOrEditSharedAddresses()

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

Expand Down
3 changes: 3 additions & 0 deletions src/app_service/service/chat/async_tasks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type
AsyncCheckAllChannelsPermissionsTaskArg = ref object of QObjectTaskArg
communityId: string
addresses: seq[string]
requestId: CommunityPermissionsCheckRequestID

const asyncCheckAllChannelsPermissionsTask: Task = proc(argEncoded: string) {.gcsafe, nimcall.} =
let arg = decode[AsyncCheckAllChannelsPermissionsTaskArg](argEncoded)
Expand All @@ -53,10 +54,12 @@ const asyncCheckAllChannelsPermissionsTask: Task = proc(argEncoded: string) {.gc
arg.finish(%* {
"response": response,
"communityId": arg.communityId,
"requestId": arg.requestId,
"error": "",
})
except Exception as e:
arg.finish(%* {
"communityId": arg.communityId,
"requestId": arg.requestId,
"error": e.msg,
})
18 changes: 16 additions & 2 deletions src/app_service/service/chat/service.nim
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type
checkChannelPermissionsResponse*: CheckChannelPermissionsResponseDto

CheckAllChannelsPermissionsResponseArgs* = ref object of Args
requestId*: CommunityPermissionsCheckRequestID
communityId*: string
checkAllChannelsPermissionsResponse*: CheckAllChannelsPermissionsResponseDto

Expand Down Expand Up @@ -848,19 +849,28 @@ QtObject:
let errMsg = e.msg
error "error checking all channel permissions: ", errMsg

proc asyncCheckAllChannelsPermissions*(self: Service, communityId: string, addresses: seq[string]) =
proc asyncCheckAllChannelsPermissions*(self: Service, communityId: string, addresses: seq[string], requestId: CommunityPermissionsCheckRequestID) =
let arg = AsyncCheckAllChannelsPermissionsTaskArg(
tptr: cast[ByteAddress](asyncCheckAllChannelsPermissionsTask),
vptr: cast[ByteAddress](self.vptr),
slot: "onAsyncCheckAllChannelsPermissionsDone",
communityId: communityId,
addresses: addresses,
requestId: requestId
)
self.threadpool.start(arg)

proc onAsyncCheckAllChannelsPermissionsDone*(self: Service, rpcResponse: string) {.slot.} =
let rpcResponseObj = rpcResponse.parseJson
let communityId = rpcResponseObj{"communityId"}.getStr()

var requestId: CommunityPermissionsCheckRequestID
var requestIdInt: int
if (rpcResponseObj.getProp("requestId", requestIdInt) and
(requestIdInt >= ord(low(CommunityPermissionsCheckRequestID)) and
requestIdInt <= ord(high(CommunityPermissionsCheckRequestID)))):
requestId = CommunityPermissionsCheckRequestID(requestIdInt)

try:
if rpcResponseObj{"error"}.kind != JNull and rpcResponseObj{"error"}.getStr != "":
raise newException(CatchableError, rpcResponseObj["error"].getStr)
Expand All @@ -871,7 +881,11 @@ QtObject:

let checkAllChannelsPermissionsResponse = rpcResponseObj["response"]["result"].toCheckAllChannelsPermissionsResponseDto()
self.channelGroups[communityId].channelPermissions = checkAllChannelsPermissionsResponse
self.events.emit(SIGNAL_CHECK_ALL_CHANNELS_PERMISSIONS_RESPONSE, CheckAllChannelsPermissionsResponseArgs(communityId: communityId, checkAllChannelsPermissionsResponse: checkAllChannelsPermissionsResponse))
self.events.emit(SIGNAL_CHECK_ALL_CHANNELS_PERMISSIONS_RESPONSE, CheckAllChannelsPermissionsResponseArgs(
communityId: communityId,
checkAllChannelsPermissionsResponse: checkAllChannelsPermissionsResponse,
requestId: requestId
))
except Exception as e:
let errMsg = e.msg
error "error checking all channels permissions: ", errMsg
Expand Down
3 changes: 3 additions & 0 deletions src/app_service/service/community/async_tasks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ type
AsyncCheckPermissionsToJoinTaskArg = ref object of QObjectTaskArg
communityId: string
addresses: seq[string]
requestId: CommunityPermissionsCheckRequestID

const asyncCheckPermissionsToJoinTask: Task = proc(argEncoded: string) {.gcsafe, nimcall.} =
let arg = decode[AsyncCheckPermissionsToJoinTaskArg](argEncoded)
Expand All @@ -215,11 +216,13 @@ const asyncCheckPermissionsToJoinTask: Task = proc(argEncoded: string) {.gcsafe,
arg.finish(%* {
"response": response,
"communityId": arg.communityId,
"requestId": arg.requestId,
"error": "",
})
except Exception as e:
arg.finish(%* {
"communityId": arg.communityId,
"requestId": arg.requestId,
"error": e.msg,
})

Expand Down
5 changes: 5 additions & 0 deletions src/app_service/service/community/dto/community.nim
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ type
KickPending,
BannedWithAllMessagesDelete

type
CommunityPermissionsCheckRequestID* = enum
GeneralCommunity = 0,
SharedAddressesCheck

proc isBanned*(state: CommunityMemberPendingBanOrKick): bool =
return state == CommunityMemberPendingBanOrKick.Banned or state == CommunityMemberPendingBanOrKick.BannedWithAllMessagesDelete

Expand Down
17 changes: 14 additions & 3 deletions src/app_service/service/community/service.nim
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ type
currentChunk*: int

CheckPermissionsToJoinResponseArgs* = ref object of Args
requestId*: CommunityPermissionsCheckRequestID
communityId*: string
checkPermissionsToJoinResponse*: CheckPermissionsToJoinResponseDto

Expand Down Expand Up @@ -1601,19 +1602,28 @@ QtObject:
self.events.emit(SIGNAL_COMMUNITY_DATA_IMPORTED, CommunityArgs(community: community))
self.events.emit(SIGNAL_COMMUNITIES_UPDATE, CommunitiesArgs(communities: @[community]))

proc asyncCheckPermissionsToJoin*(self: Service, communityId: string, addresses: seq[string]) =
proc asyncCheckPermissionsToJoin*(self: Service, communityId: string, addresses: seq[string], requestId: CommunityPermissionsCheckRequestID) =
let arg = AsyncCheckPermissionsToJoinTaskArg(
tptr: cast[ByteAddress](asyncCheckPermissionsToJoinTask),
vptr: cast[ByteAddress](self.vptr),
slot: "onAsyncCheckPermissionsToJoinDone",
communityId: communityId,
addresses: addresses
addresses: addresses,
requestId: requestId
)
self.threadpool.start(arg)

proc onAsyncCheckPermissionsToJoinDone*(self: Service, rpcResponse: string) {.slot.} =
let rpcResponseObj = rpcResponse.parseJson
let communityId = rpcResponseObj{"communityId"}.getStr()

var requestId: CommunityPermissionsCheckRequestID
var requestIdInt: int
if (rpcResponseObj.getProp("requestId", requestIdInt) and
(requestIdInt >= ord(low(CommunityPermissionsCheckRequestID)) and
requestIdInt <= ord(high(CommunityPermissionsCheckRequestID)))):
requestId = CommunityPermissionsCheckRequestID(requestIdInt)

try:
if rpcResponseObj{"error"}.kind != JNull and rpcResponseObj{"error"}.getStr != "":
raise newException(CatchableError, rpcResponseObj["error"].getStr)
Expand All @@ -1625,7 +1635,8 @@ QtObject:
let checkPermissionsToJoinResponse = rpcResponseObj["response"]["result"].toCheckPermissionsToJoinResponseDto
self.events.emit(SIGNAL_CHECK_PERMISSIONS_TO_JOIN_RESPONSE, CheckPermissionsToJoinResponseArgs(
communityId: communityId,
checkPermissionsToJoinResponse: checkPermissionsToJoinResponse
checkPermissionsToJoinResponse: checkPermissionsToJoinResponse,
requestId: requestId
))
except Exception as e:
let errMsg = e.msg
Expand Down
2 changes: 1 addition & 1 deletion ui/app/AppLayouts/Chat/stores/RootStore.qml
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,6 @@ QtObject {
}

function updatePermissionsModel(communityId, sharedAddresses) {
communitiesModuleInst.checkPermissions(communityId, JSON.stringify(sharedAddresses))
communitiesModuleInst.checkPermissions(communityId, JSON.stringify(sharedAddresses), false)
}
}
2 changes: 1 addition & 1 deletion ui/app/AppLayouts/stores/RootStore.qml
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ QtObject {
}

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

function promoteSelfToControlNode(communityId) {
Expand Down

0 comments on commit c99e106

Please sign in to comment.