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 src/notifications.js to reduce cognitive complexity from 16 to 15 #578

Open
wants to merge 1 commit into
base: f24
Choose a base branch
from
Open
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
22 changes: 13 additions & 9 deletions src/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,7 @@ Notifications.getMultiple = async function (nids) {
notification.bodyLong = utils.stripHTMLTags(notification.bodyLong, ['img', 'p', 'a']);
}

notification.user = usersData[index];
if (notification.user && notification.from) {
notification.image = notification.user.picture || null;
if (notification.user.username === '[[global:guest]]') {
notification.bodyShort = notification.bodyShort.replace(/([\s\S]*?),[\s\S]*?,([\s\S]*?)/, '$1, [[global:guest]], $2');
}
} else if (notification.image === 'brand:logo' || !notification.image) {
notification.image = meta.config['brand:logo'] || `${nconf.get('relative_path')}/logo.png`;
}
setNotificationImage(notification, index, usersData);
}
});
return notifications;
Expand Down Expand Up @@ -182,6 +174,18 @@ Notifications.push = async function (notification, uids) {
}, 500);
};

function setNotificationImage(notification, index, usersData) {
notification.user = usersData[index];
if (notification.user && notification.from) {
notification.image = notification.user.picture || null;
if (notification.user.username === '[[global:guest]]') {
notification.bodyShort = notification.bodyShort.replace(/([\s\S]*?),[\s\S]*?,([\s\S]*?)/, '$1, [[global:guest]], $2');
}
} else if (notification.image === 'brand:logo' || !notification.image) {
notification.image = meta.config['brand:logo'] || `${nconf.get('relative_path')}/logo.png`;
}
}

async function pushToUids(uids, notification) {
async function sendNotification(uids) {
if (!uids.length) {
Expand Down