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

[FIX] Fix bug when user try recreate channel or group with same name and remove room from cache when user leaves room #12341

Merged
merged 8 commits into from
Jan 29, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -339,20 +339,35 @@ export class CachedCollection {
this.collection.remove({});
}

removeRoomFromCacheWhenUserLeaves(roomId, ChatRoom, CachedChatRoom) {
ChatRoom.remove(roomId);
CachedChatRoom.saveCache();
}

async setupListener(eventType, eventName) {
Meteor.startup(async() => {
const { Notifications } = await import('meteor/rocketchat:notifications');
const { RoomManager } = await import('meteor/rocketchat:ui');
const { ChatRoom } = await import('meteor/rocketchat:models');
const { CachedChatRoom } = await import('meteor/rocketchat:models');
Notifications[eventType || this.eventType](eventName || this.eventName, (t, record) => {
this.log('record received', t, record);
callbacks.run(`cachedCollection-received-${ this.name }`, record, t);
if (t === 'removed') {
let room;
if (this.eventName === 'subscriptions-changed') {
room = ChatRoom.findOne(record.rid);
this.removeRoomFromCacheWhenUserLeaves(room._id, ChatRoom, CachedChatRoom);
} else {
room = this.collection.findOne({ _id: record._id });
}
if (room) {
RoomManager.close(room.t + room.name);
}
this.collection.remove(record._id);
RoomManager.close(record.t + record.name);
} else {
this.collection.upsert({ _id: record._id }, _.omit(record, '_id'));
}

this.saveCache();
});
});
Expand Down
18 changes: 1 addition & 17 deletions packages/rocketchat-ui-utils/client/lib/ChannelActions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Meteor } from 'meteor/meteor';
import { FlowRouter } from 'meteor/kadira:flow-router';
import { Session } from 'meteor/session';
import { t, UiTextContext, roomTypes, handleError } from 'meteor/rocketchat:utils';
import { modal } from './modal';
import { ChatSubscription } from 'meteor/rocketchat:models';
import { call } from './callMethod';

export function hide(type, rid, name) {
Expand Down Expand Up @@ -36,20 +34,6 @@ export function hide(type, rid, name) {
return false;
}

const leaveRoom = async(rid) => {
if (!Meteor.userId()) {
return false;
}
const tmp = ChatSubscription.findOne({ rid, 'u._id': Meteor.userId() });
ChatSubscription.remove({ rid, 'u._id': Meteor.userId() });
try {
await call('leaveRoom', rid);
} catch (error) {
ChatSubscription.insert(tmp);
throw error;
}
};

export async function leave(type, rid, name) {
const { RoomManager } = await import('meteor/rocketchat:ui');
const warnText = roomTypes.roomTypes[type].getUiText(UiTextContext.LEAVE_WARNING);
Expand All @@ -69,7 +53,7 @@ export async function leave(type, rid, name) {
return;
}
try {
await leaveRoom(rid);
await call('leaveRoom', rid);
modal.close();
if (['channel', 'group', 'direct'].includes(FlowRouter.getRouteName()) && (Session.get('openedRoom') === rid)) {
FlowRouter.go('home');
Expand Down
10 changes: 10 additions & 0 deletions server/publications/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ Meteor.methods({
},
});

const getSubscriptions = (id) => {
const fields = { 'u._id': 1 };
return RocketChat.models.Subscriptions.trashFind({ rid: id }, { fields });
};

RocketChat.models.Rooms.on('change', ({ clientAction, id, data }) => {
switch (clientAction) {
case 'updated':
Expand All @@ -126,6 +131,11 @@ RocketChat.models.Rooms.on('change', ({ clientAction, id, data }) => {
}

if (data) {
if (clientAction === 'removed') {
getSubscriptions(clientAction, id).forEach(({ u }) => {
RocketChat.Notifications.notifyUserInThisInstance(u._id, 'rooms-changed', clientAction, data);
});
}
RocketChat.Notifications.streamUser.__emit(id, clientAction, data);
}
});