diff --git a/service/actions/websocket.js b/service/actions/websocket.js index 6d54a4952..3aaceaba6 100644 --- a/service/actions/websocket.js +++ b/service/actions/websocket.js @@ -31,7 +31,7 @@ import { } from 'service/constants'; import {getCurrentChannelStats} from 'service/selectors/entities/channels'; import {getUserIdFromChannelName} from 'service/utils/channel_utils'; -import {isSystemMessage} from 'service/utils/post_utils'; +import {isSystemMessage, shouldIgnorePost} from 'service/utils/post_utils'; import EventEmitter from 'service/utils/event_emitter'; export function init(siteUrl, token, optionalWebSocket) { @@ -224,7 +224,21 @@ async function handleNewPostEvent(msg, dispatch, getState) { } ]), getState); - if (userId === users.currentId || post.channel_id === currentChannelId || isSystemMessage(post)) { + if (shouldIgnorePost(post)) { + // if the post type is in the ignore list we'll do nothing with the read state + return; + } + + let markAsRead = false; + if (userId === users.currentId && !isSystemMessage(post)) { + // In case the current user posted the message and that message wasn't triggered by a system message + markAsRead = true; + } else if (post.channel_id === currentChannelId) { + // if the post is for the channel that the user is currently viewing we'll mark the channel as read + markAsRead = true; + } + + if (markAsRead) { markChannelAsRead(post.channel_id)(dispatch, getState); } else { markChannelAsUnread(post.channel_id, msg.data.mentions)(dispatch, getState); diff --git a/service/constants/constants.js b/service/constants/constants.js index 70b191cf1..236953cc5 100644 --- a/service/constants/constants.js +++ b/service/constants/constants.js @@ -54,7 +54,31 @@ const FileConstants = { WORD_TYPES: ['doc', 'docx'] }; +const PostsTypes = { + ADD_REMOVE: 'system_add_remove', + ADD_TO_CHANNEL: 'system_add_to_channel', + CHANNEL_DELETED: 'system_channel_deleted', + DISPLAYNAME_CHANGE: 'system_displayname_change', + EPHEMERAL: 'system_ephemeral', + HEADER_CHANGE: 'system_header_change', + JOIN_CHANNEL: 'system_join_channel', + JOIN_LEAVE: 'system_join_leave', + LEAVE_CHANNEL: 'system_leave_channel', + PURPOSE_CHANGE: 'system_purpose_change', + REMOVE_FROM_CHANNEL: 'system_remove_from_channel' +}; + export default { ...Constants, - ...FileConstants + ...FileConstants, + ...PostsTypes, + IGNORE_POST_TYPES: [ + PostsTypes.ADD_REMOVE, + PostsTypes.ADD_TO_CHANNEL, + PostsTypes.CHANNEL_DELETED, + PostsTypes.JOIN_LEAVE, + PostsTypes.JOIN_CHANNEL, + PostsTypes.LEAVE_CHANNEL, + PostsTypes.REMOVE_FROM_CHANNEL + ] }; diff --git a/service/utils/post_utils.js b/service/utils/post_utils.js index 9c1cb7f00..45a7609af 100644 --- a/service/utils/post_utils.js +++ b/service/utils/post_utils.js @@ -7,6 +7,10 @@ export function isSystemMessage(post) { return post.type && post.type.startsWith(Constants.SYSTEM_MESSAGE_PREFIX); } +export function shouldIgnorePost(post) { + return Constants.IGNORE_POST_TYPES.includes(post.type); +} + export function addDatesToPostList(posts, indicateNewMessages, currentUserId, lastViewedAt) { const out = [];