refactor(osnotification): os notification service updated

- os notification doesn't belong to core features so it's moved from there
- os notification service updated and read to be used as other service
- leftovers moved from `status-lib` that's its version is updated
- using os notification service is commented out in the old code
This commit is contained in:
Sale Djenic 2021-11-17 10:16:10 +01:00
parent a99422a00a
commit 4b882be8bd
8 changed files with 127 additions and 73 deletions

View File

@ -1,5 +1,6 @@
import NimQml, os, strformat
import ../../app_service/service/os_notification/service as os_notification_service
import ../../app_service/service/keychain/service as keychain_service
import ../../app_service/service/accounts/service as accounts_service
import ../../app_service/service/contacts/service as contacts_service
@ -68,6 +69,7 @@ type
AppController* = ref object of RootObj
appService: AppService
# Services
osNotificationService: os_notification_service.Service
keychainService: keychain_service.Service
accountsService: accounts_service.Service
contactsService: contacts_service.Service
@ -133,6 +135,7 @@ proc newAppController*(appService: AppService): AppController =
result = AppController()
result.appService = appService
# Services
result.osNotificationService = os_notification_service.newService(appService.status.events)
result.keychainService = keychain_service.newService(appService.status.events)
result.settingService = setting_service.newService()
result.settingsService = settings_service.newService()
@ -217,6 +220,7 @@ proc newAppController*(appService: AppService): AppController =
#result.connect()
proc delete*(self: AppController) =
self.osNotificationService.delete
self.contactsService.delete
self.chatService.delete
self.communityService.delete

View File

@ -2,7 +2,7 @@ import NimQml, chronicles, tables
import status/chat as chat_model
import status/messages as messages_model
import status/[chat, contacts, status, wallet, stickers, settings]
import status/types/[message, transaction, os_notification, setting]
import status/types/[message, transaction, setting]
import ../core/[main]
import view, views/channels_list, views/message_list, views/reactions, views/stickers as stickers_view
import eventemitter

View File

@ -195,6 +195,8 @@ proc handleMailserverEvents(self: ChatController) =
mailserverWorker.start(task)
proc handleSystemEvents(self: ChatController) =
self.status.events.on("osNotificationClicked") do(e:Args):
let arg = OsNotificationsArgs(e)
self.view.onOsNotificationClicked(arg.details)
discard
# Not refactored yet - don't delete
# self.status.events.on("osNotificationClicked") do(e:Args):
# let arg = OsNotificationsArgs(e)
# self.view.onOsNotificationClicked(arg.details)

View File

