From eaf1b0911d560af27e4833616d94e2ecec7533ec Mon Sep 17 00:00:00 2001 From: Mateusz Russak Date: Sat, 23 Mar 2024 17:41:55 +0100 Subject: [PATCH] feat: redirect to main channel when channel can't be found --- packages/app/src/js/services/init.js | 12 +++++++++--- packages/app/src/js/store/modules/channels.js | 1 + packages/app/src/js/store/modules/progress.js | 18 +++++++++++------- .../server/src/app/actions/message/create.js | 2 +- packages/server/src/app/actions/message/pin.js | 2 +- .../src/app/actions/readReceipt/getChannel.js | 2 +- packages/server/src/app/commands/join.js | 2 +- packages/server/src/app/common/channel.js | 1 + 8 files changed, 26 insertions(+), 14 deletions(-) diff --git a/packages/app/src/js/services/init.js b/packages/app/src/js/services/init.js index d4703e96..ee16e4e2 100644 --- a/packages/app/src/js/services/init.js +++ b/packages/app/src/js/services/init.js @@ -11,12 +11,18 @@ const initApp = () => async (dispatch, getState) => { const config = await methods.config.load(); actions.stream.setMain(config.mainChannelId); await initNotifications(config); - methods.users.load(); - methods.channels.load(); + await methods.users.load(); + await methods.channels.load(); await methods.emojis.load(); await methods.progress.loadBadges(); - if (!getState().stream.main.channelId) { + const channelId = getState().stream.main.channelId; + if (!channelId) { actions.stream.open({id: 'main', value: {type: 'live'}}); + }else if( !getState().channels[channelId] ){ + const c = await methods.channels.find(channelId); + if(!c || c.length === 0) { + actions.stream.open({id: 'main', value: {type: 'live'}}); + } } }; diff --git a/packages/app/src/js/store/modules/channels.js b/packages/app/src/js/store/modules/channels.js index 2900d1a6..ced755e1 100644 --- a/packages/app/src/js/store/modules/channels.js +++ b/packages/app/src/js/store/modules/channels.js @@ -32,6 +32,7 @@ export default createModule({ find: (id) => async ({actions}, getState, {client}) => { const res = await client.req({ type: 'channel:get', id }); actions.channels.add(res.data); + return res.data; }, }, }); diff --git a/packages/app/src/js/store/modules/progress.js b/packages/app/src/js/store/modules/progress.js index 9ea8acf2..e6f20a9f 100644 --- a/packages/app/src/js/store/modules/progress.js +++ b/packages/app/src/js/store/modules/progress.js @@ -27,13 +27,17 @@ export default createModule({ }, loadProgress: (stream) => async ({actions}, getState, {client}) => { - if (!stream.channelId) return; - const { data } = await client.req({ - type: 'readReceipt:getChannel', - channelId: stream.channelId, - parentId: stream.parentId, - }); - actions.progress.add(data); + try{ + if (!stream.channelId) return; + const { data } = await client.req({ + type: 'readReceipt:getChannel', + channelId: stream.channelId, + parentId: stream.parentId, + }); + actions.progress.add(data); + }catch(err){ + console.log(err) + } }, update: (messageId) => async ({actions}, getState, {client}) => { diff --git a/packages/server/src/app/actions/message/create.js b/packages/server/src/app/actions/message/create.js index ba3e9a74..7dff0405 100644 --- a/packages/server/src/app/actions/message/create.js +++ b/packages/server/src/app/actions/message/create.js @@ -29,7 +29,7 @@ module.exports = { handler: async (req, res) => { const msg = req.body; const channel = await repo.channel.get({ id: msg.channelId }); - if (!await channelHelper.haveAccess(req.userId, channel.id)) { + if (!await channelHelper.haveAccess(req.userId, channel?.id)) { throw AccessDenied(); } diff --git a/packages/server/src/app/actions/message/pin.js b/packages/server/src/app/actions/message/pin.js index 56b7431e..c936b649 100644 --- a/packages/server/src/app/actions/message/pin.js +++ b/packages/server/src/app/actions/message/pin.js @@ -19,7 +19,7 @@ module.exports = { const message = await repo.message.get({ id }); if (!message) throw MessageNotExist(); // TODO: test if permissions work in tests - if (!await ChannelHelper.haveAccess(req.userId, message.channelId)) { + if (!await ChannelHelper.haveAccess(req.userId, message?.channelId)) { throw AccessDenied(); } diff --git a/packages/server/src/app/actions/readReceipt/getChannel.js b/packages/server/src/app/actions/readReceipt/getChannel.js index 04739904..28e9bf3f 100644 --- a/packages/server/src/app/actions/readReceipt/getChannel.js +++ b/packages/server/src/app/actions/readReceipt/getChannel.js @@ -17,7 +17,7 @@ module.exports = { if (!msg.channelId) throw MissingChannel(); const channel = await repo.channel.get({ id: msg.channelId }); - if (!await ChannelHelper.haveAccess(req.userId, channel.id)) { + if (!await ChannelHelper.haveAccess(req.userId, channel?.id)) { throw AccessDenied(); } const badges = await repo.badge.getAll({ channelId: channel.id, parentId: msg.parentId }); diff --git a/packages/server/src/app/commands/join.js b/packages/server/src/app/commands/join.js index 208b5a6f..19de4ff6 100644 --- a/packages/server/src/app/commands/join.js +++ b/packages/server/src/app/commands/join.js @@ -11,7 +11,7 @@ module.exports = { const { channelId } = req.body.context; const channel = await repo.channel.get({ id: channelId }); - if (!await channelHelper.haveAccess(req.userId, channel.id)) { + if (!await channelHelper.haveAccess(req.userId, channel?.id)) { throw AccessDenied(); } if (!channel) throw ChannelNotExist(); diff --git a/packages/server/src/app/common/channel.js b/packages/server/src/app/common/channel.js index b3ae2a7b..3c9df8ec 100644 --- a/packages/server/src/app/common/channel.js +++ b/packages/server/src/app/common/channel.js @@ -2,6 +2,7 @@ const repo = require('../../infra/repositories'); module.exports = { haveAccess: async (userId, id) => { + if (!id) return false; const channel = await repo.channel.get({ id }); if (channel?.private