From 2a47fda0ffd217cbccc66a3cdca0ec7ad3341c96 Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Tue, 16 Oct 2018 16:03:22 -0300 Subject: [PATCH 1/4] Fix bug when user try recreate channel or group with same name --- packages/rocketchat-lib/client/lib/cachedCollection.js | 3 ++- server/publications/room.js | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/rocketchat-lib/client/lib/cachedCollection.js b/packages/rocketchat-lib/client/lib/cachedCollection.js index aabc101b42aa7..4202dec73ae9b 100644 --- a/packages/rocketchat-lib/client/lib/cachedCollection.js +++ b/packages/rocketchat-lib/client/lib/cachedCollection.js @@ -337,8 +337,9 @@ class CachedCollection { this.log('record received', t, record); RocketChat.callbacks.run(`cachedCollection-received-${ this.name }`, record, t); if (t === 'removed') { + const type = FlowRouter.current().route.name === 'channel' ? 'c' : 'p'; this.collection.remove(record._id); - RoomManager.close(record.t + record.name); + RoomManager.close(type + FlowRouter.getParam('name')); } else { this.collection.upsert({ _id: record._id }, _.omit(record, '_id')); } diff --git a/server/publications/room.js b/server/publications/room.js index d93e173d6254d..df8b1544f3f9b 100644 --- a/server/publications/room.js +++ b/server/publications/room.js @@ -111,6 +111,13 @@ Meteor.methods({ }, }); +const getSubscriptions = (clientAction, id) => { + const fields = { 'u._id': 1 }; + return clientAction === 'removed' + ? RocketChat.models.Subscriptions.trashFind({ rid: id }, { fields }) + : RocketChat.models.Subscriptions.findByRoomId(id, { fields }); +}; + RocketChat.models.Rooms.on('change', ({ clientAction, id, data }) => { switch (clientAction) { case 'updated': @@ -125,7 +132,7 @@ RocketChat.models.Rooms.on('change', ({ clientAction, id, data }) => { } if (data) { - RocketChat.models.Subscriptions.findByRoomId(id, { fields: { 'u._id': 1 } }).forEach(({ u }) => { + getSubscriptions(clientAction, id).forEach(({ u }) => { RocketChat.Notifications.notifyUserInThisInstance(u._id, 'rooms-changed', clientAction, data); }); } From ca445f488f5911df6a8f2c0ff4910f7acda4617a Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Mon, 22 Oct 2018 15:27:07 -0300 Subject: [PATCH 2/4] Remove flowrouter from cachedcollection --- packages/rocketchat-lib/client/lib/cachedCollection.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/rocketchat-lib/client/lib/cachedCollection.js b/packages/rocketchat-lib/client/lib/cachedCollection.js index 4202dec73ae9b..387af542db2a4 100644 --- a/packages/rocketchat-lib/client/lib/cachedCollection.js +++ b/packages/rocketchat-lib/client/lib/cachedCollection.js @@ -337,9 +337,11 @@ class CachedCollection { this.log('record received', t, record); RocketChat.callbacks.run(`cachedCollection-received-${ this.name }`, record, t); if (t === 'removed') { - const type = FlowRouter.current().route.name === 'channel' ? 'c' : 'p'; + const room = this.collection.findOne({ _id: record._id }); this.collection.remove(record._id); - RoomManager.close(type + FlowRouter.getParam('name')); + if (room) { + RoomManager.close(room.t + room.name); + } } else { this.collection.upsert({ _id: record._id }, _.omit(record, '_id')); } From 118cd0304edc504da32ec05d4b32c9ef9d3f0d11 Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Tue, 6 Nov 2018 20:33:13 -0200 Subject: [PATCH 3/4] Remove room from cache when user leaves room --- .../rocketchat-lib/client/lib/ChannelActions.js | 16 +--------------- .../client/lib/cachedCollection.js | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/packages/rocketchat-lib/client/lib/ChannelActions.js b/packages/rocketchat-lib/client/lib/ChannelActions.js index 9dbcc0fde93fb..72dbc99033451 100644 --- a/packages/rocketchat-lib/client/lib/ChannelActions.js +++ b/packages/rocketchat-lib/client/lib/ChannelActions.js @@ -30,20 +30,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 function leave(type, rid, name) { const warnText = RocketChat.roomTypes.roomTypes[type].getUiText(UiTextContext.LEAVE_WARNING); @@ -62,7 +48,7 @@ export 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'); diff --git a/packages/rocketchat-lib/client/lib/cachedCollection.js b/packages/rocketchat-lib/client/lib/cachedCollection.js index 387af542db2a4..62ed6820cc387 100644 --- a/packages/rocketchat-lib/client/lib/cachedCollection.js +++ b/packages/rocketchat-lib/client/lib/cachedCollection.js @@ -1,3 +1,4 @@ +/* globals CachedChatRoom */ import localforage from 'localforage'; import _ from 'underscore'; @@ -332,16 +333,27 @@ class CachedCollection { this.collection.remove({}); } + removeRoomFromCacheWhenUserLeaves(roomId) { + ChatRoom.remove(roomId); + CachedChatRoom.saveCache(); + } + setupListener(eventType, eventName) { RocketChat.Notifications[eventType || this.eventType](eventName || this.eventName, (t, record) => { this.log('record received', t, record); RocketChat.callbacks.run(`cachedCollection-received-${ this.name }`, record, t); if (t === 'removed') { - const room = this.collection.findOne({ _id: record._id }); - this.collection.remove(record._id); + let room; + if (this.eventName === 'subscriptions-changed') { + room = ChatRoom.findOne(this.collection.findOne({ _id: record._id }).rid); + this.removeRoomFromCacheWhenUserLeaves(room._id); + } else { + room = this.collection.findOne({ _id: record._id }); + } if (room) { RoomManager.close(room.t + room.name); } + this.collection.remove(record._id); } else { this.collection.upsert({ _id: record._id }, _.omit(record, '_id')); } From b8d4ec322ce5f2a51fa424779cc077725cc64101 Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Mon, 14 Jan 2019 17:25:42 -0200 Subject: [PATCH 4/4] Fix deletions and leaves from rooms in client, and rooms stream when delete room --- .../client/models/CachedCollection.js | 17 +++++++++++++---- .../client/lib/ChannelActions.js | 2 -- server/publications/room.js | 14 +++++++------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/packages/rocketchat-models/client/models/CachedCollection.js b/packages/rocketchat-models/client/models/CachedCollection.js index 5012db12e2a1b..821d71daa4adf 100644 --- a/packages/rocketchat-models/client/models/CachedCollection.js +++ b/packages/rocketchat-models/client/models/CachedCollection.js @@ -345,20 +345,29 @@ export class CachedCollection { CachedChatRoom.saveCache(); } - setupListener(eventType, eventName) { - RocketChat.Notifications[eventType || this.eventType](eventName || this.eventName, (t, record) => { + async setupListener(eventType, eventName) { + if (!this.Notifications) { + const { Notifications } = await import('meteor/rocketchat:notifications'); + this.Notifications = Notifications; + } + if (!this.RoomManager) { + const { RoomManager } = await import('meteor/rocketchat:ui'); + this.RoomManager = RoomManager; + } + this.Notifications[eventType || this.eventType](eventName || this.eventName, (t, record) => { this.log('record received', t, record); - RocketChat.callbacks.run(`cachedCollection-received-${ this.name }`, record, t); + callbacks.run(`cachedCollection-received-${ this.name }`, record, t); if (t === 'removed') { let room; if (this.eventName === 'subscriptions-changed') { room = ChatRoom.findOne(this.collection.findOne({ _id: record._id }).rid); + room = ChatRoom.findOne(record.rid); this.removeRoomFromCacheWhenUserLeaves(room._id); } else { room = this.collection.findOne({ _id: record._id }); } if (room) { - RoomManager.close(room.t + room.name); + this.RoomManager.close(room.t + room.name); } this.collection.remove(record._id); } else { diff --git a/packages/rocketchat-ui-utils/client/lib/ChannelActions.js b/packages/rocketchat-ui-utils/client/lib/ChannelActions.js index 1db244694f73b..5782aa9a85483 100644 --- a/packages/rocketchat-ui-utils/client/lib/ChannelActions.js +++ b/packages/rocketchat-ui-utils/client/lib/ChannelActions.js @@ -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) { diff --git a/server/publications/room.js b/server/publications/room.js index 226a332924bc5..a67b92d986d8f 100644 --- a/server/publications/room.js +++ b/server/publications/room.js @@ -112,11 +112,9 @@ Meteor.methods({ }, }); -const getSubscriptions = (clientAction, id) => { +const getSubscriptions = (id) => { const fields = { 'u._id': 1 }; - return clientAction === 'removed' - ? RocketChat.models.Subscriptions.trashFind({ rid: id }, { fields }) - : RocketChat.models.Subscriptions.findByRoomId(id, { fields }); + return RocketChat.models.Subscriptions.trashFind({ rid: id }, { fields }); }; RocketChat.models.Rooms.on('change', ({ clientAction, id, data }) => { @@ -133,9 +131,11 @@ RocketChat.models.Rooms.on('change', ({ clientAction, id, data }) => { } if (data) { - getSubscriptions(clientAction, id).forEach(({ u }) => { - RocketChat.Notifications.notifyUserInThisInstance(u._id, 'rooms-changed', clientAction, 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); } });