@ -6,11 +6,10 @@ import status/messages as status_messages
import status/contacts as status_contacts
import status/ens as status_ens
import status/chat/[chat]
import status/types/[activity_center_notification, os_notification, rpc_response, profile]
import status/types/[activity_center_notification, rpc_response, profile]
import ../core/[main]
import ../core/tasks/[qt, threadpool]
import ../core/tasks/marathon/mailserver/worker
import status/notifications/[os_notifications]
import ../utils/image_utils
import web3/[conversions, ethtypes]
import views/[channels_list, message_list, chat_item, reactions, stickers, groups, transactions, communities, community_list, community_item, format_input, ens, activity_notification_list, channel, messages, message_item, gif]
@ -497,36 +496,45 @@ QtObject:
# self.switchTo(info.communityId, info.channelId, info.messageId)
#################################################
## Don't delete this part, it's not refactored yet, just commented
## OsNotificationService is ready, but not not put in a play.
##
##
proc notificationClicked*(self:ChatsView, notificationType: int) {.signal.}
proc onOsNotificationClicked*(self: ChatsView, details: OsNotificationDetails) =
# A logic what should be done depends on details.notificationType and should be
# defined here in this method.
# So far if notificationType is:
# - NewContactRequest or AcceptedContactRequest we are switching to Chat section
# - JoinCommunityRequest or AcceptedIntoCommunity we are switching to that Community
# - RejectedByCommunity we are switching to Chat section
# - NewMessage we are switching to appropriate chat/channel and a message inside it
# proc onOsNotificationClicked*(self: ChatsView, details: OsNotificationDetails) =
# # A logic what should be done depends on details.notificationType and should be
# # defined here in this method.
# # So far if notificationType is:
# # - NewContactRequest or AcceptedContactRequest we are switching to Chat section
# # - JoinCommunityRequest or AcceptedIntoCommunity we are switching to that Community
# # - RejectedByCommunity we are switching to Chat section
# # - NewMessage we are switching to appropriate chat/channel and a message inside it
self.switchTo(details.communityId, details.channelId, details.messageId)
# self.switchTo(details.communityId, details.channelId, details.messageId)
# Notify qml about the changes, cause changing section cannot be performed
# completely from the nim side.
self.notificationClicked(details.notificationType.int)
# # Notify qml about the changes, cause changing section cannot be performed
# # completely from the nim side.
# self.notificationClicked(details.notificationType.int)
proc showOSNotification*(self: ChatsView, title: string, message: string,
notificationType: int, communityId: string, channelId: string,
messageId: string, useOSNotifications: bool) {.slot.} =
let details = OsNotificationDetails(
notificationType: notificationType.OsNotificationType,
communityId: communityId,
channelId: channelId,
messageId: messageId
)
proc showOSNotification*(self: ChatsView, title: string, message: string, notificationType: int, communityId: string,
channelId: string, messageId: string, useOSNotifications: bool) {.slot.} =
discard
# Not refactored yet
# let details = OsNotificationDetails(
# notificationType: notificationType.OsNotificationType,
# communityId: communityId,
# channelId: channelId,
# messageId: messageId
# )
self.appService.osNotificationService.showNotification(title, message,
details, useOSNotifications)
# Once this part gets refactored os notification service from the services will be used
# instead fetching that service from the "core/main"
#self.appService.osNotificationService.showNotification(title, message, details, useOSNotifications)
##
##
#################################################
proc handleProtocolUri*(self: ChatsView, uri: string) {.slot.} =
# for now this only supports links to 1-1 chats, e.g.

View File

@ -6,24 +6,18 @@ import
./tasks/threadpool,
./signals/signal_controller
import ../../app_service/service/os_notification/service as os_notification_service
export status_lib_status
export marathon, task_runner, signal_controller
export os_notification_service
logScope:
topics = "app-services"
type AppService* = ref object
type AppService* = ref object # AppService should be renamed to "Foundation"
status*: Status # in one point of time this should be completely removed
# foundation
threadpool*: ThreadPool
marathon*: Marathon
signalController*: SignalsController
# services
osNotificationService*: OsNotificationService
# async services
proc newAppService*(status: Status, worker: MarathonWorker): AppService =
result = AppService()
@ -31,13 +25,11 @@ proc newAppService*(status: Status, worker: MarathonWorker): AppService =
result.threadpool = newThreadPool()
result.marathon = newMarathon(worker)
result.signalController = newSignalsController(status)
result.osNotificationService = newOsNotificationService(status)
proc delete*(self: AppService) =
self.threadpool.teardown()
self.marathon.teardown()
self.signalController.delete()
self.osNotificationService.delete()
proc onLoggedIn*(self: AppService) =
self.marathon.onLoggedIn()

View File

@ -10,8 +10,7 @@ import status/profile as status_profile
import status/status
import status/ens as status_ens
import status/chat/chat
import status/types/[setting, os_notification, profile]
import status/notifications/[os_notifications]
import status/types/[setting, profile]
import ../chat/views/channels_list
import ../../constants
@ -173,15 +172,23 @@ QtObject:
QtProperty[QVariant] mutedChats:
read = getMutedChats
proc showOSNotification*(self: ProfileView, title: string, message: string,
notificationType: int, useOSNotifications: bool) {.slot.} =
proc setSendUserStatus*(self: ProfileView, sendUserStatus: bool) {.slot.} =
if (sendUserStatus == self.profile.sendUserStatus):
return
self.profile.setSendUserStatus(sendUserStatus)
self.status.saveSetting(Setting.SendUserStatus, sendUserStatus)
let details = OsNotificationDetails(
notificationType: notificationType.OsNotificationType
)
proc showOSNotification*(self: ProfileView, title: string, message: string, notificationType: int,
useOSNotifications: bool) {.slot.} =
discard
# Not refactored yet - don't delete
# let details = OsNotificationDetails(
# notificationType: notificationType.OsNotificationType
# )
self.appService.osNotificationService.showNotification(title, message,
details, useOSNotifications)
# Once this part gets refactored os notification service from the services will be used
# instead fetching that service from the "core/main"
#self.appService.osNotificationService.showNotification(title, message, details, useOSNotifications)
proc logDir*(self: ProfileView): string {.slot.} =
url_fromLocalFile(constants.LOGDIR)

