diff --git a/apps/meteor/app/ui-utils/client/lib/RoomManager.ts b/apps/meteor/app/ui-utils/client/lib/RoomManager.ts index c47cc9e55e65..85f281f9440f 100644 --- a/apps/meteor/app/ui-utils/client/lib/RoomManager.ts +++ b/apps/meteor/app/ui-utils/client/lib/RoomManager.ts @@ -60,7 +60,7 @@ const openedRooms: Record< template?: Blaze.View; streamActive?: boolean; unreadSince: ReactiveVar; - lastSeen?: Date; + lastSeen: Date; unreadFirstId?: string; } > = {}; @@ -105,9 +105,8 @@ function closeOlderRooms() { if (Object.keys(openedRooms).length <= maxRoomsOpen) { return; } - const roomsToClose = Object.values(openedRooms) - .sort((a, b) => (a.lastSeen?.getTime() ?? 0) - (b.lastSeen?.getTime() ?? 0)) + .sort((a, b) => b.lastSeen.getTime() - a.lastSeen.getTime()) .slice(maxRoomsOpen); return Array.from(roomsToClose).map((roomToClose) => close(roomToClose.typeName)); } @@ -225,6 +224,7 @@ function open({ typeName, rid }: { typeName: string; rid: IRoom['_id'] }) { active: false, ready: false, unreadSince: new ReactiveVar(undefined), + lastSeen: new Date(), }; } diff --git a/apps/meteor/app/ui-utils/client/lib/openRoom.tsx b/apps/meteor/app/ui-utils/client/lib/openRoom.tsx index 8d854feee0c7..f46fd83b36b8 100644 --- a/apps/meteor/app/ui-utils/client/lib/openRoom.tsx +++ b/apps/meteor/app/ui-utils/client/lib/openRoom.tsx @@ -21,98 +21,100 @@ import MainLayout from '../../../../client/views/root/MainLayout'; import BlazeTemplate from '../../../../client/views/root/BlazeTemplate'; export async function openRoom(type: RoomType, name: string, render = true) { - RoomManager.currentTracker?.stop(); - RoomManager.currentTracker = Tracker.autorun(async function (c) { - const user = Meteor.user(); - if ((user && user.username == null) || (user == null && settings.get('Accounts_AllowAnonymousRead') === false)) { - appLayout.render(); - return; - } - - try { - const room = roomCoordinator.getRoomDirectives(type)?.findRoom(name) || (await call('getRoomByTypeAndName', type, name)); - Rooms.upsert({ _id: room._id }, _.omit(room, '_id')); - - if (room._id !== name && type === 'd') { - // Redirect old url using username to rid - RoomManager.close(type + name); - return FlowRouter.go('direct', { rid: room._id }, FlowRouter.current().queryParams); - } - - if (room._id === Session.get('openedRoom') && !FlowRouter.getQueryParam('msg')) { + setTimeout(() => { + RoomManager.currentTracker?.stop(); + RoomManager.currentTracker = Tracker.autorun(async function (c) { + const user = Meteor.user(); + if ((user && user.username == null) || (user == null && settings.get('Accounts_AllowAnonymousRead') === false)) { + appLayout.render(); return; } - RoomManager.open({ typeName: type + name, rid: room._id }); + try { + const room = roomCoordinator.getRoomDirectives(type)?.findRoom(name) || (await call('getRoomByTypeAndName', type, name)); + Rooms.upsert({ _id: room._id }, _.omit(room, '_id')); - if (render) { - appLayout.render( - - - , - ); - } + if (room._id !== name && type === 'd') { + // Redirect old url using username to rid + RoomManager.close(type + name); + return FlowRouter.go('direct', { rid: room._id }, FlowRouter.current().queryParams); + } - c.stop(); + if (room._id === Session.get('openedRoom') && !FlowRouter.getQueryParam('msg')) { + return; + } - if (RoomManager.currentTracker) { - RoomManager.currentTracker = undefined; - } + RoomManager.open({ typeName: type + name, rid: room._id }); - NewRoomManager.open(room._id); - Session.set('openedRoom', room._id); + if (render) { + appLayout.render( + + + , + ); + } - fireGlobalEvent('room-opened', _.omit(room, 'usernames')); + c.stop(); - Session.set('editRoomTitle', false); - // KonchatNotification.removeRoomNotification(params._id) - // update user's room subscription - const sub = ChatSubscription.findOne({ rid: room._id }); - if (sub && sub.open === false) { - await callWithErrorHandling('openRoom', room._id); - } + if (RoomManager.currentTracker) { + RoomManager.currentTracker = undefined; + } - if (FlowRouter.getQueryParam('msg')) { - const messageId = FlowRouter.getQueryParam('msg'); - const msg = { _id: messageId, rid: room._id }; + NewRoomManager.open(room._id); + Session.set('openedRoom', room._id); - const message = Messages.findOne({ _id: msg._id }) || (await callWithErrorHandling('getMessages', [msg._id]))[0]; + fireGlobalEvent('room-opened', _.omit(room, 'usernames')); - if (message && (message.tmid || message.tcount)) { - return FlowRouter.setParams({ tab: 'thread', context: message.tmid || message._id }); + Session.set('editRoomTitle', false); + // KonchatNotification.removeRoomNotification(params._id) + // update user's room subscription + const sub = ChatSubscription.findOne({ rid: room._id }); + if (sub && sub.open === false) { + await callWithErrorHandling('openRoom', room._id); } - RoomHistoryManager.getSurroundingMessages(msg); - FlowRouter.setQueryParams({ - msg: null, - }); - } + if (FlowRouter.getQueryParam('msg')) { + const messageId = FlowRouter.getQueryParam('msg'); + const msg = { _id: messageId, rid: room._id }; - return callbacks.run('enter-room', sub); - } catch (error) { - c.stop(); + const message = Messages.findOne({ _id: msg._id }) || (await callWithErrorHandling('getMessages', [msg._id]))[0]; - if (FlowRouter.getQueryParam('msg')) { - FlowRouter.setQueryParams({ - msg: null, - }); - } + if (message && (message.tmid || message.tcount)) { + return FlowRouter.setParams({ tab: 'thread', context: message.tmid || message._id }); + } + + RoomHistoryManager.getSurroundingMessages(msg); + FlowRouter.setQueryParams({ + msg: null, + }); + } + + return callbacks.run('enter-room', sub); + } catch (error) { + c.stop(); - if (type === 'd') { - try { - const { rid } = await call('createDirectMessage', ...name.split(', ')); - await waitUntilFind(() => Subscriptions.findOne({ rid })); - return FlowRouter.go('direct', { rid }, FlowRouter.current().queryParams); - } catch (error) { - console.error(error); + if (FlowRouter.getQueryParam('msg')) { + FlowRouter.setQueryParams({ + msg: null, + }); } + + if (type === 'd') { + try { + const { rid } = await call('createDirectMessage', ...name.split(', ')); + await waitUntilFind(() => Subscriptions.findOne({ rid })); + return FlowRouter.go('direct', { rid }, FlowRouter.current().queryParams); + } catch (error) { + console.error(error); + } + } + Session.set('roomNotFound', { type, name, error }); + appLayout.render( + + + , + ); } - Session.set('roomNotFound', { type, name, error }); - appLayout.render( - - - , - ); - } + }); }); } diff --git a/apps/meteor/client/startup/banners.ts b/apps/meteor/client/startup/banners.ts index 776d54501343..23d1a2619255 100644 --- a/apps/meteor/client/startup/banners.ts +++ b/apps/meteor/client/startup/banners.ts @@ -81,12 +81,26 @@ Meteor.startup(() => { return; } - FlowRouter.watchPathChange(); - - if (FlowRouter.getRouteName() === 'setup-wizard') { + if (Tracker.nonreactive(() => FlowRouter.getRouteName()) === 'setup-wizard') { + Tracker.autorun((c) => { + if (FlowRouter.getRouteName() !== 'setup-wizard') { + unwatchBanners = Tracker.nonreactive(watchBanners); + c.stop(); + } + }); return; } unwatchBanners = Tracker.nonreactive(watchBanners); }); }); + +Meteor.startup(() => { + Tracker.autorun(() => { + if (!Meteor.userId()) { + return; + } + + console.log(FlowRouter.getRouteName()); + }); +});