Fixed websockets in navigation

This commit is contained in:
Torsten Schulz
2024-12-04 22:51:04 +01:00
parent 069c97fa90
commit 8c15fb7f2b
9 changed files with 61 additions and 34 deletions

View File

@@ -3,6 +3,7 @@ import UserParam from '../models/community/user_param.js';
import UserRight from '../models/community/user_right.js'; import UserRight from '../models/community/user_right.js';
import UserRightType from '../models/type/user_right.js'; import UserRightType from '../models/type/user_right.js';
import UserParamType from '../models/type/user_param.js'; import UserParamType from '../models/type/user_param.js';
import FalukantUser from '../models/falukant/data/user.js';
const menuStructure = { const menuStructure = {
home: { home: {
@@ -236,17 +237,20 @@ class NavigationController {
return age; return age;
} }
filterMenu(menu, rights, age) { async filterMenu(menu, rights, age, userId) {
console.log(userId);
const filteredMenu = {}; const filteredMenu = {};
const hasFalukantAccount = await this.hasFalukantAccount(userId);
for (const [key, value] of Object.entries(menu)) { for (const [key, value] of Object.entries(menu)) {
if (value.visible.includes("all") if (value.visible.includes("all")
|| value.visible.some(v => rights.includes(v) || value.visible.some(v => rights.includes(v) || (value.visible.includes("anyadmin") && rights.length > 0))
|| (value.visible.includes("anyadmin") && rights.length > 0)) || (value.visible.includes("over14") && age >= 14)
|| (value.visible.includes("over14") && age >= 14)) { || (value.visible.includes('nofalukantaccount') && !hasFalukantAccount)
|| (value.visible.includes('hasfalukantaccount') && hasFalukantAccount)) {
const { visible, ...itemWithoutVisible } = value; const { visible, ...itemWithoutVisible } = value;
filteredMenu[key] = { ...itemWithoutVisible }; filteredMenu[key] = { ...itemWithoutVisible };
if (value.children) { if (value.children) {
filteredMenu[key].children = this.filterMenu(value.children, rights, age); filteredMenu[key].children = await this.filterMenu(value.children, rights, age, userId);
} }
} }
} }
@@ -281,13 +285,18 @@ class NavigationController {
const birthDate = userBirthdateParams.length > 0 ? userBirthdateParams[0].value : (new Date()).toDateString(); const birthDate = userBirthdateParams.length > 0 ? userBirthdateParams[0].value : (new Date()).toDateString();
const age = this.calculateAge(birthDate); const age = this.calculateAge(birthDate);
const rights = userRights.map(ur => ur.rightType.title); const rights = userRights.map(ur => ur.rightType.title);
const filteredMenu = this.filterMenu(menuStructure, rights, age); const filteredMenu = await this.filterMenu(menuStructure, rights, age, user.id);
res.status(200).json(filteredMenu); res.status(200).json(filteredMenu);
} catch (error) { } catch (error) {
console.error('Error fetching menu:', error); console.error('Error fetching menu:', error);
res.status(500).json({ error: 'An error occurred while fetching the menu' }); 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; export default NavigationController;

View File

@@ -65,8 +65,6 @@ export default {
MessageDialog, MessageDialog,
}, },
created() { created() {
this.$store.dispatch('loadLoginState');
this.$store.dispatch('loadMenu');
this.$i18n.locale = this.$store.getters.language; this.$i18n.locale = this.$store.getters.language;
}, },
}; };

View File

@@ -54,15 +54,24 @@ export default {
} }
}, },
computed: { computed: {
...mapGetters(['menu', 'user', 'menuNeedsUpdate']), ...mapGetters(['menu', 'user', 'menuNeedsUpdate', 'socket']),
}, },
watch: { watch: {
menuNeedsUpdate(newValue) { menuNeedsUpdate(newValue) {
if (newValue) { if (newValue) {
console.log('Menu needs update, loading menu...');
this.loadMenu(); this.loadMenu();
} }
}, },
socket(newValue) {
if (newValue) {
newValue.on('forumschanged', (data) => {
this.fetchForums();
});
newValue.on('friendloginchanged', () => {
this.fetchFriends();
});
}
}
}, },
created() { created() {
if (this.user && this.user.id) { if (this.user && this.user.id) {
@@ -75,10 +84,12 @@ export default {
}, },
mounted() { mounted() {
if (this.$store.getters.socket) { if (this.$store.getters.socket) {
console.log('connect sockets in navigation')
this.$store.getters.socket.on('forumschanged', (data) => { this.$store.getters.socket.on('forumschanged', (data) => {
this.fetchForums(); this.fetchForums();
}); });
this.$store.getters.socket.on('friendloginchanged', () => { this.$store.getters.socket.on('friendloginchanged', () => {
console.log('update friends');
this.fetchFriends(); this.fetchFriends();
}); });
} }

View File

@@ -0,0 +1,5 @@
{
"falukant": {
}
}

View File

@@ -44,6 +44,18 @@
"manageFriends": "Freunde verwalten", "manageFriends": "Freunde verwalten",
"chat": "Chatten", "chat": "Chatten",
"profile": "Profil" "profile": "Profil"
},
"m-falukant": {
"create": "Erstellen",
"overview": "Übersicht",
"towns": "Niederlassungen",
"directors": "Direktoren",
"factory": "Produktion",
"family": "Familie",
"house": "Haus",
"darknet": "Untergrund",
"reputation": "Reputation",
"moneyhistory": "Geldfluss"
} }
} }
} }