View File

@ -0,0 +1,40 @@
{.used.}
import json
type
OsNotificationType* {.pure.} = enum
NewContactRequest = 1,
AcceptedContactRequest,
JoinCommunityRequest,
AcceptedIntoCommunity,
RejectedByCommunity,
NewMessage
OsNotificationDetails* = object
notificationType*: OsNotificationType
communityId*: string
channelId*: string
messageId*: string
proc toOsNotificationDetails*(json: JsonNode): OsNotificationDetails =
if (not (json.contains("notificationType") and
json.contains("communityId") and
json.contains("channelId") and
json.contains("messageId"))):
return OsNotificationDetails()
return OsNotificationDetails(
notificationType: json{"notificationType"}.getInt.OsNotificationType,
communityId: json{"communityId"}.getStr,
channelId: json{"channelId"}.getStr,
messageId: json{"messageId"}.getStr
)
proc toJsonNode*(self: OsNotificationDetails): JsonNode =
result = %* {
"notificationType": self.notificationType.int,
"communityId": self.communityId,
"channelId": self.channelId,
"messageId": self.messageId
}

View File

@ -1,33 +1,38 @@
import NimQml, json, chronicles
import status/[status]
import status/notifications/[os_notifications]
import status/types/[os_notification]
import details
import eventemitter
logScope:
topics = "os-notification-service"
QtObject:
type OsNotificationService* = ref object of QObject
status: Status
notification: StatusOSNotification
notificationSetUp: bool
# Signals which may be emitted by this service:
const SIGNAL_OS_NOTIFICATION_CLICKED* = "new-osNotificationClicked" #Once we are done with refactoring we should remove "new-" from all signals
proc setup(self: OsNotificationService, status: Status) =
type
OsNotificationsArgs* = ref object of Args
details*: OsNotificationDetails
QtObject:
type Service* = ref object of QObject
events: EventEmitter
notification: StatusOSNotification
proc setup(self: Service, events: EventEmitter) =
self.QObject.setup
self.status = status
self.events = events
self.notification = newStatusOSNotification()
signalConnect(self.notification, "notificationClicked(QString)", self, "onNotificationClicked(QString)", 2)
proc delete*(self: OsNotificationService) =
if self.notificationSetUp:
self.notification.delete
proc delete*(self: Service) =
self.notification.delete
self.QObject.delete
proc newOsNotificationService*(status: Status): OsNotificationService =
proc newService*(events: EventEmitter): Service =
new(result, delete)
result.setup(status)
result.setup(events)
proc showNotification*(self: OsNotificationService, title: string,
message: string, details: OsNotificationDetails, useOSNotifications: bool) =
proc showNotification*(self: Service, title: string, message: string, details: OsNotificationDetails,
useOSNotifications: bool) =
## This method will add new notification to the Notification center. Param
## "details" is used to uniquely define a notification bubble.
@ -41,12 +46,8 @@ QtObject:
let identifier = $(details.toJsonNode())
self.notification.showNotification(title, message, identifier)
proc onNotificationClicked*(self: OsNotificationService, identifier: string) {.slot.} =
proc onNotificationClicked*(self: Service, identifier: string) {.slot.} =
## This slot is called once user clicks a notificaiton bubble, "identifier"
## contains data which uniquely define that notification.
self.status.osnotifications.onNotificationClicked(identifier)
proc onLoggedIn*(self: OsNotificationService) =
self.notification = newStatusOSNotification()
self.notificationSetUp = true
signalConnect(self.notification, "notificationClicked(QString)", self, "onNotificationClicked(QString)", 2)
let details = toOsNotificationDetails(parseJson(identifier))
self.events.emit(SIGNAL_OS_NOTIFICATION_CLICKED, OsNotificationsArgs(details: details))