-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(notifications): Add functionality to normaliser for user notific…
…ations Remove hardcoded behaviour from normaliser for user notifications while fixing type definitions to be flow acceptable, adding notification normaliser spec and finally updating the Notifications spec to run with actual normaliser
- Loading branch information
Showing
6 changed files
with
128 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 51 additions & 35 deletions
86
src/notifications/__tests__/__fixtures__/notifications.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,53 @@ | ||
export default { | ||
notifications: [ | ||
{ | ||
created_at: "2019-02-16T14:37:18.891Z", | ||
data: { | ||
display_username: "system", | ||
group_id: 1, | ||
topic_title: "Backup failed", | ||
}, | ||
fancy_title: "Backup failed", | ||
id: 1, | ||
notification_type: 7, | ||
post_number: 1, | ||
read: false, | ||
slug: "backup-failed", | ||
topic_id: 11, | ||
export const notifications = [ | ||
{ | ||
created_at: "2019-03-24T10:24:32.495Z", | ||
data: { | ||
display_username: "janedoe", | ||
original_post_id: 41, | ||
original_post_type: 1, | ||
original_username: "janedoe", | ||
revision_number: null, | ||
topic_title: "Hello, I'm Jane Doe", | ||
}, | ||
{ | ||
created_at: "2019-03-24T10:24:32.495Z", | ||
data: { | ||
display_username: "janedoe", | ||
original_post_id: 41, | ||
original_post_type: 1, | ||
original_username: "janedoe", | ||
revision_number: null, | ||
topic_title: "Hello, I'm Jane Doe", | ||
}, | ||
fancy_title: "Hello, I’m Jane Doe", | ||
id: 18, | ||
notification_type: 6, | ||
post_number: 1, | ||
read: false, | ||
slug: "hello-im-jane-doe", | ||
topic_id: 26, | ||
fancy_title: "Hello, I’m Jane Doe", | ||
id: 18, | ||
notification_type: 6, | ||
post_number: 1, | ||
read: false, | ||
slug: "hello-im-jane-doe", | ||
topic_id: 26, | ||
}, | ||
{ | ||
created_at: "2019-03-24T10:37:18.891Z", | ||
data: { | ||
display_username: "system", | ||
group_id: 1, | ||
topic_title: "Backup failed", | ||
}, | ||
], | ||
}; | ||
fancy_title: "Backup failed", | ||
id: 1, | ||
notification_type: 7, | ||
post_number: 1, | ||
read: false, | ||
slug: "backup-failed", | ||
topic_id: 11, | ||
}, | ||
{ | ||
created_at: "2019-03-24T11:24:32.495Z", | ||
data: { | ||
display_username: "janedoe", | ||
original_post_id: 42, | ||
original_post_type: 1, | ||
original_username: "janedoe", | ||
revision_number: null, | ||
topic_title: "Elephant", | ||
}, | ||
fancy_title: "Elephant", | ||
id: 19, | ||
notification_type: 5, | ||
post_number: 1, | ||
read: false, | ||
slug: "hello-im-jane-doe", | ||
topic_id: 26, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// @flow | ||
|
||
import { normaliseUserNotifications } from "../normaliser"; | ||
import { notifications } from "./__fixtures__/notifications"; | ||
|
||
describe("normaliser", () => { | ||
it("returns categorised notifications by alerts and messages", () => { | ||
const result = normaliseUserNotifications(notifications); | ||
|
||
expect(result.alerts).toEqual([notifications[1]]); | ||
expect(result.messages).toEqual([notifications[0], notifications[2]]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,47 @@ | ||
export const normaliseUserNotifications = notifications => { | ||
return notifications; | ||
// @flow | ||
|
||
/** | ||
* check discourse notification types at: https://goo.gl/Lcyhp3 | ||
* besides, keep into account that Discourse return `notification_type` | ||
* as a number which map to the below indexes | ||
*/ | ||
const notificationTypes = [ | ||
"mentioned", | ||
"replied", | ||
"quoted", | ||
"edited", | ||
"liked", | ||
"privateMessage", | ||
"invitedToPrivateMessage", | ||
"inviteeAccepted", | ||
"posted", | ||
"movedPost", | ||
"linked", | ||
"grantedBadge", | ||
"invitedToTopic", | ||
"custom", | ||
"groupMentioned", | ||
"groupMessageSummary", | ||
"watchingFirstPost", | ||
"topicReminder", | ||
]; | ||
|
||
const getMessages = notifications => { | ||
const messagesTypes = [notificationTypes[5], notificationTypes[6]]; | ||
|
||
return notifications.filter( | ||
n => messagesTypes.indexOf(notificationTypes[n.notification_type]) > -1 | ||
); | ||
}; | ||
|
||
export const normaliseUserNotifications = ( | ||
notifications: Array<Notification> | ||
): { alerts: Array<Alert>, messages: Array<Message> } => { | ||
const messages = getMessages(notifications); | ||
const alerts = notifications.filter(n => !messages.includes(n)); | ||
|
||
return { | ||
alerts, | ||
messages, | ||
}; | ||
}; |