Skip to content

Commit

Permalink
Merge pull request #8506 from OpenMined/feature/enable_notifications_…
Browse files Browse the repository at this point in the history
…via_settings

Enable notifications via settings api
  • Loading branch information
jcardonnet committed Feb 19, 2024
2 parents 308726e + 8007878 commit 825f685
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 19 deletions.
54 changes: 35 additions & 19 deletions packages/syft/src/syft/service/notifier/notifier_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def turn_on(
notifier = result.ok()
# 2 - If email token is not provided and notifier doesn't exist, return an error
if not email_token and not notifier.email_token:
return SyftError(message="Email token is required to turn on the notifier")
return SyftError(
message="No valid token has been added to the domain."
+ "You can add a new token via client.settings.enable_notifications(token=TOKEN)"
)

# 3 - Activate the notifier
notifier.active = True
Expand All @@ -74,7 +77,7 @@ def turn_on(
result = self.stash.update(credentials=context.credentials, settings=notifier)
if result.is_err():
return SyftError(message=result.err())
return SyftSuccess(message="Notifier turned on")
return SyftSuccess(message="Notifications enabled successfully.")

@service_method(path="notifier.turn_off", name="turn_off", roles=ADMIN_ROLE_LEVEL)
def turn_off(
Expand All @@ -91,35 +94,45 @@ def turn_off(
result = self.stash.update(credentials=context.credentials, settings=notifier)
if result.is_err():
return SyftError(message=result.err())
return SyftSuccess(message="Notifier turned off")
return SyftSuccess(message="Notifications disabled succesfullly")

@service_method(
path="notifier.enable_notifications",
name="enable_notifications",
path="notifier.activate",
name="activate",
roles=DATA_SCIENTIST_ROLE_LEVEL,
)
def enable_notifications(
def activate(
self,
context: AuthedServiceContext,
) -> Union[SyftSuccess, SyftError]:
return SyftError(message="Not Implemented")
# Enable current account notifier notifications
# (Notifications for this user will still be saved in Notifications Service)
# Store the current notifications state in the stash
result = self.stash.get(credentials=context.credentials)

if result.is_err():
return SyftError(message=result.err())

notifier = result.ok()
if notifier.active:
return SyftSuccess(
message="Successfully activated notifications via email."
)
else:
return SyftError(message="Notifications are disabled by the domain owner.")

@service_method(
path="notifier.disable_notifications",
name="disable_notifications",
path="notifier.deactivate",
name="deactivate",
roles=DATA_SCIENTIST_ROLE_LEVEL,
)
def disable_notifications(
def deactivate(
self,
context: AuthedServiceContext,
) -> Union[SyftSuccess, SyftError]:
return SyftError(message="Not Implemented")
# Enable current account notifier notifications
# (Notifications for this user will still be saved in Notifications Service)
# Store the current notifications state in the stash
result = self.stash.get(credentials=context.credentials)

if result.is_err():
return SyftError(message=result.err())

return SyftSuccess(message="Successfully deactivated notifications via email.")

@staticmethod
def init_notifier(
Expand Down Expand Up @@ -163,11 +176,14 @@ def init_notifier(
# This method is used by other services to dispatch notifications internally
def dispatch_notification(
self, node: AbstractNode, notification: Notification
) -> Union[SyftSuccess, SyftError]:
) -> Union[SyftError]:
admin_key = node.get_service("userservice").admin_verify_key()
notifier = self.stash.get(admin_key)
if notifier.is_err():
return SyftError(message=notifier.err())
return SyftError(
message="The mail service ran out of quota or some notifications failed to be delivered.\n"
+ "Please check the health of the mailing server."
)

notifier: NotifierSettings = notifier.ok()
# If notifier is active
Expand Down
25 changes: 25 additions & 0 deletions packages/syft/src/syft/service/settings/settings_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# stdlib

# stdlib
from typing import Optional
from typing import Union

# third party
Expand All @@ -18,6 +19,7 @@
from ..response import SyftSuccess
from ..service import AbstractService
from ..service import service_method
from ..user.user_roles import ADMIN_ROLE_LEVEL
from ..warnings import HighSideCRUDWarning
from .settings import NodeSettingsUpdate
from .settings import NodeSettingsV2
Expand Down Expand Up @@ -80,6 +82,29 @@ def update(
else:
return SyftError(message=result.err())

@service_method(
path="settings.enable_notifications",
name="enable_notifications",
roles=ADMIN_ROLE_LEVEL,
)
def enable_notifications(
self, context: AuthedServiceContext, token: Optional[str] = None
) -> Union[SyftSuccess, SyftError]:
notifier_service = context.node.get_service("notifierservice")
return notifier_service.turn_on(context=context, email_token=token)

@service_method(
path="settings.disable_notifications",
name="disable_notifications",
roles=ADMIN_ROLE_LEVEL,
)
def disable_notifications(
self,
context: AuthedServiceContext,
) -> Union[SyftSuccess, SyftError]:
notifier_service = context.node.get_service("notifierservice")
return notifier_service.turn_off(context=context)

@service_method(
path="settings.allow_guest_signup",
name="allow_guest_signup",
Expand Down

0 comments on commit 825f685

Please sign in to comment.