Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions shared/notification-to-text.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import onlyContainsEmoji from 'shared/only-contains-emoji';
import sentencify from 'shared/sentencify';
import { timeDifferenceShort } from 'shared/time-difference';
import sortByDate from 'shared/sort-by-date';
import { toState, toPlainText } from 'shared/draft-utils';

Expand All @@ -16,13 +14,6 @@ const sortThreads = (entities, currentUser) => {
return threads;
};

// parse date => modifiedAt to timeAgo
const parseNotificationDate = date => {
const now = new Date().getTime();
const timestamp = new Date(date).getTime();
return timeDifferenceShort(now, timestamp);
};

export const parseActors = (actors, currentUser) => {
const filteredActors = actors
.filter(actor => actor.id !== currentUser.id)
Expand All @@ -31,12 +22,13 @@ export const parseActors = (actors, currentUser) => {
};

const EVENT_VERB = {
MESSAGE_CREATED: 'replied',
MESSAGE_CREATED: 'replied in',
REACTION_CREATED: 'liked',
CHANNEL_CREATED: 'created in',
USER_JOINED_COMMUNITY: 'joined',
MENTION_MESSAGE: 'mentioned you in',
MENTION_THREAD: 'mentioned you in',
THREAD_REACTION_CREATED: 'liked',
};

const contextToString = (context, currentUser) => {
Expand All @@ -45,18 +37,23 @@ const contextToString = (context, currentUser) => {
case 'THREAD': {
const payload = context.payload;
const isCreator = payload.creatorId === currentUser.id;
const str = isCreator ? 'in your thread' : 'in';
const str = isCreator ? 'your thread' : '';
return `${str} ${payload.content.title}`;
}
case 'DIRECT_MESSAGE_THREAD': {
return 'in a direct message thread';
}
case 'THREAD_REACTION': {
return 'your thread';
}
case 'MESSAGE':
return 'your reply';
case 'COMMUNITY':
return context.payload.name;
case 'CHANNEL':
return context.payload.name;
default:
return;
}
};

Expand Down Expand Up @@ -84,16 +81,13 @@ const parseNotification = notification => {

const formatNotification = (incomingNotification, currentUserId) => {
const notification = parseNotification(incomingNotification);

const actors =
notification.actors &&
parseActors(notification.actors, { id: currentUserId });
const event = notification.event && EVENT_VERB[notification.event];
const context =
notification.context &&
contextToString(notification.context, { id: currentUserId });
const date =
notification.modifiedAt && parseNotificationDate(notification.modifiedAt);

let title = `${actors} ${event} ${context}`;
let href, body;
Expand Down Expand Up @@ -171,6 +165,16 @@ const formatNotification = (incomingNotification, currentUserId) => {
: message.content.body;
break;
}
case 'THREAD_REACTION_CREATED': {
const thread = notification.context.payload;

href = `/thread/${thread.id}`;
body =
thread.type === 'draftjs'
? toPlainText(toState(thread.content.body))
: thread.content.body;
break;
}
case 'CHANNEL_CREATED': {
const entities = notification.entities;
const newChannelCount =
Expand All @@ -187,6 +191,7 @@ const formatNotification = (incomingNotification, currentUserId) => {
title = `${actors} ${event} ${context}`;
break;
}

case 'MENTION_THREAD': {
// sort and order the threads
const threads = sortThreads(notification.entities, { id: currentUserId });
Expand Down Expand Up @@ -221,6 +226,8 @@ const formatNotification = (incomingNotification, currentUserId) => {
title = `${actors} invited you to join their community, ${context}`;
break;
}
default:
return;
}

const data = href && {
Expand Down
14 changes: 12 additions & 2 deletions shared/test/__snapshots__/notification-to-text.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Object {
"data": Object {
"href": "/thread/2182d960-5970-46e0-801f-ab0b8e02cfbc",
},
"title": "Max Stoiber mentioned you in in Show us your Dangerfile",
"title": "Max Stoiber mentioned you in Show us your Dangerfile",
}
`;

Expand Down Expand Up @@ -73,7 +73,17 @@ Object {
"data": Object {
"href": "/thread/f40e1d78-10e4-484a-b07d-7e7c88afd167",
},
"title": "Max Stoiber mentioned you in in your thread Testing",
"title": "Max Stoiber mentioned you in your thread Testing",
}
`;

exports[`should format a thread reaction notification 1`] = `
Object {
"body": "{\\"nodes\\":[{\\"kind\\":\\"block\\",\\"type\\":\\"line\\",\\"nodes\\":[{\\"kind\\":\\"text\\",\\"text\\":\\"\\"}]}]}",
"data": Object {
"href": "/thread/ef021b3b-c520-4133-8749-e96252eff5fc",
},
"title": "Max Stoiber liked your thread New thread by @mxstbr",
}
`;

Expand Down
28 changes: 28 additions & 0 deletions shared/test/fixtures/THREAD_REACTION_CREATED.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"actors": [
{
"id": "41f5151f-7309-4c95-b2af-d45ea2b0ad82",
"payload": "{\"createdAt\":\"2017-07-09T21:47:15.354Z\",\"email\":\"vutpaisawesome@gmail.com\",\"fbProviderId\":null,\"githubProviderId\":null,\"googleProviderId\":null,\"id\":\"41f5151f-7309-4c95-b2af-d45ea2b0ad82\",\"isOnline\":true,\"lastSeen\":\"2017-07-12T04:31:27.939Z\",\"name\":\"Max Stoiber\",\"profilePhoto\":\"https://pbs.twimg.com/profile_images/802074607222132736/G_LVLtD7_normal.jpg\",\"providerId\":\"3195884567\",\"timezone\":-420,\"username\":\"mstoiber05\"}",
"type": "USER"
}
],
"context": {
"id": "ef021b3b-c520-4133-8749-e96252eff5fc",
"payload": "{\"attachments\":[],\"channelId\":\"0961e72c-087f-4e80-a5d6-3714022094c2\",\"communityId\":\"a719409a-3964-49f6-a570-edfe9db641a9\",\"content\":{\"body\":\"{\\\"nodes\\\":[{\\\"kind\\\":\\\"block\\\",\\\"type\\\":\\\"line\\\",\\\"nodes\\\":[{\\\"kind\\\":\\\"text\\\",\\\"text\\\":\\\"\\\"}]}]}\",\"title\":\"New thread by @mxstbr\"},\"createdAt\":\"2017-07-12T21:43:28.409Z\",\"creatorId\":\"gVk5mYwccUOEKiN5vtOouqroGKo1\",\"edits\":[],\"id\":\"ef021b3b-c520-4133-8749-e96252eff5fc\",\"isLocked\":false,\"isPublished\":true,\"lastActive\":\"2017-07-12T21:43:50.279Z\",\"modifiedAt\":null,\"type\":\"SLATE\"}",
"type": "THREAD"
},
"createdAt": "2017-07-12T21:43:50.399Z",
"entities": [
{
"id": "343d0e33-8010-4d45-b1d7-bb7d14cd5ed5",
"payload": "{\"createdAt\":1529543590050,\"deletedAt\":\"2018-06-21T01:15:49.678Z\",\"id\":\"343d0e33-8010-4d45-b1d7-bb7d14cd5ed5\",\"threadId\":\"5e333eab-6a46-4a9d-b270-0d3cad8114d0\",\"type\":\"like\",\"userId\":\"01p2A7kDCWUjGj6zQLlMQUOSQL42\"}",
"type": "THREAD_REACTION"
}
],
"event": "THREAD_REACTION_CREATED",
"id": "eb3464c9-e533-43ba-a913-924429bfe7a2",
"isRead": false,
"isSeen": false,
"modifiedAt": "2017-07-12T21:43:50.399Z",
"userId": "gVk5mYwccUOEKiN5vtOouqroGKo1"
}
8 changes: 8 additions & 0 deletions shared/test/notification-to-text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import THREAD_CREATED_FIXTURE from './fixtures/THREAD_CREATED.json';
import USER_JOINED_COMMUNITY_FIXTURE from './fixtures/USER_JOINED_COMMUNITY.json';
import MENTION_MESSAGE_FIXTURE from './fixtures/MENTION_MESSAGE';
import MENTION_THREAD_FIXTURE from './fixtures/MENTION_THREAD';
import THREAD_REACTION_FIXTURE from './fixtures/THREAD_REACTION_CREATED.json';

const USER_ID = 'gVk5mYwccUOEKiN5vtOouqroGKo1';

Expand Down Expand Up @@ -74,3 +75,10 @@ it('should format a thread mention notification', () => {
delete result.raw; // Don't need raw data
expect(result).toMatchSnapshot();
});

it('should format a thread reaction notification', () => {
let result = formatNotification(THREAD_REACTION_FIXTURE, USER_ID);
expect(result.raw).toBeDefined();
delete result.raw; // Don't need raw data
expect(result).toMatchSnapshot();
});