From e96455c146141bdb180787725a7e1a5df048994a Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Sun, 21 Feb 2021 17:20:06 +0100 Subject: [PATCH 01/54] start adding notifications from 3.0 package PR --- .../drivers/notification/BaseDriver.py | 32 +++ .../drivers/notification/BroadcastDriver.py | 73 ++++++ .../drivers/notification/DatabaseDriver.py | 42 ++++ .../drivers/notification/MailDriver.py | 66 ++++++ .../drivers/notification/SlackDriver.py | 215 ++++++++++++++++++ .../drivers/notification/VonageDriver.py | 112 +++++++++ src/masonite/drivers/notification/__init__.py | 6 + src/masonite/exceptions/exceptions.py | 4 + src/masonite/foundation/Kernel.py | 3 + .../notification/AnonymousNotifiable.py | 36 +++ .../notification/DatabaseNotification.py | 38 ++++ src/masonite/notification/Notifiable.py | 53 +++++ src/masonite/notification/Notification.py | 51 +++++ .../notification/NotificationManager.py | 151 ++++++++++++ src/masonite/notification/__init__.py | 3 + .../providers/NotificationProvider.py | 45 ++++ src/masonite/providers/__init__.py | 1 + .../features/notification/test_mail_driver.py | 50 ++++ tests/integrations/config/notification.py | 11 + tests/integrations/config/providers.py | 2 + 20 files changed, 994 insertions(+) create mode 100644 src/masonite/drivers/notification/BaseDriver.py create mode 100644 src/masonite/drivers/notification/BroadcastDriver.py create mode 100644 src/masonite/drivers/notification/DatabaseDriver.py create mode 100644 src/masonite/drivers/notification/MailDriver.py create mode 100644 src/masonite/drivers/notification/SlackDriver.py create mode 100644 src/masonite/drivers/notification/VonageDriver.py create mode 100644 src/masonite/drivers/notification/__init__.py create mode 100644 src/masonite/notification/AnonymousNotifiable.py create mode 100644 src/masonite/notification/DatabaseNotification.py create mode 100644 src/masonite/notification/Notifiable.py create mode 100644 src/masonite/notification/Notification.py create mode 100644 src/masonite/notification/NotificationManager.py create mode 100644 src/masonite/notification/__init__.py create mode 100644 src/masonite/providers/NotificationProvider.py create mode 100644 tests/features/notification/test_mail_driver.py create mode 100644 tests/integrations/config/notification.py diff --git a/src/masonite/drivers/notification/BaseDriver.py b/src/masonite/drivers/notification/BaseDriver.py new file mode 100644 index 00000000..35f42a8a --- /dev/null +++ b/src/masonite/drivers/notification/BaseDriver.py @@ -0,0 +1,32 @@ +from abc import abstractmethod +from abc import ABC + + +class BaseDriver(ABC): + @abstractmethod + def send(self, notifiable, notification): + """Implements sending the notification to notifiables through + this channel.""" + raise NotImplementedError( + "send() method must be implemented for a notification channel." + ) + + @abstractmethod + def queue(self, notifiable, notification): + """Implements queuing the notification to be sent later to notifiables through + this channel.""" + raise NotImplementedError( + "queue() method must be implemented for a notification channel." + ) + + def get_data(self, channel, notifiable, notification): + """Get the data for the notification.""" + method_name = "to_{0}".format(channel) + try: + method = getattr(notification, method_name) + except AttributeError: + raise NotImplementedError( + "Notification model should implement {}() method.".format(method_name) + ) + else: + return method(notifiable) diff --git a/src/masonite/drivers/notification/BroadcastDriver.py b/src/masonite/drivers/notification/BroadcastDriver.py new file mode 100644 index 00000000..c37b7f3f --- /dev/null +++ b/src/masonite/drivers/notification/BroadcastDriver.py @@ -0,0 +1,73 @@ +"""Broadcast driver Class.""" +from masonite.app import App +from masonite import Queue +from masonite.helpers import config +from masonite.drivers import BaseDriver + +from ..NotificationContract import NotificationContract +from ..exceptions import BroadcastOnNotImplemented + + +class BroadcastDriver(BaseDriver, NotificationContract): + _driver = None + + def __init__(self, app: App): + """Broadcast Driver Constructor.""" + self.app = app + self._driver = None + + def driver(self, driver): + """Specifies the driver to use. + + Arguments: + driver {string} -- The name of the driver. + + Returns: + self + """ + self._driver = driver + return self + + def send(self, notifiable, notification): + """Used to broadcast a notification.""" + channels, data, driver = self._prepare_message_to_broadcast(notifiable, notification) + for channel in channels: + driver.channel(channel, data) + + def queue(self, notifiable, notification): + """Used to queue the notification to be broadcasted.""" + channels, data, driver = self._prepare_message_to_broadcast(notifiable, notification) + for channel in channels: + self.app.make(Queue).push(driver.channel, args=(channel, data)) + + def _prepare_message_to_broadcast(self, notifiable, notification): + data = self.get_data("broadcast", notifiable, notification) + driver_instance = self.get_broadcast_driver() + channels = self.broadcast_on(notifiable, notification) + return channels, data, driver_instance + + def get_broadcast_driver(self): + """Shortcut method to get given broadcast driver instance.""" + driver = config("broadcast.driver") if not self._driver else None + return self.app.make("BroadcastManager").driver(driver) + + def broadcast_on(self, notifiable, notification): + """Get the channels the notification should be broadcasted on.""" + channels = notification.broadcast_on() + if not channels: + from ..AnonymousNotifiable import AnonymousNotifiable + + if isinstance(notifiable, AnonymousNotifiable): + channels = notifiable.route_notification_for("broadcast", notification) + else: + try: + channels = notifiable.receives_broadcast_notifications_on() + except AttributeError: + raise BroadcastOnNotImplemented( + """No broadcast channels defined for the Notification with broadcast_on(), + receives_broadcast_notifications_on() must be defined on the Notifiable.""" + ) + + if isinstance(channels, str): + channels = [channels] + return channels diff --git a/src/masonite/drivers/notification/DatabaseDriver.py b/src/masonite/drivers/notification/DatabaseDriver.py new file mode 100644 index 00000000..636a176c --- /dev/null +++ b/src/masonite/drivers/notification/DatabaseDriver.py @@ -0,0 +1,42 @@ +"""Database driver Class.""" +import json +from masonite.app import App +from masonite import Queue +from masonite.drivers import BaseDriver + +from ..NotificationContract import NotificationContract +from ..models import DatabaseNotification + + +class DatabaseDriver(BaseDriver, NotificationContract): + def __init__(self, app: App): + """Database Driver Constructor.""" + self.app = app + + def send(self, notifiable, notification): + """Used to send the email and run the logic for sending emails.""" + model_data = self.build_payload(notifiable, notification) + return DatabaseNotification.create(model_data) + + def queue(self, notifiable, notification): + """Used to queue the database notification creation.""" + model_data = self.build_payload(notifiable, notification) + return self.app.make(Queue).push( + DatabaseNotification.create, args=(model_data,) + ) + + def serialize_data(self, data): + return json.dumps(data) + + def build_payload(self, notifiable, notification): + """Build an array payload for the DatabaseNotification Model.""" + return { + "id": str(notification.id), + "type": notification.notification_type(), + "notifiable_id": notifiable.id, + "notifiable_type": notifiable.get_table_name(), + "data": self.serialize_data( + self.get_data("database", notifiable, notification) + ), + "read_at": None, + } diff --git a/src/masonite/drivers/notification/MailDriver.py b/src/masonite/drivers/notification/MailDriver.py new file mode 100644 index 00000000..93d91ec3 --- /dev/null +++ b/src/masonite/drivers/notification/MailDriver.py @@ -0,0 +1,66 @@ +"""Mail driver Class.""" + +from ...exceptions.exceptions import NotificationException +from .BaseDriver import BaseDriver + + +class MailDriver(BaseDriver): + def __init__(self, application): + self.application = application + self.options = {} + + def set_options(self, options): + self.options = options + return self + + def send(self, notifiable, notification): + """Used to send the email.""" + # return method(*method_args) + mailable = self.get_data("mail", notifiable, notification) + # check that if no _to has been defined specify it + if not mailable._to: + recipients = self.get_recipients(notifiable, notification) + mailable = mailable.to(recipients) + # TODO: allow changing driver how ????? + return self.application.make("mail").mailable(mailable).send(driver="terminal") + + def queue(self, notifiable, notification): + """Used to queue the email to send.""" + # return method(*method_args) + mailable = self.get_data("mail", notifiable, notification) + # check that if no _to has been defined specify it + if not mailable._to: + recipients = self.get_recipients(notifiable, notification) + mailable = mailable.to(recipients) + # TODO: allow changing driver + return self.application.make("queue").push( + self.application.make("mail").mailable(mailable).send, driver="async" + ) + + def get_recipients(self, notifiable, notification): + """Get recipients which can be defined through notifiable route method. + return email + return {email: name} + return [email1, email2] + return [{email1: ''}, {email2: name2}] + """ + # TODO: use Recipient from M4 + recipients = notifiable.route_notification_for("mail", notification) + # multiple recipients + if isinstance(recipients, list): + _recipients = [] + for recipient in recipients: + _recipients.append(self._format_address(recipient)) + else: + _recipients = [self._format_address(recipients)] + return _recipients + + def _format_address(self, recipient): + if isinstance(recipient, str): + return recipient + elif isinstance(recipient, tuple): + if len(recipient) != 2 or not recipient[1]: + raise NotificationException( + "route_notification_for_mail() should return a string or a tuple (email, name)" + ) + return "{1} <{0}>".format(*recipient) diff --git a/src/masonite/drivers/notification/SlackDriver.py b/src/masonite/drivers/notification/SlackDriver.py new file mode 100644 index 00000000..8af50e67 --- /dev/null +++ b/src/masonite/drivers/notification/SlackDriver.py @@ -0,0 +1,215 @@ +"""Slack driver Class""" +import requests +from masonite.app import App +from masonite.drivers import BaseDriver +from masonite.helpers import config +from masonite.managers.QueueManager import Queue + +from ..exceptions import ( + SlackChannelNotFound, + SlackInvalidMessage, + SlackInvalidWorkspace, + SlackChannelArchived, + SlackInvalidWebhook, + SlackAPIError +) +from ..NotificationContract import NotificationContract + + +class SlackDriver(BaseDriver, NotificationContract): + + app = None + WEBHOOK_MODE = 1 + API_MODE = 2 + sending_mode = WEBHOOK_MODE + + def __init__(self, app: App): + """Slack Driver Constructor. + + Arguments: + app {masonite.app.App} -- The Masonite container object. + """ + self.app = app + self._debug = False + self._token = config("notifications.slack.token", None) + + def send(self, notifiable, notification): + """Used to send the notification to slack.""" + method, method_args = self._prepare_slack_message(notifiable, notification) + return method(*method_args) + + def queue(self, notifiable, notification): + """Used to queue the notification to be sent to slack.""" + method, method_args = self._prepare_slack_message(notifiable, notification) + return self.app.make(Queue).push(method, args=method_args) + + def _prepare_slack_message(self, notifiable, notification): + """Prepare slack message to be sent.""" + data = self.get_data("slack", notifiable, notification) + recipients = self.get_recipients(notifiable, notification) + if self.sending_mode == self.WEBHOOK_MODE: + send_method = self.send_via_webhook + else: + send_method = self.send_via_api + return send_method, (data, recipients) + + def get_recipients(self, notifiable, notification): + """Get recipients which can be defined through notifiable route method. + For Slack it can be: + - an incoming webhook (or a list of incoming webhooks) that you defined in your app + return webhook_url + return [webhook_url_1, webhook_url_2] + - a channel name or ID (it will use Slack API and requires a Slack token + for accessing your workspace) + return "{channel_name or channel_id}" + return [channel_name_1, channel_name_2] + You cannot mix both. + """ + recipients = notifiable.route_notification_for("slack", notification) + if isinstance(recipients, list): + _modes = [] + for recipient in recipients: + _modes.append(self._check_recipient_type(recipient)) + if self.API_MODE in _modes and self.WEBHOOK_MODE in _modes: + raise ValueError( + "NotificationSlackDriver: sending mode cannot be mixed." + ) + mode = _modes[0] + else: + mode = self._check_recipient_type(recipients) + recipients = [recipients] + + self.sending_mode = mode + return recipients + + def _check_recipient_type(self, recipient): + if recipient.startswith("https://hooks.slack.com"): + return self.WEBHOOK_MODE + else: + return self.API_MODE + + def send_via_webhook(self, payload, webhook_urls): + data = payload.as_dict() + if self._debug: + print(data) + for webhook_url in webhook_urls: + response = requests.post( + webhook_url, data=data, headers={"Content-Type": "application/json"} + ) + if response.status_code != 200: + self._handle_webhook_error(response, data) + + def send_via_api(self, payload, channels): + """Send Slack notification with Slack Web API as documented + here https://api.slack.com/methods/chat.postMessage""" + for channel in channels: + # if notification._as_snippet: + # requests.post('https://slack.com/api/files.upload', { + # 'token': notification._token, + # 'channel': channel, + # 'content': notification._text, + # 'filename': notification._snippet_name, + # 'filetype': notification._type, + # 'initial_comment': notification._initial_comment, + # 'title': notification._title, + # }) + # else: + # use notification defined token else globally configured token + token = payload._token or self._token + channel = self._get_channel_id(channel, token) + payload = { + **payload.as_dict(), + # mandatory + "token": token, + "channel": channel, + } + if self._debug: + print(payload) + self._call_slack_api("https://slack.com/api/chat.postMessage", payload) + + def _call_slack_api(self, url, payload): + response = requests.post(url, payload) + data = response.json() + if not data["ok"]: + self._raise_related_error(data["error"], payload) + else: + return data + + def _handle_webhook_error(self, response, payload): + self._raise_related_error(response.text, payload) + + def _raise_related_error(self, error_key, payload): + if error_key == "invalid_payload": + raise SlackInvalidMessage( + "The message is malformed: perhaps the JSON is structured incorrectly, or the message text is not properly escaped." + ) + elif error_key == "invalid_auth": + raise SlackAPIError( + "Some aspect of authentication cannot be validated. Either the provided token is invalid or the request originates from an IP address disallowed from making the request." + ) + elif error_key == "too_many_attachments": + raise SlackInvalidMessage( + "Too many attachments: the message can have a maximum of 100 attachments associated with it." + ) + elif error_key == "channel_not_found": + raise SlackChannelNotFound( + "The user or channel being addressed either do not exist or is invalid: {}".format( + payload["channel"] + ) + ) + elif error_key == "channel_is_archived": + raise SlackChannelArchived( + "The channel being addressed has been archived and is no longer accepting new messages: {}".format( + payload["channel"] + ) + ) + elif error_key in [ + "action_prohibited", + "posting_to_general_channel_denied", + ]: + raise SlackAPIError( + "You don't have the permission to post to this channel right now: {}".format( + payload["channel"] + ) + ) + elif error_key in ["no_service", "no_service_id"]: + raise SlackInvalidWebhook( + "The provided incoming webhook is either disabled, removed or invalid." + ) + elif error_key in ["no_team", "team_disabled"]: + raise SlackInvalidWorkspace( + "The Slack workspace is no longer active or is missing or invalid." + ) + else: + raise SlackAPIError("{}. Check Slack API docs.".format(error_key)) + + def _get_channel_id(self, name, token): + """"Returns Slack channel's ID from given channel.""" + if "#" in name: + return self._find_channel(name, token) + else: + return name + + def _find_channel(self, name, token): + """Calls the Slack API to find the channel name. This is so we do not have to specify the channel ID's. + Slack requires channel ID's to be used. + + Arguments: + name {string} -- The channel name to find. + + Raises: + SlackChannelNotFound -- Thrown if the channel name is not found. + + Returns: + self + """ + response = self._call_slack_api('https://slack.com/api/conversations.list', { + 'token': token + }) + for channel in response['channels']: + if channel['name'] == name.split('#')[1]: + return channel['id'] + + raise SlackChannelNotFound( + "The user or channel being addressed either do not exist or is invalid: {}".format(name) + ) diff --git a/src/masonite/drivers/notification/VonageDriver.py b/src/masonite/drivers/notification/VonageDriver.py new file mode 100644 index 00000000..d0c93a1e --- /dev/null +++ b/src/masonite/drivers/notification/VonageDriver.py @@ -0,0 +1,112 @@ +"""Vonage driver Class.""" +from masonite.drivers import BaseDriver +from masonite.app import App +from masonite.helpers import config +from masonite import Queue + +from ..NotificationContract import NotificationContract +from ..exceptions import VonageInvalidMessage, VonageAPIError +from ..components import VonageComponent + + +class VonageDriver(BaseDriver, NotificationContract): + def __init__(self, app: App): + """Vonage Driver Constructor. + + Arguments: + app {masonite.app.App} -- The Masonite container object. + """ + self.app = app + import vonage + + self._client = vonage.Client( + key=config("notifications.vonage.key"), + secret=config("notifications.vonage.secret"), + ) + self._sms_from = config("notifications.vonage.sms_from", None) + + def send(self, notifiable, notification): + """Used to send the SMS.""" + data, recipients, sms = self._prepare_sms(notifiable, notification) + responses = [] + for recipient in recipients: + payload = self.build_payload(data, recipient) + response = sms.send_message(payload) + self._handle_errors(response) + responses.append(response) + return responses + + def queue(self, notifiable, notification): + """Used to queue the SMS notification to be send.""" + data, recipients, sms = self._prepare_sms(notifiable, notification) + for recipient in recipients: + payload = self.build_payload(data, recipient) + self.app.make(Queue).push(sms.send_message, args=(payload,)) + + def _prepare_sms(self, notifiable, notification): + """Prepare SMS and list of recipients.""" + data = self.get_data("vonage", notifiable, notification) + recipients = self.get_recipients(notifiable, notification) + from vonage.sms import Sms + + sms = Sms(self._client) + return data, recipients, sms + + def get_recipients(self, notifiable, notification): + """Get recipients which can be defined through notifiable route method. + It can be one or a list of phone numbers. + return phone + return [phone1, phone2] + """ + recipients = notifiable.route_notification_for("vonage", notification) + # multiple recipients + if isinstance(recipients, list): + _recipients = [] + for recipient in recipients: + _recipients.append(recipient) + else: + _recipients = [recipients] + return _recipients + + def build_payload(self, data, recipient): + """Build SMS payload sent to Vonage API.""" + + if isinstance(data, str): + data = VonageComponent(data) + + # define send_from from config if not set + if not data._from: + data = data.send_from(self._sms_from) + payload = {**data.as_dict(), "to": recipient} + self._validate_payload(payload) + return payload + + def _validate_payload(self, payload): + """Validate SMS payload before sending by checking that from et to + are correctly set.""" + if not payload.get("from", None): + raise VonageInvalidMessage("from must be specified.") + if not payload.get("to", None): + raise VonageInvalidMessage("to must be specified.") + + def _handle_errors(self, response): + """Handle errors of Vonage API. Raises VonageAPIError if request does + not succeed. + + An error message is structured as follows: + {'message-count': '1', 'messages': [{'status': '2', 'error-text': 'Missing api_key'}]} + As a success message can be structured as follows: + {'message-count': '1', 'messages': [{'to': '3365231278', 'message-id': '140000012BD37332', 'status': '0', + 'remaining-balance': '1.87440000', 'message-price': '0.06280000', 'network': '20810'}]} + + More informations on status code errors: https://developer.nexmo.com/api-errors/sms + + """ + for message in response.get("messages", []): + status = message["status"] + if status != "0": + raise VonageAPIError( + "Code [{0}]: {1}. Please refer to API documentation for more details.".format( + status, message["error-text"] + ) + ) diff --git a/src/masonite/drivers/notification/__init__.py b/src/masonite/drivers/notification/__init__.py new file mode 100644 index 00000000..2758e815 --- /dev/null +++ b/src/masonite/drivers/notification/__init__.py @@ -0,0 +1,6 @@ +# from .BroadcastDriver import BroadcastDriver +from .MailDriver import MailDriver + +# from .DatabaseDriver import DatabaseDriver +# from .SlackDriver import SlackDriver +# from .VonageDriver import VonageDriver diff --git a/src/masonite/exceptions/exceptions.py b/src/masonite/exceptions/exceptions.py index b70d8dd9..4b986673 100644 --- a/src/masonite/exceptions/exceptions.py +++ b/src/masonite/exceptions/exceptions.py @@ -104,3 +104,7 @@ class ProjectProviderHttpError(Exception): class ProjectTargetNotEmpty(Exception): pass + + +class NotificationException(Exception): + pass diff --git a/src/masonite/foundation/Kernel.py b/src/masonite/foundation/Kernel.py index 0ff7341a..26cc52b5 100644 --- a/src/masonite/foundation/Kernel.py +++ b/src/masonite/foundation/Kernel.py @@ -39,6 +39,9 @@ def set_framework_options(self): self.application.bind("config.session", "tests.integrations.config.session") self.application.bind("config.queue", "tests.integrations.config.queue") self.application.bind("config.database", "tests.integrations.config.database") + self.application.bind( + "config.notification", "tests.integrations.config.notification" + ) def register_controllers(self): self.application.bind("controller.location", "tests.integrations.controllers") diff --git a/src/masonite/notification/AnonymousNotifiable.py b/src/masonite/notification/AnonymousNotifiable.py new file mode 100644 index 00000000..2ddf7015 --- /dev/null +++ b/src/masonite/notification/AnonymousNotifiable.py @@ -0,0 +1,36 @@ +"""Anonymous Notifiable mixin""" + +from .Notifiable import Notifiable + + +class AnonymousNotifiable(Notifiable): + """Anonymous notifiable allowing to send notification without having + a notifiable entity.""" + + def __init__(self): + self._routes = {} + + def route(self, channel, route): + """Add routing information to the target.""" + if channel == "database": + raise ValueError( + "The database channel does not support on-demand notifications." + ) + self._routes[channel] = route + return self + + def route_notification_for(self, channel, notification=None): + try: + return self._routes[channel] + except KeyError: + raise ValueError( + "Routing has not been defined for the channel {}".format(channel) + ) + + def send(self, notification, dry=False, fail_silently=False): + """Send the given notification.""" + from wsgi import application + + return application.make("notification").send( + self, notification, self._routes, dry, fail_silently + ) diff --git a/src/masonite/notification/DatabaseNotification.py b/src/masonite/notification/DatabaseNotification.py new file mode 100644 index 00000000..9610fbd8 --- /dev/null +++ b/src/masonite/notification/DatabaseNotification.py @@ -0,0 +1,38 @@ +"""DatabaseNotification Model.""" +import pendulum +from masoniteorm.relationships import morph_to +from masoniteorm.models import Model + + +class DatabaseNotification(Model): + """DatabaseNotification Model.""" + + __fillable__ = ["id", "type", "data", "read_at", "notifiable_id", "notifiable_type"] + __table__ = "notifications" + + @morph_to("notifiable_type", "notifiable_id") + def notifiable(self): + """Get the notifiable entity that the notification belongs to.""" + return + + def mark_as_read(self): + """Mark the notification as read.""" + if not self.read_at: + self.read_at = pendulum.now() + return self.save(query=True) + + def mark_as_unread(self): + """Mark the notification as unread.""" + if self.read_at: + self.read_at = None + return self.save(query=True) + + @property + def is_read(self): + """Determine if a notification has been read.""" + return self.read_at is not None + + @property + def is_unread(self): + """Determine if a notification has not been read yet.""" + return self.read_at is None diff --git a/src/masonite/notification/Notifiable.py b/src/masonite/notification/Notifiable.py new file mode 100644 index 00000000..3708332e --- /dev/null +++ b/src/masonite/notification/Notifiable.py @@ -0,0 +1,53 @@ +"""Notifiable mixin""" +from masoniteorm.relationships import has_many + +from .DatabaseNotification import DatabaseNotification +from ..exceptions.exceptions import NotificationException + + +class Notifiable(object): + def notify(self, notification, channels=[], dry=False, fail_silently=False): + """Send the given notification.""" + from wsgi import application + + return application.make("notification").send( + self, notification, channels, dry, fail_silently + ) + + def route_notification_for(self, channel): + """Get the notification routing information for the given channel.""" + # check if routing has been specified on the model + method_name = "route_notification_for_{0}".format(channel) + + try: + method = getattr(self, method_name) + return method(self) + except AttributeError: + # if no method is defined on notifiable use default + if channel == "database": + # with database channel, notifications are saved to database + pass + elif channel == "mail": + return self.email + else: + raise NotificationException( + "Notifiable model does not implement {}".format(method_name) + ) + + @has_many("id", "notifiable_id") + def notifications(self): + return DatabaseNotification.where("notifiable_type", "users").order_by( + "created_at", direction="DESC" + ) + + @property + def unread_notifications(self): + """Get the entity's unread notifications. Only for 'database' + notifications.""" + return self.notifications.where("read_at", "==", None) + + @property + def read_notifications(self): + """Get the entity's read notifications. Only for 'database' + notifications.""" + return self.notifications.where("read_at", "!=", None) diff --git a/src/masonite/notification/Notification.py b/src/masonite/notification/Notification.py new file mode 100644 index 00000000..c7655ca8 --- /dev/null +++ b/src/masonite/notification/Notification.py @@ -0,0 +1,51 @@ +"""Base Notification facade.""" +from abc import ABC, abstractmethod + + +class Notification(ABC): + """Base Notification.""" + + def __init__(self, *args, **kwargs): + self.id = None + self._run = True + self._fail_silently = False + + def broadcast_on(self): + """Get the channels the event should broadcast on.""" + return [] + + @abstractmethod + def via(self, notifiable): + """Defines the notification's delivery channels.""" + return [] + + @property + def should_send(self): + return self._run + + @property + def ignore_errors(self): + return self._fail_silently + + @classmethod + def notification_type(cls): + """Get notification type defined with class name.""" + return cls.__name__ + + def dry(self): + """Sets whether the notification should be sent or not. + + Returns: + self + """ + self._run = False + return self + + def fail_silently(self): + """Sets whether the notification can fail silently (without raising exceptions). + + Returns: + self + """ + self._fail_silently = True + return self diff --git a/src/masonite/notification/NotificationManager.py b/src/masonite/notification/NotificationManager.py new file mode 100644 index 00000000..83ec77a1 --- /dev/null +++ b/src/masonite/notification/NotificationManager.py @@ -0,0 +1,151 @@ +"""Notification handler class""" +import uuid +from masoniteorm.models import Model + + +from ..drivers.notification.BaseDriver import BaseDriver +from ..exceptions.exceptions import NotificationException, DriverNotFound +from ..queues import ShouldQueue + + +class NotificationManager(object): + """Notification handler which handle sending/queuing notifications anonymously + or to notifiables through different channels.""" + + called_notifications = [] + + def __init__(self, application, driver_config=None): + self.application = application + self.drivers = {} + self.driver_config = driver_config or {} + self.options = {"dry": False} + + def add_driver(self, name, driver): + self.drivers.update({name: driver}) + + def get_driver(self, name): + return self.drivers[name] + + def set_configuration(self, config): + self.driver_config = config + return self + + def get_config_options(self, driver): + return self.driver_config.get(driver, {}) + + def send( + self, notifiables, notification, channels=[], dry=False, fail_silently=False + ): + """Send the given notification to the given notifiables immediately.""" + notifiables = self.prepare_notifiables(notifiables) + for notifiable in notifiables: + # get channels for this notification + # allow override of channels list at send + _channels = channels if channels else notification.via(notifiable) + _channels = self.prepare_channels(_channels) + if not _channels: + raise NotificationException( + "No channels have been defined in via() method of {0} class.".format( + notification.notification_type() + ) + ) + for channel in _channels: + from .AnonymousNotifiable import AnonymousNotifiable + + if ( + isinstance(notifiable, AnonymousNotifiable) + and channel == "database" + ): + # this case is not possible but that should not stop other channels to be used + continue + notification_id = uuid.uuid4() + self.send_or_queue( + notifiable, + notification, + notification_id, + channel, + dry=dry, + fail_silently=fail_silently, + ) + + def is_custom_channel(self, channel): + return issubclass(channel, BaseDriver) + + def send_or_queue( + self, + notifiable, + notification, + notification_id, + channel_instance, + dry=False, + fail_silently=False, + ): + """Send or queue the given notification through the given channel to one notifiable.""" + if not notification.id: + notification.id = notification_id + if not notification.should_send or dry: + return + try: + # TODO: adapt with + # self.get_driver(driver).set_options(self.options).send() + if isinstance(notification, ShouldQueue): + return channel_instance.queue(notifiable, notification) + else: + return channel_instance.send(notifiable, notification) + except Exception as e: + if notification.ignore_errors or fail_silently: + pass + else: + raise e + + # TODO (later): dispatch send event + + def prepare_notifiables(self, notifiables): + from .AnonymousNotifiable import AnonymousNotifiable + + if isinstance(notifiables, Model) or isinstance( + notifiables, AnonymousNotifiable + ): + return [notifiables] + else: + # could be a list or a Collection + return notifiables + + def prepare_channels(self, channels): + """Check channels list to get a list of channels string name which + will be fetched from container later and also checks if custom notifications + classes are provided. + + For custom notifications check that the class implements NotificationContract. + For driver notifications (official or not) check that the driver exists in the container. + """ + _channels = [] + for channel in channels: + if isinstance(channel, str): + # check that related notification driver is known and registered in the container + try: + _channels.append( + self.application.make("notification").get_driver(channel) + ) + except DriverNotFound: + raise NotificationException( + "{0} notification driver has not been found in the container. Check that it is registered correctly.".format( + channel + ) + ) + elif self.is_custom_channel(channel): + _channels.append(channel()) + else: + raise NotificationException( + "{0} notification class cannot be used because it does not implements NotificationContract.".format( + channel + ) + ) + + return _channels + + def route(self, channel, route): + """Send a notification to an anonymous notifiable.""" + from .AnonymousNotifiable import AnonymousNotifiable + + return AnonymousNotifiable().route(channel, route) diff --git a/src/masonite/notification/__init__.py b/src/masonite/notification/__init__.py new file mode 100644 index 00000000..13065562 --- /dev/null +++ b/src/masonite/notification/__init__.py @@ -0,0 +1,3 @@ +from .NotificationManager import NotificationManager +from .Notification import Notification +from .Notifiable import Notifiable \ No newline at end of file diff --git a/src/masonite/providers/NotificationProvider.py b/src/masonite/providers/NotificationProvider.py new file mode 100644 index 00000000..ec54a20c --- /dev/null +++ b/src/masonite/providers/NotificationProvider.py @@ -0,0 +1,45 @@ +from .Provider import Provider +from ..utils.structures import load +from ..drivers.notification import ( + MailDriver, + # BroadcastDriver, + # DatabaseDriver, + # SlackDriver, + # VonageDriver, +) +from ..notification import NotificationManager + + +class NotificationProvider(Provider): + """Notifications Provider""" + + def __init__(self, application): + self.application = application + + def register(self): + notification_manager = NotificationManager(self.application).set_configuration( + load(self.application.make("config.notification")).DRIVERS + ) + notification_manager.add_driver("mail", MailDriver(self.application)) + # notification_manager.add_driver("broadcast", BroadcastDriver(self.application)) + # notification_manager.add_driver("database", DatabaseDriver(self.application)) + # notification_manager.add_driver("slack", SlackDriver(self.application)) + # notification_manager.add_driver("vonage", VonageDriver(self.application)) + # TODO: to rewrite + # self.app.bind("NotificationCommand", NotificationCommand()) + self.application.bind("notification", notification_manager) + + def boot(self): + # TODO: to rewrite + # migration_path = os.path.join(os.path.dirname(__file__), "../migrations") + # config_path = os.path.join(os.path.dirname(__file__), "../config") + # self.publishes( + # {os.path.join(config_path, "notifications.py"): "config/notifications.py"}, + # tag="config", + # ) + # self.publishes_migrations( + # [ + # os.path.join(migration_path, "create_notifications_table.py"), + # ], + # ) + pass \ No newline at end of file diff --git a/src/masonite/providers/__init__.py b/src/masonite/providers/__init__.py index a465a8c8..ee619654 100644 --- a/src/masonite/providers/__init__.py +++ b/src/masonite/providers/__init__.py @@ -8,3 +8,4 @@ from .MailProvider import MailProvider from .SessionProvider import SessionProvider from .QueueProvider import QueueProvider +from .NotificationProvider import NotificationProvider diff --git a/tests/features/notification/test_mail_driver.py b/tests/features/notification/test_mail_driver.py new file mode 100644 index 00000000..ccf91ba8 --- /dev/null +++ b/tests/features/notification/test_mail_driver.py @@ -0,0 +1,50 @@ +from tests import TestCase +from src.masonite.notification import Notification, Notifiable +from src.masonite.mail import Mailable +from masoniteorm.models import Model + + +class User(Model, Notifiable): + """User Model""" + + __fillable__ = ["name", "email", "password"] + + +class WelcomeUserNotification(Notification): + def to_mail(self, notifiable): + return ( + Mailable() + .to(notifiable.email) + .subject("Masonite 4") + .from_("joe@masoniteproject.com") + .text(f"Hello {notifiable.name}") + ) + + def via(self, notifiable): + return ["mail"] + + +class WelcomeNotification(Notification): + def to_mail(self, notifiable): + return ( + Mailable() + .subject("Masonite 4") + .from_("joe@masoniteproject.com") + .text("Hello from Masonite!") + ) + + def via(self, notifiable): + return ["mail"] + + +class TestMailDriver(TestCase): + def setUp(self): + super().setUp() + self.notification = self.application.make("notification") + + def test_send_to_anonymous(self): + self.notification.route("mail", "test@mail.com").send(WelcomeNotification()) + + def test_send_to_notifiable(self): + user = User.find(1) + user.notify(WelcomeUserNotification()) diff --git a/tests/integrations/config/notification.py b/tests/integrations/config/notification.py new file mode 100644 index 00000000..a5d51ac4 --- /dev/null +++ b/tests/integrations/config/notification.py @@ -0,0 +1,11 @@ +"""Notifications Settings.""" +import os + +DRIVERS = { + "slack": {"token": os.getenv("SLACK_TOKEN", "")}, + "vonage": { + "key": os.getenv("VONAGE_KEY", ""), + "secret": os.getenv("VONAGE_SECRET", ""), + "sms_from": os.getenv("VONAGE_SMS_FROM", "+33000000000"), + }, +} \ No newline at end of file diff --git a/tests/integrations/config/providers.py b/tests/integrations/config/providers.py index 43a261a3..93c26492 100644 --- a/tests/integrations/config/providers.py +++ b/tests/integrations/config/providers.py @@ -6,6 +6,7 @@ WhitenoiseProvider, ExceptionProvider, MailProvider, + NotificationProvider, SessionProvider, QueueProvider, ) @@ -20,6 +21,7 @@ WhitenoiseProvider, ExceptionProvider, MailProvider, + NotificationProvider, SessionProvider, QueueProvider, ScheduleProvider, From 0eec8e94b7c8b99c2de2de506f7ab9568043ca75 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Sun, 21 Feb 2021 17:36:14 +0100 Subject: [PATCH 02/54] add notification class tests --- src/masonite/notification/Notification.py | 2 +- .../notification/test_notification.py | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 tests/features/notification/test_notification.py diff --git a/src/masonite/notification/Notification.py b/src/masonite/notification/Notification.py index c7655ca8..cd252076 100644 --- a/src/masonite/notification/Notification.py +++ b/src/masonite/notification/Notification.py @@ -28,7 +28,7 @@ def ignore_errors(self): return self._fail_silently @classmethod - def notification_type(cls): + def type(cls): """Get notification type defined with class name.""" return cls.__name__ diff --git a/tests/features/notification/test_notification.py b/tests/features/notification/test_notification.py new file mode 100644 index 00000000..cbf6cd23 --- /dev/null +++ b/tests/features/notification/test_notification.py @@ -0,0 +1,35 @@ +from tests import TestCase + +from src.masonite.notification import Notification + + +class WelcomeNotification(Notification): + def to_mail(self, notifiable): + return "" + + def via(self, notifiable): + return ["mail"] + + +class TestNotification(TestCase): + def test_should_send(self): + notification = WelcomeNotification() + self.assertTrue(notification.should_send) + notification.dry() + self.assertFalse(notification.should_send) + + def test_ignore_errors(self): + notification = WelcomeNotification() + self.assertFalse(notification.ignore_errors) + notification.fail_silently() + self.assertTrue(notification.ignore_errors) + + def test_notification_type(self): + self.assertEqual("WelcomeNotification", WelcomeNotification().type()) + + def test_that_via_should_be_implemented(self): + class WelcomeNotification(Notification): + pass + + with self.assertRaises(TypeError): + WelcomeNotification() From c175cfc3275b546d6da2aad0beeb4956ef2f8781 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Sun, 21 Feb 2021 18:19:59 +0100 Subject: [PATCH 03/54] continue refactoring --- .../drivers/notification/BroadcastDriver.py | 22 ++++--- .../drivers/notification/DatabaseDriver.py | 13 ++-- .../drivers/notification/SlackDriver.py | 34 +++++----- .../drivers/notification/VonageDriver.py | 64 +++++++++---------- src/masonite/exceptions/__init__.py | 1 + .../notification/NotificationManager.py | 21 +++--- src/masonite/notification/__init__.py | 3 +- .../providers/NotificationProvider.py | 7 +- .../notification/test_anonymous_notifiable.py | 47 ++++++++++++++ .../features/notification/test_mail_driver.py | 9 +++ 10 files changed, 135 insertions(+), 86 deletions(-) create mode 100644 tests/features/notification/test_anonymous_notifiable.py diff --git a/src/masonite/drivers/notification/BroadcastDriver.py b/src/masonite/drivers/notification/BroadcastDriver.py index c37b7f3f..1b89ec56 100644 --- a/src/masonite/drivers/notification/BroadcastDriver.py +++ b/src/masonite/drivers/notification/BroadcastDriver.py @@ -2,18 +2,16 @@ from masonite.app import App from masonite import Queue from masonite.helpers import config -from masonite.drivers import BaseDriver -from ..NotificationContract import NotificationContract from ..exceptions import BroadcastOnNotImplemented +from .BaseDriver import BaseDriver -class BroadcastDriver(BaseDriver, NotificationContract): - _driver = None - - def __init__(self, app: App): - """Broadcast Driver Constructor.""" - self.app = app +class BroadcastDriver(BaseDriver): + def __init__(self, application): + self.application = application + self.options = {} + # TODO self._driver = None def driver(self, driver): @@ -30,13 +28,17 @@ def driver(self, driver): def send(self, notifiable, notification): """Used to broadcast a notification.""" - channels, data, driver = self._prepare_message_to_broadcast(notifiable, notification) + channels, data, driver = self._prepare_message_to_broadcast( + notifiable, notification + ) for channel in channels: driver.channel(channel, data) def queue(self, notifiable, notification): """Used to queue the notification to be broadcasted.""" - channels, data, driver = self._prepare_message_to_broadcast(notifiable, notification) + channels, data, driver = self._prepare_message_to_broadcast( + notifiable, notification + ) for channel in channels: self.app.make(Queue).push(driver.channel, args=(channel, data)) diff --git a/src/masonite/drivers/notification/DatabaseDriver.py b/src/masonite/drivers/notification/DatabaseDriver.py index 636a176c..0debd0ae 100644 --- a/src/masonite/drivers/notification/DatabaseDriver.py +++ b/src/masonite/drivers/notification/DatabaseDriver.py @@ -2,16 +2,15 @@ import json from masonite.app import App from masonite import Queue -from masonite.drivers import BaseDriver -from ..NotificationContract import NotificationContract from ..models import DatabaseNotification +from .BaseDriver import BaseDriver -class DatabaseDriver(BaseDriver, NotificationContract): - def __init__(self, app: App): - """Database Driver Constructor.""" - self.app = app +class DatabaseDriver(BaseDriver): + def __init__(self, application): + self.application = application + self.options = {} def send(self, notifiable, notification): """Used to send the email and run the logic for sending emails.""" @@ -32,7 +31,7 @@ def build_payload(self, notifiable, notification): """Build an array payload for the DatabaseNotification Model.""" return { "id": str(notification.id), - "type": notification.notification_type(), + "type": notification.type(), "notifiable_id": notifiable.id, "notifiable_type": notifiable.get_table_name(), "data": self.serialize_data( diff --git a/src/masonite/drivers/notification/SlackDriver.py b/src/masonite/drivers/notification/SlackDriver.py index 8af50e67..c7ef7cc5 100644 --- a/src/masonite/drivers/notification/SlackDriver.py +++ b/src/masonite/drivers/notification/SlackDriver.py @@ -1,7 +1,6 @@ """Slack driver Class""" import requests from masonite.app import App -from masonite.drivers import BaseDriver from masonite.helpers import config from masonite.managers.QueueManager import Queue @@ -11,25 +10,22 @@ SlackInvalidWorkspace, SlackChannelArchived, SlackInvalidWebhook, - SlackAPIError + SlackAPIError, ) -from ..NotificationContract import NotificationContract +from .BaseDriver import BaseDriver -class SlackDriver(BaseDriver, NotificationContract): +class SlackDriver(BaseDriver): app = None WEBHOOK_MODE = 1 API_MODE = 2 sending_mode = WEBHOOK_MODE - def __init__(self, app: App): - """Slack Driver Constructor. - - Arguments: - app {masonite.app.App} -- The Masonite container object. - """ - self.app = app + def __init__(self, application): + self.application = application + self.options = {} + # TODO self._debug = False self._token = config("notifications.slack.token", None) @@ -203,13 +199,15 @@ def _find_channel(self, name, token): Returns: self """ - response = self._call_slack_api('https://slack.com/api/conversations.list', { - 'token': token - }) - for channel in response['channels']: - if channel['name'] == name.split('#')[1]: - return channel['id'] + response = self._call_slack_api( + "https://slack.com/api/conversations.list", {"token": token} + ) + for channel in response["channels"]: + if channel["name"] == name.split("#")[1]: + return channel["id"] raise SlackChannelNotFound( - "The user or channel being addressed either do not exist or is invalid: {}".format(name) + "The user or channel being addressed either do not exist or is invalid: {}".format( + name + ) ) diff --git a/src/masonite/drivers/notification/VonageDriver.py b/src/masonite/drivers/notification/VonageDriver.py index d0c93a1e..01a6be11 100644 --- a/src/masonite/drivers/notification/VonageDriver.py +++ b/src/masonite/drivers/notification/VonageDriver.py @@ -1,33 +1,38 @@ """Vonage driver Class.""" -from masonite.drivers import BaseDriver -from masonite.app import App -from masonite.helpers import config -from masonite import Queue +from ...exceptions import NotificationException +from .BaseDriver import BaseDriver -from ..NotificationContract import NotificationContract -from ..exceptions import VonageInvalidMessage, VonageAPIError -from ..components import VonageComponent +class VonageDriver(BaseDriver): + def __init__(self, application): + self.app = application + self.options = {} + self._client = self.get_client() -class VonageDriver(BaseDriver, NotificationContract): - def __init__(self, app: App): - """Vonage Driver Constructor. + def set_options(self, options): + self.options = options + return self - Arguments: - app {masonite.app.App} -- The Masonite container object. - """ - self.app = app + def build(self, notifiable, notification): + """Prepare SMS and list of recipients.""" + data = self.get_data("vonage", notifiable, notification) + recipients = self.get_recipients("vonage", notifiable, notification) + from vonage.sms import Sms + + sms = Sms(self._client) + return data, recipients, sms + + def get_client(self): import vonage - self._client = vonage.Client( - key=config("notifications.vonage.key"), - secret=config("notifications.vonage.secret"), + client = vonage.Client( + key=self.options.get("key"), secret=self.options.get("secret") ) - self._sms_from = config("notifications.vonage.sms_from", None) + return client def send(self, notifiable, notification): """Used to send the SMS.""" - data, recipients, sms = self._prepare_sms(notifiable, notification) + data, recipients, sms = self.build(notifiable, notification) responses = [] for recipient in recipients: payload = self.build_payload(data, recipient) @@ -41,16 +46,7 @@ def queue(self, notifiable, notification): data, recipients, sms = self._prepare_sms(notifiable, notification) for recipient in recipients: payload = self.build_payload(data, recipient) - self.app.make(Queue).push(sms.send_message, args=(payload,)) - - def _prepare_sms(self, notifiable, notification): - """Prepare SMS and list of recipients.""" - data = self.get_data("vonage", notifiable, notification) - recipients = self.get_recipients(notifiable, notification) - from vonage.sms import Sms - - sms = Sms(self._client) - return data, recipients, sms + self.application.make("queue").push(sms.send_message, args=(payload,)) def get_recipients(self, notifiable, notification): """Get recipients which can be defined through notifiable route method. @@ -76,7 +72,7 @@ def build_payload(self, data, recipient): # define send_from from config if not set if not data._from: - data = data.send_from(self._sms_from) + data = data.send_from(self.options.get("sms_from")) payload = {**data.as_dict(), "to": recipient} self._validate_payload(payload) return payload @@ -85,9 +81,9 @@ def _validate_payload(self, payload): """Validate SMS payload before sending by checking that from et to are correctly set.""" if not payload.get("from", None): - raise VonageInvalidMessage("from must be specified.") + raise NotificationException("from must be specified.") if not payload.get("to", None): - raise VonageInvalidMessage("to must be specified.") + raise NotificationException("to must be specified.") def _handle_errors(self, response): """Handle errors of Vonage API. Raises VonageAPIError if request does @@ -105,8 +101,8 @@ def _handle_errors(self, response): for message in response.get("messages", []): status = message["status"] if status != "0": - raise VonageAPIError( - "Code [{0}]: {1}. Please refer to API documentation for more details.".format( + raise NotificationException( + "Vonage Code [{0}]: {1}. Please refer to API documentation for more details.".format( status, message["error-text"] ) ) diff --git a/src/masonite/exceptions/__init__.py b/src/masonite/exceptions/__init__.py index c939a01a..b94e2098 100644 --- a/src/masonite/exceptions/__init__.py +++ b/src/masonite/exceptions/__init__.py @@ -11,4 +11,5 @@ ViewException, InvalidSecretKey, InvalidCSRFToken, + NotificationException, ) diff --git a/src/masonite/notification/NotificationManager.py b/src/masonite/notification/NotificationManager.py index 83ec77a1..474be1fe 100644 --- a/src/masonite/notification/NotificationManager.py +++ b/src/masonite/notification/NotificationManager.py @@ -2,7 +2,7 @@ import uuid from masoniteorm.models import Model - +from ..utils.collections import Collection from ..drivers.notification.BaseDriver import BaseDriver from ..exceptions.exceptions import NotificationException, DriverNotFound from ..queues import ShouldQueue @@ -37,7 +37,7 @@ def send( self, notifiables, notification, channels=[], dry=False, fail_silently=False ): """Send the given notification to the given notifiables immediately.""" - notifiables = self.prepare_notifiables(notifiables) + notifiables = self._format_notifiables(notifiables) for notifiable in notifiables: # get channels for this notification # allow override of channels list at send @@ -46,7 +46,7 @@ def send( if not _channels: raise NotificationException( "No channels have been defined in via() method of {0} class.".format( - notification.notification_type() + notification.type() ) ) for channel in _channels: @@ -100,16 +100,11 @@ def send_or_queue( # TODO (later): dispatch send event - def prepare_notifiables(self, notifiables): - from .AnonymousNotifiable import AnonymousNotifiable - - if isinstance(notifiables, Model) or isinstance( - notifiables, AnonymousNotifiable - ): - return [notifiables] - else: - # could be a list or a Collection + def _format_notifiables(self, notifiables): + if isinstance(notifiables, list) or isinstance(notifiables, Collection): return notifiables + else: + return [notifiables] def prepare_channels(self, channels): """Check channels list to get a list of channels string name which @@ -145,7 +140,7 @@ def prepare_channels(self, channels): return _channels def route(self, channel, route): - """Send a notification to an anonymous notifiable.""" + """Specify how to send a notification to an anonymous notifiable.""" from .AnonymousNotifiable import AnonymousNotifiable return AnonymousNotifiable().route(channel, route) diff --git a/src/masonite/notification/__init__.py b/src/masonite/notification/__init__.py index 13065562..80173e42 100644 --- a/src/masonite/notification/__init__.py +++ b/src/masonite/notification/__init__.py @@ -1,3 +1,4 @@ from .NotificationManager import NotificationManager from .Notification import Notification -from .Notifiable import Notifiable \ No newline at end of file +from .Notifiable import Notifiable +from .AnonymousNotifiable import AnonymousNotifiable diff --git a/src/masonite/providers/NotificationProvider.py b/src/masonite/providers/NotificationProvider.py index ec54a20c..d967ec26 100644 --- a/src/masonite/providers/NotificationProvider.py +++ b/src/masonite/providers/NotificationProvider.py @@ -2,6 +2,7 @@ from ..utils.structures import load from ..drivers.notification import ( MailDriver, + VonageDriver, # BroadcastDriver, # DatabaseDriver, # SlackDriver, @@ -21,10 +22,10 @@ def register(self): load(self.application.make("config.notification")).DRIVERS ) notification_manager.add_driver("mail", MailDriver(self.application)) - # notification_manager.add_driver("broadcast", BroadcastDriver(self.application)) + notification_manager.add_driver("vonage", VonageDriver(self.application)) # notification_manager.add_driver("database", DatabaseDriver(self.application)) # notification_manager.add_driver("slack", SlackDriver(self.application)) - # notification_manager.add_driver("vonage", VonageDriver(self.application)) + # notification_manager.add_driver("broadcast", BroadcastDriver(self.application)) # TODO: to rewrite # self.app.bind("NotificationCommand", NotificationCommand()) self.application.bind("notification", notification_manager) @@ -42,4 +43,4 @@ def boot(self): # os.path.join(migration_path, "create_notifications_table.py"), # ], # ) - pass \ No newline at end of file + pass diff --git a/tests/features/notification/test_anonymous_notifiable.py b/tests/features/notification/test_anonymous_notifiable.py new file mode 100644 index 00000000..ad9fd0af --- /dev/null +++ b/tests/features/notification/test_anonymous_notifiable.py @@ -0,0 +1,47 @@ +from tests import TestCase + +from src.masonite.notification import Notification, AnonymousNotifiable + + +class WelcomeNotification(Notification): + def to_mail(self, notifiable): + pass + + def via(self, notifiable): + return ["mail"] + + +class TestAnonymousNotifiable(TestCase): + def test_one_routing(self): + notifiable = AnonymousNotifiable().route("mail", "user@example.com") + self.assertDictEqual({"mail": "user@example.com"}, notifiable._routes) + + def test_multiple_routing(self): + notifiable = ( + AnonymousNotifiable() + .route("mail", "user@example.com") + .route("slack", "#general") + ) + self.assertDictEqual( + {"mail": "user@example.com", "slack": "#general"}, notifiable._routes + ) + + def test_that_sending_with_unexisting_driver_raise_exception(self): + with self.assertRaises(ValueError) as err: + AnonymousNotifiable().route_notification_for("custom_sms", "+337232323232") + self.assertEqual( + "Routing has not been defined for the channel custom_sms", + str(err.exception), + ) + + def test_can_override_dry_when_sending(self): + AnonymousNotifiable().route("mail", "user@example.com").send( + WelcomeNotification(), dry=True + ) + # TODO: assert it + + def test_can_override_fail_silently_when_sending(self): + AnonymousNotifiable().route("mail", "user@example.com").send( + WelcomeNotification(), fail_silently=True + ) + # TODO: assert it diff --git a/tests/features/notification/test_mail_driver.py b/tests/features/notification/test_mail_driver.py index ccf91ba8..4a468805 100644 --- a/tests/features/notification/test_mail_driver.py +++ b/tests/features/notification/test_mail_driver.py @@ -48,3 +48,12 @@ def test_send_to_anonymous(self): def test_send_to_notifiable(self): user = User.find(1) user.notify(WelcomeUserNotification()) + + def test_send_and_override_driver(self): + # TODO: but I don't really know how to proceed as driver can't be defined anymore + # in the Mailable + # Some API solutions: + # self.notification.route("mail", "test@mail.com").send(WelcomeNotification()).driver("log") + # self.notification.route("mail", "test@mail.com").send(WelcomeNotification(), driver="log") + # self.notification.route("mail", "test@mail.com", driver="log").send(WelcomeNotification()) + pass From 0d47a11958f9761202a06268ba3366b0e6e11595 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Sun, 21 Feb 2021 18:29:30 +0100 Subject: [PATCH 04/54] refactor vonage driver --- src/masonite/drivers/notification/__init__.py | 4 +- .../drivers/notification/vonage/Sms.py | 46 +++++++++++++++++++ .../notification/{ => vonage}/VonageDriver.py | 23 +++++----- 3 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 src/masonite/drivers/notification/vonage/Sms.py rename src/masonite/drivers/notification/{ => vonage}/VonageDriver.py (90%) diff --git a/src/masonite/drivers/notification/__init__.py b/src/masonite/drivers/notification/__init__.py index 2758e815..c2aeccae 100644 --- a/src/masonite/drivers/notification/__init__.py +++ b/src/masonite/drivers/notification/__init__.py @@ -1,6 +1,6 @@ -# from .BroadcastDriver import BroadcastDriver from .MailDriver import MailDriver +from .vonage.VonageDriver import VonageDriver +# from .BroadcastDriver import BroadcastDriver # from .DatabaseDriver import DatabaseDriver # from .SlackDriver import SlackDriver -# from .VonageDriver import VonageDriver diff --git a/src/masonite/drivers/notification/vonage/Sms.py b/src/masonite/drivers/notification/vonage/Sms.py new file mode 100644 index 00000000..374dc270 --- /dev/null +++ b/src/masonite/drivers/notification/vonage/Sms.py @@ -0,0 +1,46 @@ +"""Vonage Sms Component""" + + +class Sms: + + _from = "" + _text = "" + _client_ref = "" + _type = "text" + + def __init__(self, text=""): + self._text = text + + def send_from(self, number): + """Set the name or number the message should be sent from. Numbers should + be specified in E.164 format. Details can be found here: + https://developer.nexmo.com/messaging/sms/guides/custom-sender-id""" + self._from = number + return self + + def text(self, text): + self._text = text + return self + + def set_unicode(self): + """Set message as unicode to handle unicode characters in text.""" + self._type = "unicode" + return self + + def client_ref(self, client_ref): + """Set your own client reference (up to 40 characters).""" + if len(client_ref) > 40: + raise ValueError("client_ref should have less then 40 characters.") + self._client_ref = client_ref + return self + + def as_dict(self): + base_dict = { + "from": self._from, + "text": self._text, + } + if self._type: + base_dict.update({"type": self._type}) + if self._client_ref: + base_dict.update({"client-ref": self._client_ref}) + return base_dict diff --git a/src/masonite/drivers/notification/VonageDriver.py b/src/masonite/drivers/notification/vonage/VonageDriver.py similarity index 90% rename from src/masonite/drivers/notification/VonageDriver.py rename to src/masonite/drivers/notification/vonage/VonageDriver.py index 01a6be11..e66f181c 100644 --- a/src/masonite/drivers/notification/VonageDriver.py +++ b/src/masonite/drivers/notification/vonage/VonageDriver.py @@ -1,13 +1,14 @@ """Vonage driver Class.""" -from ...exceptions import NotificationException -from .BaseDriver import BaseDriver +from ....exceptions import NotificationException +from ..BaseDriver import BaseDriver +from .Sms import Sms class VonageDriver(BaseDriver): def __init__(self, application): self.app = application self.options = {} - self._client = self.get_client() + self._client = self.get_sms_client() def set_options(self, options): self.options = options @@ -17,26 +18,24 @@ def build(self, notifiable, notification): """Prepare SMS and list of recipients.""" data = self.get_data("vonage", notifiable, notification) recipients = self.get_recipients("vonage", notifiable, notification) - from vonage.sms import Sms - - sms = Sms(self._client) - return data, recipients, sms + return data, recipients - def get_client(self): + def get_sms_client(self): import vonage + from vonage.sms import Sms client = vonage.Client( key=self.options.get("key"), secret=self.options.get("secret") ) - return client + return Sms(client) def send(self, notifiable, notification): """Used to send the SMS.""" - data, recipients, sms = self.build(notifiable, notification) + data, recipients = self.build(notifiable, notification) responses = [] for recipient in recipients: payload = self.build_payload(data, recipient) - response = sms.send_message(payload) + response = self._client.send_message(payload) self._handle_errors(response) responses.append(response) return responses @@ -68,7 +67,7 @@ def build_payload(self, data, recipient): """Build SMS payload sent to Vonage API.""" if isinstance(data, str): - data = VonageComponent(data) + data = Sms(data) # define send_from from config if not set if not data._from: From 71e12ccc80e02e6475290673d0443926b56a9336 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Sat, 13 Mar 2021 09:56:15 +0100 Subject: [PATCH 05/54] remove contracts classes --- src/masonite/drivers/notification/BaseDriver.py | 8 +------- src/masonite/drivers/notification/DatabaseDriver.py | 1 - src/masonite/notification/Notification.py | 6 ++---- 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/src/masonite/drivers/notification/BaseDriver.py b/src/masonite/drivers/notification/BaseDriver.py index 35f42a8a..dea29857 100644 --- a/src/masonite/drivers/notification/BaseDriver.py +++ b/src/masonite/drivers/notification/BaseDriver.py @@ -1,9 +1,4 @@ -from abc import abstractmethod -from abc import ABC - - -class BaseDriver(ABC): - @abstractmethod +class BaseDriver: def send(self, notifiable, notification): """Implements sending the notification to notifiables through this channel.""" @@ -11,7 +6,6 @@ def send(self, notifiable, notification): "send() method must be implemented for a notification channel." ) - @abstractmethod def queue(self, notifiable, notification): """Implements queuing the notification to be sent later to notifiables through this channel.""" diff --git a/src/masonite/drivers/notification/DatabaseDriver.py b/src/masonite/drivers/notification/DatabaseDriver.py index 0debd0ae..644c8e9e 100644 --- a/src/masonite/drivers/notification/DatabaseDriver.py +++ b/src/masonite/drivers/notification/DatabaseDriver.py @@ -1,6 +1,5 @@ """Database driver Class.""" import json -from masonite.app import App from masonite import Queue from ..models import DatabaseNotification diff --git a/src/masonite/notification/Notification.py b/src/masonite/notification/Notification.py index cd252076..64809abf 100644 --- a/src/masonite/notification/Notification.py +++ b/src/masonite/notification/Notification.py @@ -1,9 +1,8 @@ """Base Notification facade.""" -from abc import ABC, abstractmethod -class Notification(ABC): - """Base Notification.""" +class Notification: + """Notification class representing a notification.""" def __init__(self, *args, **kwargs): self.id = None @@ -14,7 +13,6 @@ def broadcast_on(self): """Get the channels the event should broadcast on.""" return [] - @abstractmethod def via(self, notifiable): """Defines the notification's delivery channels.""" return [] From 1c9c4f2e57ea246f67b5fbc4d12973ef5fd73330 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Sat, 13 Mar 2021 10:22:31 +0100 Subject: [PATCH 06/54] fix tests --- src/masonite/notification/Notification.py | 2 +- tests/features/notification/test_notification.py | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/masonite/notification/Notification.py b/src/masonite/notification/Notification.py index 64809abf..9b5ef567 100644 --- a/src/masonite/notification/Notification.py +++ b/src/masonite/notification/Notification.py @@ -15,7 +15,7 @@ def broadcast_on(self): def via(self, notifiable): """Defines the notification's delivery channels.""" - return [] + raise NotImplementedError("via() method should be implemented.") @property def should_send(self): diff --git a/tests/features/notification/test_notification.py b/tests/features/notification/test_notification.py index cbf6cd23..2fb064e8 100644 --- a/tests/features/notification/test_notification.py +++ b/tests/features/notification/test_notification.py @@ -26,10 +26,3 @@ def test_ignore_errors(self): def test_notification_type(self): self.assertEqual("WelcomeNotification", WelcomeNotification().type()) - - def test_that_via_should_be_implemented(self): - class WelcomeNotification(Notification): - pass - - with self.assertRaises(TypeError): - WelcomeNotification() From 3e4d43a3a3e468b47070124a9491fb73846ca1fb Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Sat, 13 Mar 2021 10:26:29 +0100 Subject: [PATCH 07/54] remove need for vonage to be installed if not used --- .../drivers/notification/vonage/VonageDriver.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/masonite/drivers/notification/vonage/VonageDriver.py b/src/masonite/drivers/notification/vonage/VonageDriver.py index e66f181c..75775ccc 100644 --- a/src/masonite/drivers/notification/vonage/VonageDriver.py +++ b/src/masonite/drivers/notification/vonage/VonageDriver.py @@ -8,7 +8,6 @@ class VonageDriver(BaseDriver): def __init__(self, application): self.app = application self.options = {} - self._client = self.get_sms_client() def set_options(self, options): self.options = options @@ -21,9 +20,13 @@ def build(self, notifiable, notification): return data, recipients def get_sms_client(self): - import vonage - from vonage.sms import Sms - + try: + import vonage + from vonage.sms import Sms + except ImportError: + raise ModuleNotFoundError( + "Could not find the 'vonage' library. Run 'pip install vonage' to fix this." + ) client = vonage.Client( key=self.options.get("key"), secret=self.options.get("secret") ) @@ -33,9 +36,11 @@ def send(self, notifiable, notification): """Used to send the SMS.""" data, recipients = self.build(notifiable, notification) responses = [] + client = self.get_sms_client() + for recipient in recipients: payload = self.build_payload(data, recipient) - response = self._client.send_message(payload) + response = client.send_message(payload) self._handle_errors(response) responses.append(response) return responses From 17519eb84d6f8cb041b0b26a959b6ffa7e6d96c2 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Sat, 13 Mar 2021 12:03:38 +0100 Subject: [PATCH 08/54] rewrite ndriver handling and vonage driver --- .../drivers/notification/MailDriver.py | 41 +------ .../notification/vonage/VonageDriver.py | 73 +++---------- .../notification/AnonymousNotifiable.py | 2 +- src/masonite/notification/Notifiable.py | 2 +- .../notification/NotificationManager.py | 72 +++---------- .../vonage => notification}/Sms.py | 18 +++- src/masonite/notification/__init__.py | 1 + tests/features/notification/test_sms.py | 24 +++++ .../notification/test_vonage_driver.py | 100 ++++++++++++++++++ tests/integrations/app/User.py | 2 +- tests/integrations/config/notification.py | 4 +- .../2021_01_09_043202_create_users_table.py | 1 + 12 files changed, 182 insertions(+), 158 deletions(-) rename src/masonite/{drivers/notification/vonage => notification}/Sms.py (80%) create mode 100644 tests/features/notification/test_sms.py create mode 100644 tests/features/notification/test_vonage_driver.py diff --git a/src/masonite/drivers/notification/MailDriver.py b/src/masonite/drivers/notification/MailDriver.py index 93d91ec3..307401d5 100644 --- a/src/masonite/drivers/notification/MailDriver.py +++ b/src/masonite/drivers/notification/MailDriver.py @@ -1,6 +1,5 @@ -"""Mail driver Class.""" +"""Mail notification driver.""" -from ...exceptions.exceptions import NotificationException from .BaseDriver import BaseDriver @@ -15,52 +14,20 @@ def set_options(self, options): def send(self, notifiable, notification): """Used to send the email.""" - # return method(*method_args) mailable = self.get_data("mail", notifiable, notification) - # check that if no _to has been defined specify it if not mailable._to: - recipients = self.get_recipients(notifiable, notification) + recipients = notifiable.route_notification_for("mail", notification) mailable = mailable.to(recipients) # TODO: allow changing driver how ????? return self.application.make("mail").mailable(mailable).send(driver="terminal") def queue(self, notifiable, notification): """Used to queue the email to send.""" - # return method(*method_args) mailable = self.get_data("mail", notifiable, notification) - # check that if no _to has been defined specify it if not mailable._to: - recipients = self.get_recipients(notifiable, notification) + recipients = notifiable.route_notification_for("mail", notification) mailable = mailable.to(recipients) - # TODO: allow changing driver + # TODO: allow changing driver for queueing + for sending mail ? return self.application.make("queue").push( self.application.make("mail").mailable(mailable).send, driver="async" ) - - def get_recipients(self, notifiable, notification): - """Get recipients which can be defined through notifiable route method. - return email - return {email: name} - return [email1, email2] - return [{email1: ''}, {email2: name2}] - """ - # TODO: use Recipient from M4 - recipients = notifiable.route_notification_for("mail", notification) - # multiple recipients - if isinstance(recipients, list): - _recipients = [] - for recipient in recipients: - _recipients.append(self._format_address(recipient)) - else: - _recipients = [self._format_address(recipients)] - return _recipients - - def _format_address(self, recipient): - if isinstance(recipient, str): - return recipient - elif isinstance(recipient, tuple): - if len(recipient) != 2 or not recipient[1]: - raise NotificationException( - "route_notification_for_mail() should return a string or a tuple (email, name)" - ) - return "{1} <{0}>".format(*recipient) diff --git a/src/masonite/drivers/notification/vonage/VonageDriver.py b/src/masonite/drivers/notification/vonage/VonageDriver.py index 75775ccc..6af2d30a 100644 --- a/src/masonite/drivers/notification/vonage/VonageDriver.py +++ b/src/masonite/drivers/notification/vonage/VonageDriver.py @@ -1,7 +1,6 @@ -"""Vonage driver Class.""" +"""Vonage notification driver.""" from ....exceptions import NotificationException from ..BaseDriver import BaseDriver -from .Sms import Sms class VonageDriver(BaseDriver): @@ -14,10 +13,14 @@ def set_options(self, options): return self def build(self, notifiable, notification): - """Prepare SMS and list of recipients.""" - data = self.get_data("vonage", notifiable, notification) - recipients = self.get_recipients("vonage", notifiable, notification) - return data, recipients + """Build SMS payload sent to Vonage API.""" + sms = self.get_data("vonage", notifiable, notification) + if not sms._from: + sms = sms.from_(self.options.get("sms_from")) + if not sms._to: + recipients = notifiable.route_notification_for("vonage") + sms = sms.to(recipients) + return sms.build().get_options() def get_sms_client(self): try: @@ -34,60 +37,18 @@ def get_sms_client(self): def send(self, notifiable, notification): """Used to send the SMS.""" - data, recipients = self.build(notifiable, notification) - responses = [] + sms = self.build(notifiable, notification) client = self.get_sms_client() - - for recipient in recipients: - payload = self.build_payload(data, recipient) - response = client.send_message(payload) - self._handle_errors(response) - responses.append(response) - return responses + # TODO: here if multiple recipients are defined in Sms it won't work ? check with Vonage API + response = client.send_message(sms) + self._handle_errors(response) + return response def queue(self, notifiable, notification): """Used to queue the SMS notification to be send.""" - data, recipients, sms = self._prepare_sms(notifiable, notification) - for recipient in recipients: - payload = self.build_payload(data, recipient) - self.application.make("queue").push(sms.send_message, args=(payload,)) - - def get_recipients(self, notifiable, notification): - """Get recipients which can be defined through notifiable route method. - It can be one or a list of phone numbers. - return phone - return [phone1, phone2] - """ - recipients = notifiable.route_notification_for("vonage", notification) - # multiple recipients - if isinstance(recipients, list): - _recipients = [] - for recipient in recipients: - _recipients.append(recipient) - else: - _recipients = [recipients] - return _recipients - - def build_payload(self, data, recipient): - """Build SMS payload sent to Vonage API.""" - - if isinstance(data, str): - data = Sms(data) - - # define send_from from config if not set - if not data._from: - data = data.send_from(self.options.get("sms_from")) - payload = {**data.as_dict(), "to": recipient} - self._validate_payload(payload) - return payload - - def _validate_payload(self, payload): - """Validate SMS payload before sending by checking that from et to - are correctly set.""" - if not payload.get("from", None): - raise NotificationException("from must be specified.") - if not payload.get("to", None): - raise NotificationException("to must be specified.") + sms = self.build(notifiable, notification) + client = self.get_sms_client() + self.application.make("queue").push(client.send_message, args=(sms,)) def _handle_errors(self, response): """Handle errors of Vonage API. Raises VonageAPIError if request does diff --git a/src/masonite/notification/AnonymousNotifiable.py b/src/masonite/notification/AnonymousNotifiable.py index 2ddf7015..6a3427f0 100644 --- a/src/masonite/notification/AnonymousNotifiable.py +++ b/src/masonite/notification/AnonymousNotifiable.py @@ -19,7 +19,7 @@ def route(self, channel, route): self._routes[channel] = route return self - def route_notification_for(self, channel, notification=None): + def route_notification_for(self, channel): try: return self._routes[channel] except KeyError: diff --git a/src/masonite/notification/Notifiable.py b/src/masonite/notification/Notifiable.py index 3708332e..7f23f957 100644 --- a/src/masonite/notification/Notifiable.py +++ b/src/masonite/notification/Notifiable.py @@ -21,7 +21,7 @@ def route_notification_for(self, channel): try: method = getattr(self, method_name) - return method(self) + return method() except AttributeError: # if no method is defined on notifiable use default if channel == "database": diff --git a/src/masonite/notification/NotificationManager.py b/src/masonite/notification/NotificationManager.py index 474be1fe..443bcd34 100644 --- a/src/masonite/notification/NotificationManager.py +++ b/src/masonite/notification/NotificationManager.py @@ -34,28 +34,25 @@ def get_config_options(self, driver): return self.driver_config.get(driver, {}) def send( - self, notifiables, notification, channels=[], dry=False, fail_silently=False + self, notifiables, notification, drivers=[], dry=False, fail_silently=False ): """Send the given notification to the given notifiables immediately.""" notifiables = self._format_notifiables(notifiables) for notifiable in notifiables: - # get channels for this notification - # allow override of channels list at send - _channels = channels if channels else notification.via(notifiable) - _channels = self.prepare_channels(_channels) - if not _channels: + # get drivers to use for sending this notification + drivers = drivers if drivers else notification.via(notifiable) + if not drivers: raise NotificationException( "No channels have been defined in via() method of {0} class.".format( notification.type() ) ) - for channel in _channels: + for driver in drivers: + self.options.update(self.get_config_options(driver)) + driver_instance = self.get_driver(driver).set_options(self.options) from .AnonymousNotifiable import AnonymousNotifiable - if ( - isinstance(notifiable, AnonymousNotifiable) - and channel == "database" - ): + if isinstance(notifiable, AnonymousNotifiable) and driver == "database": # this case is not possible but that should not stop other channels to be used continue notification_id = uuid.uuid4() @@ -63,20 +60,20 @@ def send( notifiable, notification, notification_id, - channel, + driver_instance, dry=dry, fail_silently=fail_silently, ) - def is_custom_channel(self, channel): - return issubclass(channel, BaseDriver) + # def is_custom_channel(self, channel): + # return issubclass(channel, BaseDriver) def send_or_queue( self, notifiable, notification, notification_id, - channel_instance, + driver_instance, dry=False, fail_silently=False, ): @@ -86,61 +83,24 @@ def send_or_queue( if not notification.should_send or dry: return try: - # TODO: adapt with - # self.get_driver(driver).set_options(self.options).send() if isinstance(notification, ShouldQueue): - return channel_instance.queue(notifiable, notification) + return driver_instance.queue(notifiable, notification) else: - return channel_instance.send(notifiable, notification) + return driver_instance.send(notifiable, notification) except Exception as e: if notification.ignore_errors or fail_silently: pass else: raise e - # TODO (later): dispatch send event - def _format_notifiables(self, notifiables): if isinstance(notifiables, list) or isinstance(notifiables, Collection): return notifiables else: return [notifiables] - def prepare_channels(self, channels): - """Check channels list to get a list of channels string name which - will be fetched from container later and also checks if custom notifications - classes are provided. - - For custom notifications check that the class implements NotificationContract. - For driver notifications (official or not) check that the driver exists in the container. - """ - _channels = [] - for channel in channels: - if isinstance(channel, str): - # check that related notification driver is known and registered in the container - try: - _channels.append( - self.application.make("notification").get_driver(channel) - ) - except DriverNotFound: - raise NotificationException( - "{0} notification driver has not been found in the container. Check that it is registered correctly.".format( - channel - ) - ) - elif self.is_custom_channel(channel): - _channels.append(channel()) - else: - raise NotificationException( - "{0} notification class cannot be used because it does not implements NotificationContract.".format( - channel - ) - ) - - return _channels - - def route(self, channel, route): + def route(self, driver, route): """Specify how to send a notification to an anonymous notifiable.""" from .AnonymousNotifiable import AnonymousNotifiable - return AnonymousNotifiable().route(channel, route) + return AnonymousNotifiable().route(driver, route) diff --git a/src/masonite/drivers/notification/vonage/Sms.py b/src/masonite/notification/Sms.py similarity index 80% rename from src/masonite/drivers/notification/vonage/Sms.py rename to src/masonite/notification/Sms.py index 374dc270..271098b7 100644 --- a/src/masonite/drivers/notification/vonage/Sms.py +++ b/src/masonite/notification/Sms.py @@ -1,9 +1,10 @@ -"""Vonage Sms Component""" +"""Sms Component""" class Sms: _from = "" + _to = "" _text = "" _client_ref = "" _type = "text" @@ -11,7 +12,7 @@ class Sms: def __init__(self, text=""): self._text = text - def send_from(self, number): + def from_(self, number): """Set the name or number the message should be sent from. Numbers should be specified in E.164 format. Details can be found here: https://developer.nexmo.com/messaging/sms/guides/custom-sender-id""" @@ -22,6 +23,10 @@ def text(self, text): self._text = text return self + def to(self, to): + self._to = to + return self + def set_unicode(self): """Set message as unicode to handle unicode characters in text.""" self._type = "unicode" @@ -34,13 +39,16 @@ def client_ref(self, client_ref): self._client_ref = client_ref return self - def as_dict(self): + def build(self, *args, **kwargs): + return self + + def get_options(self): base_dict = { + "to": self._to, "from": self._from, "text": self._text, + "type": self._type, } - if self._type: - base_dict.update({"type": self._type}) if self._client_ref: base_dict.update({"client-ref": self._client_ref}) return base_dict diff --git a/src/masonite/notification/__init__.py b/src/masonite/notification/__init__.py index 80173e42..124e914a 100644 --- a/src/masonite/notification/__init__.py +++ b/src/masonite/notification/__init__.py @@ -2,3 +2,4 @@ from .Notification import Notification from .Notifiable import Notifiable from .AnonymousNotifiable import AnonymousNotifiable +from .Sms import Sms \ No newline at end of file diff --git a/tests/features/notification/test_sms.py b/tests/features/notification/test_sms.py new file mode 100644 index 00000000..e8e2b49a --- /dev/null +++ b/tests/features/notification/test_sms.py @@ -0,0 +1,24 @@ +from tests import TestCase +from src.masonite.notification import Sms + + +class Welcome(Sms): + def build(self): + return self.to("+33612345678").from_("+44123456789").text("Masonite 4") + + +class TestSms(TestCase): + def test_build_sms(self): + sms = Welcome().build().get_options() + self.assertEqual(sms.get("to"), "+33612345678") + self.assertEqual(sms.get("from"), "+44123456789") + self.assertEqual(sms.get("text"), "Masonite 4") + self.assertEqual(sms.get("type"), "text") + + def test_set_unicode(self): + sms = Welcome().set_unicode().build().get_options() + self.assertEqual(sms.get("type"), "unicode") + + def test_adding_client_ref(self): + sms = Welcome().client_ref("ABCD").build().get_options() + self.assertEqual(sms.get("client-ref"), "ABCD") diff --git a/tests/features/notification/test_vonage_driver.py b/tests/features/notification/test_vonage_driver.py new file mode 100644 index 00000000..10f2b230 --- /dev/null +++ b/tests/features/notification/test_vonage_driver.py @@ -0,0 +1,100 @@ +from tests import TestCase +import pytest +from unittest.mock import patch +from src.masonite.notification import Notification, Notifiable, Sms +from src.masonite.exceptions import NotificationException + +from masoniteorm.models import Model + + +class User(Model, Notifiable): + """User Model""" + + __fillable__ = ["name", "email", "password", "phone"] + + def route_notification_for_vonage(self): + return "+33123456789" + + +class WelcomeUserNotification(Notification): + def to_vonage(self, notifiable): + return Sms().to(notifiable.phone).text("Welcome !").from_("123456") + + def via(self, notifiable): + return ["vonage"] + + +class WelcomeNotification(Notification): + def to_vonage(self, notifiable): + return Sms().text("Welcome !").from_("123456") + + def via(self, notifiable): + return ["vonage"] + + +class OtherNotification(Notification): + def to_vonage(self, notifiable): + return Sms().text("Welcome !") + + def via(self, notifiable): + return ["vonage"] + + +class VonageAPIMock(object): + @staticmethod + def send_success(): + return {"hoho": "hihi", "message-count": 1, "messages": [{"status": "0"}]} + + @staticmethod + def send_error(error="Missing api_key", status=2): + return { + "message-count": 1, + "messages": [{"status": str(status), "error-text": error}], + } + + +class TestVonageDriver(TestCase): + def setUp(self): + super().setUp() + self.notification = self.application.make("notification") + + def test_sending_without_credentials(self): + with self.assertRaises(NotificationException) as e: + self.notification.route("vonage", "+33123456789").send( + WelcomeNotification() + ) + error_message = str(e.exception) + self.assertIn("Code [2]", error_message) + self.assertIn("api_key", error_message) + + def test_send_to_anonymous(self): + with patch("vonage.sms.Sms") as MockSmsClass: + MockSmsClass.return_value.send_message.return_value = ( + VonageAPIMock().send_success() + ) + self.notification.route("vonage", "+33123456789").send( + WelcomeNotification() + ) + + @pytest.mark.skip("Waiting for adding phone field to user.") + def test_send_to_notifiable(self): + with patch("vonage.sms.Sms") as MockSmsClass: + MockSmsClass.return_value.send_message.return_value = ( + VonageAPIMock().send_success() + ) + user = User.find(1) + user.notify(WelcomeUserNotification()) + + def test_send_to_notifiable_with_route_notification_for(self): + with patch("vonage.sms.Sms") as MockSmsClass: + MockSmsClass.return_value.send_message.return_value = ( + VonageAPIMock().send_success() + ) + user = User.find(1) + user.notify(WelcomeNotification()) + + def test_global_send_from_is_used_when_not_specified(self): + self.notification.route("vonage", "+33123456789").send(OtherNotification()) + import pdb + + pdb.set_trace() diff --git a/tests/integrations/app/User.py b/tests/integrations/app/User.py index 124b62d8..34f7c164 100644 --- a/tests/integrations/app/User.py +++ b/tests/integrations/app/User.py @@ -3,4 +3,4 @@ class User(Model, Authenticates): - __fillable__ = ["name", "password", "email"] + __fillable__ = ["name", "password", "email", "phone"] diff --git a/tests/integrations/config/notification.py b/tests/integrations/config/notification.py index a5d51ac4..1b96c477 100644 --- a/tests/integrations/config/notification.py +++ b/tests/integrations/config/notification.py @@ -8,4 +8,6 @@ "secret": os.getenv("VONAGE_SECRET", ""), "sms_from": os.getenv("VONAGE_SMS_FROM", "+33000000000"), }, -} \ No newline at end of file +} + +DRY = False \ No newline at end of file diff --git a/tests/integrations/databases/migrations/2021_01_09_043202_create_users_table.py b/tests/integrations/databases/migrations/2021_01_09_043202_create_users_table.py index 8101844a..a8da836c 100644 --- a/tests/integrations/databases/migrations/2021_01_09_043202_create_users_table.py +++ b/tests/integrations/databases/migrations/2021_01_09_043202_create_users_table.py @@ -11,6 +11,7 @@ def up(self): table.string("password") table.string("second_password").nullable() table.string("remember_token").nullable() + table.string("phone").nullable() table.timestamp("verified_at").nullable() table.timestamps() From ad48dfec78ab4dc4b541dfa38645760a0dd7b7d2 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Sat, 13 Mar 2021 12:09:36 +0100 Subject: [PATCH 09/54] simplify code again --- src/masonite/notification/NotificationManager.py | 14 +++++--------- src/masonite/providers/NotificationProvider.py | 3 +-- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/masonite/notification/NotificationManager.py b/src/masonite/notification/NotificationManager.py index 443bcd34..68c2c48b 100644 --- a/src/masonite/notification/NotificationManager.py +++ b/src/masonite/notification/NotificationManager.py @@ -1,10 +1,8 @@ """Notification handler class""" import uuid -from masoniteorm.models import Model from ..utils.collections import Collection -from ..drivers.notification.BaseDriver import BaseDriver -from ..exceptions.exceptions import NotificationException, DriverNotFound +from ..exceptions.exceptions import NotificationException from ..queues import ShouldQueue @@ -27,7 +25,8 @@ def get_driver(self, name): return self.drivers[name] def set_configuration(self, config): - self.driver_config = config + self.driver_config = config.DRIVERS + self.options = {"dry": config.DRY} return self def get_config_options(self, driver): @@ -55,11 +54,11 @@ def send( if isinstance(notifiable, AnonymousNotifiable) and driver == "database": # this case is not possible but that should not stop other channels to be used continue - notification_id = uuid.uuid4() + if not notification.id: + notification.id = uuid.uuid4() self.send_or_queue( notifiable, notification, - notification_id, driver_instance, dry=dry, fail_silently=fail_silently, @@ -72,14 +71,11 @@ def send_or_queue( self, notifiable, notification, - notification_id, driver_instance, dry=False, fail_silently=False, ): """Send or queue the given notification through the given channel to one notifiable.""" - if not notification.id: - notification.id = notification_id if not notification.should_send or dry: return try: diff --git a/src/masonite/providers/NotificationProvider.py b/src/masonite/providers/NotificationProvider.py index d967ec26..e2d4345c 100644 --- a/src/masonite/providers/NotificationProvider.py +++ b/src/masonite/providers/NotificationProvider.py @@ -6,7 +6,6 @@ # BroadcastDriver, # DatabaseDriver, # SlackDriver, - # VonageDriver, ) from ..notification import NotificationManager @@ -19,7 +18,7 @@ def __init__(self, application): def register(self): notification_manager = NotificationManager(self.application).set_configuration( - load(self.application.make("config.notification")).DRIVERS + load(self.application.make("config.notification")) ) notification_manager.add_driver("mail", MailDriver(self.application)) notification_manager.add_driver("vonage", VonageDriver(self.application)) From 2f1811d81be6328fe5aae0c96072cc43241b1cf8 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Sat, 13 Mar 2021 12:11:27 +0100 Subject: [PATCH 10/54] simplification --- .../notification/NotificationManager.py | 42 ++++++------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/src/masonite/notification/NotificationManager.py b/src/masonite/notification/NotificationManager.py index 68c2c48b..795deaf2 100644 --- a/src/masonite/notification/NotificationManager.py +++ b/src/masonite/notification/NotificationManager.py @@ -36,6 +36,9 @@ def send( self, notifiables, notification, drivers=[], dry=False, fail_silently=False ): """Send the given notification to the given notifiables immediately.""" + if not notification.should_send or dry: + return + notifiables = self._format_notifiables(notifiables) for notifiable in notifiables: # get drivers to use for sending this notification @@ -56,39 +59,20 @@ def send( continue if not notification.id: notification.id = uuid.uuid4() - self.send_or_queue( - notifiable, - notification, - driver_instance, - dry=dry, - fail_silently=fail_silently, - ) + try: + if isinstance(notification, ShouldQueue): + return driver_instance.queue(notifiable, notification) + else: + return driver_instance.send(notifiable, notification) + except Exception as e: + if notification.ignore_errors or fail_silently: + pass + else: + raise e # def is_custom_channel(self, channel): # return issubclass(channel, BaseDriver) - def send_or_queue( - self, - notifiable, - notification, - driver_instance, - dry=False, - fail_silently=False, - ): - """Send or queue the given notification through the given channel to one notifiable.""" - if not notification.should_send or dry: - return - try: - if isinstance(notification, ShouldQueue): - return driver_instance.queue(notifiable, notification) - else: - return driver_instance.send(notifiable, notification) - except Exception as e: - if notification.ignore_errors or fail_silently: - pass - else: - raise e - def _format_notifiables(self, notifiables): if isinstance(notifiables, list) or isinstance(notifiables, Collection): return notifiables From e1533d793a7ca8a8758798c04ce2e60132b37fb4 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Sat, 13 Mar 2021 12:14:32 +0100 Subject: [PATCH 11/54] clean code --- src/masonite/drivers/notification/MailDriver.py | 4 ++-- tests/features/notification/test_anonymous_notifiable.py | 8 -------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/masonite/drivers/notification/MailDriver.py b/src/masonite/drivers/notification/MailDriver.py index 307401d5..e8e2380e 100644 --- a/src/masonite/drivers/notification/MailDriver.py +++ b/src/masonite/drivers/notification/MailDriver.py @@ -16,7 +16,7 @@ def send(self, notifiable, notification): """Used to send the email.""" mailable = self.get_data("mail", notifiable, notification) if not mailable._to: - recipients = notifiable.route_notification_for("mail", notification) + recipients = notifiable.route_notification_for("mail") mailable = mailable.to(recipients) # TODO: allow changing driver how ????? return self.application.make("mail").mailable(mailable).send(driver="terminal") @@ -25,7 +25,7 @@ def queue(self, notifiable, notification): """Used to queue the email to send.""" mailable = self.get_data("mail", notifiable, notification) if not mailable._to: - recipients = notifiable.route_notification_for("mail", notification) + recipients = notifiable.route_notification_for("mail") mailable = mailable.to(recipients) # TODO: allow changing driver for queueing + for sending mail ? return self.application.make("queue").push( diff --git a/tests/features/notification/test_anonymous_notifiable.py b/tests/features/notification/test_anonymous_notifiable.py index ad9fd0af..3b06dc3b 100644 --- a/tests/features/notification/test_anonymous_notifiable.py +++ b/tests/features/notification/test_anonymous_notifiable.py @@ -26,14 +26,6 @@ def test_multiple_routing(self): {"mail": "user@example.com", "slack": "#general"}, notifiable._routes ) - def test_that_sending_with_unexisting_driver_raise_exception(self): - with self.assertRaises(ValueError) as err: - AnonymousNotifiable().route_notification_for("custom_sms", "+337232323232") - self.assertEqual( - "Routing has not been defined for the channel custom_sms", - str(err.exception), - ) - def test_can_override_dry_when_sending(self): AnonymousNotifiable().route("mail", "user@example.com").send( WelcomeNotification(), dry=True From 3eea238a42231f20cc6e4eca104c6eeeab9deba0 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Sat, 13 Mar 2021 12:17:34 +0100 Subject: [PATCH 12/54] start adding mock notification class --- src/masonite/notification/MockNotification.py | 13 +++++++++++++ src/masonite/providers/NotificationProvider.py | 2 ++ 2 files changed, 15 insertions(+) create mode 100644 src/masonite/notification/MockNotification.py diff --git a/src/masonite/notification/MockNotification.py b/src/masonite/notification/MockNotification.py new file mode 100644 index 00000000..9c8b0499 --- /dev/null +++ b/src/masonite/notification/MockNotification.py @@ -0,0 +1,13 @@ +from .NotificationManager import NotificationManager + + +class MockNotification(NotificationManager): + def __init__(self, application, *args, **kwargs): + super().__init__(application, *args, **kwargs) + self.count = 0 + + def send( + self, notifiables, notification, drivers=[], dry=False, fail_silently=False + ): + self.called_notifications.append(notification) + return self diff --git a/src/masonite/providers/NotificationProvider.py b/src/masonite/providers/NotificationProvider.py index e2d4345c..250bff4d 100644 --- a/src/masonite/providers/NotificationProvider.py +++ b/src/masonite/providers/NotificationProvider.py @@ -8,6 +8,7 @@ # SlackDriver, ) from ..notification import NotificationManager +from ..notification import MockNotification class NotificationProvider(Provider): @@ -28,6 +29,7 @@ def register(self): # TODO: to rewrite # self.app.bind("NotificationCommand", NotificationCommand()) self.application.bind("notification", notification_manager) + self.application.bind("mock.notification", MockNotification) def boot(self): # TODO: to rewrite From 45d228eaa3e56b3350dbc1f4027f9acc2cc7799d Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Mon, 15 Mar 2021 19:45:43 +0100 Subject: [PATCH 13/54] add registering of notifications mocking --- src/masonite/notification/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/masonite/notification/__init__.py b/src/masonite/notification/__init__.py index 124e914a..e23fa956 100644 --- a/src/masonite/notification/__init__.py +++ b/src/masonite/notification/__init__.py @@ -1,4 +1,5 @@ from .NotificationManager import NotificationManager +from .MockNotification import MockNotification from .Notification import Notification from .Notifiable import Notifiable from .AnonymousNotifiable import AnonymousNotifiable From 27d851aa24b7631ff462cb9e403d3a03618afaac Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Mon, 15 Mar 2021 20:04:38 +0100 Subject: [PATCH 14/54] update database with phone data in user --- database.sqlite3 | Bin 348160 -> 24576 bytes .../notification/test_vonage_driver.py | 1 - 2 files changed, 1 deletion(-) diff --git a/database.sqlite3 b/database.sqlite3 index fb3cf1b008a84ba54e5354b9b1670e36a67e6282..7c96ecbd5c2a11124a5c730f0a07c492ba59400c 100644 GIT binary patch delta 514 zcmZozAlh(%ae|Z(0~-Sa7y~IVV4kRBBmorBb3V+=AI8ASlfl3r$LGl}&Xd6>w^>l& z6Sq{eF$=r6xHw~*Ye`~KPHJg!YEdzmV4b{z_bO{aMt)xEg5kzazPBtJWK@&$fLpVW$i%%ard_{0(g7sn9S z5KmuM1;0=qA036{qSVB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ests/features/notification/test_vonage_driver.py index 10f2b230..4807b80e 100644 --- a/tests/features/notification/test_vonage_driver.py +++ b/tests/features/notification/test_vonage_driver.py @@ -76,7 +76,6 @@ def test_send_to_anonymous(self): WelcomeNotification() ) - @pytest.mark.skip("Waiting for adding phone field to user.") def test_send_to_notifiable(self): with patch("vonage.sms.Sms") as MockSmsClass: MockSmsClass.return_value.send_message.return_value = ( From 5ce1cd62161989f970ededa19d4db4677344ff34 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Thu, 18 Mar 2021 17:59:16 +0100 Subject: [PATCH 15/54] add notification scaffolding command --- .../commands/MakeNotificationCommand.py | 40 +++++++++++++++++++ src/masonite/commands/__init__.py | 1 + src/masonite/foundation/Kernel.py | 5 +++ src/masonite/notification/__init__.py | 3 +- .../stubs/notification/Notification.py | 16 ++++++++ 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/masonite/commands/MakeNotificationCommand.py create mode 100644 src/masonite/stubs/notification/Notification.py diff --git a/src/masonite/commands/MakeNotificationCommand.py b/src/masonite/commands/MakeNotificationCommand.py new file mode 100644 index 00000000..ba3478ec --- /dev/null +++ b/src/masonite/commands/MakeNotificationCommand.py @@ -0,0 +1,40 @@ +"""MakeNotificationCommand Class""" +from cleo import Command +from ..utils.filesystem import make_directory +import inflection +import os + + +class MakeNotificationCommand(Command): + """ + Creates a new notification class. + + notification + {name : Name of the notification} + """ + + def __init__(self, application): + super().__init__() + self.app = application + + def handle(self): + name = inflection.camelize(self.argument("name")) + + with open(self.get_mailables_path(), "r") as f: + content = f.read() + content = content.replace("__class__", name) + + file_name = os.path.join( + self.app.make("notifications.location").replace(".", "/"), name + ".py" + ) + + make_directory(file_name) + + with open(file_name, "w") as f: + f.write(content) + self.info(f"Notification Created ({file_name})") + + def get_mailables_path(self): + current_path = os.path.dirname(os.path.realpath(__file__)) + + return os.path.join(current_path, "../stubs/notification/Notification.py") diff --git a/src/masonite/commands/__init__.py b/src/masonite/commands/__init__.py index 815cb318..5c733c1d 100644 --- a/src/masonite/commands/__init__.py +++ b/src/masonite/commands/__init__.py @@ -10,3 +10,4 @@ from .MakeControllerCommand import MakeControllerCommand from .MakeJobCommand import MakeJobCommand from .MakeMailableCommand import MakeMailableCommand +from .MakeNotificationCommand import MakeNotificationCommand diff --git a/src/masonite/foundation/Kernel.py b/src/masonite/foundation/Kernel.py index 452b8f2e..9da286b1 100644 --- a/src/masonite/foundation/Kernel.py +++ b/src/masonite/foundation/Kernel.py @@ -13,6 +13,7 @@ MakeControllerCommand, MakeJobCommand, MakeMailableCommand, + MakeNotificationCommand, ) from ..storage import StorageCapsule from ..auth import Sign @@ -105,6 +106,9 @@ def register_framework(self): self.application.bind("resolver", DB) self.application.bind("jobs.location", "tests/integrations/jobs") self.application.bind("mailables.location", "tests/integrations/mailables") + self.application.bind( + "notifications.location", "tests/integrations/notifications" + ) def register_commands(self): self.application.bind( @@ -121,5 +125,6 @@ def register_commands(self): MakeControllerCommand(self.application), MakeJobCommand(self.application), MakeMailableCommand(self.application), + MakeNotificationCommand(self.application), ), ) diff --git a/src/masonite/notification/__init__.py b/src/masonite/notification/__init__.py index e23fa956..6ee89054 100644 --- a/src/masonite/notification/__init__.py +++ b/src/masonite/notification/__init__.py @@ -3,4 +3,5 @@ from .Notification import Notification from .Notifiable import Notifiable from .AnonymousNotifiable import AnonymousNotifiable -from .Sms import Sms \ No newline at end of file +from .Sms import Sms +from .SlackMessage import SlackMessage \ No newline at end of file diff --git a/src/masonite/stubs/notification/Notification.py b/src/masonite/stubs/notification/Notification.py new file mode 100644 index 00000000..1e8e72c2 --- /dev/null +++ b/src/masonite/stubs/notification/Notification.py @@ -0,0 +1,16 @@ +from masonite.notification import Notification +from masonite.mail import Mailable + + +class Welcome(Notification): + def to_mail(self, notifiable): + return ( + Mailable() + .to(notifiable.email) + .subject("Masonite 4") + .from_("sam@masoniteproject.com") + .text(f"Hello {notifiable.name}") + ) + + def via(self, notifiable): + return ["mail"] From 936b3fcd5c4f9ad17eab4709e69868ae51f94f39 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Thu, 18 Mar 2021 18:01:00 +0100 Subject: [PATCH 16/54] fix notification stub to include name --- src/masonite/stubs/notification/Notification.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/masonite/stubs/notification/Notification.py b/src/masonite/stubs/notification/Notification.py index 1e8e72c2..923dc8b7 100644 --- a/src/masonite/stubs/notification/Notification.py +++ b/src/masonite/stubs/notification/Notification.py @@ -2,7 +2,7 @@ from masonite.mail import Mailable -class Welcome(Notification): +class __class__(Notification): def to_mail(self, notifiable): return ( Mailable() From a886d4752f88f213cb96de7fb394f289289f0e27 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Thu, 18 Mar 2021 19:31:08 +0100 Subject: [PATCH 17/54] add database driver --- database.sqlite3 | Bin 24576 -> 32768 bytes .../commands/NotificationTableCommand.py | 37 +++ src/masonite/commands/__init__.py | 1 + .../drivers/notification/BaseDriver.py | 12 +- .../drivers/notification/DatabaseDriver.py | 28 +- .../drivers/notification/SlackDriver.py | 246 ++++++------------ src/masonite/drivers/notification/__init__.py | 4 +- src/masonite/foundation/Kernel.py | 2 + .../notification/AnonymousNotifiable.py | 29 ++- .../notification/DatabaseNotification.py | 2 +- src/masonite/notification/Notifiable.py | 30 ++- src/masonite/notification/Notification.py | 6 +- .../notification/NotificationManager.py | 26 +- src/masonite/notification/SlackMessage.py | 158 +++++++++++ src/masonite/notification/__init__.py | 1 + .../providers/NotificationProvider.py | 26 +- .../create_notifications_table.py | 17 ++ .../notification/test_anonymous_notifiable.py | 33 ++- .../notification/test_database_driver.py | 125 +++++++++ .../notification/test_slack_driver.py | 85 ++++++ .../notification/test_slack_message.py | 63 +++++ .../notification/test_vonage_driver.py | 1 - tests/integrations/config/notification.py | 6 +- ...03_18_190410_create_notifications_table.py | 17 ++ 24 files changed, 690 insertions(+), 265 deletions(-) create mode 100644 src/masonite/commands/NotificationTableCommand.py create mode 100644 src/masonite/notification/SlackMessage.py create mode 100644 src/masonite/stubs/notification/create_notifications_table.py create mode 100644 tests/features/notification/test_database_driver.py create mode 100644 tests/features/notification/test_slack_driver.py create mode 100644 tests/features/notification/test_slack_message.py create mode 100644 tests/integrations/databases/migrations/2021_03_18_190410_create_notifications_table.py diff --git a/database.sqlite3 b/database.sqlite3 index 7c96ecbd5c2a11124a5c730f0a07c492ba59400c..9cd1ba5c1f7c1d2eca9f6183706e911d2d8a7b27 100644 GIT binary patch delta 392 zcmZoTz}V2hG(lRBgMonogkgYXqK>gN2ZNsTVP5_)26nEK4E%9?p8VoG8GLeFCwX)? zHu`ggH6^mKi>s?MwlbC^Cgr5&<(FipWhN(pwOCvW5zXDO*HNS%ClLhhWZ72y8F4hD0uqePy{jzW&@HjMX8A?lW*|JO@79!HTgELF}tQEJDa$#`ea#t z30V|3#v7t=IW}kT%wgtY<}YC2Kg++EzhJYVKoY;PAu|g@Dx;Bskzu@nalD~LyrHFm diJ?J!GSH%u)OZvl;=v)tw0W_7tivJ&1ptNfbnyTH delta 82 zcmZo@U}`wPI6+#Fje&sygkgYrqK>gR8-t$nVP5_)23DR72L3ocPkwQp3_iKdf&!no eH)rt7Vdh}sU(CRNmVfbPL51o3n-|N+IsgDpz7awI diff --git a/src/masonite/commands/NotificationTableCommand.py b/src/masonite/commands/NotificationTableCommand.py new file mode 100644 index 00000000..6897f42b --- /dev/null +++ b/src/masonite/commands/NotificationTableCommand.py @@ -0,0 +1,37 @@ +"""Notification Table Command.""" +from cleo import Command +from ..utils.filesystem import make_directory +import os +import pathlib +import datetime + + +class NotificationTableCommand(Command): + """ + Creates the notifications table needed for storing notifications in database. + + notification:table + {--d|--directory=database/migrations : Specifies the directory to create the migration in} + """ + + def handle(self): + now = datetime.datetime.today() + + with open( + os.path.join( + pathlib.Path(__file__).parent.absolute(), + "../", + "stubs/notification/create_notifications_table.py", + ) + ) as fp: + output = fp.read() + + file_name = f"{now.strftime('%Y_%m_%d_%H%M%S')}_create_notifications_table.py" + + path = os.path.join(os.getcwd(), self.option("directory"), file_name) + make_directory(path) + + with open(path, "w") as fp: + fp.write(output) + + self.info(f"Migration file created: {file_name}") diff --git a/src/masonite/commands/__init__.py b/src/masonite/commands/__init__.py index 5c733c1d..c1b102b3 100644 --- a/src/masonite/commands/__init__.py +++ b/src/masonite/commands/__init__.py @@ -11,3 +11,4 @@ from .MakeJobCommand import MakeJobCommand from .MakeMailableCommand import MakeMailableCommand from .MakeNotificationCommand import MakeNotificationCommand +from .NotificationTableCommand import NotificationTableCommand diff --git a/src/masonite/drivers/notification/BaseDriver.py b/src/masonite/drivers/notification/BaseDriver.py index dea29857..866a9609 100644 --- a/src/masonite/drivers/notification/BaseDriver.py +++ b/src/masonite/drivers/notification/BaseDriver.py @@ -1,21 +1,21 @@ class BaseDriver: def send(self, notifiable, notification): """Implements sending the notification to notifiables through - this channel.""" + this driver.""" raise NotImplementedError( - "send() method must be implemented for a notification channel." + "send() method must be implemented for a notification driver." ) def queue(self, notifiable, notification): """Implements queuing the notification to be sent later to notifiables through - this channel.""" + this driver.""" raise NotImplementedError( - "queue() method must be implemented for a notification channel." + "queue() method must be implemented for a notification driver." ) - def get_data(self, channel, notifiable, notification): + def get_data(self, driver, notifiable, notification): """Get the data for the notification.""" - method_name = "to_{0}".format(channel) + method_name = "to_{0}".format(driver) try: method = getattr(notification, method_name) except AttributeError: diff --git a/src/masonite/drivers/notification/DatabaseDriver.py b/src/masonite/drivers/notification/DatabaseDriver.py index 644c8e9e..229f93e2 100644 --- a/src/masonite/drivers/notification/DatabaseDriver.py +++ b/src/masonite/drivers/notification/DatabaseDriver.py @@ -1,8 +1,7 @@ -"""Database driver Class.""" +"""Database notification driver.""" import json -from masonite import Queue -from ..models import DatabaseNotification +from ...notification import DatabaseNotification from .BaseDriver import BaseDriver @@ -11,30 +10,29 @@ def __init__(self, application): self.application = application self.options = {} + def set_options(self, options): + self.options = options + return self + def send(self, notifiable, notification): """Used to send the email and run the logic for sending emails.""" - model_data = self.build_payload(notifiable, notification) - return DatabaseNotification.create(model_data) + data = self.build(notifiable, notification) + return DatabaseNotification.create(data) def queue(self, notifiable, notification): """Used to queue the database notification creation.""" - model_data = self.build_payload(notifiable, notification) - return self.app.make(Queue).push( - DatabaseNotification.create, args=(model_data,) + data = self.build(notifiable, notification) + return self.application.make("queue").push( + DatabaseNotification.create, args=(data,) ) - def serialize_data(self, data): - return json.dumps(data) - - def build_payload(self, notifiable, notification): + def build(self, notifiable, notification): """Build an array payload for the DatabaseNotification Model.""" return { "id": str(notification.id), "type": notification.type(), "notifiable_id": notifiable.id, "notifiable_type": notifiable.get_table_name(), - "data": self.serialize_data( - self.get_data("database", notifiable, notification) - ), + "data": json.dumps(self.get_data("database", notifiable, notification)), "read_at": None, } diff --git a/src/masonite/drivers/notification/SlackDriver.py b/src/masonite/drivers/notification/SlackDriver.py index c7ef7cc5..a7f01d32 100644 --- a/src/masonite/drivers/notification/SlackDriver.py +++ b/src/masonite/drivers/notification/SlackDriver.py @@ -1,213 +1,113 @@ -"""Slack driver Class""" +"""Slack notification driver""" import requests -from masonite.app import App -from masonite.helpers import config -from masonite.managers.QueueManager import Queue - -from ..exceptions import ( - SlackChannelNotFound, - SlackInvalidMessage, - SlackInvalidWorkspace, - SlackChannelArchived, - SlackInvalidWebhook, - SlackAPIError, -) + +from ...exceptions import NotificationException from .BaseDriver import BaseDriver class SlackDriver(BaseDriver): - app = None WEBHOOK_MODE = 1 API_MODE = 2 - sending_mode = WEBHOOK_MODE + send_url = "https://slack.com/api/chat.postMessage" + channel_url = "https://slack.com/api/conversations.list" def __init__(self, application): self.application = application self.options = {} - # TODO - self._debug = False - self._token = config("notifications.slack.token", None) + self.mode = self.WEBHOOK_MODE + + def set_options(self, options): + self.options = options + return self def send(self, notifiable, notification): """Used to send the notification to slack.""" - method, method_args = self._prepare_slack_message(notifiable, notification) - return method(*method_args) - - def queue(self, notifiable, notification): - """Used to queue the notification to be sent to slack.""" - method, method_args = self._prepare_slack_message(notifiable, notification) - return self.app.make(Queue).push(method, args=method_args) - - def _prepare_slack_message(self, notifiable, notification): - """Prepare slack message to be sent.""" - data = self.get_data("slack", notifiable, notification) - recipients = self.get_recipients(notifiable, notification) - if self.sending_mode == self.WEBHOOK_MODE: - send_method = self.send_via_webhook - else: - send_method = self.send_via_api - return send_method, (data, recipients) - - def get_recipients(self, notifiable, notification): - """Get recipients which can be defined through notifiable route method. - For Slack it can be: - - an incoming webhook (or a list of incoming webhooks) that you defined in your app - return webhook_url - return [webhook_url_1, webhook_url_2] - - a channel name or ID (it will use Slack API and requires a Slack token - for accessing your workspace) - return "{channel_name or channel_id}" - return [channel_name_1, channel_name_2] - You cannot mix both. - """ - recipients = notifiable.route_notification_for("slack", notification) - if isinstance(recipients, list): - _modes = [] - for recipient in recipients: - _modes.append(self._check_recipient_type(recipient)) - if self.API_MODE in _modes and self.WEBHOOK_MODE in _modes: - raise ValueError( - "NotificationSlackDriver: sending mode cannot be mixed." - ) - mode = _modes[0] + self.mode = self.get_sending_mode() + slack_message = self.build(notifiable, notification) + if self.mode == self.WEBHOOK_MODE: + self.send_via_webhook(slack_message) else: - mode = self._check_recipient_type(recipients) + self.send_via_api(slack_message) + + # def queue(self, notifiable, notification): + # TODO + # """Used to queue the notification to be sent to slack.""" + # method, payload = self.prepare(notifiable, notification) + # return self.application.make("queue").push(method, args=payload) + + def build(self, notifiable, notification): + """Build Slack message payload sent to Slack API or through Slack webhook.""" + slack_message = self.get_data("slack", notifiable, notification) + if self.mode == self.WEBHOOK_MODE and not slack_message._webhook: + webhooks = self.get_recipients(notifiable) + slack_message = slack_message.webhook(webhooks) + elif self.mode == self.API_MODE: + if not slack_message._channel: + channels = self.get_recipients(notifiable) + slack_message = slack_message.channel(channels) + if not slack_message._token: + slack_message = slack_message.token(self.options.get("token")) + return slack_message.build().get_options() + + def get_recipients(self, notifiable): + recipients = notifiable.route_notification_for("slack") + if not isinstance(recipients, (list, tuple)): recipients = [recipients] - - self.sending_mode = mode return recipients - def _check_recipient_type(self, recipient): - if recipient.startswith("https://hooks.slack.com"): + def get_sending_mode(self): + # if recipient.startswith("https://hooks.slack.com"): + # return self.WEBHOOK_MODE + # else: + # return self.API_MODE + mode = self.options.get("mode", "webhook") + if mode == "webhook": return self.WEBHOOK_MODE else: return self.API_MODE - def send_via_webhook(self, payload, webhook_urls): - data = payload.as_dict() - if self._debug: - print(data) - for webhook_url in webhook_urls: + def send_via_webhook(self, slack_message): + for webhook_url in slack_message._webhook: response = requests.post( - webhook_url, data=data, headers={"Content-Type": "application/json"} + webhook_url, + data=slack_message, + headers={"Content-Type": "application/json"}, ) if response.status_code != 200: - self._handle_webhook_error(response, data) + raise NotificationException( + "{}. Check Slack webhooks docs.".format(response.text) + ) - def send_via_api(self, payload, channels): + def send_via_api(self, slack_message): """Send Slack notification with Slack Web API as documented here https://api.slack.com/methods/chat.postMessage""" - for channel in channels: - # if notification._as_snippet: - # requests.post('https://slack.com/api/files.upload', { - # 'token': notification._token, - # 'channel': channel, - # 'content': notification._text, - # 'filename': notification._snippet_name, - # 'filetype': notification._type, - # 'initial_comment': notification._initial_comment, - # 'title': notification._title, - # }) - # else: - # use notification defined token else globally configured token - token = payload._token or self._token - channel = self._get_channel_id(channel, token) - payload = { - **payload.as_dict(), - # mandatory - "token": token, - "channel": channel, - } - if self._debug: - print(payload) - self._call_slack_api("https://slack.com/api/chat.postMessage", payload) - - def _call_slack_api(self, url, payload): - response = requests.post(url, payload) - data = response.json() - if not data["ok"]: - self._raise_related_error(data["error"], payload) - else: - return data - - def _handle_webhook_error(self, response, payload): - self._raise_related_error(response.text, payload) - - def _raise_related_error(self, error_key, payload): - if error_key == "invalid_payload": - raise SlackInvalidMessage( - "The message is malformed: perhaps the JSON is structured incorrectly, or the message text is not properly escaped." - ) - elif error_key == "invalid_auth": - raise SlackAPIError( - "Some aspect of authentication cannot be validated. Either the provided token is invalid or the request originates from an IP address disallowed from making the request." - ) - elif error_key == "too_many_attachments": - raise SlackInvalidMessage( - "Too many attachments: the message can have a maximum of 100 attachments associated with it." - ) - elif error_key == "channel_not_found": - raise SlackChannelNotFound( - "The user or channel being addressed either do not exist or is invalid: {}".format( - payload["channel"] - ) - ) - elif error_key == "channel_is_archived": - raise SlackChannelArchived( - "The channel being addressed has been archived and is no longer accepting new messages: {}".format( - payload["channel"] + # TODO: how to get channels + + for channel in slack_message._channel: + channel = self.convert_channel(channel, slack_message._token) + slack_message.to(channel) + response = requests.post(self.send_url, slack_message).json() + if not response["ok"]: + raise NotificationException( + "{}. Check Slack API docs.".format(response["error"]) ) - ) - elif error_key in [ - "action_prohibited", - "posting_to_general_channel_denied", - ]: - raise SlackAPIError( - "You don't have the permission to post to this channel right now: {}".format( - payload["channel"] - ) - ) - elif error_key in ["no_service", "no_service_id"]: - raise SlackInvalidWebhook( - "The provided incoming webhook is either disabled, removed or invalid." - ) - elif error_key in ["no_team", "team_disabled"]: - raise SlackInvalidWorkspace( - "The Slack workspace is no longer active or is missing or invalid." - ) - else: - raise SlackAPIError("{}. Check Slack API docs.".format(error_key)) - - def _get_channel_id(self, name, token): - """"Returns Slack channel's ID from given channel.""" - if "#" in name: - return self._find_channel(name, token) - else: - return name + else: + return response - def _find_channel(self, name, token): - """Calls the Slack API to find the channel name. This is so we do not have to specify the channel ID's. - Slack requires channel ID's to be used. + def convert_channel(self, name, token): + """Calls the Slack API to find the channel ID if not already a channel ID. Arguments: name {string} -- The channel name to find. - - Raises: - SlackChannelNotFound -- Thrown if the channel name is not found. - - Returns: - self """ - response = self._call_slack_api( - "https://slack.com/api/conversations.list", {"token": token} - ) + if "#" not in name: + return name + response = requests.post(self.channel_url, {"token": token}).json() for channel in response["channels"]: if channel["name"] == name.split("#")[1]: return channel["id"] - raise SlackChannelNotFound( - "The user or channel being addressed either do not exist or is invalid: {}".format( - name - ) + raise NotificationException( + f"The user or channel being addressed either do not exist or is invalid: {name}" ) diff --git a/src/masonite/drivers/notification/__init__.py b/src/masonite/drivers/notification/__init__.py index c2aeccae..78725e51 100644 --- a/src/masonite/drivers/notification/__init__.py +++ b/src/masonite/drivers/notification/__init__.py @@ -1,6 +1,6 @@ from .MailDriver import MailDriver from .vonage.VonageDriver import VonageDriver +from .DatabaseDriver import DatabaseDriver +from .SlackDriver import SlackDriver # from .BroadcastDriver import BroadcastDriver -# from .DatabaseDriver import DatabaseDriver -# from .SlackDriver import SlackDriver diff --git a/src/masonite/foundation/Kernel.py b/src/masonite/foundation/Kernel.py index 9da286b1..524a8743 100644 --- a/src/masonite/foundation/Kernel.py +++ b/src/masonite/foundation/Kernel.py @@ -14,6 +14,7 @@ MakeJobCommand, MakeMailableCommand, MakeNotificationCommand, + NotificationTableCommand, ) from ..storage import StorageCapsule from ..auth import Sign @@ -126,5 +127,6 @@ def register_commands(self): MakeJobCommand(self.application), MakeMailableCommand(self.application), MakeNotificationCommand(self.application), + NotificationTableCommand(), ), ) diff --git a/src/masonite/notification/AnonymousNotifiable.py b/src/masonite/notification/AnonymousNotifiable.py index 6a3427f0..d54bf5bc 100644 --- a/src/masonite/notification/AnonymousNotifiable.py +++ b/src/masonite/notification/AnonymousNotifiable.py @@ -5,32 +5,35 @@ class AnonymousNotifiable(Notifiable): """Anonymous notifiable allowing to send notification without having - a notifiable entity.""" + a notifiable entity. - def __init__(self): + Usage: + self.notification.route("sms", "+3346474764").send(WelcomeNotification()) + """ + + def __init__(self, application=None): + self.application = application self._routes = {} - def route(self, channel, route): - """Add routing information to the target.""" - if channel == "database": + def route(self, driver, recipient): + """Define which driver using to route the notification.""" + if driver == "database": raise ValueError( - "The database channel does not support on-demand notifications." + "The database driver does not support on-demand notifications." ) - self._routes[channel] = route + self._routes[driver] = recipient return self - def route_notification_for(self, channel): + def route_notification_for(self, driver): try: - return self._routes[channel] + return self._routes[driver] except KeyError: raise ValueError( - "Routing has not been defined for the channel {}".format(channel) + "Routing has not been defined for the driver {}".format(driver) ) def send(self, notification, dry=False, fail_silently=False): """Send the given notification.""" - from wsgi import application - - return application.make("notification").send( + return self.application.make("notification").send( self, notification, self._routes, dry, fail_silently ) diff --git a/src/masonite/notification/DatabaseNotification.py b/src/masonite/notification/DatabaseNotification.py index 9610fbd8..e5979549 100644 --- a/src/masonite/notification/DatabaseNotification.py +++ b/src/masonite/notification/DatabaseNotification.py @@ -5,7 +5,7 @@ class DatabaseNotification(Model): - """DatabaseNotification Model.""" + """DatabaseNotification Model allowing notifications to be stored in database.""" __fillable__ = ["id", "type", "data", "read_at", "notifiable_id", "notifiable_type"] __table__ = "notifications" diff --git a/src/masonite/notification/Notifiable.py b/src/masonite/notification/Notifiable.py index 7f23f957..c1c46722 100644 --- a/src/masonite/notification/Notifiable.py +++ b/src/masonite/notification/Notifiable.py @@ -5,29 +5,37 @@ from ..exceptions.exceptions import NotificationException -class Notifiable(object): - def notify(self, notification, channels=[], dry=False, fail_silently=False): +class Notifiable: + """Notifiable mixin allowing to send notification to a model. It's often used with the + User model. + + Usage: + user.notify(WelcomeNotification()) + """ + + def notify(self, notification, drivers=[], dry=False, fail_silently=False): """Send the given notification.""" from wsgi import application return application.make("notification").send( - self, notification, channels, dry, fail_silently + self, notification, drivers, dry, fail_silently ) - def route_notification_for(self, channel): - """Get the notification routing information for the given channel.""" + def route_notification_for(self, driver): + """Get the notification routing information for the given driver. If method has not been + defined on the model: for mail driver try to use 'email' field of model.""" # check if routing has been specified on the model - method_name = "route_notification_for_{0}".format(channel) + method_name = "route_notification_for_{0}".format(driver) try: method = getattr(self, method_name) return method() except AttributeError: # if no method is defined on notifiable use default - if channel == "database": + if driver == "database": # with database channel, notifications are saved to database pass - elif channel == "mail": + elif driver == "mail": return self.email else: raise NotificationException( @@ -36,18 +44,20 @@ def route_notification_for(self, channel): @has_many("id", "notifiable_id") def notifications(self): + """Get all notifications sent to the model instance. Only for 'database' + notifications.""" return DatabaseNotification.where("notifiable_type", "users").order_by( "created_at", direction="DESC" ) @property def unread_notifications(self): - """Get the entity's unread notifications. Only for 'database' + """Get the model instance unread notifications. Only for 'database' notifications.""" return self.notifications.where("read_at", "==", None) @property def read_notifications(self): - """Get the entity's read notifications. Only for 'database' + """Get the model instance read notifications. Only for 'database' notifications.""" return self.notifications.where("read_at", "!=", None) diff --git a/src/masonite/notification/Notification.py b/src/masonite/notification/Notification.py index 9b5ef567..1bb7db4f 100644 --- a/src/masonite/notification/Notification.py +++ b/src/masonite/notification/Notification.py @@ -6,7 +6,7 @@ class Notification: def __init__(self, *args, **kwargs): self.id = None - self._run = True + self._dry = False self._fail_silently = False def broadcast_on(self): @@ -19,7 +19,7 @@ def via(self, notifiable): @property def should_send(self): - return self._run + return not self._dry @property def ignore_errors(self): @@ -36,7 +36,7 @@ def dry(self): Returns: self """ - self._run = False + self._dry = True return self def fail_silently(self): diff --git a/src/masonite/notification/NotificationManager.py b/src/masonite/notification/NotificationManager.py index 795deaf2..725ee0a9 100644 --- a/src/masonite/notification/NotificationManager.py +++ b/src/masonite/notification/NotificationManager.py @@ -4,13 +4,16 @@ from ..utils.collections import Collection from ..exceptions.exceptions import NotificationException from ..queues import ShouldQueue +from .AnonymousNotifiable import AnonymousNotifiable class NotificationManager(object): """Notification handler which handle sending/queuing notifications anonymously or to notifiables through different channels.""" - called_notifications = [] + # those classes are use for mock => should we put them only in NotificationMock ? + sent_notifications = {} + dry_notifications = {} def __init__(self, application, driver_config=None): self.application = application @@ -35,8 +38,11 @@ def get_config_options(self, driver): def send( self, notifiables, notification, drivers=[], dry=False, fail_silently=False ): - """Send the given notification to the given notifiables immediately.""" + """Send the given notification to the given notifiables.""" if not notification.should_send or dry: + self.dry_notifications.update( + {notification.__class__.__name__: notifiables} + ) return notifiables = self._format_notifiables(notifiables) @@ -45,15 +51,13 @@ def send( drivers = drivers if drivers else notification.via(notifiable) if not drivers: raise NotificationException( - "No channels have been defined in via() method of {0} class.".format( + "No drivers have been defined in via() method of {0} notification.".format( notification.type() ) ) for driver in drivers: self.options.update(self.get_config_options(driver)) driver_instance = self.get_driver(driver).set_options(self.options) - from .AnonymousNotifiable import AnonymousNotifiable - if isinstance(notifiable, AnonymousNotifiable) and driver == "database": # this case is not possible but that should not stop other channels to be used continue @@ -65,22 +69,22 @@ def send( else: return driver_instance.send(notifiable, notification) except Exception as e: - if notification.ignore_errors or fail_silently: - pass - else: + if not notification.ignore_errors and not fail_silently: raise e # def is_custom_channel(self, channel): # return issubclass(channel, BaseDriver) def _format_notifiables(self, notifiables): - if isinstance(notifiables, list) or isinstance(notifiables, Collection): + if isinstance(notifiables, (list, tuple, Collection)): return notifiables else: return [notifiables] def route(self, driver, route): """Specify how to send a notification to an anonymous notifiable.""" - from .AnonymousNotifiable import AnonymousNotifiable + return AnonymousNotifiable(self.application).route(driver, route) - return AnonymousNotifiable().route(driver, route) + # TESTING + def assertNotificationDried(self, notification_class): + assert notification_class.__name__ in self.dry_notifications.keys() diff --git a/src/masonite/notification/SlackMessage.py b/src/masonite/notification/SlackMessage.py new file mode 100644 index 00000000..037b9375 --- /dev/null +++ b/src/masonite/notification/SlackMessage.py @@ -0,0 +1,158 @@ +"""Class modelling a Slack message.""" +import json + + +class SlackMessage: + def __init__(self): + self._text = "" + self._username = "masonite-bot" + self._icon_emoji = "" + self._icon_url = "" + self._channel = "" + self._text = "" + self._mrkdwn = True + self._as_current_user = False + self._reply_broadcast = False + # Indicates if channel names and usernames should be linked. + self._link_names = False + # Indicates if you want a preview of links inlined in the message. + self._unfurl_links = False + # Indicates if you want a preview of links to media inlined in the message. + self._unfurl_media = False + self._blocks = [] + + self._token = "" + self._webhook = "" + + def from_(self, username, icon=None, url=None): + """Set a custom username and optional emoji icon for the Slack message.""" + self._username = username + if icon: + self._icon_emoji = icon + elif url: + self._icon_url = url + return self + + def to(self, to): + """Specifies the channel to send the message to. It can be a list or single + element. It can be either a channel ID or a channel name (with #), if it's + a channel name the channel ID will be resolved later. + """ + self._to = to + return self + + def text(self, text): + """Specifies the text to be sent in the message. + + Arguments: + text {string} -- The text to show in the message. + + Returns: + self + """ + self._text = text + return self + + def link_names(self): + """Find and link channel names and usernames in message.""" + self._link_names = True + return self + + def unfurl_links(self): + """Whether the message should unfurl any links. + + Unfurling is when it shows a bigger part of the message after the text is sent + like when pasting a link and it showing the header images. + + Returns: + self + """ + self._unfurl_links = True + self._unfurl_media = True + return self + + def without_markdown(self): + """Specifies whether the message should explicitly not honor markdown text. + + Returns: + self + """ + self._mrkdwn = False + return self + + def can_reply(self): + """Whether the message should be ably to be replied back to. + + Returns: + self + """ + self._reply_broadcast = True + return self + + def build(self, *args, **kwargs): + return self + + def get_options(self): + return { + "text": self._text, + # this one is overriden when using api mode + "channel": self._channel, + # optional + "link_names": self._link_names, + "unfurl_links": self._unfurl_links, + "unfurl_media": self._unfurl_media, + "username": self._username, + "as_user": self._as_current_user, + "icon_emoji": self._icon_emoji, + "icon_url": self._icon_url, + "mrkdwn": self._mrkdwn, + "reply_broadcast": self._reply_broadcast, + "blocks": json.dumps([block._resolve() for block in self._blocks]), + } + + def token(self, token): + """[API_MODE only] Specifies the token to use for Slack authentication. + + Arguments: + token {string} -- The Slack authentication token. + + Returns: + self + """ + self._token = token + return self + + def as_current_user(self): + """[API_MODE only] Send message as the currently authenticated user. + + Returns: + self + """ + self._as_current_user = True + return self + + def webhook(self, webhook): + """[WEBHOOK_MODE only] Specifies the webhook to use to send the message and authenticate + to Slack. + + Arguments: + webhook {string} -- Slack configured webhook url. + + Returns: + self + """ + self._webhook = webhook + return self + + def block(self, block_instance): + try: + from slackblocks.blocks import Block + except ImportError: + raise ModuleNotFoundError( + "Could not find the 'slackblocks' library. Run 'pip install slackblocks' to fix this." + ) + + if not isinstance(block_instance, Block): + raise Exception("Blocks should be imported from 'slackblocks' package.") + self._blocks.append(block_instance) + return self diff --git a/src/masonite/notification/__init__.py b/src/masonite/notification/__init__.py index 6ee89054..782acdc9 100644 --- a/src/masonite/notification/__init__.py +++ b/src/masonite/notification/__init__.py @@ -1,5 +1,6 @@ from .NotificationManager import NotificationManager from .MockNotification import MockNotification +from .DatabaseNotification import DatabaseNotification from .Notification import Notification from .Notifiable import Notifiable from .AnonymousNotifiable import AnonymousNotifiable diff --git a/src/masonite/providers/NotificationProvider.py b/src/masonite/providers/NotificationProvider.py index 250bff4d..272450b4 100644 --- a/src/masonite/providers/NotificationProvider.py +++ b/src/masonite/providers/NotificationProvider.py @@ -1,11 +1,11 @@ from .Provider import Provider from ..utils.structures import load from ..drivers.notification import ( + DatabaseDriver, MailDriver, + SlackDriver, VonageDriver, # BroadcastDriver, - # DatabaseDriver, - # SlackDriver, ) from ..notification import NotificationManager from ..notification import MockNotification @@ -23,25 +23,9 @@ def register(self): ) notification_manager.add_driver("mail", MailDriver(self.application)) notification_manager.add_driver("vonage", VonageDriver(self.application)) - # notification_manager.add_driver("database", DatabaseDriver(self.application)) - # notification_manager.add_driver("slack", SlackDriver(self.application)) + notification_manager.add_driver("slack", SlackDriver(self.application)) + notification_manager.add_driver("database", DatabaseDriver(self.application)) # notification_manager.add_driver("broadcast", BroadcastDriver(self.application)) - # TODO: to rewrite - # self.app.bind("NotificationCommand", NotificationCommand()) + self.application.bind("notification", notification_manager) self.application.bind("mock.notification", MockNotification) - - def boot(self): - # TODO: to rewrite - # migration_path = os.path.join(os.path.dirname(__file__), "../migrations") - # config_path = os.path.join(os.path.dirname(__file__), "../config") - # self.publishes( - # {os.path.join(config_path, "notifications.py"): "config/notifications.py"}, - # tag="config", - # ) - # self.publishes_migrations( - # [ - # os.path.join(migration_path, "create_notifications_table.py"), - # ], - # ) - pass diff --git a/src/masonite/stubs/notification/create_notifications_table.py b/src/masonite/stubs/notification/create_notifications_table.py new file mode 100644 index 00000000..b16ea397 --- /dev/null +++ b/src/masonite/stubs/notification/create_notifications_table.py @@ -0,0 +1,17 @@ +from masoniteorm.migrations import Migration + + +class CreateNotificationsTable(Migration): + def up(self): + """Run the migrations.""" + with self.schema.create("notifications") as table: + table.string("id", 36).primary() + table.string("type") + table.text("data") + table.morphs("notifiable") + table.datetime("read_at").nullable() + table.timestamps() + + def down(self): + """Revert the migrations.""" + self.schema.drop("notifications") diff --git a/tests/features/notification/test_anonymous_notifiable.py b/tests/features/notification/test_anonymous_notifiable.py index 3b06dc3b..d0393eb0 100644 --- a/tests/features/notification/test_anonymous_notifiable.py +++ b/tests/features/notification/test_anonymous_notifiable.py @@ -1,11 +1,12 @@ from tests import TestCase from src.masonite.notification import Notification, AnonymousNotifiable +from src.masonite.mail import Mailable class WelcomeNotification(Notification): def to_mail(self, notifiable): - pass + return Mailable().text("Welcome") def via(self, notifiable): return ["mail"] @@ -13,12 +14,14 @@ def via(self, notifiable): class TestAnonymousNotifiable(TestCase): def test_one_routing(self): - notifiable = AnonymousNotifiable().route("mail", "user@example.com") + notifiable = AnonymousNotifiable(self.application).route( + "mail", "user@example.com" + ) self.assertDictEqual({"mail": "user@example.com"}, notifiable._routes) def test_multiple_routing(self): notifiable = ( - AnonymousNotifiable() + AnonymousNotifiable(self.application) .route("mail", "user@example.com") .route("slack", "#general") ) @@ -26,14 +29,28 @@ def test_multiple_routing(self): {"mail": "user@example.com", "slack": "#general"}, notifiable._routes ) + def test_sending_notification(self): + self.application.make("notification").route("mail", "user@example.com").send( + WelcomeNotification() + ) + def test_can_override_dry_when_sending(self): - AnonymousNotifiable().route("mail", "user@example.com").send( + AnonymousNotifiable(self.application).route("mail", "user@example.com").send( WelcomeNotification(), dry=True ) - # TODO: assert it + self.application.make("notification").assertNotificationDried( + WelcomeNotification + ) def test_can_override_fail_silently_when_sending(self): - AnonymousNotifiable().route("mail", "user@example.com").send( - WelcomeNotification(), fail_silently=True + class FailingNotification(Notification): + def to_slack(self, notifiable): + raise Exception("Error") + + def via(self, notifiable): + return ["slack"] + + AnonymousNotifiable(self.application).route("slack", "#general").send( + FailingNotification(), fail_silently=True ) - # TODO: assert it + # no assertion raised :) \ No newline at end of file diff --git a/tests/features/notification/test_database_driver.py b/tests/features/notification/test_database_driver.py new file mode 100644 index 00000000..54180e95 --- /dev/null +++ b/tests/features/notification/test_database_driver.py @@ -0,0 +1,125 @@ +from masoniteorm import connections +from masoniteorm.models import Model +import pendulum + +from tests import TestCase +from src.masonite.tests import DatabaseTransactions +from src.masonite.notification import Notification, Notifiable +from src.masonite.notification import DatabaseNotification + + +class User(Model, Notifiable): + """User Model""" + + __fillable__ = ["name", "email", "password"] + + +class WelcomeNotification(Notification): + def to_database(self, notifiable): + return {"data": "Welcome {0}!".format(notifiable.name)} + + def via(self, notifiable): + return ["database"] + + +notif_data = { + "id": "1234", + "read_at": None, + "type": "TestNotification", + "data": "test", + "notifiable_id": 1, + "notifiable_type": "users", +} + + +class TestDatabaseDriver(TestCase, DatabaseTransactions): + connection = None + + def setUp(self): + super().setUp() + self.notification = self.application.make("notification") + + def test_send_to_notifiable(self): + user = User.find(1) + count = user.notifications.count() + user.notify(WelcomeNotification()) + assert user.notifications.count() == count + 1 + + def test_database_notification_is_created_correctly(self): + user = User.find(1) + notification = user.notify(WelcomeNotification()) + assert notification.id + assert not notification.read_at + assert notification.data == '{"data": "Welcome idmann509!"}' + assert notification.notifiable_id == user.id + assert notification.notifiable_type == "users" + + +class TestDatabaseNotification(TestCase, DatabaseTransactions): + connection = None + + def test_database_notification_read_state(self): + notification = DatabaseNotification.create( + { + **notif_data, + "read_at": pendulum.now().to_datetime_string(), + } + ) + self.assertTrue(notification.is_read) + notification.read_at = None + self.assertFalse(notification.is_read) + + def test_database_notification_unread_state(self): + notification = DatabaseNotification.create( + { + **notif_data, + "read_at": pendulum.yesterday().to_datetime_string(), + } + ) + self.assertFalse(notification.is_unread) + notification.read_at = None + self.assertTrue(notification.is_unread) + + def test_database_notification_mark_as_read(self): + notification = DatabaseNotification.create(notif_data) + notification.mark_as_read() + self.assertNotEqual(None, notification.read_at) + + def test_database_notification_mark_as_unread(self): + notification = DatabaseNotification.create( + { + **notif_data, + "read_at": pendulum.now().to_datetime_string(), + } + ) + notification.mark_as_unread() + self.assertEqual(None, notification.read_at) + + def test_notifiable_get_notifications(self): + user = User.find(1) + self.assertEqual(0, user.notifications.count()) + user.notify(WelcomeNotification()) + self.assertEqual(1, user.notifications.count()) + + def test_notifiable_get_read_notifications(self): + user = User.find(1) + self.assertEqual(0, user.read_notifications.count()) + DatabaseNotification.create( + { + **notif_data, + "read_at": pendulum.yesterday().to_datetime_string(), + "notifiable_id": user.id, + } + ) + self.assertEqual(1, user.read_notifications.count()) + + def test_notifiable_get_unread_notifications(self): + user = User.find(1) + self.assertEqual(0, user.unread_notifications.count()) + DatabaseNotification.create( + { + **notif_data, + "notifiable_id": user.id, + } + ) + self.assertEqual(1, user.unread_notifications.count()) \ No newline at end of file diff --git a/tests/features/notification/test_slack_driver.py b/tests/features/notification/test_slack_driver.py new file mode 100644 index 00000000..074c1110 --- /dev/null +++ b/tests/features/notification/test_slack_driver.py @@ -0,0 +1,85 @@ +from tests import TestCase +import responses +from src.masonite.notification import Notification, Notifiable, SlackMessage + +from masoniteorm.models import Model + +# fake webhook for tests +webhook_url = "https://hooks.slack.com/services/X/Y/Z" +webhook_url_2 = "https://hooks.slack.com/services/A/B/C" + + +class User(Model, Notifiable): + """User Model""" + + __fillable__ = ["name", "email", "password", "phone"] + + def route_notification_for_slack(self): + return "#general" + return ["#general", "#news"] + return webhook_url + return [webhook_url, webhook_url_2] + + +# class WelcomeUserNotification(Notification): +# def to_slack(self, notifiable): +# return Sms().to(notifiable.phone).text("Welcome !").from_("123456") + +# def via(self, notifiable): +# return ["slack"] + + +class WelcomeNotification(Notification): + def to_slack(self, notifiable): + return SlackMessage().text("Welcome !").from_("test-bot") + + def via(self, notifiable): + return ["slack"] + + +class OtherNotification(Notification): + def to_slack(self, notifiable): + return ( + SlackMessage() + .channel(["#general", "#news"]) + .text("Welcome !") + .from_("test-bot") + ) + + def via(self, notifiable): + return ["slack"] + + +class WebhookNotification(Notification): + def to_slack(self, notifiable): + return SlackMessage().webhook(webhook_url).text("Welcome !").from_("test-bot") + + def via(self, notifiable): + return ["slack"] + + +class TestSlackDriver(TestCase): + def setUp(self): + super().setUp() + self.notification = self.application.make("notification") + + @responses.activate + def test_sending_to_anonymous_via_webhook(self): + responses.add(responses.POST, webhook_url, body=b"ok") + self.notification.route("slack", webhook_url).notify(WelcomeNotification()) + self.assertTrue(responses.assert_call_count(webhook_url, 1)) + + # def test_send_to_anonymous(self): + # with patch("vonage.sms.Sms") as MockSmsClass: + # MockSmsClass.return_value.send_message.return_value = ( + # VonageAPIMock().send_success() + # ) + # self.notification.route("slack", "#general").send(WelcomeNotification()) + + # def test_send_to_notifiable(self): + # with patch("vonage.sms.Sms") as MockSmsClass: + # MockSmsClass.return_value.send_message.return_value = ( + # VonageAPIMock().send_success() + # ) + # user = User.find(1) + # user.notify(WelcomeUserNotification()) \ No newline at end of file diff --git a/tests/features/notification/test_slack_message.py b/tests/features/notification/test_slack_message.py new file mode 100644 index 00000000..fc6f89df --- /dev/null +++ b/tests/features/notification/test_slack_message.py @@ -0,0 +1,63 @@ +from tests import TestCase +from src.masonite.notification import SlackMessage + + +class WelcomeToSlack(SlackMessage): + def build(self): + return ( + self.to("#general") + .from_("sam") + .text("Hello from Masonite!") + .link_names() + .unfurl_links() + .without_markdown() + .can_reply() + ) + + +class TestSlackMessage(TestCase): + def test_build_message(self): + slack_message = WelcomeToSlack().build().get_options() + self.assertEqual(slack_message.get("channel"), "#general") + self.assertEqual(slack_message.get("username"), "sam") + self.assertEqual(slack_message.get("text"), "Hello from Masonite!") + self.assertEqual(slack_message.get("link_names"), True) + self.assertEqual(slack_message.get("unfurl_links"), True) + self.assertEqual(slack_message.get("unfurl_media"), True) + self.assertEqual(slack_message.get("mrkdwn"), False) + self.assertEqual(slack_message.get("reply_broadcast"), True) + self.assertEqual(slack_message.get("as_user"), False) + self.assertEqual(slack_message.get("blocks"), "[]") + + def test_from_options(self): + slack_message = SlackMessage().from_("sam", icon=":ghost").build().get_options() + self.assertEqual(slack_message.get("username"), "sam") + self.assertEqual(slack_message.get("icon_emoji"), ":ghost") + self.assertEqual(slack_message.get("icon_url"), "") + slack_message = SlackMessage().from_("sam", url="#").build().get_options() + self.assertEqual(slack_message.get("username"), "sam") + self.assertEqual(slack_message.get("icon_url"), "#") + self.assertEqual(slack_message.get("icon_emoji"), "") + + def test_build_with_blocks(self): + from slackblocks import DividerBlock, HeaderBlock + + slack_message = ( + SlackMessage() + .from_("Sam") + .text("Hello") + .block(HeaderBlock("Header title", block_id="1")) + .block(DividerBlock(block_id="2")) + .build() + .get_options() + ) + self.assertEqual( + slack_message.get("blocks"), + '[{"type": "header", "block_id": "1", "text": {"type": "plain_text", "text": "Header title"}}, {"type": "divider", "block_id": "2"}]', + ) + + def test_api_mode_options(self): + slack_message = SlackMessage().as_current_user().token("123456") + self.assertEqual(slack_message._token, "123456") + slack_message_options = slack_message.build().get_options() + self.assertEqual(slack_message_options.get("as_user"), True) diff --git a/tests/features/notification/test_vonage_driver.py b/tests/features/notification/test_vonage_driver.py index 4807b80e..41e04147 100644 --- a/tests/features/notification/test_vonage_driver.py +++ b/tests/features/notification/test_vonage_driver.py @@ -1,5 +1,4 @@ from tests import TestCase -import pytest from unittest.mock import patch from src.masonite.notification import Notification, Notifiable, Sms from src.masonite.exceptions import NotificationException diff --git a/tests/integrations/config/notification.py b/tests/integrations/config/notification.py index 1b96c477..59de99bc 100644 --- a/tests/integrations/config/notification.py +++ b/tests/integrations/config/notification.py @@ -2,7 +2,11 @@ import os DRIVERS = { - "slack": {"token": os.getenv("SLACK_TOKEN", "")}, + "slack": { + "token": os.getenv("SLACK_TOKEN", ""), # used for API mode + "webhook": os.getenv("SLACK_WEBHOOK", ""), # used for webhook mode + "mode": os.getenv("SLACK_MODE", "webhook"), # webhook or api + }, "vonage": { "key": os.getenv("VONAGE_KEY", ""), "secret": os.getenv("VONAGE_SECRET", ""), diff --git a/tests/integrations/databases/migrations/2021_03_18_190410_create_notifications_table.py b/tests/integrations/databases/migrations/2021_03_18_190410_create_notifications_table.py new file mode 100644 index 00000000..b16ea397 --- /dev/null +++ b/tests/integrations/databases/migrations/2021_03_18_190410_create_notifications_table.py @@ -0,0 +1,17 @@ +from masoniteorm.migrations import Migration + + +class CreateNotificationsTable(Migration): + def up(self): + """Run the migrations.""" + with self.schema.create("notifications") as table: + table.string("id", 36).primary() + table.string("type") + table.text("data") + table.morphs("notifiable") + table.datetime("read_at").nullable() + table.timestamps() + + def down(self): + """Revert the migrations.""" + self.schema.drop("notifications") From 63744a6584148c444f7e85c397c8c79e6f372b75 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Thu, 18 Mar 2021 19:39:19 +0100 Subject: [PATCH 18/54] update database driver to use builder --- src/masonite/drivers/notification/DatabaseDriver.py | 12 +++++++++--- tests/features/notification/test_database_driver.py | 10 +++++----- tests/integrations/config/notification.py | 4 ++++ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/masonite/drivers/notification/DatabaseDriver.py b/src/masonite/drivers/notification/DatabaseDriver.py index 229f93e2..52886e0e 100644 --- a/src/masonite/drivers/notification/DatabaseDriver.py +++ b/src/masonite/drivers/notification/DatabaseDriver.py @@ -1,7 +1,6 @@ """Database notification driver.""" import json -from ...notification import DatabaseNotification from .BaseDriver import BaseDriver @@ -14,16 +13,23 @@ def set_options(self, options): self.options = options return self + def get_builder(self): + return ( + self.application.make("builder") + .on(self.options.get("connection")) + .table(self.options.get("table")) + ) + def send(self, notifiable, notification): """Used to send the email and run the logic for sending emails.""" data = self.build(notifiable, notification) - return DatabaseNotification.create(data) + return self.get_builder().new().create(data) def queue(self, notifiable, notification): """Used to queue the database notification creation.""" data = self.build(notifiable, notification) return self.application.make("queue").push( - DatabaseNotification.create, args=(data,) + self.get_builder().new().create, args=(data,) ) def build(self, notifiable, notification): diff --git a/tests/features/notification/test_database_driver.py b/tests/features/notification/test_database_driver.py index 54180e95..6a80628f 100644 --- a/tests/features/notification/test_database_driver.py +++ b/tests/features/notification/test_database_driver.py @@ -48,11 +48,11 @@ def test_send_to_notifiable(self): def test_database_notification_is_created_correctly(self): user = User.find(1) notification = user.notify(WelcomeNotification()) - assert notification.id - assert not notification.read_at - assert notification.data == '{"data": "Welcome idmann509!"}' - assert notification.notifiable_id == user.id - assert notification.notifiable_type == "users" + assert notification["id"] + assert not notification["read_at"] + assert notification["data"] == '{"data": "Welcome idmann509!"}' + assert notification["notifiable_id"] == user.id + assert notification["notifiable_type"] == "users" class TestDatabaseNotification(TestCase, DatabaseTransactions): diff --git a/tests/integrations/config/notification.py b/tests/integrations/config/notification.py index 59de99bc..feb676f2 100644 --- a/tests/integrations/config/notification.py +++ b/tests/integrations/config/notification.py @@ -12,6 +12,10 @@ "secret": os.getenv("VONAGE_SECRET", ""), "sms_from": os.getenv("VONAGE_SMS_FROM", "+33000000000"), }, + "database": { + "connection": "sqlite", + "table": "notifications", + }, } DRY = False \ No newline at end of file From f55a792d8a19b477687f60e91790e1c2e0674d7e Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Thu, 18 Mar 2021 19:52:05 +0100 Subject: [PATCH 19/54] start adding broadcast driver for notifications --- .../drivers/notification/BroadcastDriver.py | 71 ++++--------------- src/masonite/drivers/notification/__init__.py | 7 +- .../providers/NotificationProvider.py | 4 +- 3 files changed, 18 insertions(+), 64 deletions(-) diff --git a/src/masonite/drivers/notification/BroadcastDriver.py b/src/masonite/drivers/notification/BroadcastDriver.py index 1b89ec56..e2a7b9d4 100644 --- a/src/masonite/drivers/notification/BroadcastDriver.py +++ b/src/masonite/drivers/notification/BroadcastDriver.py @@ -1,9 +1,5 @@ -"""Broadcast driver Class.""" -from masonite.app import App -from masonite import Queue -from masonite.helpers import config +"""Broadcast notification driver.""" -from ..exceptions import BroadcastOnNotImplemented from .BaseDriver import BaseDriver @@ -11,65 +7,24 @@ class BroadcastDriver(BaseDriver): def __init__(self, application): self.application = application self.options = {} - # TODO - self._driver = None - def driver(self, driver): - """Specifies the driver to use. - - Arguments: - driver {string} -- The name of the driver. - - Returns: - self - """ - self._driver = driver + def set_options(self, options): + self.options = options return self def send(self, notifiable, notification): """Used to broadcast a notification.""" - channels, data, driver = self._prepare_message_to_broadcast( - notifiable, notification + data = self.get_data("broadcast", notifiable, notification) + channels = notification.broadcast_on() or notifiable.route_notification_for( + "broadcast" ) - for channel in channels: - driver.channel(channel, data) + self.application.make("broadcast").channel(channels, data) def queue(self, notifiable, notification): """Used to queue the notification to be broadcasted.""" - channels, data, driver = self._prepare_message_to_broadcast( - notifiable, notification - ) - for channel in channels: - self.app.make(Queue).push(driver.channel, args=(channel, data)) - - def _prepare_message_to_broadcast(self, notifiable, notification): - data = self.get_data("broadcast", notifiable, notification) - driver_instance = self.get_broadcast_driver() - channels = self.broadcast_on(notifiable, notification) - return channels, data, driver_instance - - def get_broadcast_driver(self): - """Shortcut method to get given broadcast driver instance.""" - driver = config("broadcast.driver") if not self._driver else None - return self.app.make("BroadcastManager").driver(driver) - - def broadcast_on(self, notifiable, notification): - """Get the channels the notification should be broadcasted on.""" - channels = notification.broadcast_on() - if not channels: - from ..AnonymousNotifiable import AnonymousNotifiable - - if isinstance(notifiable, AnonymousNotifiable): - channels = notifiable.route_notification_for("broadcast", notification) - else: - try: - channels = notifiable.receives_broadcast_notifications_on() - except AttributeError: - raise BroadcastOnNotImplemented( - """No broadcast channels defined for the Notification with broadcast_on(), - receives_broadcast_notifications_on() must be defined on the Notifiable.""" - ) - - if isinstance(channels, str): - channels = [channels] - return channels + # Makes sense ?????? + # data = self.get_data("broadcast", notifiable, notification) + # channels = notification.broadcast_on() or notifiable.route_notification_for( + # "broadcast" + # ) + # self.application.make("queue").push(driver.channel, args=(channel, data)) diff --git a/src/masonite/drivers/notification/__init__.py b/src/masonite/drivers/notification/__init__.py index 78725e51..04c637c2 100644 --- a/src/masonite/drivers/notification/__init__.py +++ b/src/masonite/drivers/notification/__init__.py @@ -1,6 +1,5 @@ -from .MailDriver import MailDriver -from .vonage.VonageDriver import VonageDriver +from .BroadcastDriver import BroadcastDriver from .DatabaseDriver import DatabaseDriver +from .MailDriver import MailDriver from .SlackDriver import SlackDriver - -# from .BroadcastDriver import BroadcastDriver +from .vonage.VonageDriver import VonageDriver diff --git a/src/masonite/providers/NotificationProvider.py b/src/masonite/providers/NotificationProvider.py index 272450b4..b9265e14 100644 --- a/src/masonite/providers/NotificationProvider.py +++ b/src/masonite/providers/NotificationProvider.py @@ -1,11 +1,11 @@ from .Provider import Provider from ..utils.structures import load from ..drivers.notification import ( + BroadcastDriver, DatabaseDriver, MailDriver, SlackDriver, VonageDriver, - # BroadcastDriver, ) from ..notification import NotificationManager from ..notification import MockNotification @@ -25,7 +25,7 @@ def register(self): notification_manager.add_driver("vonage", VonageDriver(self.application)) notification_manager.add_driver("slack", SlackDriver(self.application)) notification_manager.add_driver("database", DatabaseDriver(self.application)) - # notification_manager.add_driver("broadcast", BroadcastDriver(self.application)) + notification_manager.add_driver("broadcast", BroadcastDriver(self.application)) self.application.bind("notification", notification_manager) self.application.bind("mock.notification", MockNotification) From ed2649b3c77448b2f0b650f0c589ecd147a4e97e Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Thu, 18 Mar 2021 20:01:34 +0100 Subject: [PATCH 20/54] add broadcast driver --- .../drivers/notification/BroadcastDriver.py | 3 +- .../notification/NotificationManager.py | 4 +-- .../notification/test_broadcast_driver.py | 33 +++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 tests/features/notification/test_broadcast_driver.py diff --git a/src/masonite/drivers/notification/BroadcastDriver.py b/src/masonite/drivers/notification/BroadcastDriver.py index e2a7b9d4..e9d4db5e 100644 --- a/src/masonite/drivers/notification/BroadcastDriver.py +++ b/src/masonite/drivers/notification/BroadcastDriver.py @@ -18,7 +18,8 @@ def send(self, notifiable, notification): channels = notification.broadcast_on() or notifiable.route_notification_for( "broadcast" ) - self.application.make("broadcast").channel(channels, data) + event = notification.type() + self.application.make("broadcast").channel(channels, event, data) def queue(self, notifiable, notification): """Used to queue the notification to be broadcasted.""" diff --git a/src/masonite/notification/NotificationManager.py b/src/masonite/notification/NotificationManager.py index 725ee0a9..aafbd6d6 100644 --- a/src/masonite/notification/NotificationManager.py +++ b/src/masonite/notification/NotificationManager.py @@ -40,9 +40,7 @@ def send( ): """Send the given notification to the given notifiables.""" if not notification.should_send or dry: - self.dry_notifications.update( - {notification.__class__.__name__: notifiables} - ) + self.dry_notifications.update({notification.type(): notifiables}) return notifiables = self._format_notifiables(notifiables) diff --git a/tests/features/notification/test_broadcast_driver.py b/tests/features/notification/test_broadcast_driver.py new file mode 100644 index 00000000..dcef1e45 --- /dev/null +++ b/tests/features/notification/test_broadcast_driver.py @@ -0,0 +1,33 @@ +from tests import TestCase +from src.masonite.notification import Notification, Notifiable +from masoniteorm.models import Model + + +class User(Model, Notifiable): + """User Model""" + + __fillable__ = ["name", "email", "password"] + + def route_notification_for_broadcast(self): + return f"user.{self.id}" + + +class WelcomeNotification(Notification): + def to_broadcast(self, notifiable): + return {"data": "Welcome"} + + def via(self, notifiable): + return ["broadcast"] + + +class TestBroadcastDriver(TestCase): + def setUp(self): + super().setUp() + self.notification = self.application.make("notification") + + def test_send_to_anonymous(self): + self.notification.route("broadcast", "all").send(WelcomeNotification()) + + def test_send_to_notifiable(self): + user = User.find(1) + user.notify(WelcomeNotification()) From 0b2654aa6c06d0fa33897db43e5675f65557976b Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Thu, 18 Mar 2021 20:20:12 +0100 Subject: [PATCH 21/54] add responses dependency for mocking purposes --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index a29d8ad3..054dc13d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,5 @@ black cryptography masonite-orm python-dotenv -waitress \ No newline at end of file +waitress +responses \ No newline at end of file From e96a6cbda1bdf75071b5b410f8de49d8b3f4933e Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Thu, 18 Mar 2021 20:21:08 +0100 Subject: [PATCH 22/54] add missing hashids module --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 054dc13d..44f59152 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,5 @@ cryptography masonite-orm python-dotenv waitress -responses \ No newline at end of file +responses +hashids \ No newline at end of file From 73d17f2423d8057a7404ae5aa4000aa281300648 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Thu, 18 Mar 2021 20:25:23 +0100 Subject: [PATCH 23/54] fix notification provider by adding boot() back --- src/masonite/providers/NotificationProvider.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/masonite/providers/NotificationProvider.py b/src/masonite/providers/NotificationProvider.py index b9265e14..fb266894 100644 --- a/src/masonite/providers/NotificationProvider.py +++ b/src/masonite/providers/NotificationProvider.py @@ -29,3 +29,6 @@ def register(self): self.application.bind("notification", notification_manager) self.application.bind("mock.notification", MockNotification) + + def boot(self): + pass \ No newline at end of file From ea578efa9f4b7f07ccc4f0a17399318047e0371e Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Thu, 18 Mar 2021 20:29:18 +0100 Subject: [PATCH 24/54] fix linting --- src/masonite/notification/__init__.py | 2 +- src/masonite/providers/NotificationProvider.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/masonite/notification/__init__.py b/src/masonite/notification/__init__.py index 782acdc9..c32e771c 100644 --- a/src/masonite/notification/__init__.py +++ b/src/masonite/notification/__init__.py @@ -5,4 +5,4 @@ from .Notifiable import Notifiable from .AnonymousNotifiable import AnonymousNotifiable from .Sms import Sms -from .SlackMessage import SlackMessage \ No newline at end of file +from .SlackMessage import SlackMessage diff --git a/src/masonite/providers/NotificationProvider.py b/src/masonite/providers/NotificationProvider.py index fb266894..426792c1 100644 --- a/src/masonite/providers/NotificationProvider.py +++ b/src/masonite/providers/NotificationProvider.py @@ -31,4 +31,4 @@ def register(self): self.application.bind("mock.notification", MockNotification) def boot(self): - pass \ No newline at end of file + pass From e7aece4e5faf4feb7e9b165d3a919809d865b0a2 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Sat, 10 Apr 2021 18:25:23 +0200 Subject: [PATCH 25/54] update tests --- .../drivers/notification/SlackDriver.py | 56 ++++--- .../notification/NotificationManager.py | 6 +- src/masonite/notification/SlackMessage.py | 16 +- .../notification/test_slack_driver.py | 157 +++++++++++++----- .../notification/test_slack_message.py | 1 + .../notification/test_vonage_driver.py | 9 +- tests/integrations/config/notification.py | 2 +- 7 files changed, 171 insertions(+), 76 deletions(-) diff --git a/src/masonite/drivers/notification/SlackDriver.py b/src/masonite/drivers/notification/SlackDriver.py index a7f01d32..3da75ba8 100644 --- a/src/masonite/drivers/notification/SlackDriver.py +++ b/src/masonite/drivers/notification/SlackDriver.py @@ -23,9 +23,8 @@ def set_options(self, options): def send(self, notifiable, notification): """Used to send the notification to slack.""" - self.mode = self.get_sending_mode() slack_message = self.build(notifiable, notification) - if self.mode == self.WEBHOOK_MODE: + if slack_message._mode == self.WEBHOOK_MODE: self.send_via_webhook(slack_message) else: self.send_via_api(slack_message) @@ -39,16 +38,18 @@ def send(self, notifiable, notification): def build(self, notifiable, notification): """Build Slack message payload sent to Slack API or through Slack webhook.""" slack_message = self.get_data("slack", notifiable, notification) - if self.mode == self.WEBHOOK_MODE and not slack_message._webhook: - webhooks = self.get_recipients(notifiable) - slack_message = slack_message.webhook(webhooks) - elif self.mode == self.API_MODE: - if not slack_message._channel: - channels = self.get_recipients(notifiable) - slack_message = slack_message.channel(channels) + recipients = self.get_recipients(notifiable) + mode = self.get_sending_mode(recipients) + slack_message = slack_message.mode(mode) + + if mode == self.WEBHOOK_MODE: # and not slack_message._webhook: + slack_message = slack_message.to(recipients) + elif mode == self.API_MODE: + # if not slack_message._channel: + slack_message = slack_message.to(recipients) if not slack_message._token: slack_message = slack_message.token(self.options.get("token")) - return slack_message.build().get_options() + return slack_message def get_recipients(self, notifiable): recipients = notifiable.route_notification_for("slack") @@ -56,22 +57,24 @@ def get_recipients(self, notifiable): recipients = [recipients] return recipients - def get_sending_mode(self): - # if recipient.startswith("https://hooks.slack.com"): - # return self.WEBHOOK_MODE - # else: - # return self.API_MODE - mode = self.options.get("mode", "webhook") - if mode == "webhook": - return self.WEBHOOK_MODE - else: - return self.API_MODE + def get_sending_mode(self, recipients): + modes = [] + for recipient in recipients: + if recipient.startswith("https://hooks.slack.com"): + modes.append(self.WEBHOOK_MODE) + else: + modes.append(self.API_MODE) + if len(set(modes)) > 1: + raise NotificationException("Slack sending mode cannot be mixed.") + return modes[0] def send_via_webhook(self, slack_message): - for webhook_url in slack_message._webhook: + webhook_urls = slack_message._to + payload = slack_message.build().get_options() + for webhook_url in webhook_urls: response = requests.post( webhook_url, - data=slack_message, + payload, headers={"Content-Type": "application/json"}, ) if response.status_code != 200: @@ -82,12 +85,13 @@ def send_via_webhook(self, slack_message): def send_via_api(self, slack_message): """Send Slack notification with Slack Web API as documented here https://api.slack.com/methods/chat.postMessage""" - # TODO: how to get channels - - for channel in slack_message._channel: + channels = slack_message._to + for channel in channels: channel = self.convert_channel(channel, slack_message._token) + # set only one recipient at a time slack_message.to(channel) - response = requests.post(self.send_url, slack_message).json() + payload = slack_message.build().get_options() + response = requests.post(self.send_url, payload).json() if not response["ok"]: raise NotificationException( "{}. Check Slack API docs.".format(response["error"]) diff --git a/src/masonite/notification/NotificationManager.py b/src/masonite/notification/NotificationManager.py index aafbd6d6..c4aa536a 100644 --- a/src/masonite/notification/NotificationManager.py +++ b/src/masonite/notification/NotificationManager.py @@ -23,13 +23,14 @@ def __init__(self, application, driver_config=None): def add_driver(self, name, driver): self.drivers.update({name: driver}) + self.get_driver(name).set_options(self.get_config_options(name)) def get_driver(self, name): return self.drivers[name] def set_configuration(self, config): self.driver_config = config.DRIVERS - self.options = {"dry": config.DRY} + self.options.update({"dry": config.DRY}) return self def get_config_options(self, driver): @@ -54,8 +55,7 @@ def send( ) ) for driver in drivers: - self.options.update(self.get_config_options(driver)) - driver_instance = self.get_driver(driver).set_options(self.options) + driver_instance = self.get_driver(driver) if isinstance(notifiable, AnonymousNotifiable) and driver == "database": # this case is not possible but that should not stop other channels to be used continue diff --git a/src/masonite/notification/SlackMessage.py b/src/masonite/notification/SlackMessage.py index 037b9375..18131787 100644 --- a/src/masonite/notification/SlackMessage.py +++ b/src/masonite/notification/SlackMessage.py @@ -3,12 +3,14 @@ class SlackMessage: + WEBHOOK_MODE = 1 + API_MODE = 2 + def __init__(self): self._text = "" self._username = "masonite-bot" self._icon_emoji = "" self._icon_url = "" - self._channel = "" self._text = "" self._mrkdwn = True self._as_current_user = False @@ -23,6 +25,7 @@ def __init__(self): self._token = "" self._webhook = "" + self._mode = None def from_(self, username, icon=None, url=None): """Set a custom username and optional emoji icon for the Slack message.""" @@ -93,10 +96,8 @@ def build(self, *args, **kwargs): return self def get_options(self): - return { + options = { "text": self._text, - # this one is overriden when using api mode - "channel": self._channel, # optional "link_names": self._link_names, "unfurl_links": self._unfurl_links, @@ -109,6 +110,9 @@ def get_options(self): "reply_broadcast": self._reply_broadcast, "blocks": json.dumps([block._resolve() for block in self._blocks]), } + if self._mode == self.API_MODE: + options.update({"channel": self._to, "token": self._token}) + return options def token(self, token): """[API_MODE only] Specifies the token to use for Slack authentication. @@ -156,3 +160,7 @@ def block(self, block_instance): raise Exception("Blocks should be imported from 'slackblocks' package.") self._blocks.append(block_instance) return self + + def mode(self, mode): + self._mode = mode + return self diff --git a/tests/features/notification/test_slack_driver.py b/tests/features/notification/test_slack_driver.py index 074c1110..120c8963 100644 --- a/tests/features/notification/test_slack_driver.py +++ b/tests/features/notification/test_slack_driver.py @@ -1,6 +1,7 @@ -from tests import TestCase import responses +from tests import TestCase from src.masonite.notification import Notification, Notifiable, SlackMessage +from src.masonite.exceptions import NotificationException from masoniteorm.models import Model @@ -9,24 +10,25 @@ webhook_url_2 = "https://hooks.slack.com/services/A/B/C" +def route_for_slack(self): + return "#bot" + + class User(Model, Notifiable): """User Model""" __fillable__ = ["name", "email", "password", "phone"] def route_notification_for_slack(self): - return "#general" - return ["#general", "#news"] - return webhook_url - return [webhook_url, webhook_url_2] + return route_for_slack(self) -# class WelcomeUserNotification(Notification): -# def to_slack(self, notifiable): -# return Sms().to(notifiable.phone).text("Welcome !").from_("123456") +class WelcomeUserNotification(Notification): + def to_slack(self, notifiable): + return SlackMessage().text(f"Welcome {notifiable.name}!").from_("test-bot") -# def via(self, notifiable): -# return ["slack"] + def via(self, notifiable): + return ["slack"] class WelcomeNotification(Notification): @@ -40,46 +42,125 @@ def via(self, notifiable): class OtherNotification(Notification): def to_slack(self, notifiable): return ( - SlackMessage() - .channel(["#general", "#news"]) - .text("Welcome !") - .from_("test-bot") + SlackMessage().to(["#general", "#news"]).text("Welcome !").from_("test-bot") ) def via(self, notifiable): return ["slack"] -class WebhookNotification(Notification): - def to_slack(self, notifiable): - return SlackMessage().webhook(webhook_url).text("Welcome !").from_("test-bot") - - def via(self, notifiable): - return ["slack"] - - -class TestSlackDriver(TestCase): +class TestSlackWebhookDriver(TestCase): def setUp(self): super().setUp() self.notification = self.application.make("notification") @responses.activate - def test_sending_to_anonymous_via_webhook(self): + def test_sending_to_anonymous(self): responses.add(responses.POST, webhook_url, body=b"ok") self.notification.route("slack", webhook_url).notify(WelcomeNotification()) self.assertTrue(responses.assert_call_count(webhook_url, 1)) - # def test_send_to_anonymous(self): - # with patch("vonage.sms.Sms") as MockSmsClass: - # MockSmsClass.return_value.send_message.return_value = ( - # VonageAPIMock().send_success() - # ) - # self.notification.route("slack", "#general").send(WelcomeNotification()) - - # def test_send_to_notifiable(self): - # with patch("vonage.sms.Sms") as MockSmsClass: - # MockSmsClass.return_value.send_message.return_value = ( - # VonageAPIMock().send_success() - # ) - # user = User.find(1) - # user.notify(WelcomeUserNotification()) \ No newline at end of file + @responses.activate + def test_sending_to_notifiable(self): + responses.add(responses.POST, webhook_url, body=b"ok") + User.route_notification_for_slack = lambda notifiable: webhook_url + user = User.find(1) + user.notify(WelcomeNotification()) + self.assertTrue(responses.assert_call_count(webhook_url, 1)) + User.route_notification_for_slack = route_for_slack + + @responses.activate + def test_sending_to_multiple_webhooks(self): + responses.add(responses.POST, webhook_url, body=b"ok") + responses.add(responses.POST, webhook_url_2, body=b"ok") + User.route_notification_for_slack = lambda notifiable: [ + webhook_url, + webhook_url_2, + ] + user = User.find(1) + user.notify(WelcomeNotification()) + self.assertTrue(responses.assert_call_count(webhook_url, 1)) + self.assertTrue(responses.assert_call_count(webhook_url_2, 1)) + User.route_notification_for_slack = route_for_slack + + +class TestSlackAPIDriver(TestCase): + url = "https://slack.com/api/chat.postMessage" + channel_url = "https://slack.com/api/conversations.list" + + def setUp(self): + super().setUp() + self.notification = self.application.make("notification") + + def test_sending_without_credentials(self): + with self.assertRaises(NotificationException) as e: + self.notification.route("slack", "123456").notify(WelcomeNotification()) + self.assertIn("not_authed", str(e.exception)) + + @responses.activate + def test_sending_to_anonymous(self): + responses.add( + responses.POST, + self.url, + body=b'{"ok": "True"}', + ) + responses.add( + responses.POST, + self.channel_url, + body=b'{"channels": [{"name": "bot", "id": "123"}]}', + ) + self.notification.route("slack", "#bot").notify(WelcomeNotification()) + # to convert #bot to Channel ID + self.assertTrue(responses.assert_call_count(self.channel_url, 1)) + self.assertTrue(responses.assert_call_count(self.url, 1)) + + @responses.activate + def test_sending_to_notifiable(self): + user = User.find(1) + responses.add( + responses.POST, + self.url, + body=b'{"ok": "True"}', + ) + responses.add( + responses.POST, + self.channel_url, + body=b'{"channels": [{"name": "bot", "id": "123"}]}', + ) + user.notify(WelcomeUserNotification()) + self.assertTrue(responses.assert_call_count(self.url, 1)) + + @responses.activate + def test_sending_to_multiple_channels(self): + user = User.find(1) + responses.add( + responses.POST, + self.url, + body=b'{"ok": "True"}', + ) + responses.add( + responses.POST, + self.channel_url, + body=b'{"channels": [{"name": "bot", "id": "123"}, {"name": "general", "id": "456"}]}', + ) + user.notify(OtherNotification()) + # TODO: does not work because user defined routing takes precedence. Should it be like this ?? + self.assertTrue(responses.assert_call_count(self.channel_url, 2)) + self.assertTrue(responses.assert_call_count(self.url, 2)) + + @responses.activate + def test_convert_channel(self): + channel_id = self.notification.get_driver("slack").convert_channel( + "123456", "token" + ) + self.assertEqual(channel_id, "123456") + + responses.add( + responses.POST, + self.channel_url, + body=b'{"channels": [{"name": "general", "id": "654321"}]}', + ) + channel_id = self.notification.get_driver("slack").convert_channel( + "#general", "token" + ) + self.assertEqual(channel_id, "654321") diff --git a/tests/features/notification/test_slack_message.py b/tests/features/notification/test_slack_message.py index fc6f89df..83d42f48 100644 --- a/tests/features/notification/test_slack_message.py +++ b/tests/features/notification/test_slack_message.py @@ -12,6 +12,7 @@ def build(self): .unfurl_links() .without_markdown() .can_reply() + .mode(2) # API MODE ) diff --git a/tests/features/notification/test_vonage_driver.py b/tests/features/notification/test_vonage_driver.py index 41e04147..f1bc461d 100644 --- a/tests/features/notification/test_vonage_driver.py +++ b/tests/features/notification/test_vonage_driver.py @@ -92,7 +92,8 @@ def test_send_to_notifiable_with_route_notification_for(self): user.notify(WelcomeNotification()) def test_global_send_from_is_used_when_not_specified(self): - self.notification.route("vonage", "+33123456789").send(OtherNotification()) - import pdb - - pdb.set_trace() + notifiable = self.notification.route("vonage", "+33123456789") + sms = self.notification.get_driver("vonage").build( + notifiable, OtherNotification() + ) + self.assertEqual(sms.get("from"), "+33000000000") diff --git a/tests/integrations/config/notification.py b/tests/integrations/config/notification.py index feb676f2..d8b9a857 100644 --- a/tests/integrations/config/notification.py +++ b/tests/integrations/config/notification.py @@ -5,7 +5,7 @@ "slack": { "token": os.getenv("SLACK_TOKEN", ""), # used for API mode "webhook": os.getenv("SLACK_WEBHOOK", ""), # used for webhook mode - "mode": os.getenv("SLACK_MODE", "webhook"), # webhook or api + # "mode": os.getenv("SLACK_MODE", "webhook"), # webhook or api }, "vonage": { "key": os.getenv("VONAGE_KEY", ""), From 99de0f96d137baed6f5237f89a6c2c9433d51db0 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Sat, 10 Apr 2021 20:30:40 +0200 Subject: [PATCH 26/54] add mocking notifications and tests --- src/masonite/notification/MockNotification.py | 45 +++++++- .../notification/NotificationManager.py | 9 +- .../notification/test_mock_notification.py | 102 ++++++++++++++++++ .../notification/test_notification.py | 50 ++++++++- 4 files changed, 200 insertions(+), 6 deletions(-) create mode 100644 tests/features/notification/test_mock_notification.py diff --git a/src/masonite/notification/MockNotification.py b/src/masonite/notification/MockNotification.py index 9c8b0499..72084f82 100644 --- a/src/masonite/notification/MockNotification.py +++ b/src/masonite/notification/MockNotification.py @@ -1,4 +1,5 @@ from .NotificationManager import NotificationManager +from .AnonymousNotifiable import AnonymousNotifiable class MockNotification(NotificationManager): @@ -9,5 +10,47 @@ def __init__(self, application, *args, **kwargs): def send( self, notifiables, notification, drivers=[], dry=False, fail_silently=False ): - self.called_notifications.append(notification) + _notifiables = [] + for notifiable in self._format_notifiables(notifiables): + if isinstance(notifiable, AnonymousNotifiable): + _notifiables.extend(notifiable._routes.values()) + else: + _notifiables.append(notifiable) + key = notification.type() + # store notifications instead of sending them + old_notifiables = self.sent_notifications.get(key, []) + self.sent_notifications.update({key: old_notifiables + _notifiables}) + self.count += len(_notifiables) + return self + + def resetCount(self): + """Reset sent notifications count.""" + self.count = 0 + self.sent_notifications = {} + return self + + def assertNothingSent(self): + assert self.count == 0, f"{self.count} notifications have been sent." + # assert len(self.sent_notifications.keys()) == 0 + return self + + def assertCount(self, count): + assert ( + self.count == count + ), f"{self.count} notifications have been sent, not {count}." + return self + + def assertSentTo(self, notifiable, notification_class, count=None): + from collections import Counter + + notifiables = self.sent_notifications.get(notification_class.__name__, []) + assert notifiable in notifiables + if count: + counter = Counter(notifiables) + assert counter[notifiable] == count + return self + + def assertNotSentTo(self, notifiable, notification_class): + notifiables = self.sent_notifications.get(notification_class.__name__, []) + assert notifiable not in notifiables return self diff --git a/src/masonite/notification/NotificationManager.py b/src/masonite/notification/NotificationManager.py index c4aa536a..de325681 100644 --- a/src/masonite/notification/NotificationManager.py +++ b/src/masonite/notification/NotificationManager.py @@ -40,11 +40,14 @@ def send( self, notifiables, notification, drivers=[], dry=False, fail_silently=False ): """Send the given notification to the given notifiables.""" - if not notification.should_send or dry: - self.dry_notifications.update({notification.type(): notifiables}) + notifiables = self._format_notifiables(notifiables) + if not notification.should_send or dry or self.options.get("dry"): + key = notification.type() + self.dry_notifications.update( + {key: notifiables + self.dry_notifications.get(key, [])} + ) return - notifiables = self._format_notifiables(notifiables) for notifiable in notifiables: # get drivers to use for sending this notification drivers = drivers if drivers else notification.via(notifiable) diff --git a/tests/features/notification/test_mock_notification.py b/tests/features/notification/test_mock_notification.py new file mode 100644 index 00000000..3be1ee1e --- /dev/null +++ b/tests/features/notification/test_mock_notification.py @@ -0,0 +1,102 @@ +from tests import TestCase + +from src.masonite.notification import Notification, Notifiable +from src.masonite.mail import Mailable +from masoniteorm.models import Model + + +class User(Model, Notifiable): + """User Model""" + + __fillable__ = ["name", "email", "password"] + + +class WelcomeNotification(Notification): + def to_mail(self, notifiable): + return ( + Mailable() + .subject("Masonite 4") + .from_("joe@masoniteproject.com") + .text("Hello from Masonite!") + ) + + def via(self, notifiable): + return ["mail"] + + +class OtherNotification(Notification): + def to_mail(self, notifiable): + return ( + Mailable() + .subject("Other") + .from_("sam@masoniteproject.com") + .text("Hello again!") + ) + + def via(self, notifiable): + return ["mail"] + + +class TestMockNotification(TestCase): + def setUp(self): + super().setUp() + self.fake("notification") + + def test_assert_nothing_sent(self): + notification = self.application.make("notification") + notification.assertNothingSent() + + def test_assert_count(self): + notification = self.application.make("notification") + notification.assertCount(0) + notification.route("mail", "test@mail.com").send(WelcomeNotification()) + notification.assertCount(1) + notification.route("mail", "test2@mail.com").send(WelcomeNotification()) + notification.assertCount(2) + + def test_reset_count(self): + notification = self.application.make("notification") + notification.assertNothingSent() + notification.route("mail", "test@mail.com").send(WelcomeNotification()) + notification.resetCount() + notification.assertNothingSent() + + def test_assert_sent_to_with_anonymous(self): + notification = self.application.make("notification") + notification.route("mail", "test@mail.com").send(WelcomeNotification()) + notification.assertSentTo("test@mail.com", WelcomeNotification) + + notification.route("vonage", "123456").route("slack", "#general").send( + WelcomeNotification() + ) + notification.assertSentTo("123456", WelcomeNotification) + notification.assertSentTo("#general", WelcomeNotification) + + def test_assert_not_sent_to(self): + notification = self.application.make("notification") + notification.resetCount() + notification.assertNotSentTo("test@mail.com", WelcomeNotification) + notification.route("vonage", "123456").send(OtherNotification()) + notification.assertNotSentTo("123456", WelcomeNotification) + notification.assertNotSentTo("test@mail.com", OtherNotification) + + def test_assert_sent_to_with_notifiable(self): + notification = self.application.make("notification") + user = User.find(1) + user.notify(WelcomeNotification()) + notification.assertSentTo(user, WelcomeNotification) + user.notify(OtherNotification()) + notification.assertSentTo(user, OtherNotification) + notification.assertCount(2) + + def test_assert_sent_to_with_count(self): + notification = self.application.make("notification") + user = User.find(1) + user.notify(WelcomeNotification()) + user.notify(WelcomeNotification()) + notification.assertSentTo(user, WelcomeNotification, 2) + + user.notify(OtherNotification()) + user.notify(OtherNotification()) + with self.assertRaises(AssertionError): + notification.assertSentTo(user, OtherNotification, 1) diff --git a/tests/features/notification/test_notification.py b/tests/features/notification/test_notification.py index 2fb064e8..c37b2dec 100644 --- a/tests/features/notification/test_notification.py +++ b/tests/features/notification/test_notification.py @@ -1,11 +1,24 @@ from tests import TestCase -from src.masonite.notification import Notification +from src.masonite.notification import Notification, Notifiable +from src.masonite.mail import Mailable +from masoniteorm.models import Model + + +class User(Model, Notifiable): + """User Model""" + + __fillable__ = ["name", "email", "password"] class WelcomeNotification(Notification): def to_mail(self, notifiable): - return "" + return ( + Mailable() + .subject("Masonite 4") + .from_("joe@masoniteproject.com") + .text("Hello from Masonite!") + ) def via(self, notifiable): return ["mail"] @@ -26,3 +39,36 @@ def test_ignore_errors(self): def test_notification_type(self): self.assertEqual("WelcomeNotification", WelcomeNotification().type()) + + +DRY = True + + +class TestNotificationManager(TestCase): + def test_dry_mode(self): + # when sending to anonymous or notifiable + self.assertEqual( + self.application.make("notification") + .route("mail", "test@mail.com") + .send(WelcomeNotification(), dry=True), + None, + ) + + import pdb + + pdb.set_trace() + + user = User.find(1) + user.notify(WelcomeNotification(), dry=True) + + # globally + # override settings for testing purposes + self.application.bind( + "config.notification", "tests.features.notification.test_notification" + ) + self.assertEqual( + self.application.make("notification") + .route("mail", "test@mail.com") + .send(WelcomeNotification()), + None, + ) From 374690fb19f9e4e318ba6baebb57e20ca68664c0 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Sat, 10 Apr 2021 20:33:39 +0200 Subject: [PATCH 27/54] add missing dependencies --- setup.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/setup.py b/setup.py index d78dfb0c..b77d2702 100644 --- a/setup.py +++ b/setup.py @@ -125,6 +125,8 @@ "boto3", "pusher", "pymemcache", + "vonage", + "slackblocks", ], }, # If there are data files included in your packages that need to be From 8bd2b5143b90c20bc05ebb54f88c1d48172feda6 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Mon, 12 Apr 2021 18:19:52 +0200 Subject: [PATCH 28/54] fix tests --- tests/features/notification/test_mock_notification.py | 4 ++++ tests/features/notification/test_notification.py | 4 ---- tests/features/notification/test_slack_driver.py | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/features/notification/test_mock_notification.py b/tests/features/notification/test_mock_notification.py index 3be1ee1e..6b7fe1c4 100644 --- a/tests/features/notification/test_mock_notification.py +++ b/tests/features/notification/test_mock_notification.py @@ -42,6 +42,10 @@ def setUp(self): super().setUp() self.fake("notification") + def tearDown(self): + super().tearDown() + self.restore("notification") + def test_assert_nothing_sent(self): notification = self.application.make("notification") notification.assertNothingSent() diff --git a/tests/features/notification/test_notification.py b/tests/features/notification/test_notification.py index c37b2dec..3f59e2a5 100644 --- a/tests/features/notification/test_notification.py +++ b/tests/features/notification/test_notification.py @@ -54,10 +54,6 @@ def test_dry_mode(self): None, ) - import pdb - - pdb.set_trace() - user = User.find(1) user.notify(WelcomeNotification(), dry=True) diff --git a/tests/features/notification/test_slack_driver.py b/tests/features/notification/test_slack_driver.py index 120c8963..2062f944 100644 --- a/tests/features/notification/test_slack_driver.py +++ b/tests/features/notification/test_slack_driver.py @@ -1,3 +1,4 @@ +import pytest import responses from tests import TestCase from src.masonite.notification import Notification, Notifiable, SlackMessage @@ -131,6 +132,7 @@ def test_sending_to_notifiable(self): self.assertTrue(responses.assert_call_count(self.url, 1)) @responses.activate + @pytest.mark.skip(reason="Failing because user defined routing takes precedence. What should be the behaviour ?") def test_sending_to_multiple_channels(self): user = User.find(1) responses.add( @@ -144,7 +146,6 @@ def test_sending_to_multiple_channels(self): body=b'{"channels": [{"name": "bot", "id": "123"}, {"name": "general", "id": "456"}]}', ) user.notify(OtherNotification()) - # TODO: does not work because user defined routing takes precedence. Should it be like this ?? self.assertTrue(responses.assert_call_count(self.channel_url, 2)) self.assertTrue(responses.assert_call_count(self.url, 2)) From bd95d68267180e466be29c43ca59bc1fc2b772e9 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Mon, 12 Apr 2021 18:38:29 +0200 Subject: [PATCH 29/54] fix mock mail tests to have better idempotence --- tests/features/mail/test_mock_mail.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/features/mail/test_mock_mail.py b/tests/features/mail/test_mock_mail.py index aa37926a..04c96eee 100644 --- a/tests/features/mail/test_mock_mail.py +++ b/tests/features/mail/test_mock_mail.py @@ -14,6 +14,14 @@ def build(self): class TestSMTPDriver(TestCase): + def setUp(self): + super().setUp() + self.fake("mail") + + def tearDown(self): + super().tearDown() + self.restore("mail") + def test_mock_mail(self): self.fake("mail") welcome_email = self.application.make("mail").mailable(Welcome()).send() From 2b7016b6c2050d91094d69066a86e6e23f518c3c Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Mon, 12 Apr 2021 20:25:19 +0200 Subject: [PATCH 30/54] finalize adding mock capabilities --- .../drivers/notification/DatabaseDriver.py | 3 + src/masonite/notification/MockNotification.py | 94 ++++++++++++++++--- .../notification/NotificationManager.py | 10 +- .../notification/test_database_driver.py | 7 ++ .../notification/test_integrations.py | 92 ++++++++++++++++++ .../notification/test_mock_notification.py | 66 ++++++++++++- .../notification/test_notification.py | 2 +- 7 files changed, 250 insertions(+), 24 deletions(-) create mode 100644 tests/features/notification/test_integrations.py diff --git a/src/masonite/drivers/notification/DatabaseDriver.py b/src/masonite/drivers/notification/DatabaseDriver.py index 52886e0e..a04ffb10 100644 --- a/src/masonite/drivers/notification/DatabaseDriver.py +++ b/src/masonite/drivers/notification/DatabaseDriver.py @@ -23,6 +23,9 @@ def get_builder(self): def send(self, notifiable, notification): """Used to send the email and run the logic for sending emails.""" data = self.build(notifiable, notification) + import pdb + + pdb.set_trace() return self.get_builder().new().create(data) def queue(self, notifiable, notification): diff --git a/src/masonite/notification/MockNotification.py b/src/masonite/notification/MockNotification.py index 72084f82..f9c40e3c 100644 --- a/src/masonite/notification/MockNotification.py +++ b/src/masonite/notification/MockNotification.py @@ -1,11 +1,43 @@ from .NotificationManager import NotificationManager from .AnonymousNotifiable import AnonymousNotifiable +from .Notification import Notification + + +class NotificationWithAsserts(Notification): + def assertSentVia(self, *drivers): + sent_via = self.via(self.notifiable) + for driver in drivers: + assert ( + driver in sent_via + ), f"notification sent via {sent_via}, not {driver}." + return self + + def assertEqual(self, value, reference): + assert value == reference, "{value} not equal to {reference}." + return self + + def assertNotEqual(self, value, reference): + assert value != reference, "{value} equal to {reference}." + return self + + def assertIn(self, value, container): + assert value in container, "{value} not in {container}." + return self + + @classmethod + def patch(cls, target): + for k in cls.__dict__: + obj = getattr(cls, k) + if not k.startswith("_") and callable(obj): + setattr(target, k, obj) class MockNotification(NotificationManager): def __init__(self, application, *args, **kwargs): super().__init__(application, *args, **kwargs) self.count = 0 + self.last_notifiable = None + self.last_notification = None def send( self, notifiables, notification, drivers=[], dry=False, fail_silently=False @@ -16,22 +48,34 @@ def send( _notifiables.extend(notifiable._routes.values()) else: _notifiables.append(notifiable) - key = notification.type() - # store notifications instead of sending them - old_notifiables = self.sent_notifications.get(key, []) - self.sent_notifications.update({key: old_notifiables + _notifiables}) - self.count += len(_notifiables) + + notification_key = notification.type() + NotificationWithAsserts.patch(notification.__class__) + for notifiable in _notifiables: + notification.notifiable = notifiable # for asserts + old_notifs = self.sent_notifications.get(notifiable, {}) + old_notifs.update( + { + notification_key: old_notifs.get(notification_key, []) + + [notification] + } + ) + self.sent_notifications.update({notifiable: old_notifs}) + self.count += 1 + self.last_notification = notification + self.last_notifiable = notifiable return self def resetCount(self): """Reset sent notifications count.""" self.count = 0 self.sent_notifications = {} + self.last_notifiable = None + self.last_notification = None return self def assertNothingSent(self): assert self.count == 0, f"{self.count} notifications have been sent." - # assert len(self.sent_notifications.keys()) == 0 return self def assertCount(self, count): @@ -40,17 +84,37 @@ def assertCount(self, count): ), f"{self.count} notifications have been sent, not {count}." return self - def assertSentTo(self, notifiable, notification_class, count=None): - from collections import Counter - - notifiables = self.sent_notifications.get(notification_class.__name__, []) - assert notifiable in notifiables + def assertSentTo( + self, notifiable, notification_class, callable_assert=None, count=None + ): + notification_key = notification_class.__name__ + notifiable_notifications = self.sent_notifications.get(notifiable, []) + assert notification_key in notifiable_notifications if count: - counter = Counter(notifiables) - assert counter[notifiable] == count + sent_count = len(notifiable_notifications.get(notification_key, [])) + assert ( + sent_count == count + ), f"{notification_key} has been sent to {notifiable} {sent_count} times" + if callable_assert: + # assert last notification sent for this notifiable + notification = notifiable_notifications.get(notification_key)[-1] + assert callable_assert(notifiable, notification) + return self + + def last(self): + """Get last sent mocked notification if any.""" + return self.last_notification + + def assertLast(self, callable_assert): + if not self.last_notifiable or not self.last_notification: + raise AssertionError("No notification has been sent.") + assert callable_assert(self.last_notifiable, self.last_notification) return self def assertNotSentTo(self, notifiable, notification_class): - notifiables = self.sent_notifications.get(notification_class.__name__, []) - assert notifiable not in notifiables + notification_key = notification_class.__name__ + notifiable_notifications = self.sent_notifications.get(notifiable, []) + assert ( + notification_key not in notifiable_notifications + ), f"{notification_key} has been sent to {notifiable}." return self diff --git a/src/masonite/notification/NotificationManager.py b/src/masonite/notification/NotificationManager.py index de325681..93e9fd5c 100644 --- a/src/masonite/notification/NotificationManager.py +++ b/src/masonite/notification/NotificationManager.py @@ -1,7 +1,6 @@ """Notification handler class""" import uuid -from ..utils.collections import Collection from ..exceptions.exceptions import NotificationException from ..queues import ShouldQueue from .AnonymousNotifiable import AnonymousNotifiable @@ -57,18 +56,17 @@ def send( notification.type() ) ) + notification.id = uuid.uuid4() for driver in drivers: driver_instance = self.get_driver(driver) if isinstance(notifiable, AnonymousNotifiable) and driver == "database": # this case is not possible but that should not stop other channels to be used continue - if not notification.id: - notification.id = uuid.uuid4() try: if isinstance(notification, ShouldQueue): - return driver_instance.queue(notifiable, notification) + driver_instance.queue(notifiable, notification) else: - return driver_instance.send(notifiable, notification) + driver_instance.send(notifiable, notification) except Exception as e: if not notification.ignore_errors and not fail_silently: raise e @@ -77,6 +75,8 @@ def send( # return issubclass(channel, BaseDriver) def _format_notifiables(self, notifiables): + from masoniteorm.collection import Collection + if isinstance(notifiables, (list, tuple, Collection)): return notifiables else: diff --git a/tests/features/notification/test_database_driver.py b/tests/features/notification/test_database_driver.py index 6a80628f..9204f619 100644 --- a/tests/features/notification/test_database_driver.py +++ b/tests/features/notification/test_database_driver.py @@ -54,6 +54,13 @@ def test_database_notification_is_created_correctly(self): assert notification["notifiable_id"] == user.id assert notification["notifiable_type"] == "users" + def test_notify_multiple_users(self): + User.create({"name": "sam", "email": "sam@test.com", "password": "secret"}) + users = User.all() # == 2 users + self.notification.send(users, WelcomeNotification()) + assert users[0].notifications.count() == 1 + assert users[1].notifications.count() == 1 + class TestDatabaseNotification(TestCase, DatabaseTransactions): connection = None diff --git a/tests/features/notification/test_integrations.py b/tests/features/notification/test_integrations.py new file mode 100644 index 00000000..a71e752b --- /dev/null +++ b/tests/features/notification/test_integrations.py @@ -0,0 +1,92 @@ +from tests.features.notification.test_anonymous_notifiable import WelcomeNotification +from tests import TestCase +from src.masonite.mail import Mailable +from src.masonite.notification import Notification, SlackMessage, Sms, Notifiable +from masoniteorm.models import Model + + +class User(Model, Notifiable): + """User Model""" + + __fillable__ = ["name", "email", "password"] + + def route_notification_for_broadcast(self): + return f"user.{self.id}" + + def route_notification_for_slack(self): + return "#bot" + + def route_notification_for_vonage(self): + return "+33123456789" + + def route_notification_for_mail(self): + return self.email + + +class WelcomeNotification(Notification): + def to_mail(self, notifiable): + return ( + Mailable() + .subject("Masonite 4") + .from_("joe@masoniteproject.com") + .text("Hello from Masonite!") + ) + + def to_database(self, notifiable): + return {"data": "Welcome {0}!".format(notifiable.name)} + + def to_broadcast(self, notifiable): + return {"data": "Welcome"} + + def to_slack(self, notifiable): + return SlackMessage().text("Welcome !").from_("test-bot") + + def to_vonage(self, notifiable): + return Sms().text("Welcome !").from_("123456") + + def via(self, notifiable): + return ["mail", "database", "slack", "vonage", "broadcast"] + + +class TestIntegrationsNotifications(TestCase): + def setUp(self): + super().setUp() + self.fake("notification") + + def tearDown(self): + super().tearDown() + self.restore("notification") + + # def test_all_drivers_with_anonymous(self): + # notif = ( + # self.application.make("notification") + # .route("mail", "user@example.com") + # .route("slack", "#general") + # .route("broadcast", "all") + # .route("vonage", "+33456789012") + # ) + # notif.send(WelcomeNotification()).assertSentTo( + # "user@example.com", WelcomeNotification + # ).assertSentTo("#general", WelcomeNotification).assertSentTo( + # "all", WelcomeNotification + # ).assertSentTo( + # "+33456789012", WelcomeNotification + # ) + + def test_all_drivers_with_notifiable(self): + self.application.make("notification").assertNothingSent() + user = User.find(1) + user.notify(WelcomeNotification()) + import pdb + + pdb.set_trace() + self.application.make("notification").assertSentTo( + "user@example.com", WelcomeNotification + ).assertSentTo("#general", WelcomeNotification).assertSentTo( + "all", WelcomeNotification + ).assertSentTo( + "+33456789012", WelcomeNotification + ) + import pdb + + pdb.set_trace() diff --git a/tests/features/notification/test_mock_notification.py b/tests/features/notification/test_mock_notification.py index 6b7fe1c4..b53f07c0 100644 --- a/tests/features/notification/test_mock_notification.py +++ b/tests/features/notification/test_mock_notification.py @@ -1,6 +1,6 @@ from tests import TestCase -from src.masonite.notification import Notification, Notifiable +from src.masonite.notification import Notification, Notifiable, SlackMessage from src.masonite.mail import Mailable from masoniteorm.models import Model @@ -37,6 +37,25 @@ def via(self, notifiable): return ["mail"] +class OrderNotification(Notification): + def __init__(self, order_id): + self.order_id = order_id + + def to_mail(self, notifiable): + return ( + Mailable() + .subject(f"Order {self.order_id} shipped !") + .from_("sam@masoniteproject.com") + .text(f"{notifiable.name}, your order has been shipped") + ) + + def to_slack(self, notifiable): + return SlackMessage().text(f"Order {self.order_id} has been shipped !") + + def via(self, notifiable): + return ["mail", "slack"] + + class TestMockNotification(TestCase): def setUp(self): super().setUp() @@ -98,9 +117,50 @@ def test_assert_sent_to_with_count(self): user = User.find(1) user.notify(WelcomeNotification()) user.notify(WelcomeNotification()) - notification.assertSentTo(user, WelcomeNotification, 2) + notification.assertSentTo(user, WelcomeNotification, count=2) user.notify(OtherNotification()) user.notify(OtherNotification()) with self.assertRaises(AssertionError): - notification.assertSentTo(user, OtherNotification, 1) + notification.assertSentTo(user, OtherNotification, count=1) + + def test_assert_with_assertions_on_notification(self): + user = User.find(1) + user.notify(OrderNotification(6)) + self.application.make("notification").assertSentTo( + user, + OrderNotification, + lambda user, notification: ( + notification.assertSentVia("mail", "slack") + .assertEqual(notification.order_id, 6) + .assertEqual( + notification.to_mail(user).get_options().get("subject"), + "Order 6 shipped !", + ) + .assertIn( + user.name, + notification.to_mail(user).get_options().get("text_content"), + ) + ), + ) + + def test_last_notification(self): + notification = self.application.make("notification") + message = WelcomeNotification() + notification.route("mail", "test@mail.com").send(message) + assert message == notification.last() + + def test_assert_last(self): + self.application.make("notification").route("mail", "test@mail.com").route( + "slack", "#general" + ).send(OrderNotification(10)) + self.application.make("notification").assertLast( + lambda user, notif: ( + notif.assertSentVia("mail") + .assertEqual(notif.order_id, 10) + .assertEqual( + notif.to_slack(user).get_options().get("text"), + "Order 10 has been shipped !", + ) + ) + ) diff --git a/tests/features/notification/test_notification.py b/tests/features/notification/test_notification.py index 3f59e2a5..4afc2a92 100644 --- a/tests/features/notification/test_notification.py +++ b/tests/features/notification/test_notification.py @@ -46,7 +46,7 @@ def test_notification_type(self): class TestNotificationManager(TestCase): def test_dry_mode(self): - # when sending to anonymous or notifiable + # locally when sending to anonymous or notifiable self.assertEqual( self.application.make("notification") .route("mail", "test@mail.com") From d5754fee19bff822d3e827a812d3e01d6a3d8949 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Mon, 12 Apr 2021 20:28:25 +0200 Subject: [PATCH 31/54] return notifications sent from list --- .../drivers/notification/DatabaseDriver.py | 3 -- .../notification/NotificationManager.py | 7 ++-- .../notification/test_integrations.py | 36 ++++++++----------- 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/masonite/drivers/notification/DatabaseDriver.py b/src/masonite/drivers/notification/DatabaseDriver.py index a04ffb10..52886e0e 100644 --- a/src/masonite/drivers/notification/DatabaseDriver.py +++ b/src/masonite/drivers/notification/DatabaseDriver.py @@ -23,9 +23,6 @@ def get_builder(self): def send(self, notifiable, notification): """Used to send the email and run the logic for sending emails.""" data = self.build(notifiable, notification) - import pdb - - pdb.set_trace() return self.get_builder().new().create(data) def queue(self, notifiable, notification): diff --git a/src/masonite/notification/NotificationManager.py b/src/masonite/notification/NotificationManager.py index 93e9fd5c..0c9e2d6b 100644 --- a/src/masonite/notification/NotificationManager.py +++ b/src/masonite/notification/NotificationManager.py @@ -47,6 +47,7 @@ def send( ) return + results = [] for notifiable in notifiables: # get drivers to use for sending this notification drivers = drivers if drivers else notification.via(notifiable) @@ -64,13 +65,15 @@ def send( continue try: if isinstance(notification, ShouldQueue): - driver_instance.queue(notifiable, notification) + results.append(driver_instance.queue(notifiable, notification)) else: - driver_instance.send(notifiable, notification) + results.append(driver_instance.send(notifiable, notification)) except Exception as e: if not notification.ignore_errors and not fail_silently: raise e + return results[0] if len(results) == 1 else results + # def is_custom_channel(self, channel): # return issubclass(channel, BaseDriver) diff --git a/tests/features/notification/test_integrations.py b/tests/features/notification/test_integrations.py index a71e752b..36b20466 100644 --- a/tests/features/notification/test_integrations.py +++ b/tests/features/notification/test_integrations.py @@ -57,29 +57,26 @@ def tearDown(self): super().tearDown() self.restore("notification") - # def test_all_drivers_with_anonymous(self): - # notif = ( - # self.application.make("notification") - # .route("mail", "user@example.com") - # .route("slack", "#general") - # .route("broadcast", "all") - # .route("vonage", "+33456789012") - # ) - # notif.send(WelcomeNotification()).assertSentTo( - # "user@example.com", WelcomeNotification - # ).assertSentTo("#general", WelcomeNotification).assertSentTo( - # "all", WelcomeNotification - # ).assertSentTo( - # "+33456789012", WelcomeNotification - # ) + def test_all_drivers_with_anonymous(self): + notif = ( + self.application.make("notification") + .route("mail", "user@example.com") + .route("slack", "#general") + .route("broadcast", "all") + .route("vonage", "+33456789012") + ) + notif.send(WelcomeNotification()).assertSentTo( + "user@example.com", WelcomeNotification + ).assertSentTo("#general", WelcomeNotification).assertSentTo( + "all", WelcomeNotification + ).assertSentTo( + "+33456789012", WelcomeNotification + ) def test_all_drivers_with_notifiable(self): self.application.make("notification").assertNothingSent() user = User.find(1) user.notify(WelcomeNotification()) - import pdb - - pdb.set_trace() self.application.make("notification").assertSentTo( "user@example.com", WelcomeNotification ).assertSentTo("#general", WelcomeNotification).assertSentTo( @@ -87,6 +84,3 @@ def test_all_drivers_with_notifiable(self): ).assertSentTo( "+33456789012", WelcomeNotification ) - import pdb - - pdb.set_trace() From a2be1cb7050b4cdae49d1b3f28179d41a84327c0 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Tue, 27 Apr 2021 19:55:55 +0200 Subject: [PATCH 32/54] Apply suggestions from code review Co-authored-by: Joseph Mancuso --- src/masonite/commands/NotificationTableCommand.py | 2 +- src/masonite/drivers/notification/BaseDriver.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/masonite/commands/NotificationTableCommand.py b/src/masonite/commands/NotificationTableCommand.py index 6897f42b..4e002950 100644 --- a/src/masonite/commands/NotificationTableCommand.py +++ b/src/masonite/commands/NotificationTableCommand.py @@ -8,7 +8,7 @@ class NotificationTableCommand(Command): """ - Creates the notifications table needed for storing notifications in database. + Creates the notifications table needed for storing notifications in the database. notification:table {--d|--directory=database/migrations : Specifies the directory to create the migration in} diff --git a/src/masonite/drivers/notification/BaseDriver.py b/src/masonite/drivers/notification/BaseDriver.py index 866a9609..1107e530 100644 --- a/src/masonite/drivers/notification/BaseDriver.py +++ b/src/masonite/drivers/notification/BaseDriver.py @@ -7,7 +7,7 @@ def send(self, notifiable, notification): ) def queue(self, notifiable, notification): - """Implements queuing the notification to be sent later to notifiables through + """Implements queueing the notification to be sent later this driver.""" raise NotImplementedError( "queue() method must be implemented for a notification driver." @@ -15,7 +15,7 @@ def queue(self, notifiable, notification): def get_data(self, driver, notifiable, notification): """Get the data for the notification.""" - method_name = "to_{0}".format(driver) + method_name = f"to_{driver}" try: method = getattr(notification, method_name) except AttributeError: From c61919cb37b7ad77eab1412937ad1f779655938d Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Tue, 27 Apr 2021 19:56:46 +0200 Subject: [PATCH 33/54] add f-string --- src/masonite/drivers/notification/BaseDriver.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/masonite/drivers/notification/BaseDriver.py b/src/masonite/drivers/notification/BaseDriver.py index 1107e530..c23db62e 100644 --- a/src/masonite/drivers/notification/BaseDriver.py +++ b/src/masonite/drivers/notification/BaseDriver.py @@ -7,7 +7,7 @@ def send(self, notifiable, notification): ) def queue(self, notifiable, notification): - """Implements queueing the notification to be sent later + """Implements queueing the notification to be sent later this driver.""" raise NotImplementedError( "queue() method must be implemented for a notification driver." @@ -20,7 +20,7 @@ def get_data(self, driver, notifiable, notification): method = getattr(notification, method_name) except AttributeError: raise NotImplementedError( - "Notification model should implement {}() method.".format(method_name) + f"Notification model should implement {method_name}() method." ) else: return method(notifiable) From 74c13eb425f7a2235b2a5d550540de4684716209 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Tue, 27 Apr 2021 19:58:10 +0200 Subject: [PATCH 34/54] make notification template more generic --- src/masonite/stubs/notification/Notification.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/masonite/stubs/notification/Notification.py b/src/masonite/stubs/notification/Notification.py index 923dc8b7..285806da 100644 --- a/src/masonite/stubs/notification/Notification.py +++ b/src/masonite/stubs/notification/Notification.py @@ -8,7 +8,7 @@ def to_mail(self, notifiable): Mailable() .to(notifiable.email) .subject("Masonite 4") - .from_("sam@masoniteproject.com") + .from_("hello@email.com") .text(f"Hello {notifiable.name}") ) From f56cad8139d916c5374e7143559d64719256eb46 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Tue, 27 Apr 2021 20:04:47 +0200 Subject: [PATCH 35/54] move notification drivers inside notifications feature folder --- .../notification => notification/drivers}/BaseDriver.py | 0 .../notification => notification/drivers}/BroadcastDriver.py | 0 .../notification => notification/drivers}/DatabaseDriver.py | 0 .../notification => notification/drivers}/MailDriver.py | 0 .../notification => notification/drivers}/SlackDriver.py | 0 .../{drivers/notification => notification/drivers}/__init__.py | 0 .../drivers}/vonage/VonageDriver.py | 0 src/masonite/providers/NotificationProvider.py | 2 +- 8 files changed, 1 insertion(+), 1 deletion(-) rename src/masonite/{drivers/notification => notification/drivers}/BaseDriver.py (100%) rename src/masonite/{drivers/notification => notification/drivers}/BroadcastDriver.py (100%) rename src/masonite/{drivers/notification => notification/drivers}/DatabaseDriver.py (100%) rename src/masonite/{drivers/notification => notification/drivers}/MailDriver.py (100%) rename src/masonite/{drivers/notification => notification/drivers}/SlackDriver.py (100%) rename src/masonite/{drivers/notification => notification/drivers}/__init__.py (100%) rename src/masonite/{drivers/notification => notification/drivers}/vonage/VonageDriver.py (100%) diff --git a/src/masonite/drivers/notification/BaseDriver.py b/src/masonite/notification/drivers/BaseDriver.py similarity index 100% rename from src/masonite/drivers/notification/BaseDriver.py rename to src/masonite/notification/drivers/BaseDriver.py diff --git a/src/masonite/drivers/notification/BroadcastDriver.py b/src/masonite/notification/drivers/BroadcastDriver.py similarity index 100% rename from src/masonite/drivers/notification/BroadcastDriver.py rename to src/masonite/notification/drivers/BroadcastDriver.py diff --git a/src/masonite/drivers/notification/DatabaseDriver.py b/src/masonite/notification/drivers/DatabaseDriver.py similarity index 100% rename from src/masonite/drivers/notification/DatabaseDriver.py rename to src/masonite/notification/drivers/DatabaseDriver.py diff --git a/src/masonite/drivers/notification/MailDriver.py b/src/masonite/notification/drivers/MailDriver.py similarity index 100% rename from src/masonite/drivers/notification/MailDriver.py rename to src/masonite/notification/drivers/MailDriver.py diff --git a/src/masonite/drivers/notification/SlackDriver.py b/src/masonite/notification/drivers/SlackDriver.py similarity index 100% rename from src/masonite/drivers/notification/SlackDriver.py rename to src/masonite/notification/drivers/SlackDriver.py diff --git a/src/masonite/drivers/notification/__init__.py b/src/masonite/notification/drivers/__init__.py similarity index 100% rename from src/masonite/drivers/notification/__init__.py rename to src/masonite/notification/drivers/__init__.py diff --git a/src/masonite/drivers/notification/vonage/VonageDriver.py b/src/masonite/notification/drivers/vonage/VonageDriver.py similarity index 100% rename from src/masonite/drivers/notification/vonage/VonageDriver.py rename to src/masonite/notification/drivers/vonage/VonageDriver.py diff --git a/src/masonite/providers/NotificationProvider.py b/src/masonite/providers/NotificationProvider.py index 426792c1..be96c003 100644 --- a/src/masonite/providers/NotificationProvider.py +++ b/src/masonite/providers/NotificationProvider.py @@ -1,6 +1,6 @@ from .Provider import Provider from ..utils.structures import load -from ..drivers.notification import ( +from ..notification.drivers import ( BroadcastDriver, DatabaseDriver, MailDriver, From 73b79fe42b1677bcf88adee1891b443b9952686a Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Fri, 7 May 2021 20:09:18 -0400 Subject: [PATCH 36/54] fixed tests --- database.sqlite3 | Bin 348160 -> 348160 bytes requirements.txt | 1 + .../notification/test_database_driver.py | 2 +- .../notification/test_notification.py | 5 +---- tests/integrations/app/Kernel/Kernel.py | 1 + 5 files changed, 4 insertions(+), 5 deletions(-) diff --git a/database.sqlite3 b/database.sqlite3 index de3161d5a0073eb9a531228fd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oxzkbu$TxSxx` zi4gRMqrq_WQ5?pTlarA!7i@nJdkEshGLyv@*2VLbT}TKh(iApIK#yzc)RMTQQCUp zScmJR4*Y)|wEN^4HmuNrIl(@q=jAjcYeGe>4}>ylO|K#|N=h^eb8eg2;wYZh9=8Tx;&+ zwj^jNKyv|_3UIyvjX8LQ((*NEP*}af>J(P1FsQH^g`HEFO<`vhR-H5X->52LWf84K zv=s470UKYhWBcjU8c5;uRZEwJ%++*yZVfEp^O~hwV&*3Sz<~U?DQ0#EAg+G-fPwzQ zI!vmoTIy5S4TVY9KL16z^18yV Date: Fri, 7 May 2021 20:11:09 -0400 Subject: [PATCH 37/54] formatted --- tests/features/notification/test_anonymous_notifiable.py | 2 +- tests/features/notification/test_database_driver.py | 2 +- tests/features/notification/test_slack_driver.py | 4 +++- tests/integrations/app/Kernel/Kernel.py | 4 +++- tests/integrations/config/notification.py | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/features/notification/test_anonymous_notifiable.py b/tests/features/notification/test_anonymous_notifiable.py index d0393eb0..2c7976f4 100644 --- a/tests/features/notification/test_anonymous_notifiable.py +++ b/tests/features/notification/test_anonymous_notifiable.py @@ -53,4 +53,4 @@ def via(self, notifiable): AnonymousNotifiable(self.application).route("slack", "#general").send( FailingNotification(), fail_silently=True ) - # no assertion raised :) \ No newline at end of file + # no assertion raised :) diff --git a/tests/features/notification/test_database_driver.py b/tests/features/notification/test_database_driver.py index ace368c6..440f651e 100644 --- a/tests/features/notification/test_database_driver.py +++ b/tests/features/notification/test_database_driver.py @@ -129,4 +129,4 @@ def test_notifiable_get_unread_notifications(self): "notifiable_id": user.id, } ) - self.assertEqual(1, user.unread_notifications.count()) \ No newline at end of file + self.assertEqual(1, user.unread_notifications.count()) diff --git a/tests/features/notification/test_slack_driver.py b/tests/features/notification/test_slack_driver.py index 2062f944..b4031ede 100644 --- a/tests/features/notification/test_slack_driver.py +++ b/tests/features/notification/test_slack_driver.py @@ -132,7 +132,9 @@ def test_sending_to_notifiable(self): self.assertTrue(responses.assert_call_count(self.url, 1)) @responses.activate - @pytest.mark.skip(reason="Failing because user defined routing takes precedence. What should be the behaviour ?") + @pytest.mark.skip( + reason="Failing because user defined routing takes precedence. What should be the behaviour ?" + ) def test_sending_to_multiple_channels(self): user = User.find(1) responses.add( diff --git a/tests/integrations/app/Kernel/Kernel.py b/tests/integrations/app/Kernel/Kernel.py index 70bfaab0..f46a3dae 100644 --- a/tests/integrations/app/Kernel/Kernel.py +++ b/tests/integrations/app/Kernel/Kernel.py @@ -67,7 +67,9 @@ def register_configurations(self): self.application.bind("config.cache", "tests.integrations.config.cache") self.application.bind("config.broadcast", "tests.integrations.config.broadcast") self.application.bind("config.auth", "tests.integrations.config.auth") - self.application.bind("config.notification", "tests.integrations.config.notification") + self.application.bind( + "config.notification", "tests.integrations.config.notification" + ) self.application.bind( "config.filesystem", "tests.integrations.config.filesystem" ) diff --git a/tests/integrations/config/notification.py b/tests/integrations/config/notification.py index d8b9a857..72cc691d 100644 --- a/tests/integrations/config/notification.py +++ b/tests/integrations/config/notification.py @@ -18,4 +18,4 @@ }, } -DRY = False \ No newline at end of file +DRY = False From 2fd9d1fb85fae60a4f9b1753d3affd3416356108 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Fri, 7 May 2021 20:14:29 -0400 Subject: [PATCH 38/54] fixed duplicate key --- src/masonite/providers/HelpersProvider.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/masonite/providers/HelpersProvider.py b/src/masonite/providers/HelpersProvider.py index 05960827..1aa6e9bf 100644 --- a/src/masonite/providers/HelpersProvider.py +++ b/src/masonite/providers/HelpersProvider.py @@ -17,7 +17,6 @@ def boot(self): { "request": lambda: request, "auth": request.user, - "route": self.application.make("router").route, "cookie": request.cookie, "asset": AssetHelper(self.application).asset, "url": UrlHelper(self.application).url, From a4c7b8370234e4fea6393d06b850c7e2eb1f8e97 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Fri, 7 May 2021 21:47:11 -0400 Subject: [PATCH 39/54] modified notifications --- src/masonite/notification/Notification.py | 20 ++++++------------ .../notification/NotificationManager.py | 7 +++---- src/masonite/notification/Textable.py | 6 ++++++ src/masonite/notification/__init__.py | 1 + tests/integrations/app/User.py | 3 ++- .../notifications/OneTimePassword.py | 21 +++++++++++++++++++ 6 files changed, 39 insertions(+), 19 deletions(-) create mode 100644 src/masonite/notification/Textable.py create mode 100644 tests/integrations/notifications/OneTimePassword.py diff --git a/src/masonite/notification/Notification.py b/src/masonite/notification/Notification.py index 1bb7db4f..d876d471 100644 --- a/src/masonite/notification/Notification.py +++ b/src/masonite/notification/Notification.py @@ -1,29 +1,18 @@ """Base Notification facade.""" -class Notification: - """Notification class representing a notification.""" - - def __init__(self, *args, **kwargs): - self.id = None - self._dry = False - self._fail_silently = False - def broadcast_on(self): - """Get the channels the event should broadcast on.""" - return [] +class Notification: def via(self, notifiable): """Defines the notification's delivery channels.""" raise NotImplementedError("via() method should be implemented.") - @property def should_send(self): - return not self._dry + return True - @property def ignore_errors(self): - return self._fail_silently + return False @classmethod def type(cls): @@ -47,3 +36,6 @@ def fail_silently(self): """ self._fail_silently = True return self + + def text(self, text): + return Sms().text(text) diff --git a/src/masonite/notification/NotificationManager.py b/src/masonite/notification/NotificationManager.py index 0c9e2d6b..01fcd81c 100644 --- a/src/masonite/notification/NotificationManager.py +++ b/src/masonite/notification/NotificationManager.py @@ -6,7 +6,7 @@ from .AnonymousNotifiable import AnonymousNotifiable -class NotificationManager(object): +class NotificationManager: """Notification handler which handle sending/queuing notifications anonymously or to notifiables through different channels.""" @@ -40,13 +40,12 @@ def send( ): """Send the given notification to the given notifiables.""" notifiables = self._format_notifiables(notifiables) - if not notification.should_send or dry or self.options.get("dry"): + if not notification.should_send() or dry or self.options.get("dry"): key = notification.type() self.dry_notifications.update( {key: notifiables + self.dry_notifications.get(key, [])} ) return - results = [] for notifiable in notifiables: # get drivers to use for sending this notification @@ -69,7 +68,7 @@ def send( else: results.append(driver_instance.send(notifiable, notification)) except Exception as e: - if not notification.ignore_errors and not fail_silently: + if not notification.ignore_errors() and not fail_silently: raise e return results[0] if len(results) == 1 else results diff --git a/src/masonite/notification/Textable.py b/src/masonite/notification/Textable.py new file mode 100644 index 00000000..433abbf5 --- /dev/null +++ b/src/masonite/notification/Textable.py @@ -0,0 +1,6 @@ +from .Sms import Sms + +class Textable: + + def text_message(self, message): + return Sms().text(message) \ No newline at end of file diff --git a/src/masonite/notification/__init__.py b/src/masonite/notification/__init__.py index c32e771c..0db22d80 100644 --- a/src/masonite/notification/__init__.py +++ b/src/masonite/notification/__init__.py @@ -6,3 +6,4 @@ from .AnonymousNotifiable import AnonymousNotifiable from .Sms import Sms from .SlackMessage import SlackMessage +from .Textable import Textable diff --git a/tests/integrations/app/User.py b/tests/integrations/app/User.py index 34f7c164..a6a2da64 100644 --- a/tests/integrations/app/User.py +++ b/tests/integrations/app/User.py @@ -1,6 +1,7 @@ from masoniteorm.models import Model from src.masonite.authentication import Authenticates +from src.masonite.notification import Notifiable -class User(Model, Authenticates): +class User(Model, Authenticates, Notifiable): __fillable__ = ["name", "password", "email", "phone"] diff --git a/tests/integrations/notifications/OneTimePassword.py b/tests/integrations/notifications/OneTimePassword.py new file mode 100644 index 00000000..0303ba00 --- /dev/null +++ b/tests/integrations/notifications/OneTimePassword.py @@ -0,0 +1,21 @@ +from src.masonite.notification import Notification, Textable +from src.masonite.mail import Mailable +from src.masonite.mail import Mailable + + +class OneTimePassword(Notification, Mailable, Textable): + + def to_mail(self, notifiable): + return ( + self + .to(notifiable.email) + .subject("Masonite 4") + .from_("hello@email.com") + .text(f"Hello {notifiable.name}") + ) + + def to_vonage(self, notifiable): + return self.text_message("Welcome !").to('6314870798').from_("33123456789") + + def via(self, notifiable): + return ["vonage"] From aafc2a9d294003e64c022742db6eaa622a74ffa8 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Fri, 7 May 2021 21:56:46 -0400 Subject: [PATCH 40/54] fixed tests --- src/masonite/notification/Notification.py | 3 +++ tests/features/notification/test_notification.py | 8 ++------ tests/features/notification/test_vonage_driver.py | 12 +++++++----- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/masonite/notification/Notification.py b/src/masonite/notification/Notification.py index d876d471..c922498c 100644 --- a/src/masonite/notification/Notification.py +++ b/src/masonite/notification/Notification.py @@ -14,6 +14,9 @@ def should_send(self): def ignore_errors(self): return False + def broadcast_on(self): + return 'broadcast' + @classmethod def type(cls): """Get notification type defined with class name.""" diff --git a/tests/features/notification/test_notification.py b/tests/features/notification/test_notification.py index f3ad1663..d31c7d44 100644 --- a/tests/features/notification/test_notification.py +++ b/tests/features/notification/test_notification.py @@ -27,15 +27,11 @@ def via(self, notifiable): class TestNotification(TestCase): def test_should_send(self): notification = WelcomeNotification() - self.assertTrue(notification.should_send) - notification.dry() - self.assertFalse(notification.should_send) + self.assertTrue(notification.should_send()) def test_ignore_errors(self): notification = WelcomeNotification() - self.assertFalse(notification.ignore_errors) - notification.fail_silently() - self.assertTrue(notification.ignore_errors) + self.assertFalse(notification.ignore_errors()) def test_notification_type(self): self.assertEqual("WelcomeNotification", WelcomeNotification().type()) diff --git a/tests/features/notification/test_vonage_driver.py b/tests/features/notification/test_vonage_driver.py index f1bc461d..460734b2 100644 --- a/tests/features/notification/test_vonage_driver.py +++ b/tests/features/notification/test_vonage_driver.py @@ -1,6 +1,6 @@ from tests import TestCase from unittest.mock import patch -from src.masonite.notification import Notification, Notifiable, Sms +from src.masonite.notification import Notification, Notifiable, Sms, Textable from src.masonite.exceptions import NotificationException from masoniteorm.models import Model @@ -15,9 +15,9 @@ def route_notification_for_vonage(self): return "+33123456789" -class WelcomeUserNotification(Notification): +class WelcomeUserNotification(Notification, Textable): def to_vonage(self, notifiable): - return Sms().to(notifiable.phone).text("Welcome !").from_("123456") + return self.text_message("Welcome !").from_("123456") def via(self, notifiable): return ["vonage"] @@ -30,6 +30,9 @@ def to_vonage(self, notifiable): def via(self, notifiable): return ["vonage"] + def should_send(self): + return True + class OtherNotification(Notification): def to_vonage(self, notifiable): @@ -63,8 +66,7 @@ def test_sending_without_credentials(self): WelcomeNotification() ) error_message = str(e.exception) - self.assertIn("Code [2]", error_message) - self.assertIn("api_key", error_message) + self.assertIn("Code [29]", error_message) def test_send_to_anonymous(self): with patch("vonage.sms.Sms") as MockSmsClass: From 760117c44a21c0c537503119ba99c207b2085da4 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Fri, 7 May 2021 21:56:52 -0400 Subject: [PATCH 41/54] added database --- database.sqlite3 | Bin 348160 -> 348160 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/database.sqlite3 b/database.sqlite3 index cb0778557e6ed0c795188db07c6808d004b55da1..fafde5a6a8be7ec097501ead7f37f5cbd6cb6341 100644 GIT binary patch delta 89 zcmZozAlk4%bb>Tv&O{k!#+;1_&iX2;mKK&NCZ;C3#)+nux+az>sk#=aNk+Ps2A1Z@ t#^x!;$ri?wkL&AmStuA7TA7+z8Cx_Nv>7nA88EdOFt-`7EHhwL004e^7!v>h delta 89 zcmZozAlk4%bb>Tv$V3@u#*mE(&iX3mW(EePX{O1#sg_A5x+bRP#=1#~=0>_{7RDB) rsb*$rCMhP9kL&AmnJX9>TbWt{QIkQN0b`p1Q=0*En*qx*16Bn9YK0fv From fbafb9a137136f4a57a97bf00c9fcc6b11fe65a2 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Fri, 7 May 2021 21:58:00 -0400 Subject: [PATCH 42/54] formatted --- src/masonite/notification/Notification.py | 6 ++---- src/masonite/notification/Textable.py | 4 ++-- tests/integrations/notifications/OneTimePassword.py | 6 ++---- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/masonite/notification/Notification.py b/src/masonite/notification/Notification.py index c922498c..2c670e72 100644 --- a/src/masonite/notification/Notification.py +++ b/src/masonite/notification/Notification.py @@ -1,9 +1,7 @@ """Base Notification facade.""" - class Notification: - def via(self, notifiable): """Defines the notification's delivery channels.""" raise NotImplementedError("via() method should be implemented.") @@ -15,7 +13,7 @@ def ignore_errors(self): return False def broadcast_on(self): - return 'broadcast' + return "broadcast" @classmethod def type(cls): @@ -39,6 +37,6 @@ def fail_silently(self): """ self._fail_silently = True return self - + def text(self, text): return Sms().text(text) diff --git a/src/masonite/notification/Textable.py b/src/masonite/notification/Textable.py index 433abbf5..a1fa5372 100644 --- a/src/masonite/notification/Textable.py +++ b/src/masonite/notification/Textable.py @@ -1,6 +1,6 @@ from .Sms import Sms -class Textable: +class Textable: def text_message(self, message): - return Sms().text(message) \ No newline at end of file + return Sms().text(message) diff --git a/tests/integrations/notifications/OneTimePassword.py b/tests/integrations/notifications/OneTimePassword.py index 0303ba00..9a49c8e7 100644 --- a/tests/integrations/notifications/OneTimePassword.py +++ b/tests/integrations/notifications/OneTimePassword.py @@ -4,18 +4,16 @@ class OneTimePassword(Notification, Mailable, Textable): - def to_mail(self, notifiable): return ( - self - .to(notifiable.email) + self.to(notifiable.email) .subject("Masonite 4") .from_("hello@email.com") .text(f"Hello {notifiable.name}") ) def to_vonage(self, notifiable): - return self.text_message("Welcome !").to('6314870798').from_("33123456789") + return self.text_message("Welcome !").to("6314870798").from_("33123456789") def via(self, notifiable): return ["vonage"] From c358d3ab8a3f3b82fb125dd7abc9ec0471d06378 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Fri, 7 May 2021 22:00:10 -0400 Subject: [PATCH 43/54] fixed test --- tests/features/notification/test_vonage_driver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/features/notification/test_vonage_driver.py b/tests/features/notification/test_vonage_driver.py index 460734b2..81bb835e 100644 --- a/tests/features/notification/test_vonage_driver.py +++ b/tests/features/notification/test_vonage_driver.py @@ -66,7 +66,7 @@ def test_sending_without_credentials(self): WelcomeNotification() ) error_message = str(e.exception) - self.assertIn("Code [29]", error_message) + self.assertIn("Code [2]", error_message) def test_send_to_anonymous(self): with patch("vonage.sms.Sms") as MockSmsClass: From b6c76ed854004b46f182d89076c54875735b590c Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Fri, 7 May 2021 22:14:51 -0400 Subject: [PATCH 44/54] moved notification command into the notifications module --- src/masonite/commands/__init__.py | 3 +-- src/masonite/foundation/Kernel.py | 5 +---- .../commands/MakeNotificationCommand.py | 2 +- .../commands/NotificationTableCommand.py | 2 +- src/masonite/notification/commands/__init__.py | 2 ++ .../providers/NotificationProvider.py | 16 +++++++++++----- src/masonite/notification/providers/__init__.py | 1 + src/masonite/providers/__init__.py | 1 - tests/integrations/config/providers.py | 3 +-- 9 files changed, 19 insertions(+), 16 deletions(-) rename src/masonite/{ => notification}/commands/MakeNotificationCommand.py (95%) rename src/masonite/{ => notification}/commands/NotificationTableCommand.py (95%) create mode 100644 src/masonite/notification/commands/__init__.py rename src/masonite/{ => notification}/providers/NotificationProvider.py (70%) create mode 100644 src/masonite/notification/providers/__init__.py diff --git a/src/masonite/commands/__init__.py b/src/masonite/commands/__init__.py index c1b102b3..d417de40 100644 --- a/src/masonite/commands/__init__.py +++ b/src/masonite/commands/__init__.py @@ -10,5 +10,4 @@ from .MakeControllerCommand import MakeControllerCommand from .MakeJobCommand import MakeJobCommand from .MakeMailableCommand import MakeMailableCommand -from .MakeNotificationCommand import MakeNotificationCommand -from .NotificationTableCommand import NotificationTableCommand + diff --git a/src/masonite/foundation/Kernel.py b/src/masonite/foundation/Kernel.py index 1349d2f2..04bcdce6 100644 --- a/src/masonite/foundation/Kernel.py +++ b/src/masonite/foundation/Kernel.py @@ -13,8 +13,6 @@ MakeControllerCommand, MakeJobCommand, MakeMailableCommand, - MakeNotificationCommand, - NotificationTableCommand, ) import os from ..environment import LoadEnvironment @@ -82,8 +80,7 @@ def register_commands(self): MakeControllerCommand(self.application), MakeJobCommand(self.application), MakeMailableCommand(self.application), - MakeNotificationCommand(self.application), - NotificationTableCommand(), + ), ) diff --git a/src/masonite/commands/MakeNotificationCommand.py b/src/masonite/notification/commands/MakeNotificationCommand.py similarity index 95% rename from src/masonite/commands/MakeNotificationCommand.py rename to src/masonite/notification/commands/MakeNotificationCommand.py index ba3478ec..b3bd2623 100644 --- a/src/masonite/commands/MakeNotificationCommand.py +++ b/src/masonite/notification/commands/MakeNotificationCommand.py @@ -1,6 +1,6 @@ """MakeNotificationCommand Class""" from cleo import Command -from ..utils.filesystem import make_directory +from ...utils.filesystem import make_directory import inflection import os diff --git a/src/masonite/commands/NotificationTableCommand.py b/src/masonite/notification/commands/NotificationTableCommand.py similarity index 95% rename from src/masonite/commands/NotificationTableCommand.py rename to src/masonite/notification/commands/NotificationTableCommand.py index 4e002950..2e7b8f6a 100644 --- a/src/masonite/commands/NotificationTableCommand.py +++ b/src/masonite/notification/commands/NotificationTableCommand.py @@ -1,6 +1,6 @@ """Notification Table Command.""" from cleo import Command -from ..utils.filesystem import make_directory +from ...utils.filesystem import make_directory import os import pathlib import datetime diff --git a/src/masonite/notification/commands/__init__.py b/src/masonite/notification/commands/__init__.py new file mode 100644 index 00000000..4376305f --- /dev/null +++ b/src/masonite/notification/commands/__init__.py @@ -0,0 +1,2 @@ +from .MakeNotificationCommand import MakeNotificationCommand +from .NotificationTableCommand import NotificationTableCommand \ No newline at end of file diff --git a/src/masonite/providers/NotificationProvider.py b/src/masonite/notification/providers/NotificationProvider.py similarity index 70% rename from src/masonite/providers/NotificationProvider.py rename to src/masonite/notification/providers/NotificationProvider.py index be96c003..e14e2733 100644 --- a/src/masonite/providers/NotificationProvider.py +++ b/src/masonite/notification/providers/NotificationProvider.py @@ -1,14 +1,16 @@ -from .Provider import Provider -from ..utils.structures import load -from ..notification.drivers import ( +from ...providers import Provider +from ...utils.structures import load +from ..drivers import ( BroadcastDriver, DatabaseDriver, MailDriver, SlackDriver, VonageDriver, ) -from ..notification import NotificationManager -from ..notification import MockNotification +from ..NotificationManager import NotificationManager +from ..MockNotification import MockNotification +from ..commands import MakeNotificationCommand, NotificationTableCommand + class NotificationProvider(Provider): @@ -29,6 +31,10 @@ def register(self): self.application.bind("notification", notification_manager) self.application.bind("mock.notification", MockNotification) + self.application.make('commands').add( + MakeNotificationCommand(self.application), + NotificationTableCommand(), + ) def boot(self): pass diff --git a/src/masonite/notification/providers/__init__.py b/src/masonite/notification/providers/__init__.py new file mode 100644 index 00000000..c2b85667 --- /dev/null +++ b/src/masonite/notification/providers/__init__.py @@ -0,0 +1 @@ +from .NotificationProvider import NotificationProvider \ No newline at end of file diff --git a/src/masonite/providers/__init__.py b/src/masonite/providers/__init__.py index 0d128a1d..bb22241e 100644 --- a/src/masonite/providers/__init__.py +++ b/src/masonite/providers/__init__.py @@ -9,7 +9,6 @@ from .SessionProvider import SessionProvider from .HelpersProvider import HelpersProvider from .QueueProvider import QueueProvider -from .NotificationProvider import NotificationProvider from .CacheProvider import CacheProvider from ..events.providers import EventProvider from ..filesystem.providers import StorageProvider diff --git a/tests/integrations/config/providers.py b/tests/integrations/config/providers.py index 00a628e4..c2ca3e40 100644 --- a/tests/integrations/config/providers.py +++ b/tests/integrations/config/providers.py @@ -5,7 +5,6 @@ WhitenoiseProvider, ExceptionProvider, MailProvider, - NotificationProvider, SessionProvider, QueueProvider, CacheProvider, @@ -17,7 +16,7 @@ ) from src.masonite.scheduling.providers import ScheduleProvider - +from src.masonite.notification.providers import NotificationProvider PROVIDERS = [ FrameworkProvider, From a445b1fbba394a6f1d59a59780587377123a36d7 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Fri, 7 May 2021 22:15:03 -0400 Subject: [PATCH 45/54] formatted --- src/masonite/commands/__init__.py | 1 - src/masonite/foundation/Kernel.py | 1 - src/masonite/notification/commands/__init__.py | 2 +- src/masonite/notification/providers/NotificationProvider.py | 3 +-- src/masonite/notification/providers/__init__.py | 2 +- 5 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/masonite/commands/__init__.py b/src/masonite/commands/__init__.py index d417de40..815cb318 100644 --- a/src/masonite/commands/__init__.py +++ b/src/masonite/commands/__init__.py @@ -10,4 +10,3 @@ from .MakeControllerCommand import MakeControllerCommand from .MakeJobCommand import MakeJobCommand from .MakeMailableCommand import MakeMailableCommand - diff --git a/src/masonite/foundation/Kernel.py b/src/masonite/foundation/Kernel.py index 04bcdce6..95d66b4a 100644 --- a/src/masonite/foundation/Kernel.py +++ b/src/masonite/foundation/Kernel.py @@ -80,7 +80,6 @@ def register_commands(self): MakeControllerCommand(self.application), MakeJobCommand(self.application), MakeMailableCommand(self.application), - ), ) diff --git a/src/masonite/notification/commands/__init__.py b/src/masonite/notification/commands/__init__.py index 4376305f..7ea3fdcb 100644 --- a/src/masonite/notification/commands/__init__.py +++ b/src/masonite/notification/commands/__init__.py @@ -1,2 +1,2 @@ from .MakeNotificationCommand import MakeNotificationCommand -from .NotificationTableCommand import NotificationTableCommand \ No newline at end of file +from .NotificationTableCommand import NotificationTableCommand diff --git a/src/masonite/notification/providers/NotificationProvider.py b/src/masonite/notification/providers/NotificationProvider.py index e14e2733..5172ff95 100644 --- a/src/masonite/notification/providers/NotificationProvider.py +++ b/src/masonite/notification/providers/NotificationProvider.py @@ -12,7 +12,6 @@ from ..commands import MakeNotificationCommand, NotificationTableCommand - class NotificationProvider(Provider): """Notifications Provider""" @@ -31,7 +30,7 @@ def register(self): self.application.bind("notification", notification_manager) self.application.bind("mock.notification", MockNotification) - self.application.make('commands').add( + self.application.make("commands").add( MakeNotificationCommand(self.application), NotificationTableCommand(), ) diff --git a/src/masonite/notification/providers/__init__.py b/src/masonite/notification/providers/__init__.py index c2b85667..acd9c98e 100644 --- a/src/masonite/notification/providers/__init__.py +++ b/src/masonite/notification/providers/__init__.py @@ -1 +1 @@ -from .NotificationProvider import NotificationProvider \ No newline at end of file +from .NotificationProvider import NotificationProvider From b5078e4d19d7e68a102de2030f5d8d532f1cc8bb Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Fri, 7 May 2021 22:32:56 -0400 Subject: [PATCH 46/54] fixed the stub --- src/masonite/stubs/notification/Notification.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/masonite/stubs/notification/Notification.py b/src/masonite/stubs/notification/Notification.py index 285806da..f14e0dbf 100644 --- a/src/masonite/stubs/notification/Notification.py +++ b/src/masonite/stubs/notification/Notification.py @@ -2,10 +2,10 @@ from masonite.mail import Mailable -class __class__(Notification): +class __class__(Notification, Mailable): def to_mail(self, notifiable): return ( - Mailable() + self .to(notifiable.email) .subject("Masonite 4") .from_("hello@email.com") From 7b88c3c291f3aec5601a89b2999392a140329b5f Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Mon, 10 May 2021 18:52:19 +0200 Subject: [PATCH 47/54] remove unused code --- src/masonite/notification/NotificationManager.py | 8 -------- tests/features/notification/test_anonymous_notifiable.py | 4 +--- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/src/masonite/notification/NotificationManager.py b/src/masonite/notification/NotificationManager.py index 01fcd81c..128883b6 100644 --- a/src/masonite/notification/NotificationManager.py +++ b/src/masonite/notification/NotificationManager.py @@ -10,7 +10,6 @@ class NotificationManager: """Notification handler which handle sending/queuing notifications anonymously or to notifiables through different channels.""" - # those classes are use for mock => should we put them only in NotificationMock ? sent_notifications = {} dry_notifications = {} @@ -73,9 +72,6 @@ def send( return results[0] if len(results) == 1 else results - # def is_custom_channel(self, channel): - # return issubclass(channel, BaseDriver) - def _format_notifiables(self, notifiables): from masoniteorm.collection import Collection @@ -87,7 +83,3 @@ def _format_notifiables(self, notifiables): def route(self, driver, route): """Specify how to send a notification to an anonymous notifiable.""" return AnonymousNotifiable(self.application).route(driver, route) - - # TESTING - def assertNotificationDried(self, notification_class): - assert notification_class.__name__ in self.dry_notifications.keys() diff --git a/tests/features/notification/test_anonymous_notifiable.py b/tests/features/notification/test_anonymous_notifiable.py index 2c7976f4..1558e603 100644 --- a/tests/features/notification/test_anonymous_notifiable.py +++ b/tests/features/notification/test_anonymous_notifiable.py @@ -38,9 +38,7 @@ def test_can_override_dry_when_sending(self): AnonymousNotifiable(self.application).route("mail", "user@example.com").send( WelcomeNotification(), dry=True ) - self.application.make("notification").assertNotificationDried( - WelcomeNotification - ) + self.application.make("notification").dry_notifications.keys() == 1 def test_can_override_fail_silently_when_sending(self): class FailingNotification(Notification): From 11e83b7379630b3f14e9aaf993a54eb78d997cc2 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Mon, 10 May 2021 18:54:19 +0200 Subject: [PATCH 48/54] remove queueing feature --- src/masonite/notification/NotificationManager.py | 8 ++++---- src/masonite/notification/drivers/BaseDriver.py | 7 ------- src/masonite/notification/drivers/BroadcastDriver.py | 9 --------- src/masonite/notification/drivers/DatabaseDriver.py | 7 ------- src/masonite/notification/drivers/MailDriver.py | 11 ----------- src/masonite/notification/drivers/SlackDriver.py | 6 ------ .../notification/drivers/vonage/VonageDriver.py | 6 ------ 7 files changed, 4 insertions(+), 50 deletions(-) diff --git a/src/masonite/notification/NotificationManager.py b/src/masonite/notification/NotificationManager.py index 128883b6..177e644d 100644 --- a/src/masonite/notification/NotificationManager.py +++ b/src/masonite/notification/NotificationManager.py @@ -62,10 +62,10 @@ def send( # this case is not possible but that should not stop other channels to be used continue try: - if isinstance(notification, ShouldQueue): - results.append(driver_instance.queue(notifiable, notification)) - else: - results.append(driver_instance.send(notifiable, notification)) + # if isinstance(notification, ShouldQueue): + # results.append(driver_instance.queue(notifiable, notification)) + # else: + results.append(driver_instance.send(notifiable, notification)) except Exception as e: if not notification.ignore_errors() and not fail_silently: raise e diff --git a/src/masonite/notification/drivers/BaseDriver.py b/src/masonite/notification/drivers/BaseDriver.py index c23db62e..04b41248 100644 --- a/src/masonite/notification/drivers/BaseDriver.py +++ b/src/masonite/notification/drivers/BaseDriver.py @@ -6,13 +6,6 @@ def send(self, notifiable, notification): "send() method must be implemented for a notification driver." ) - def queue(self, notifiable, notification): - """Implements queueing the notification to be sent later - this driver.""" - raise NotImplementedError( - "queue() method must be implemented for a notification driver." - ) - def get_data(self, driver, notifiable, notification): """Get the data for the notification.""" method_name = f"to_{driver}" diff --git a/src/masonite/notification/drivers/BroadcastDriver.py b/src/masonite/notification/drivers/BroadcastDriver.py index e9d4db5e..cdda012c 100644 --- a/src/masonite/notification/drivers/BroadcastDriver.py +++ b/src/masonite/notification/drivers/BroadcastDriver.py @@ -20,12 +20,3 @@ def send(self, notifiable, notification): ) event = notification.type() self.application.make("broadcast").channel(channels, event, data) - - def queue(self, notifiable, notification): - """Used to queue the notification to be broadcasted.""" - # Makes sense ?????? - # data = self.get_data("broadcast", notifiable, notification) - # channels = notification.broadcast_on() or notifiable.route_notification_for( - # "broadcast" - # ) - # self.application.make("queue").push(driver.channel, args=(channel, data)) diff --git a/src/masonite/notification/drivers/DatabaseDriver.py b/src/masonite/notification/drivers/DatabaseDriver.py index 52886e0e..146c58d5 100644 --- a/src/masonite/notification/drivers/DatabaseDriver.py +++ b/src/masonite/notification/drivers/DatabaseDriver.py @@ -25,13 +25,6 @@ def send(self, notifiable, notification): data = self.build(notifiable, notification) return self.get_builder().new().create(data) - def queue(self, notifiable, notification): - """Used to queue the database notification creation.""" - data = self.build(notifiable, notification) - return self.application.make("queue").push( - self.get_builder().new().create, args=(data,) - ) - def build(self, notifiable, notification): """Build an array payload for the DatabaseNotification Model.""" return { diff --git a/src/masonite/notification/drivers/MailDriver.py b/src/masonite/notification/drivers/MailDriver.py index e8e2380e..a620f6cf 100644 --- a/src/masonite/notification/drivers/MailDriver.py +++ b/src/masonite/notification/drivers/MailDriver.py @@ -20,14 +20,3 @@ def send(self, notifiable, notification): mailable = mailable.to(recipients) # TODO: allow changing driver how ????? return self.application.make("mail").mailable(mailable).send(driver="terminal") - - def queue(self, notifiable, notification): - """Used to queue the email to send.""" - mailable = self.get_data("mail", notifiable, notification) - if not mailable._to: - recipients = notifiable.route_notification_for("mail") - mailable = mailable.to(recipients) - # TODO: allow changing driver for queueing + for sending mail ? - return self.application.make("queue").push( - self.application.make("mail").mailable(mailable).send, driver="async" - ) diff --git a/src/masonite/notification/drivers/SlackDriver.py b/src/masonite/notification/drivers/SlackDriver.py index 3da75ba8..dc69f58a 100644 --- a/src/masonite/notification/drivers/SlackDriver.py +++ b/src/masonite/notification/drivers/SlackDriver.py @@ -29,12 +29,6 @@ def send(self, notifiable, notification): else: self.send_via_api(slack_message) - # def queue(self, notifiable, notification): - # TODO - # """Used to queue the notification to be sent to slack.""" - # method, payload = self.prepare(notifiable, notification) - # return self.application.make("queue").push(method, args=payload) - def build(self, notifiable, notification): """Build Slack message payload sent to Slack API or through Slack webhook.""" slack_message = self.get_data("slack", notifiable, notification) diff --git a/src/masonite/notification/drivers/vonage/VonageDriver.py b/src/masonite/notification/drivers/vonage/VonageDriver.py index 6af2d30a..2c2c7fa8 100644 --- a/src/masonite/notification/drivers/vonage/VonageDriver.py +++ b/src/masonite/notification/drivers/vonage/VonageDriver.py @@ -44,12 +44,6 @@ def send(self, notifiable, notification): self._handle_errors(response) return response - def queue(self, notifiable, notification): - """Used to queue the SMS notification to be send.""" - sms = self.build(notifiable, notification) - client = self.get_sms_client() - self.application.make("queue").push(client.send_message, args=(sms,)) - def _handle_errors(self, response): """Handle errors of Vonage API. Raises VonageAPIError if request does not succeed. From 383025a68f30617559727bd853888ad624f690a0 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Mon, 10 May 2021 19:19:09 +0200 Subject: [PATCH 49/54] fix sending to multiple recipients --- src/masonite/notification/drivers/SlackDriver.py | 3 +-- .../notification/drivers/vonage/VonageDriver.py | 10 ++++++---- tests/features/notification/test_vonage_driver.py | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/masonite/notification/drivers/SlackDriver.py b/src/masonite/notification/drivers/SlackDriver.py index dc69f58a..2d297419 100644 --- a/src/masonite/notification/drivers/SlackDriver.py +++ b/src/masonite/notification/drivers/SlackDriver.py @@ -36,10 +36,9 @@ def build(self, notifiable, notification): mode = self.get_sending_mode(recipients) slack_message = slack_message.mode(mode) - if mode == self.WEBHOOK_MODE: # and not slack_message._webhook: + if mode == self.WEBHOOK_MODE: slack_message = slack_message.to(recipients) elif mode == self.API_MODE: - # if not slack_message._channel: slack_message = slack_message.to(recipients) if not slack_message._token: slack_message = slack_message.token(self.options.get("token")) diff --git a/src/masonite/notification/drivers/vonage/VonageDriver.py b/src/masonite/notification/drivers/vonage/VonageDriver.py index 2c2c7fa8..f2ac770f 100644 --- a/src/masonite/notification/drivers/vonage/VonageDriver.py +++ b/src/masonite/notification/drivers/vonage/VonageDriver.py @@ -20,7 +20,7 @@ def build(self, notifiable, notification): if not sms._to: recipients = notifiable.route_notification_for("vonage") sms = sms.to(recipients) - return sms.build().get_options() + return sms def get_sms_client(self): try: @@ -39,9 +39,11 @@ def send(self, notifiable, notification): """Used to send the SMS.""" sms = self.build(notifiable, notification) client = self.get_sms_client() - # TODO: here if multiple recipients are defined in Sms it won't work ? check with Vonage API - response = client.send_message(sms) - self._handle_errors(response) + recipients = sms._to + for recipient in recipients: + payload = sms.to(recipient).build().get_options() + response = client.send_message(payload) + self._handle_errors(response) return response def _handle_errors(self, response): diff --git a/tests/features/notification/test_vonage_driver.py b/tests/features/notification/test_vonage_driver.py index 81bb835e..19025230 100644 --- a/tests/features/notification/test_vonage_driver.py +++ b/tests/features/notification/test_vonage_driver.py @@ -98,4 +98,4 @@ def test_global_send_from_is_used_when_not_specified(self): sms = self.notification.get_driver("vonage").build( notifiable, OtherNotification() ) - self.assertEqual(sms.get("from"), "+33000000000") + self.assertEqual(sms._from, "+33000000000") From b71eca1557b9c12de0d4726ea5b4f54883c4d26e Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Mon, 10 May 2021 19:30:23 +0200 Subject: [PATCH 50/54] remove unused code --- src/masonite/notification/Notification.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/masonite/notification/Notification.py b/src/masonite/notification/Notification.py index 2c670e72..9655c682 100644 --- a/src/masonite/notification/Notification.py +++ b/src/masonite/notification/Notification.py @@ -37,6 +37,3 @@ def fail_silently(self): """ self._fail_silently = True return self - - def text(self, text): - return Sms().text(text) From 6ba7c8b1382c46fb9e41f49a65af18b3c129eda2 Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Mon, 31 May 2021 07:11:29 +0200 Subject: [PATCH 51/54] add back sqlite database from master --- database.sqlite3 | Bin 0 -> 348160 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 database.sqlite3 diff --git a/database.sqlite3 b/database.sqlite3 new file mode 100644 index 0000000000000000000000000000000000000000..d364815fe5f7d7e29eabf9548dcd2dbf857d952d GIT binary patch literal 348160 zcmeI5TWlL?mfuTP>S9^l?N-Z8+ihBQx2?7*^QsH`=9_$J-{pJX)J3r*QKCqhq^oMK!PB6e&WKYr?29>eV#*o+MF6s^m*>7>4|AIeWWj&n$?nJztp-WbP)& zryW-pvZ>G{c3A#@2*`9Ulp%)B9+W)$*lg1YxA|~?-s5p9_#bO=|`uu z^pVYzipTo<7bja>z5Dk!Jl#?JODF02=}Kl&(M;LP*3Ex;*>@`cfxf#7+JaU_ES8Sn z)6(J%&!x-PJ(q4=xNwB~ zjifqPVpI0MdXKOd?01t1c}|UZRs2jfOB6qH^2XTM=%wr9*Uw!X zy>|Wh#VbcVeQ7nh>EEQc=wFGeiTERpJ{kj-Pu-2K-rnAZXB$SdKz}4t{J*BnG>h*M z&Unmo?$Y(qGoxeTqRTDuh^GL?bMyGv$+O4D4h6#De(`fhJjJDB^i5tNM&Hk9WG^wX zvMPS8V27TxHmA)+we)y#u~+*EGT``Ryhz?7;1#XdjBV+&_yhjX!hB&{DQqQwz#qDC z>D<*DqrAuTH?{VD&}+2E6hD%2fAN<>l-yR>u^0C?xO(^PYsj@1*T!6YDyOH) zaq}lz`UZWx;y2?z_(6*wt>DbNf+wnGC#GMpmi{i9WkdIWQs{s90s#;J0T2KI5C8!X z009sH0T2KI5U48xhZ>rd8|?vKz(4Mz|Ba0MLP7f7_(tkvd?Qy;Oi66S|0k91lO3OI z{;#es7?TA75C8!X009sH0T7UYK=bdEp73Ieaw8ZFew>49MU!m#Gq^6UF5z^;7cj7`QHLRsSP9$&nhwe_E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S%@Kmz53KELLy9^Z#}uC4=jP!5Jl-a;`PejuWYn zoopyT(F+uMcmw@kgg4OtODLfKORxX0y}_H9e4b|5W>1scq8`vObc@_;uY-;WogJWK zLgzKmF`@G+=orzdt^O|>FCx4-$TkQR=+i5dUi9e|DA1=@pm1cL-s<{)AGS?hy;<7_ z6ncUT`oG8uLH{r5|8HS|@LsV8C>X_VpkNfcoQQ(*jvl^lWMv(2{(sQ^M0dZu24_1R z*P3X@iB!lS8wyZ_fkF>&p#O{T2Ks*q1@wRU_5ToWV)A*KL-xBxLC`VM;V4Q8fQ|_r zKj@gy@qvyBoui;*M5ng;zi7OO@Me^45Gc^6S17&c(<@M*Pp?4X$UeQ*_5WkoHd(#- z2q^Rf8T5aV6@va>(*NJb0^z;lJ)mF|5ujicBThs?nY9o3x{;N2fc`&hf1>-0yas1G z9oL#@$0=VbB=~Lf_o}k-^*DcMy(tx)$*=FuYME?iAfC)>Q)x9DPbD)0>iqn`HTB`y zcz(UD@T-$IusWuU!Eh6FQec$Ar!$&@rKN zv99O@tc*cwtN)9}iwJKf*am?@;y%6BdPJ@@iTm_gjp9oz5YE#}+^5%StM~#|!B+7( zQ0NIVne~557l@+Btp8hzf|BW?&x7+@q^FMa{P5ZxX`?>39x(140KmY_l00cmw#sug-t>HdxsA9O* w5=;jf Date: Mon, 31 May 2021 07:21:14 +0200 Subject: [PATCH 52/54] update migrations to use big_increments --- src/masonite/stubs/notification/create_notifications_table.py | 2 +- .../migrations/2021_03_18_190410_create_notifications_table.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/masonite/stubs/notification/create_notifications_table.py b/src/masonite/stubs/notification/create_notifications_table.py index b16ea397..af83657c 100644 --- a/src/masonite/stubs/notification/create_notifications_table.py +++ b/src/masonite/stubs/notification/create_notifications_table.py @@ -5,7 +5,7 @@ class CreateNotificationsTable(Migration): def up(self): """Run the migrations.""" with self.schema.create("notifications") as table: - table.string("id", 36).primary() + table.big_increments("id").primary() table.string("type") table.text("data") table.morphs("notifiable") diff --git a/tests/integrations/databases/migrations/2021_03_18_190410_create_notifications_table.py b/tests/integrations/databases/migrations/2021_03_18_190410_create_notifications_table.py index b16ea397..af83657c 100644 --- a/tests/integrations/databases/migrations/2021_03_18_190410_create_notifications_table.py +++ b/tests/integrations/databases/migrations/2021_03_18_190410_create_notifications_table.py @@ -5,7 +5,7 @@ class CreateNotificationsTable(Migration): def up(self): """Run the migrations.""" with self.schema.create("notifications") as table: - table.string("id", 36).primary() + table.big_increments("id").primary() table.string("type") table.text("data") table.morphs("notifiable") From dbdc44622a39efccd5291daada5ad5b61902f85d Mon Sep 17 00:00:00 2001 From: Samuel Girardin Date: Mon, 31 May 2021 07:41:31 +0200 Subject: [PATCH 53/54] migrate db to add notifications table --- database.sqlite3 | Bin 348160 -> 348160 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/database.sqlite3 b/database.sqlite3 index d364815fe5f7d7e29eabf9548dcd2dbf857d952d..9b65a24526eb9a7aa5d1f24140816f3eeacbcc4b 100644 GIT binary patch delta 554 zcmZozAlk4%bb_=X7Xt%BC=km6u@?|?Pt-A1#=yl<$G{)Q+sCiX$>5AXmo_ zSA`HqCm&Y@B@{U&1&z!U1t(8;Prs1K8~MdqN-7IdCtu{3s82~ONmK}NjR;Zj^9SNk zA0HhBh*pqo@jzXne!-sZey%PGK)u)%fecd!a}09!a126HQIwjP5}#P2-~x12h^MbB z)bwPacu6W)Cd6~{0lsKkXM%7*ZLH42%rp4UFRrE#eI=4NMFT;vp8rqZknn4n!tHCKiTtPKXwuE=!1rr|gPgW2RX9Gny n62ED~whfHz0*s#9ISw$YGct0w8ysW=VkRJF-fnP^B*x4LcWuv}|LFv1Fs?%;L=aJcXo6g{st|{6<|?xJIC>n~`*w;?lu_O^1;MZaZX|VLBK< zc1S~9Wn^w;VhFLLQIv@t;(U<2C{*6e%E+Kmlo3hB7A|98$O}xTq6{k;_+9zBc+c~S z@I-RY<9f`c!I{gkiTyjf4O=7YF;;Gt0OqMox0vJ^Qy5lG78EE4#+()?=C%VFnlOe2 zjG+!=sKFSjAjT9XBSD+YJt~T<8M;z^^gz From 3fb268af2d3003626894bb13250fe5799eb9ca5a Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Thu, 24 Jun 2021 21:48:57 -0400 Subject: [PATCH 54/54] formatted --- src/masonite/stubs/notification/Notification.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/masonite/stubs/notification/Notification.py b/src/masonite/stubs/notification/Notification.py index f14e0dbf..b4d6330b 100644 --- a/src/masonite/stubs/notification/Notification.py +++ b/src/masonite/stubs/notification/Notification.py @@ -5,8 +5,7 @@ class __class__(Notification, Mailable): def to_mail(self, notifiable): return ( - self - .to(notifiable.email) + self.to(notifiable.email) .subject("Masonite 4") .from_("hello@email.com") .text(f"Hello {notifiable.name}")