Commit 48c408a4 authored by Jose Semedo's avatar Jose Semedo Committed by Carina Antunes
Browse files

[#54] Support for channels with category

parent d8fd240b
Pipeline #3087713 passed with stages
in 1 minute and 54 seconds
......@@ -42,9 +42,20 @@ For the `publisher` to send one message, it's necessary to open another terminal
- Enter the routing container
```bash
$ make docker-shell-env # To enter the container
```
A message send can be triggered using the `docker-send.py` script.
```
$ python scripts/docker-send.py # To send one message
```
The payload can also be customized with the following arguments:
```
usage: docker-send.py [-h] [-l] [-c] [-e E] [-ci CI] [-ni NI]
-l use to connect to localhost instead of activemq
-c use to send an email with Category
-e E use to set a target email username
-ci CI use to set a target channel id
-ni NI use to set a target notification id
```
To test the set up, it is possible to see the message in [ActiveMQ's admin UI](http://localhost:8161/admin/queues.jsp)
......
......@@ -64,7 +64,7 @@ class Router(megabus.Listener):
"""Convert message to json and retrieves target, body, created timestamp, priority."""
message_json = json.loads(message)
return {
notification = {
str(OutputMessageKeys.ID): message_json[str(InputMessageKeys.ID)],
str(OutputMessageKeys.CHANNEL_ID): message_json[str(InputMessageKeys.TARGET)][str(InputMessageKeys.ID)],
str(OutputMessageKeys.CHANNEL_NAME): message_json[str(InputMessageKeys.TARGET)][str(InputMessageKeys.NAME)],
......@@ -79,6 +79,14 @@ class Router(megabus.Listener):
str(OutputMessageKeys.IMGURL): message_json[str(InputMessageKeys.IMGURL)],
}
notification[str(OutputMessageKeys.CHANNEL_CATEGORY)] = (
message_json[str(InputMessageKeys.TARGET)][str(InputMessageKeys.CATEGORY)]
if InputMessageKeys.CATEGORY in message_json[InputMessageKeys.TARGET]
else None
)
return notification
def get_channel_users(self, channel_id):
"""Join users from our data source and the grappa system to return a unique users list."""
channel_users = self.data_source.get_channel_users(channel_id)
......
......@@ -20,6 +20,7 @@ class InputMessageKeys(StrEnum):
TARGET = "target"
ID = "id"
NAME = "name"
CATEGORY = "category_name"
SLUG = "slug"
PRIORITY = "priority"
SENT_AT = "sentAt"
......@@ -34,6 +35,7 @@ class OutputMessageKeys(StrEnum):
CHANNEL_ID = "channel_id"
CHANNEL_NAME = "channel_name"
CHANNEL_CATEGORY = "category_name"
CHANNEL_SLUG = "channel_slug"
PRIORITY = "priority"
CREATED_TIMESTAMP = "created_timestamp"
......@@ -72,20 +74,23 @@ def is_time_between(time, start_range, end_range):
def convert_notification_email_to_json_string(message, email):
"""Convert notification e-mail to json string."""
return json.dumps(
{
"channel_id": message[OutputMessageKeys.CHANNEL_ID],
"channel_name": message[OutputMessageKeys.CHANNEL_NAME],
"message_body": message[OutputMessageKeys.MESSAGE_BODY],
"summary": message[OutputMessageKeys.SUMMARY],
"link": message[OutputMessageKeys.LINK],
"img_url": message[OutputMessageKeys.IMGURL],
"email": email,
"priority": message[OutputMessageKeys.PRIORITY],
"notification_id": message[OutputMessageKeys.ID],
"created_at": message[OutputMessageKeys.CREATED_TIMESTAMP],
}
)
notif = {
"channel_id": message[OutputMessageKeys.CHANNEL_ID],
"channel_name": message[OutputMessageKeys.CHANNEL_NAME],
"message_body": message[OutputMessageKeys.MESSAGE_BODY],
"summary": message[OutputMessageKeys.SUMMARY],
"link": message[OutputMessageKeys.LINK],
"img_url": message[OutputMessageKeys.IMGURL],
"email": email,
"priority": message[OutputMessageKeys.PRIORITY],
"notification_id": message[OutputMessageKeys.ID],
"created_at": message[OutputMessageKeys.CREATED_TIMESTAMP],
}
if str(OutputMessageKeys.CHANNEL_CATEGORY) in message:
notif["category_name"] = message[OutputMessageKeys.CHANNEL_CATEGORY]
return json.dumps(notif)
def convert_notification_push_to_json_string(message, devicetoken, encoding=None):
......
"""Utility to send one message inside the docker environment."""
import argparse
import json
import stomp
conn = stomp.Connection([("activemq", 61613)])
parser = argparse.ArgumentParser(description="Send a test email notification.")
parser.add_argument("-l", action="store_true", default=False, help="use to connect to localhost instead of activemq")
parser.add_argument("-c", action="store_true", default=False, help="use to send an email with Category")
parser.add_argument("-e", default="user", help="use to set a target email username")
parser.add_argument("-ci", default="EF46ACA0-E73B-4794-8043-FFE974247CC8", help="use to set a target channel id")
parser.add_argument("-ni", default="D19EEA4-9DCA-48D9-A577-5DE9D2BF374A", help="use to set a target notification id")
args = parser.parse_args()
category = args.c
channelid = args.ci
notificationid = args.ni
email = args.e
localhost = args.l
connection_target = "localhost" if localhost else "activemq"
conn = stomp.Connection([(connection_target, 61613)])
conn.connect("admin", "admin", wait=True)
message_body = r"""{"id":"BD19EEA4-9DCA-48D9-A577-5DE9D2BF374A",
"target":{"id":"EF46ACA0-E73B-4794-8043-FFE974247CC8","slug":"test","name":"Test","description":"",
"visibility":"RESTRICTED","subscriptionPolicy":"DYNAMIC","archive":false,"APIKey":null,
"creationDate":"2021-02-26T12:58:42.131Z","lastActivityDate":"2021-02-26T12:58:42.131Z","incomingEmail":null,
"deleteDate":null}, "body":"<p>test</p>\n", "sendAt":null,"sentAt":"2021-02-26T13:59:40.754Z","users":[],
"link":"https://test-notifications.web.cern.ch/main/channels/a4db5752-8f58-4144-a4f7-a2b6454273f5/notifications/de213d1c-4438-4f24-9bc3-58c0373db527",
"summary":"test","imgUrl":null,"priority":"LOW","tags":null,"contentType":null} """
conn.send(body=message_body, destination="/queue/np.routing", headers={"persistent": "true"})
message = {
"id": "D19EEA4-9DCA-48D9-A577-5DE9D2BF374A",
"target": {
"id": "EF46ACA0-E73B-4794-8043-FFE974247CC8",
"slug": "test slug",
"name": "Test name",
"description": "",
"visibility": "RESTRICTED",
"subscriptionPolicy": "DYNAMIC",
"archive": False,
"APIKey": None,
"creationDate": "2021-02-26T12:58:42.131Z",
"lastActivityDate": "2021-02-26T12:58:42.131Z",
"incomingEmail": None,
"deleteDate": None,
},
"body": "<p>test</p>\n",
"sendAt": None,
"sentAt": "2021-08-16T10:01:33.173Z",
"users": [],
"link": "https://test-notifications.web.cern.ch/",
"summary": "test summary",
"imgUrl": None,
"priority": "important",
"tags": None,
"contentType": None,
}
message["email"] = email + "@cern.ch"
if channelid:
message["target"]["id"] = channelid
if notificationid:
message["id"] = notificationid
if category:
message["target"]["category_name"] = "Really Cool!!"
print("sending this: \n")
print(json.dumps(message, indent=4))
conn.send(body=json.dumps(message, indent=4), destination="/queue/np.routing", headers={"persistent": "true"})
conn.disconnect()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment