diff --git a/src/app_service/common/activity_center.nim b/src/app_service/common/activity_center.nim index 15d37007ea..b6ff9112c6 100644 --- a/src/app_service/common/activity_center.nim +++ b/src/app_service/common/activity_center.nim @@ -1,8 +1,16 @@ -import json - +import json, json_serialization import ../../app/core/eventemitter +include json_utils + const SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS* = "parseRawActivityCenterNotifications" type RawActivityCenterNotificationsArgs* = ref object of Args activityCenterNotifications*: JsonNode + +proc checkAndEmitACNotificationsFromResponse*(events: EventEmitter, activityCenterNotifications: JsonNode) = + if activityCenterNotifications == nil or activityCenterNotifications.kind == JNull: + return + + events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, + RawActivityCenterNotificationsArgs(activityCenterNotifications: activityCenterNotifications)) \ No newline at end of file diff --git a/src/app_service/service/activity_center/service.nim b/src/app_service/service/activity_center/service.nim index 925468f71c..75d02bbafa 100644 --- a/src/app_service/service/activity_center/service.nim +++ b/src/app_service/service/activity_center/service.nim @@ -1,11 +1,10 @@ -import NimQml, json, sequtils, chronicles, strutils, strutils, stint, sugar +import NimQml, json, sequtils, chronicles, strutils, strutils, stint, sugar, tables, json_serialization import ../../../app/core/eventemitter import ../../../app/core/[main] import ../../../app/core/tasks/[qt, threadpool] -import web3/ethtypes, web3/conversions, stew/byteutils, nimcrypto, json_serialization, chronicles -import json, tables, json_serialization +import web3/ethtypes, web3/conversions, stew/byteutils, nimcrypto import ../../../backend/backend import ../../../backend/response_type @@ -103,7 +102,7 @@ QtObject: self.events.on(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS) do(e: Args): let raw = RawActivityCenterNotificationsArgs(e) - if raw.activityCenterNotifications.kind != JNull: + if raw.activityCenterNotifications.len > 0: var activityCenterNotifications: seq[ActivityCenterNotificationDto] = @[] for notificationJson in raw.activityCenterNotifications: activityCenterNotifications.add(notificationJson.toActivityCenterNotificationDto) diff --git a/src/app_service/service/community/service.nim b/src/app_service/service/community/service.nim index 77580b948b..90423e4645 100644 --- a/src/app_service/service/community/service.nim +++ b/src/app_service/service/community/service.nim @@ -529,8 +529,7 @@ QtObject: if prev_community.isOwner and not community.isOwner: self.events.emit(SIGNAL_COMMUNITY_LOST_OWNERSHIP, CommunityIdArgs(communityId: community.id)) let response = tokens_backend.registerLostOwnershipNotification(community.id) - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: response.result["activityCenterNotifications"])) + checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) # If there's settings without `id` it means the original # signal didn't include actual communitySettings, hence we @@ -998,8 +997,6 @@ QtObject: proc leaveCommunity*(self: Service, communityId: string) = try: let response = status_go.leaveCommunity(communityId) - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: response.result["activityCenterNotifications"])) if response.error != nil: let error = Json.decode($response.error, RpcError) @@ -1018,6 +1015,7 @@ QtObject: self.messageService.resetMessageCursor(chat.id) self.events.emit(SIGNAL_COMMUNITY_LEFT, CommunityIdArgs(communityId: communityId)) + checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) except Exception as e: error "Error leaving community", msg = e.msg, communityId @@ -1626,8 +1624,7 @@ QtObject: raise newException(CatchableError, rpcResponseObj{"error"}.getStr) let rpcResponse = Json.decode($rpcResponseObj["response"], RpcResponse[JsonNode]) - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: rpcResponse.result["activityCenterNotifications"])) + checkAndEmitACNotificationsFromResponse(self.events, rpcResponse.result{"activityCenterNotifications"}) if not self.processRequestsToJoinCommunity(rpcResponse.result): raise newException(CatchableError, "no 'requestsToJoinCommunity' key in response") @@ -1718,9 +1715,8 @@ QtObject: self.events.emit(SIGNAL_COMMUNITY_MEMBER_APPROVED, CommunityMemberArgs(communityId: communityId, pubKey: userKey, requestId: requestId)) - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: - rpcResponseObj["response"]["result"]{"activityCenterNotifications"})) + let rpcResponse = Json.decode($rpcResponseObj["response"], RpcResponse[JsonNode]) + checkAndEmitACNotificationsFromResponse(self.events, rpcResponse.result{"activityCenterNotifications"}) except Exception as e: let errMsg = e.msg @@ -1986,9 +1982,8 @@ QtObject: community.pendingRequestsToJoin.delete(i) self.communities[communityId] = community - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: response.result["activityCenterNotifications"])) self.events.emit(SIGNAL_REQUEST_TO_JOIN_COMMUNITY_CANCELED, Args()) + checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) return i.inc() @@ -1998,15 +1993,13 @@ QtObject: proc declineRequestToJoinCommunity*(self: Service, communityId: string, requestId: string) = try: let response = status_go.declineRequestToJoinCommunity(requestId) - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: response.result["activityCenterNotifications"])) - let requestToJoin = response.result["requestsToJoinCommunity"][0].toCommunityMembershipRequestDto self.updateMembershipRequestToNewState(communityId, requestId, self.communities[communityId], RequestToJoinType(requestToJoin.state)) self.events.emit(SIGNAL_COMMUNITY_EDITED, CommunityArgs(community: self.communities[communityId])) + checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) except Exception as e: error "Error declining request to join community", msg = e.msg diff --git a/src/app_service/service/community_tokens/service.nim b/src/app_service/service/community_tokens/service.nim index bb612a6219..e6355fc388 100644 --- a/src/app_service/service/community_tokens/service.nim +++ b/src/app_service/service/community_tokens/service.nim @@ -297,9 +297,9 @@ QtObject: self.events.emit(SIGNAL_OWNER_TOKEN_RECEIVED, tokenReceivedArgs) let finaliseStatusArgs = FinaliseOwnershipStatusArgs(isPending: true, communityId: communityId) self.events.emit(SIGNAL_FINALISE_OWNERSHIP_STATUS, finaliseStatusArgs) + let response = tokens_backend.registerOwnerTokenReceivedNotification(communityId) - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: response.result["activityCenterNotifications"])) + checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) except Exception as e: error "Error registering owner token received notification", msg=e.msg @@ -320,9 +320,10 @@ QtObject: transactionHash: transactionArgs.transactionHash, communityId: contractDetails.communityId) self.events.emit(SIGNAL_SET_SIGNER_STATUS, data) + let response = if transactionArgs.success: tokens_backend.registerReceivedOwnershipNotification(contractDetails.communityId) else: tokens_backend.registerSetSignerFailedNotification(contractDetails.communityId) - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: response.result["activityCenterNotifications"])) + checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) + let notificationToSetRead = self.acService.getNotificationForTypeAndCommunityId(notification.ActivityCenterNotificationType.OwnerTokenReceived, contractDetails.communityId) if notificationToSetRead != nil: self.acService.markActivityCenterNotificationRead(notificationToSetRead.id) @@ -1282,8 +1283,7 @@ QtObject: discard self.acService.deleteActivityCenterNotifications(@[notification.id]) try: let response = tokens_backend.registerSetSignerDeclinedNotification(communityId) - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: response.result["activityCenterNotifications"])) + checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) except Exception as e: error "Error registering decline set signer notification", msg=e.msg let finaliseStatusArgs = FinaliseOwnershipStatusArgs(isPending: false, communityId: communityId) diff --git a/src/app_service/service/contacts/service.nim b/src/app_service/service/contacts/service.nim index d06897a056..ef73e31e3f 100644 --- a/src/app_service/service/contacts/service.nim +++ b/src/app_service/service/contacts/service.nim @@ -470,9 +470,8 @@ QtObject: return self.parseContactsResponse(response) - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: response.result["activityCenterNotifications"])) self.events.emit(SIGNAL_RELOAD_ONE_TO_ONE_CHAT, ReloadOneToOneArgs(sectionId: publicKey)) + checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) except Exception as e: error "an error occurred while sending contact request", msg = e.msg @@ -491,9 +490,8 @@ QtObject: return self.parseContactsResponse(response) - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: response.result["activityCenterNotifications"])) self.events.emit(SIGNAL_RELOAD_ONE_TO_ONE_CHAT, ReloadOneToOneArgs(sectionId: publicKey)) + checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) except Exception as e: error "an error occurred while accepting contact request", msg=e.msg @@ -512,8 +510,7 @@ QtObject: return self.parseContactsResponse(response) - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: response.result["activityCenterNotifications"])) + checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) except Exception as e: error "an error occurred while dismissing contact request", msg=e.msg @@ -561,8 +558,7 @@ QtObject: self.events.emit(SIGNAL_RELOAD_ONE_TO_ONE_CHAT, ReloadOneToOneArgs(sectionId: publicKey)) self.parseContactsResponse(response) - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: response.result["activityCenterNotifications"])) + checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) proc ensResolved*(self: Service, jsonObj: string) {.slot.} = let jsonObj = jsonObj.parseJson() @@ -620,16 +616,15 @@ QtObject: if not response.error.isNil: let msg = response.error.message raise newException(RpcException, msg) - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: response.result["activityCenterNotifications"])) if self.contacts.hasKey(publicKey): self.contacts[publicKey].dto.trustStatus = TrustStatus.Trusted self.contacts[publicKey].dto.verificationStatus = VerificationStatus.Trusted - self.events.emit(SIGNAL_CONTACT_TRUSTED, - TrustArgs(publicKey: publicKey, isUntrustworthy: false)) + self.events.emit(SIGNAL_CONTACT_TRUSTED, TrustArgs(publicKey: publicKey, isUntrustworthy: false)) self.events.emit(SIGNAL_CONTACT_VERIFIED, ContactArgs(contactId: publicKey)) + checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) + except Exception as e: error "error verified trusted request", msg=e.msg @@ -646,16 +641,15 @@ QtObject: if not response.error.isNil: let msg = response.error.message raise newException(RpcException, msg) - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: response.result["activityCenterNotifications"])) if self.contacts.hasKey(publicKey): self.contacts[publicKey].dto.trustStatus = TrustStatus.Untrustworthy self.contacts[publicKey].dto.verificationStatus = VerificationStatus.Untrustworthy - self.events.emit(SIGNAL_CONTACT_UNTRUSTWORTHY, - TrustArgs(publicKey: publicKey, isUntrustworthy: true)) + self.events.emit(SIGNAL_CONTACT_UNTRUSTWORTHY, TrustArgs(publicKey: publicKey, isUntrustworthy: true)) self.events.emit(SIGNAL_CONTACT_VERIFIED, ContactArgs(contactId: publicKey)) + checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) + except Exception as e: error "error verified untrustworthy request", msg=e.msg @@ -720,8 +714,8 @@ QtObject: self.saveContact(contact) self.events.emit(SIGNAL_CONTACT_VERIFICATION_SENT, ContactArgs(contactId: publicKey)) - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: response.result["activityCenterNotifications"])) + checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) + except Exception as e: error "Error sending verification request", msg = e.msg @@ -744,8 +738,8 @@ QtObject: self.saveContact(contact) self.events.emit(SIGNAL_CONTACT_VERIFICATION_CANCELLED, ContactArgs(contactId: publicKey)) - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: response.result["activityCenterNotifications"])) + checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) + except Exception as e: error "Error canceling verification request", msg = e.msg @@ -765,10 +759,9 @@ QtObject: request.repliedAt = getTime().toUnix * 1000 self.receivedIdentityRequests[publicKey] = request - self.events.emit(SIGNAL_CONTACT_VERIFICATION_ACCEPTED, - VerificationRequestArgs(verificationRequest: request)) - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: response.result["activityCenterNotifications"])) + self.events.emit(SIGNAL_CONTACT_VERIFICATION_ACCEPTED, VerificationRequestArgs(verificationRequest: request)) + checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) + except Exception as e: error "error accepting contact verification request", msg=e.msg @@ -787,8 +780,8 @@ QtObject: self.receivedIdentityRequests[publicKey] = request self.events.emit(SIGNAL_CONTACT_VERIFICATION_DECLINED, ContactArgs(contactId: publicKey)) - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: response.result["activityCenterNotifications"])) + checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) + except Exception as e: error "error declining contact verification request", msg=e.msg diff --git a/src/app_service/service/message/async_tasks.nim b/src/app_service/service/message/async_tasks.nim index ccfdf668ba..fc99cb1904 100644 --- a/src/app_service/service/message/async_tasks.nim +++ b/src/app_service/service/message/async_tasks.nim @@ -125,19 +125,16 @@ const asyncMarkAllMessagesReadTask: Task = proc(argEncoded: string) {.gcsafe, ni let response = status_go.markAllMessagesFromChatWithIdAsRead(arg.chatId) - var activityCenterNotifications: JsonNode + var activityCenterNotifications: JsonNode = newJObject() discard response.result.getProp("activityCenterNotifications", activityCenterNotifications) let responseJson = %*{ "chatId": arg.chatId, + "activityCenterNotifications": activityCenterNotifications, "error": response.error } - if activityCenterNotifications != nil: - responseJson["activityCenterNotifications"] = activityCenterNotifications - arg.finish(responseJson) -################################################# ################################################# # Async mark certain messages read @@ -158,7 +155,7 @@ const asyncMarkCertainMessagesReadTask: Task = proc(argEncoded: string) {.gcsafe var countWithMentions: int discard response.result.getProp("countWithMentions", countWithMentions) - var activityCenterNotifications: JsonNode + var activityCenterNotifications: JsonNode = newJObject() discard response.result.getProp("activityCenterNotifications", activityCenterNotifications) var error = "" @@ -170,12 +167,10 @@ const asyncMarkCertainMessagesReadTask: Task = proc(argEncoded: string) {.gcsafe "messagesIds": arg.messagesIds, "count": count, "countWithMentions": countWithMentions, + "activityCenterNotifications": activityCenterNotifications, "error": error } - if activityCenterNotifications != nil: - responseJson["activityCenterNotifications"] = activityCenterNotifications - arg.finish(responseJson) @@ -322,13 +317,11 @@ const asyncMarkMessageAsUnreadTask: Task = proc(argEncoded: string) {.gcsafe, ni try: let response = status_go.markMessageAsUnread(arg.chatId, arg.messageId) - var activityCenterNotifications: JsonNode + var activityCenterNotifications: JsonNode = newJObject() discard response.result.getProp("activityCenterNotifications", activityCenterNotifications) - if activityCenterNotifications != nil: - responseJson["activityCenterNotifications"] = activityCenterNotifications - responseJson["messagesCount"] = %response.result["count"] + responseJson["activityCenterNotifications"] = activityCenterNotifications responseJson["messagesWithMentionsCount"] = %response.result["countWithMentions"] if response.error != nil: diff --git a/src/app_service/service/message/service.nim b/src/app_service/service/message/service.nim index 9f105ceb41..e30d52967f 100644 --- a/src/app_service/service/message/service.nim +++ b/src/app_service/service/message/service.nim @@ -732,6 +732,9 @@ QtObject: proc onMarkAllMessagesRead*(self: Service, response: string) {.slot.} = let responseObj = response.parseJson + if (responseObj.kind != JObject): + self.finishAsyncSearchMessagesWithError("", "mark all messages read response is not an json object") + return var error: string discard responseObj.getProp("error", error) @@ -744,13 +747,7 @@ QtObject: let data = MessagesMarkedAsReadArgs(chatId: chatId, allMessagesMarked: true) self.events.emit(SIGNAL_MESSAGES_MARKED_AS_READ, data) - - var activityCenterNotifications: JsonNode - discard responseObj.getProp("activityCenterNotifications", activityCenterNotifications) - - if activityCenterNotifications != nil: - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: activityCenterNotifications)) + checkAndEmitACNotificationsFromResponse(self.events, responseObj{"activityCenterNotifications"}) proc markAllMessagesRead*(self: Service, chatId: string) = if (chatId.len == 0): @@ -798,13 +795,7 @@ QtObject: self.chatService.updateUnreadMessage(chatId, count, countWithMentions) self.events.emit(SIGNAL_MESSAGE_MARKED_AS_UNREAD, data) - - var activityCenterNotifications: JsonNode - discard responseObj.getProp("activityCenterNotifications", activityCenterNotifications) - - if activityCenterNotifications != nil: - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: activityCenterNotifications)) + checkAndEmitACNotificationsFromResponse(self.events, responseObj{"activityCenterNotifications"}) except Exception as e: error "error: ", procName="markMessageAsUnread", errName = e.name, errDesription = e.msg @@ -843,13 +834,7 @@ QtObject: messagesCount: count, messagesWithMentionsCount: countWithMentions) self.events.emit(SIGNAL_MESSAGES_MARKED_AS_READ, data) - - var activityCenterNotifications: JsonNode - discard responseObj.getProp("activityCenterNotifications", activityCenterNotifications) - - if activityCenterNotifications != nil: - self.events.emit(SIGNAL_PARSE_RAW_ACTIVITY_CENTER_NOTIFICATIONS, - RawActivityCenterNotificationsArgs(activityCenterNotifications: activityCenterNotifications)) + checkAndEmitACNotificationsFromResponse(self.events, responseObj{"activityCenterNotifications"}) proc markCertainMessagesRead*(self: Service, chatId: string, messagesIds: seq[string]) = if (chatId.len == 0):