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

refactored lines 46 to 70 to have helper functions and simplify the code, src/messaging/rooms.js #78

Open
wants to merge 2 commits into
base: f24
Choose a base branch
from
Open
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
Binary file added dump.rdb
Binary file not shown.
59 changes: 38 additions & 21 deletions src/messaging/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,36 +41,53 @@ module.exports = function (Messaging) {
modifyRoomData(roomData, fields);
return roomData;
};
// Copilot generated code

// print statements

console.log('Safa');

function modifyRoomData(rooms, fields) {
rooms.forEach((data) => {
if (data) {
db.parseIntFields(data, intFields, fields);
data.roomName = validator.escape(String(data.roomName || ''));
data.public = parseInt(data.public, 10) === 1;
data.groupChat = data.userCount > 2;

if (!fields.length || fields.includes('notificationSetting')) {
data.notificationSetting = data.notificationSetting ||
(
data.public ?
Messaging.notificationSettings.ATMENTION :
Messaging.notificationSettings.ALLMESSAGES
);
}
if (!data) return;

db.parseIntFields(data, intFields, fields);
data.roomName = validator.escape(String(data.roomName || ''));
data.public = parseInt(data.public, 10) === 1;
data.groupChat = data.userCount > 2;

if (shouldUpdateNotificationSetting(fields)) {
data.notificationSetting = data.notificationSetting ||
(data.public ?
Messaging.notificationSettings.ATMENTION :
Messaging.notificationSettings.ALLMESSAGES
);
}

if (data.hasOwnProperty('groups') || !fields.length || fields.includes('groups')) {
try {
data.groups = JSON.parse(data.groups || '[]');
} catch (err) {
winston.error(err.stack);
data.groups = [];
}
if (shouldUpdateGroups(data, fields)) {
try {
data.groups = JSON.parse(data.groups || '[]');
} catch (err) {
winston.error(err.stack);
data.groups = [];
}
}
});
}

function shouldUpdateNotificationSetting(fields) {
return !fields.length || fields.includes('notificationSetting');
}

function shouldUpdateGroups(data, fields) {
return data.hasOwnProperty('groups') || !fields.length || fields.includes('groups');
}

// print statements
console.log('Safa');

// End of Copilot generated code

Messaging.newRoom = async (uid, data) => {
// backwards compat. remove in 4.x
if (Array.isArray(data)) { // old usage second param used to be toUids
Expand Down