Skip to content

Commit

Permalink
[FIX] Fix bug when user try recreate channel or group with same name …
Browse files Browse the repository at this point in the history
…and remove room from cache when user leaves room (#12341)

* Fix bug when user try recreate channel or group with same name

* Remove flowrouter from cachedcollection

* Remove room from cache when user leaves room

* Fix deletions and leaves from rooms in client, and rooms stream when delete room
  • Loading branch information
MarcosSpessatto authored and rodrigok committed Jan 29, 2019
1 parent 08e7788 commit c420a28
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
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);
}
});

0 comments on commit c420a28

Please sign in to comment.