From 7974f1f0770cdc43b8300068b45638176de5b4ef Mon Sep 17 00:00:00 2001 From: seif khelifi Date: Thu, 5 Sep 2024 16:08:17 +0300 Subject: [PATCH] Fixed-Linter-Errors --- src/groups/create.js | 86 +++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 54 deletions(-) diff --git a/src/groups/create.js b/src/groups/create.js index b8a8764b87..4bc5b1700a 100644 --- a/src/groups/create.js +++ b/src/groups/create.js @@ -7,82 +7,48 @@ const db = require('../database'); module.exports = function (Groups) { Groups.create = async function (data) { - // Determine basic properties - const timestamp = data.timestamp || Date.now(); const isSystem = isSystemGroup(data); + const timestamp = getTimestamp(data); const disableJoinRequests = getDisableJoinRequests(data); const disableLeave = parseInt(data.disableLeave, 10) === 1 ? 1 : 0; const isHidden = parseInt(data.hidden, 10) === 1; - const isPrivate = data.private ? parseInt(data.private, 10) === 1 : true; - const memberCount = data.ownerUid ? 1 : 0; - - // Validate group name Groups.validateGroupName(data.name); - - // Check for existing groups - await checkGroupExistence(data.name); - - const a = disableLeave; - const b = memberCount; - const c = disableJoinRequests; - const d = isPrivate; - const e = isHidden; - - // Create group data - const groupData = createGroupData(data, timestamp, isSystem, e, d, c, a, b); - // Fire hooks and save group data - await plugins.hooks.fire('filter:group.create', { group: groupData, data }); - await saveGroupData(groupData, data.ownerUid, timestamp); - - // Return updated group data - const updatedGroupData = await Groups.getGroupData(groupData.name); - plugins.hooks.fire('action:group.create', { group: updatedGroupData }); - return updatedGroupData; - }; - - function getDisableJoinRequests(data) { - if (data.name === 'administrators') return 1; - return parseInt(data.disableJoinRequests, 10) === 1 ? 1 : 0; - } - - async function checkGroupExistence(name) { const [exists, privGroupExists] = await Promise.all([ - meta.userOrGroupExists(name), - privilegeGroupExists(name), + meta.userOrGroupExists(data.name), + privilegeGroupExists(data.name), ]); if (exists || privGroupExists) { throw new Error('[[error:group-already-exists]]'); } - } - // Function to create group data - function createGroupData(data, timestamp, isSystem, isHidden, isPrivate, - disableJoinRequests, disableLeave, memberCount) { - return { + + const memberCount = data.hasOwnProperty('ownerUid') ? 1 : 0; + const isPrivate = data.hasOwnProperty('private') && data.private !== undefined ? parseInt(data.private, 10) === 1 : true; + let groupData = { name: data.name, slug: slugify(data.name), createtime: timestamp, userTitle: data.userTitle || data.name, userTitleEnabled: parseInt(data.userTitleEnabled, 10) === 1 ? 1 : 0, description: data.description || '', - memberCount, + memberCount: memberCount, hidden: isHidden ? 1 : 0, system: isSystem ? 1 : 0, private: isPrivate ? 1 : 0, - disableJoinRequests, - disableLeave, + disableJoinRequests: disableJoinRequests, + disableLeave: disableLeave, }; - } - async function saveGroupData(groupData, ownerUid, timestamp) { + await plugins.hooks.fire('filter:group.create', { group: groupData, data: data }); + await db.sortedSetAdd('groups:createtime', groupData.createtime, groupData.name); await db.setObject(`group:${groupData.name}`, groupData); - if (ownerUid) { - await db.setAdd(`group:${groupData.name}:owners`, ownerUid); - await db.sortedSetAdd(`group:${groupData.name}:members`, timestamp, ownerUid); + if (data.hasOwnProperty('ownerUid')) { + await db.setAdd(`group:${groupData.name}:owners`, data.ownerUid); + await db.sortedSetAdd(`group:${groupData.name}:members`, timestamp, data.ownerUid); } - if (!groupData.hidden && !groupData.system) { + if (!isHidden && !isSystem) { await db.sortedSetAddBulk([ ['groups:visible:createtime', timestamp, groupData.name], ['groups:visible:memberCount', groupData.memberCount, groupData.name], @@ -93,18 +59,30 @@ module.exports = function (Groups) { if (!Groups.isPrivilegeGroup(groupData.name)) { await db.setObjectField('groupslug:groupname', groupData.slug, groupData.name); } - } - async function privilegeGroupExists(name) { - return Groups.isPrivilegeGroup(name) && await db.isSortedSetMember('groups:createtime', name); + groupData = await Groups.getGroupData(groupData.name); + plugins.hooks.fire('action:group.create', { group: groupData }); + return groupData; + }; + function getTimestamp(data) { + return data.timestamp || Date.now(); + } + function getDisableJoinRequests(data) { + if (data.name === 'administrators') { + return 1; + } + return parseInt(data.disableJoinRequests, 10) === 1 ? 1 : 0; } - function isSystemGroup(data) { return data.system === true || parseInt(data.system, 10) === 1 || Groups.systemGroups.includes(data.name) || Groups.isPrivilegeGroup(data.name); } + async function privilegeGroupExists(name) { + return Groups.isPrivilegeGroup(name) && await db.isSortedSetMember('groups:createtime', name); + } + Groups.validateGroupName = function (name) { if (!name) { throw new Error('[[error:group-name-too-short]]');