306 lines
9.7 KiB
JavaScript
306 lines
9.7 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';
|
|
import FalukantUser from '../models/falukant/data/user.js';
|
|
|
|
const menuStructure = {
|
|
home: {
|
|
visible: ["all"],
|
|
children: {},
|
|
path: "/",
|
|
icon: "logo_mono.png"
|
|
},
|
|
friends: {
|
|
visible: ["all"],
|
|
children: {
|
|
manageFriends: {
|
|
visible: ["all"],
|
|
path: "/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/branch"
|
|
},
|
|
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"
|
|
},
|
|
health: {
|
|
visible: ["hasfalukantaccount"],
|
|
path: "/falukant/health"
|
|
},
|
|
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"
|
|
},
|
|
flirt: {
|
|
visible: ["over14"],
|
|
path: "/settings/flirt"
|
|
},
|
|
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"
|
|
},
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
class NavigationController {
|
|
constructor() {
|
|
this.menu = this.menu.bind(this);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
async filterMenu(menu, rights, age, userId) {
|
|
const filteredMenu = {};
|
|
const hasFalukantAccount = await this.hasFalukantAccount(userId);
|
|
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)
|
|
|| (value.visible.includes('nofalukantaccount') && !hasFalukantAccount)
|
|
|| (value.visible.includes('hasfalukantaccount') && hasFalukantAccount)) {
|
|
const { visible, ...itemWithoutVisible } = value;
|
|
filteredMenu[key] = { ...itemWithoutVisible };
|
|
if (value.children) {
|
|
filteredMenu[key].children = await this.filterMenu(value.children, rights, age, userId);
|
|
}
|
|
}
|
|
}
|
|
return filteredMenu;
|
|
}
|
|
|
|
async menu(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 birthDate = userBirthdateParams.length > 0 ? userBirthdateParams[0].value : (new Date()).toDateString();
|
|
const age = this.calculateAge(birthDate);
|
|
const rights = userRights.map(ur => ur.rightType.title);
|
|
const filteredMenu = await this.filterMenu(menuStructure, rights, age, user.id);
|
|
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' });
|
|
}
|
|
}
|
|
|
|
async hasFalukantAccount(userId) {
|
|
const falukantUser = await FalukantUser.findOne({ where: { userId: userId } });
|
|
return falukantUser !== null;
|
|
}
|
|
}
|
|
|
|
export default NavigationController;
|