Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression: Banner - Room not found - Omnichannel room #26693

Merged
merged 3 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apps/meteor/app/ui-utils/client/lib/RoomManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const openedRooms: Record<
template?: Blaze.View;
streamActive?: boolean;
unreadSince: ReactiveVar<Date | undefined>;
lastSeen?: Date;
lastSeen: Date;
unreadFirstId?: string;
}
> = {};
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -225,6 +224,7 @@ function open({ typeName, rid }: { typeName: string; rid: IRoom['_id'] }) {
active: false,
ready: false,
unreadSince: new ReactiveVar(undefined),
lastSeen: new Date(),
};
}

Expand Down
150 changes: 76 additions & 74 deletions apps/meteor/app/ui-utils/client/lib/openRoom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<MainLayout />);
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(<MainLayout />);
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(
<MainLayout>
<BlazeTemplate template='room' />
</MainLayout>,
);
}
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(
<MainLayout>
<BlazeTemplate template='room' />
</MainLayout>,
);
}

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(
<MainLayout>
<BlazeTemplate template='roomNotFound' />
</MainLayout>,
);
}
Session.set('roomNotFound', { type, name, error });
appLayout.render(
<MainLayout>
<BlazeTemplate template='roomNotFound' />
</MainLayout>,
);
}
});
});
}
20 changes: 17 additions & 3 deletions apps/meteor/client/startup/banners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
});