From 4e948f897c4e83b786f7466066e5ad2e08d31842 Mon Sep 17 00:00:00 2001 From: Djorkaeff Alexandre Date: Thu, 30 Apr 2020 14:53:35 -0300 Subject: [PATCH] [IMPROVEMENT] Stop inserting last message as message object from rooms stream if room is focused (#2069) * [IMPROVEMENT] No insert last message if the room is focused * fix discussion/threads Co-authored-by: Diego Mello --- app/actions/actionsTypes.js | 2 +- app/actions/room.js | 14 ++++++++++++++ app/lib/methods/subscriptions/room.js | 5 +++++ app/lib/methods/subscriptions/rooms.js | 3 ++- app/reducers/room.js | 14 +++++++++++++- 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/app/actions/actionsTypes.js b/app/actions/actionsTypes.js index 33e202a56..13be7e6a0 100644 --- a/app/actions/actionsTypes.js +++ b/app/actions/actionsTypes.js @@ -31,7 +31,7 @@ export const ROOMS = createRequestTypes('ROOMS', [ 'OPEN_SEARCH_HEADER', 'CLOSE_SEARCH_HEADER' ]); -export const ROOM = createRequestTypes('ROOM', ['LEAVE', 'DELETE', 'REMOVED', 'USER_TYPING']); +export const ROOM = createRequestTypes('ROOM', ['SUBSCRIBE', 'UNSUBSCRIBE', 'LEAVE', 'DELETE', 'REMOVED', 'USER_TYPING']); export const APP = createRequestTypes('APP', ['START', 'READY', 'INIT', 'INIT_LOCAL_SETTINGS']); export const MESSAGES = createRequestTypes('MESSAGES', ['REPLY_BROADCAST']); export const CREATE_CHANNEL = createRequestTypes('CREATE_CHANNEL', [...defaultTypes]); diff --git a/app/actions/room.js b/app/actions/room.js index 76a37b656..3b3b213f2 100644 --- a/app/actions/room.js +++ b/app/actions/room.js @@ -1,5 +1,19 @@ import * as types from './actionsTypes'; +export function subscribeRoom(rid) { + return { + type: types.ROOM.SUBSCRIBE, + rid + }; +} + +export function unsubscribeRoom(rid) { + return { + type: types.ROOM.UNSUBSCRIBE, + rid + }; +} + export function leaveRoom(rid, t) { return { type: types.ROOM.LEAVE, diff --git a/app/lib/methods/subscriptions/room.js b/app/lib/methods/subscriptions/room.js index 507c36b09..f6df8b343 100644 --- a/app/lib/methods/subscriptions/room.js +++ b/app/lib/methods/subscriptions/room.js @@ -10,6 +10,7 @@ import reduxStore from '../../createStore'; import { addUserTyping, removeUserTyping, clearUserTyping } from '../../../actions/usersTyping'; import debounce from '../../../utils/debounce'; import RocketChat from '../../rocketchat'; +import { subscribeRoom, unsubscribeRoom } from '../../../actions/room'; const WINDOW_TIME = 1000; @@ -38,6 +39,8 @@ export default class RoomSubscription { if (!this.isAlive) { this.unsubscribe(); } + + reduxStore.dispatch(subscribeRoom(this.rid)); } unsubscribe = async() => { @@ -59,6 +62,8 @@ export default class RoomSubscription { if (this.timer) { clearTimeout(this.timer); } + + reduxStore.dispatch(unsubscribeRoom(this.rid)); } removeListener = async(promise) => { diff --git a/app/lib/methods/subscriptions/rooms.js b/app/lib/methods/subscriptions/rooms.js index d80f3dd84..85e0ac3a9 100644 --- a/app/lib/methods/subscriptions/rooms.js +++ b/app/lib/methods/subscriptions/rooms.js @@ -141,7 +141,8 @@ const createOrUpdateSubscription = async(subscription, room) => { } } - if (tmp.lastMessage) { + const { rooms } = store.getState().room; + if (tmp.lastMessage && !rooms.includes(tmp.rid)) { const lastMessage = buildMessage(tmp.lastMessage); const messagesCollection = db.collections.get('messages'); let messageRecord; diff --git a/app/reducers/room.js b/app/reducers/room.js index 6a34aa8d5..762905fea 100644 --- a/app/reducers/room.js +++ b/app/reducers/room.js @@ -2,11 +2,23 @@ import { ROOM } from '../actions/actionsTypes'; const initialState = { rid: null, - isDeleting: false + isDeleting: false, + rooms: [] }; export default function(state = initialState, action) { switch (action.type) { + case ROOM.SUBSCRIBE: + return { + ...state, + rooms: [action.rid, ...state.rooms] + }; + case ROOM.UNSUBSCRIBE: + return { + ...state, + rooms: state.rooms + .filter(room => room.rid === action.rid) + }; case ROOM.LEAVE: return { ...state,