Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Notification Handling to Reduce Cognitive Complexity #182

Closed
wants to merge 3 commits into from
Closed
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
42 changes: 22 additions & 20 deletions src/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,38 +429,39 @@ Notifications.merge = async function (notifications) {
}, []);

differentiators.forEach((differentiator) => {
console.log('Pratick: Processing differentiator', differentiator);
function typeFromLength(items) {
if (items.length === 2) {
return 'dual';
} else if (items.length === 3) {
return 'triple';
}
return 'multiple';
}
let set;
if (differentiator === 0 && differentiators.length === 1) {
set = isolated;
} else {
set = isolated.filter(n => n.mergeId === (`${mergeId}|${differentiator}`));
const lengthTypes = {
2: 'dual',
3: 'triple'
};
return lengthTypes[items.length] || 'multiple';
}

const set = (differentiator === 0 && differentiators.length === 1) ?
isolated :
isolated.filter(n => n.mergeId === (`${mergeId}|${differentiator}`));

const modifyIndex = notifications.indexOf(set[0]);
if (modifyIndex === -1 || set.length === 1) {
return notifications;
return notifications;
}

const notifObj = notifications[modifyIndex];
switch (mergeId) {
case 'new-chat': {
const { roomId, roomName, type, user } = set[0];
const isGroupChat = type === 'new-group-chat';
notifObj.bodyShort = isGroupChat || (roomName !== `[[modules:chat.room-id, ${roomId}]]`) ?
`[[notifications:new-messages-in, ${set.length}, ${roomName}]]` :
`[[notifications:new-messages-from, ${set.length}, ${user.displayname}]]`;
const isGroupChat = (type === 'new-group-chat') || (roomName !== `[[modules:chat.room-id, ${roomId}]]`);

notifObj.bodyShort = isGroupChat
? `[[notifications:new-messages-in, ${set.length}, ${roomName}]]`
: `[[notifications:new-messages-from, ${set.length}, ${user.displayname}]]`;

break;
}

case 'notifications:user-posted-in-public-room': {
const usernames = _.uniq(set.map(notifObj => notifObj && notifObj.user && notifObj.user.displayname));
const usernames = _.uniq(set.map(n => n?.user?.displayname).filter(Boolean));
if (usernames.length === 2 || usernames.length === 3) {
notifObj.bodyShort = `[[${mergeId}-${typeFromLength(usernames)}, ${usernames.join(', ')}, ${notifObj.roomIcon}, ${notifObj.roomName}]]`;
} else if (usernames.length > 3) {
Expand All @@ -479,8 +480,9 @@ Notifications.merge = async function (notifications) {
const numUsers = usernames.length;

const title = utils.decodeHTMLEntities(notifications[modifyIndex].topicTitle || '');
let titleEscaped = title.replace(/%/g, '%').replace(/,/g, ',');
titleEscaped = titleEscaped ? (`, ${titleEscaped}`) : '';
const titleEscaped = title ?
`, ${title.replace(/%/g, '%').replace(/,/g, ',')}` :
'';

if (numUsers === 2 || numUsers === 3) {
notifications[modifyIndex].bodyShort = `[[${mergeId}-${typeFromLength(usernames)}, ${usernames.join(', ')}${titleEscaped}]]`;
Expand Down
Loading