Commit 11902b0d authored by Jesús Espino's avatar Jesús Espino Committed by Elias Nahum

[MM-11170] Fix unreads for deleted users (#605)

* [MM-11170] Fix unreads for deleted users

* Add check for deleted_at for deativated entities
parent a30ac67a
......@@ -23,7 +23,7 @@ import {
import {getLastPostPerChannel, getAllPosts} from 'selectors/entities/posts';
import {getCurrentTeamId, getCurrentTeamMembership} from 'selectors/entities/teams';
import {haveICurrentChannelPermission} from 'selectors/entities/roles';
import {isCurrentUserSystemAdmin} from 'selectors/entities/users';
import {isCurrentUserSystemAdmin, getCurrentUserId} from 'selectors/entities/users';
import {
buildDisplayableChannelList,
......@@ -306,20 +306,33 @@ export const getMembersInCurrentChannel = createSelector(
export const getUnreads = createSelector(
getAllChannels,
getMyChannelMemberships,
(channels, myMembers) => {
getUsers,
getCurrentUserId,
(channels, myMembers, users, currentUserId) => {
let messageCount = 0;
let mentionCount = 0;
Object.keys(myMembers).forEach((channelId) => {
const channel = channels[channelId];
const m = myMembers[channelId];
if (channel && m) {
let otherUserId;
if (channel.type === 'D') {
mentionCount += channel.total_msg_count - m.msg_count;
} else if (m.mention_count > 0) {
otherUserId = getUserIdFromChannelName(currentUserId, channel.name);
if (users[otherUserId] && users[otherUserId].delete_at === 0) {
mentionCount += channel.total_msg_count - m.msg_count;
}
} else if (m.mention_count > 0 && channel.delete_at === 0) {
mentionCount += m.mention_count;
}
if (m.notify_props && m.notify_props.mark_unread !== 'mention' && channel.total_msg_count - m.msg_count > 0) {
messageCount += 1;
if (channel.type === 'D') {
if (users[otherUserId] && users[otherUserId].delete_at === 0) {
messageCount += 1;
}
} else if (channel.delete_at === 0) {
messageCount += 1;
}
}
}
});
......@@ -332,20 +345,32 @@ export const getUnreadsInCurrentTeam = createSelector(
getCurrentChannelId,
getMyChannels,
getMyChannelMemberships,
(currentChannelId, channels, myMembers) => {
getUsers,
getCurrentUserId,
(currentChannelId, channels, myMembers, users, currentUserId) => {
let messageCount = 0;
let mentionCount = 0;
channels.forEach((channel) => {
const m = myMembers[channel.id];
if (m && channel.id !== currentChannelId) {
let otherUserId;
if (channel.type === 'D') {
mentionCount += channel.total_msg_count - m.msg_count;
} else if (m.mention_count > 0) {
otherUserId = getUserIdFromChannelName(currentUserId, channel.name);
if (users[otherUserId] && users[otherUserId].delete_at === 0) {
mentionCount += channel.total_msg_count - m.msg_count;
}
} else if (m.mention_count > 0 && channel.delete_at === 0) {
mentionCount += m.mention_count;
}
if (m.notify_props && m.notify_props.mark_unread !== 'mention' && channel.total_msg_count - m.msg_count > 0) {
messageCount += 1;
if (channel.type === 'D') {
if (users[otherUserId] && users[otherUserId].delete_at === 0) {
messageCount += 1;
}
} else if (channel.delete_at === 0) {
messageCount += 1;
}
}
}
});
......
......@@ -12,7 +12,6 @@ import {General, Preferences} from 'constants';
describe('Selectors.Channels', () => {
const team1 = TestHelper.fakeTeamWithId();
const team2 = TestHelper.fakeTeamWithId();
const user = TestHelper.fakeUserWithId();
const user2 = TestHelper.fakeUserWithId();
const user3 = TestHelper.fakeUserWithId();
......@@ -20,6 +19,7 @@ describe('Selectors.Channels', () => {
profiles[user.id] = user;
profiles[user2.id] = user2;
profiles[user3.id] = user3;
profiles.fakeUserId = TestHelper.fakeUserWithId('fakeUserId');
const channel1 = TestHelper.fakeChannelWithId(team1.id);
channel1.display_name = 'Channel Name';
......@@ -58,6 +58,10 @@ describe('Selectors.Channels', () => {
channel12.last_post_at = Date.now();
channel12.name = getDirectChannelName(user.id, user2.id);
const channel13 = TestHelper.fakeDmChannel(user.id, 'fakeUserId');
channel13.total_msg_count = 3;
channel13.display_name = '';
const channels = {};
channels[channel1.id] = channel1;
channels[channel2.id] = channel2;
......@@ -71,11 +75,12 @@ describe('Selectors.Channels', () => {
channels[channel10.id] = channel10;
channels[channel11.id] = channel11;
channels[channel12.id] = channel12;
channels[channel13.id] = channel13;
const channelsInTeam = {};
channelsInTeam[team1.id] = [channel1.id, channel2.id, channel5.id, channel6.id, channel8.id, channel10.id, channel11.id];
channelsInTeam[team2.id] = [channel3.id];
channelsInTeam[''] = [channel4.id, channel7.id, channel9.id];
channelsInTeam[''] = [channel4.id, channel7.id, channel9.id, channel13.id];
const membersInChannel = {};
membersInChannel[channel1.id] = {};
......@@ -104,6 +109,9 @@ describe('Selectors.Channels', () => {
membersInChannel[channel12.id] = {};
membersInChannel[channel12.id][user.id] = {channel_id: channel12.id, user_id: user.id};
membersInChannel[channel12.id][user2.id] = {channel_id: channel12.id, user_id: user2.id};
membersInChannel[channel13.id] = {};
membersInChannel[channel13.id][user.id] = {channel_id: channel13.id, user_id: user.id};
membersInChannel[channel13.id][user2.id] = {channel_id: channel13.id, user_id: user2.id};
const myMembers = {};
myMembers[channel1.id] = {channel_id: channel1.id, user_id: user.id};
......@@ -112,11 +120,12 @@ describe('Selectors.Channels', () => {
myMembers[channel4.id] = {channel_id: channel4.id, user_id: user.id};
myMembers[channel5.id] = {channel_id: channel5.id, user_id: user.id};
myMembers[channel7.id] = {channel_id: channel7.id, user_id: user.id, msg_count: 0, notify_props: {}};
myMembers[channel8.id] = {channel_id: channel7.id, user_id: user.id, msg_count: 0, notify_props: {}};
myMembers[channel8.id] = {channel_id: channel8.id, user_id: user.id, msg_count: 0, notify_props: {}};
myMembers[channel9.id] = {channel_id: channel9.id, user_id: user.id};
myMembers[channel10.id] = {channel_id: channel10.id, user_id: user.id};
myMembers[channel11.id] = {channel_id: channel11.id, user_id: user.id};
myMembers[channel12.id] = {channel_id: channel12.id, user_id: user.id};
myMembers[channel12.id] = {channel_id: channel12.id, user_id: user.id, msg_count: 0, notifyProps: {}};
myMembers[channel13.id] = {channel_id: channel13.id, user_id: user.id, msg_count: 1, notifyProps: {}};
const myPreferences = {
[`${Preferences.CATEGORY_FAVORITE_CHANNEL}--${channel1.id}`]: {
......@@ -140,6 +149,7 @@ describe('Selectors.Channels', () => {
[channel7.id]: [user.id, user2.id, user3.id],
[channel12.id]: [user.id, user2.id],
},
statuses: {},
},
teams: {
currentTeamId: team1.id,
......@@ -177,6 +187,7 @@ describe('Selectors.Channels', () => {
channel4,
{...channel7, display_name: [user2.username, user3.username].sort(sortUsernames).join(', ')},
channel9,
{...channel13, display_name: profiles.fakeUserId.username},
]);
});
......@@ -193,7 +204,78 @@ describe('Selectors.Channels', () => {
});
it('get unreads for current team', () => {
assert.equal(Selectors.getUnreadsInCurrentTeam(testState).mentionCount, 1);
assert.equal(Selectors.getUnreadsInCurrentTeam(testState).mentionCount, 3);
});
it('get unreads', () => {
assert.deepEqual(Selectors.getUnreads(testState), {messageCount: 4, mentionCount: 4});
});
it('get unreads with a missing profile entity', () => {
const newProfiles = {
...testState.entities.users.profiles,
};
Reflect.deleteProperty(newProfiles, 'fakeUserId');
const newState = {
...testState,
entities: {
...testState.entities,
users: {
...testState.entities.users,
profiles: newProfiles,
},
},
};
assert.deepEqual(Selectors.getUnreads(newState), {messageCount: 4, mentionCount: 2});
assert.deepEqual(Selectors.getUnreadsInCurrentTeam(newState), {messageCount: 3, mentionCount: 1});
});
it('get unreads with a deactivated user', () => {
const newProfiles = {
...testState.entities.users.profiles,
fakeUserId: {
...testState.entities.users.profiles.fakeUserId,
delete_at: 100,
},
};
const newState = {
...testState,
entities: {
...testState.entities,
users: {
...testState.entities.users,
profiles: newProfiles,
},
},
};
assert.deepEqual(Selectors.getUnreads(newState), {messageCount: 4, mentionCount: 2});
assert.deepEqual(Selectors.getUnreadsInCurrentTeam(newState), {messageCount: 3, mentionCount: 1});
});
it('get unreads with a deactivated channel', () => {
const newChannels = {
...testState.entities.channels.channels,
[channel2.id]: {
...testState.entities.channels.channels[channel2.id],
delete_at: 100,
},
};
const newState = {
...testState,
entities: {
...testState.entities,
channels: {
...testState.entities.channels,
channels: newChannels,
},
},
};
assert.deepEqual(Selectors.getUnreads(newState), {messageCount: 3, mentionCount: 3});
assert.deepEqual(Selectors.getUnreadsInCurrentTeam(newState), {messageCount: 2, mentionCount: 2});
});
it('get channel map for current team', () => {
......@@ -634,6 +716,7 @@ describe('Selectors.Channels', () => {
assert.deepEqual(getDirectAndGroupChannels(state), [
{...channel7, display_name: [user2.username, user3.username].sort(sortUsernames).join(', ')},
{...channel12, display_name: user2.username},
{...channel13, display_name: profiles.fakeUserId.username},
]);
});
......@@ -658,6 +741,7 @@ describe('Selectors.Channels', () => {
assert.deepEqual(getDirectAndGroupChannels(state), [
{...channel7, display_name: [user2.username, user3.username].sort(sortUsernames).join(', ')},
{...channel12, display_name: user2.username},
{...channel13, display_name: profiles.fakeUserId.username},
]);
});
});
......
......@@ -228,6 +228,18 @@ class TestHelper {
};
};
fakeDmChannel = (userId, otherUserId) => {
return {
name: `${userId}__${otherUserId}`,
team_id: '',
display_name: `${otherUserId}`,
type: 'D',
status: 'offline',
teammate_id: `${otherUserId}`,
id: this.generateId(),
};
}
fakeChannelMember = (userId, channelId) => {
return {
user_id: userId,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment