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,