290 lines
8.8 KiB
JavaScript
290 lines
8.8 KiB
JavaScript
import User from '../models/community/user.js';
|
|
import UserParam from '../models/community/user_param.js';
|
|
import UserRight from '../models/community/user_right.js';
|
|
import UserRightType from '../models/type/user_right.js';
|
|
import UserParamType from '../models/type/user_param.js';
|
|
|
|
const menuStructure = {
|
|
home: {
|
|
visible: ["all"],
|
|
children: {},
|
|
path: "/",
|
|
icon: "logo_mono.png"
|
|
},
|
|
friends: {
|
|
visible: ["all"],
|
|
children: {
|
|
manageFriends : {
|
|
visible: ["all"],
|
|
path: "/socialnetwork/friends",
|
|
icon: "friends24.png"
|
|
}
|
|
},
|
|
showLoggedinFriends: 1,
|
|
icon: "friends24.png"
|
|
},
|
|
socialnetwork: {
|
|
visible: ["all"],
|
|
icon: "socialnetwork.png",
|
|
children: {
|
|
guestbook: {
|
|
visible: ["all"],
|
|
path: "/socialnetwork/guestbook"
|
|
},
|
|
usersearch: {
|
|
visible: ["all"],
|
|
path: "/socialnetwork/search"
|
|
},
|
|
forum: {
|
|
visible: ["all"],
|
|
path: "/socialnetwork/forum",
|
|
showForums: 1
|
|
},
|
|
gallery: {
|
|
visible: ["all"],
|
|
path: "/socialnetwork/gallery"
|
|
},
|
|
blockedUsers: {
|
|
visible: ["all"],
|
|
path: "/socialnetwork/blocked"
|
|
},
|
|
oneTimeInvitation: {
|
|
visible: ["all"],
|
|
path: "/socialnetwork/onetimeinvitation"
|
|
},
|
|
diary: {
|
|
visible: ["all"],
|
|
path: "/socialnetwork/diary"
|
|
}
|
|
}
|
|
},
|
|
chats: {
|
|
visible: ["over12"],
|
|
icon: "chat.png",
|
|
children: {
|
|
multiChat: {
|
|
visible: ["over12"],
|
|
action: "openMultiChat"
|
|
},
|
|
randomChat: {
|
|
visible: ["over12"],
|
|
action: "openRanomChat"
|
|
}
|
|
}
|
|
},
|
|
falukant: {
|
|
visible: ["all"],
|
|
icon: "falukant16.png",
|
|
children: {
|
|
create: {
|
|
visible: ["nofalukantaccount"],
|
|
path: "/falukant/create"
|
|
},
|
|
overview: {
|
|
visible: ["hasfalukantaccount"],
|
|
path: "/falukant/home"
|
|
},
|
|
towns: {
|
|
visible: ["hasfalukantaccount"],
|
|
path: "/falukant/towns"
|
|
},
|
|
directors: {
|
|
visible: ["hasfalukantaccount"],
|
|
path: "/falukant/directors"
|
|
},
|
|
factory: {
|
|
visible: ["hasfalukantaccount"],
|
|
path: "/falukant/factory"
|
|
},
|
|
family: {
|
|
visible: ["hasfalukantaccount"],
|
|
path: "/falukant/family"
|
|
},
|
|
house: {
|
|
visible: ["hasfalukantaccount"],
|
|
path: "/falukant/house"
|
|
},
|
|
nobility: {
|
|
visible: ["hasfalukantaccount"],
|
|
path: "/falukant/nobility"
|
|
},
|
|
politics: {
|
|
visible: ["hasfalukantaccount"],
|
|
path: "/falukant/politics"
|
|
},
|
|
education: {
|
|
visible: ["hasfalukantaccount"],
|
|
path: "/falukant/education"
|
|
},
|
|
bank: {
|
|
visible: ["hasfalukantaccount"],
|
|
path: "/falukant/bank"
|
|
},
|
|
darknet: {
|
|
visible: ["hasfalukantaccount"],
|
|
path: "/falukant/darknet"
|
|
},
|
|
reputation: {
|
|
visible: ["hasfalukantaccount"],
|
|
path: "/falukant/reputation"
|
|
},
|
|
moneyhistory: {
|
|
visible: ["hasfalukantaccount"],
|
|
path: "/falukant/moneyhistory"
|
|
}
|
|
}
|
|
},
|
|
minigames: {
|
|
visible: ["all"],
|
|
icon: "minigames16.png",
|
|
},
|
|
settings: {
|
|
visible: ["all"],
|
|
icon: "settings16.png",
|
|
children: {
|
|
homepage: {
|
|
visible: ["all"],
|
|
path: "/settings/homepage"
|
|
},
|
|
account: {
|
|
visible: ["all"],
|
|
path: "/settings/account"
|
|
},
|
|
personal: {
|
|
visible: ["all"],
|
|
path: "/settings/personal"
|
|
},
|
|
view: {
|
|
visible: ["all"],
|
|
path: "/settings/view"
|
|
},
|
|
interests: {
|
|
visible: ["all"],
|
|
path: "/settings/interests"
|
|
},
|
|
sexuality: {
|
|
visible: ["over14"],
|
|
path: "/settings/sexuality"
|
|
},
|
|
notifications: {
|
|
visible: ["all"],
|
|
path: "/settings/notifications"
|
|
}
|
|
}
|
|
},
|
|
administration: {
|
|
visible: ["anyadmin"],
|
|
children: {
|
|
contactrequests: {
|
|
visible: ["mainadmin", "contactrequests"],
|
|
path: "/admin/contacts"
|
|
},
|
|
useradministration: {
|
|
visible: ["mainadmin", "useradministration"],
|
|
path: "/admin/users"
|
|
},
|
|
forum: {
|
|
visible: ["mainadmin", "forum"],
|
|
path: "/admin/forum"
|
|
},
|
|
userrights: {
|
|
visible: ["mainadmin", "rights"],
|
|
path: "/admin/rights"
|
|
},
|
|
interests: {
|
|
visible: ["mainadmin", "interests"],
|
|
path: "/admin/interests"
|
|
},
|
|
falukant: {
|
|
visible: ["mainadmin", "falukant"],
|
|
children: {
|
|
logentries: {
|
|
visible: ["mainadmin", "falukant"],
|
|
path: "/admin/falukant/logentries"
|
|
},
|
|
edituser: {
|
|
visible: ["mainadmin", "falukant"],
|
|
path: "/admin/falukant/edituser"
|
|
},
|
|
database: {
|
|
visible: ["mainadmin", "falukant"],
|
|
path: "/admin/falukant/database"
|
|
},
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
const calculateAge = (birthDate) => {
|
|
const today = new Date();
|
|
const birthDateObj = new Date(birthDate);
|
|
let age = today.getFullYear() - birthDateObj.getFullYear();
|
|
const monthDiff = today.getMonth() - birthDateObj.getMonth();
|
|
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDateObj.getDate())) {
|
|
age--;
|
|
}
|
|
|
|
return age;
|
|
};
|
|
|
|
const filterMenu = (menu, rights, age) => {
|
|
const filteredMenu = {};
|
|
for (const [key, value] of Object.entries(menu)) {
|
|
if (value.visible.includes("all")
|
|
|| value.visible.some(v => rights.includes(v)
|
|
|| (value.visible.includes("anyadmin") && rights.length > 0))
|
|
|| (value.visible.includes("over14") && age >= 14)) {
|
|
const { visible, ...itemWithoutVisible } = value;
|
|
filteredMenu[key] = { ...itemWithoutVisible };
|
|
if (value.children) {
|
|
filteredMenu[key].children = filterMenu(value.children, rights, age);
|
|
}
|
|
}
|
|
}
|
|
return filteredMenu;
|
|
};
|
|
|
|
export const menu = async (req, res) => {
|
|
try {
|
|
const { userid } = req.params;
|
|
const user = await User.findOne({ where: { hashedId: userid } });
|
|
if (!user) {
|
|
return res.status(404).json({ error: 'User not found' });
|
|
}
|
|
const userRights = await UserRight.findAll({
|
|
where: { userId: user.id },
|
|
include: [{
|
|
model: UserRightType,
|
|
as: 'rightType',
|
|
required: false
|
|
}]
|
|
});
|
|
const userBirthdateParams = await UserParam.findAll({
|
|
where: {
|
|
userId: user.id
|
|
},
|
|
include: [
|
|
{
|
|
model: UserParamType,
|
|
as: 'paramType',
|
|
where: {
|
|
description: 'birthdate'
|
|
}
|
|
}
|
|
]
|
|
});
|
|
const ageFunction = function() {
|
|
const birthDate = userBirthdateParams.length > 0 ? userBirthdateParams[0].value : (new Date()).toDateString();
|
|
const age = calculateAge(birthDate);
|
|
return age;
|
|
}
|
|
const age = ageFunction();
|
|
const rights = userRights.map(ur => ur.rightType.title);
|
|
const filteredMenu = filterMenu(menuStructure, rights, age);
|
|
res.status(200).json(filteredMenu);
|
|
} catch (error) {
|
|
console.error('Error fetching menu:', error);
|
|
res.status(500).json({ error: 'An error occurred while fetching the menu' });
|
|
}
|
|
};
|