View File

@@ -0,0 +1,5 @@
{
"falukant": {
}
}

View File

@@ -31,6 +31,4 @@ app.use(router);
app.use(i18n); app.use(i18n);
app.use(vuetify); app.use(vuetify);
store.dispatch('loadLoginState');
app.mount('#app'); app.mount('#app');

View File

@@ -50,19 +50,13 @@ const store = createStore({
}, },
actions: { actions: {
async login({ commit, dispatch }, user) { async login({ commit, dispatch }, user) {
console.log('login', user);
await commit('dologin', user); await commit('dologin', user);
await dispatch('initializeSocket'); await dispatch('initializeSocket');
const socket = this.getters.socket; const socket = this.getters.socket;
if (socket) { if (socket) {
console.log('Emitting setUserId:', user.id);
socket.emit('setUserId', user.id); socket.emit('setUserId', user.id);
console.log('setUserId emitted successfully');
} else {
console.error('Socket not initialized');
} }
await dispatch('loadMenu'); await dispatch('loadMenu');
dispatch('startMenuReload');
}, },
logout({ commit, state }) { logout({ commit, state }) {
if (state.socket) { if (state.socket) {
@@ -74,23 +68,10 @@ const store = createStore({
}, },
initializeSocket({ commit, state }) { initializeSocket({ commit, state }) {
if (state.isLoggedIn && state.user) { if (state.isLoggedIn && state.user) {
const socket = io(import.meta.env.VITE_API_BASE_URL); // oder Ihre URL const socket = io(import.meta.env.VITE_API_BASE_URL);
console.log('Socket initialized:', socket);
socket.on('connect', () => {
console.log('Socket connected:', socket.id);
});
socket.on('disconnect', (reason) => {
console.log('Socket disconnected:', reason);
});
commit('setSocket', socket); commit('setSocket', socket);
} }
}, },
loadLoginState({ commit }) {
commit('loadLoginState');
},
setLanguage({ commit }, language) { setLanguage({ commit }, language) {
commit('setLanguage', language); commit('setLanguage', language);
}, },
@@ -110,6 +91,7 @@ const store = createStore({
language: state => state.language, language: state => state.language,
menu: state => state.menu, menu: state => state.menu,
socket: state => state.socket, socket: state => state.socket,
menuNeedsUpdate: state => state.menuNeedsUpdate
}, },
modules: { modules: {
dialogs, dialogs,

View File

@@ -13,12 +13,16 @@
<div> <div>
<input v-model="username" size="20" type="text" <input v-model="username" size="20" type="text"
:placeholder="$t('home.nologin.login.name')" :placeholder="$t('home.nologin.login.name')"
:title="$t('home.nologin.login.namedescription')"> :title="$t('home.nologin.login.namedescription')"
@keydown.enter="focusPassword">
</div> </div>
<div> <div>
<input v-model="password" size="20" type="password" <input v-model="password" size="20" type="password"
:placeholder="$t('home.nologin.login.password')" :placeholder="$t('home.nologin.login.password')"
:title="$t('home.nologin.login.passworddescription')"> :title="$t('home.nologin.login.passworddescription')"
@keydown.enter="doLogin"
ref="passwordInput"
>
</div> </div>
<div> <div>
<label><input type="checkbox"><span>Eingeloggt bleiben</span></label> <label><input type="checkbox"><span>Eingeloggt bleiben</span></label>
@@ -72,6 +76,9 @@ export default {
openPasswordResetDialog() { openPasswordResetDialog() {
this.$refs.passwordResetDialog.open(); this.$refs.passwordResetDialog.open();
}, },
focusPassword() {
this.$refs.passwordInput.focus();
},
async doLogin() { async doLogin() {
try { try {
const response = await apiClient.post('/api/auth/login', { username: this.username, password: this.password }); const response = await apiClient.post('/api/auth/login', { username: this.username, password: this.password });