Skip to content

Commit

Permalink
Fixed-Linter-Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
seif khelifi committed Sep 5, 2024
1 parent cb536a5 commit 7974f1f
Showing 1 changed file with 32 additions and 54 deletions.
86 changes: 32 additions & 54 deletions src/groups/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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]]');
Expand Down

0 comments on commit 7974f1f

Please sign in to comment.