Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Added getPostsForIds selector (#279)
Browse files Browse the repository at this point in the history
* Added getPostsForIds selector

* Changed shouldFilterPost options

* Fixed style errors
  • Loading branch information
hmhealey committed Aug 28, 2020
1 parent 4c43715 commit e087ff9
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 7 deletions.
18 changes: 14 additions & 4 deletions packages/mattermost-redux/src/selectors/entities/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ export function makeGetPostsInChannel() {
const posts = [];

const joinLeavePref = myPreferences[getPreferenceKey(Preferences.CATEGORY_ADVANCED_SETTINGS, 'join_leave')];
const filterJoinLeave = joinLeavePref ? joinLeavePref.value === 'false' : false;
const showJoinLeave = joinLeavePref ? joinLeavePref.value !== 'false' : true;

for (let i = 0; i < postIds.length; i++) {
const post = allPosts[postIds[i]];
if (!shouldFilterPost(post, {filterJoinLeave})) {
if (!shouldFilterPost(post, {showJoinLeave})) {
const previousPost = allPosts[postIds[i + 1]] || {create_at: 0};
posts.push(formatPostInChannel(post, previousPost, i, allPosts, postIds, currentUser));
}
Expand Down Expand Up @@ -240,11 +240,11 @@ export function makeGetPostsAroundPost() {

const posts = [];
const joinLeavePref = myPreferences[getPreferenceKey(Preferences.CATEGORY_ADVANCED_SETTINGS, 'join_leave')];
const filterJoinLeave = joinLeavePref ? joinLeavePref.value === 'false' : false;
const showJoinLeave = joinLeavePref ? joinLeavePref.value !== 'false' : true;

for (let i = 0; i < slicedPostIds.length; i++) {
const post = allPosts[slicedPostIds[i]];
if (!shouldFilterPost(post, {filterJoinLeave})) {
if (!shouldFilterPost(post, {showJoinLeave})) {
const previousPost = allPosts[slicedPostIds[i + 1]] || {create_at: 0};
const formattedPost = formatPostInChannel(post, previousPost, i, allPosts, slicedPostIds, currentUser);

Expand Down Expand Up @@ -330,3 +330,13 @@ export function makeGetMessageInHistoryItem(type) {
}
);
}

export function makeGetPostsForIds() {
return createIdsSelector(
getAllPosts,
(state, postIds) => postIds,
(allPosts, postIds) => {
return postIds.map((id) => allPosts[id]);
}
);
}
2 changes: 1 addition & 1 deletion packages/mattermost-redux/src/utils/post_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function shouldFilterPost(post, options = {}) {
// Add as much filters as needed here, if you want to filter the post return true
const postTypes = Posts.POST_TYPES;

if (options.filterJoinLeave) {
if ('showJoinLeave' in options && !options.showJoinLeave) {
const joinLeaveTypes = [postTypes.JOIN_LEAVE, postTypes.JOIN_CHANNEL, postTypes.LEAVE_CHANNEL, postTypes.ADD_REMOVE, postTypes.ADD_TO_CHANNEL, postTypes.REMOVE_FROM_CHANNEL];
if (joinLeaveTypes.includes(post.type)) {
return true;
Expand Down
130 changes: 128 additions & 2 deletions packages/mattermost-redux/test/selectors/posts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ describe('Selectors.Posts', () => {
});
});

describe('makeGetPostIdsForThread', () => {
describe('getPostIdsForThread', () => {
it('single post', () => {
const getPostIdsForThread = Selectors.makeGetPostIdsForThread();

Expand Down Expand Up @@ -1088,7 +1088,7 @@ describe('Selectors.Posts', () => {
});
});

describe('makeGetPostIdsAroundPost', () => {
describe('getPostIdsAroundPost', () => {
it('no posts around', () => {
const getPostIdsAroundPost = Selectors.makeGetPostIdsAroundPost();

Expand Down Expand Up @@ -1350,4 +1350,130 @@ describe('Selectors.Posts', () => {
assert.notEqual(now1, now2);
});
});

describe('getPostsForIds', () => {
it('selector', () => {
const getPostsForIds = Selectors.makeGetPostsForIds();

const testPosts = {
1000: {id: '1000'},
1001: {id: '1001'},
1002: {id: '1002'},
1003: {id: '1003'},
1004: {id: '1004'}
};
const state = {
entities: {
posts: {
posts: testPosts
}
}
};

const postIds = ['1000', '1002', '1003'];

const actual = getPostsForIds(state, postIds);
assert.equal(actual.length, 3);
assert.equal(actual[0], testPosts[postIds[0]]);
assert.equal(actual[1], testPosts[postIds[1]]);
assert.equal(actual[2], testPosts[postIds[2]]);
});

it('memoization', () => {
const getPostsForIds = Selectors.makeGetPostsForIds();

const testPosts = {
1000: {id: '1000'},
1001: {id: '1001'},
1002: {id: '1002'},
1003: {id: '1003'},
1004: {id: '1004'}
};
let state = {
entities: {
posts: {
posts: {
...testPosts
}
}
}
};
let postIds = ['1000', '1002', '1003'];

let now = getPostsForIds(state, postIds);
assert.deepEqual(now, [testPosts['1000'], testPosts['1002'], testPosts['1003']]);

// No changes
let previous = now;
now = getPostsForIds(state, postIds);
assert.deepEqual(now, [testPosts['1000'], testPosts['1002'], testPosts['1003']]);
assert.equal(now, previous);

// New, identical ids
postIds = ['1000', '1002', '1003'];

previous = now;
now = getPostsForIds(state, postIds);
assert.deepEqual(now, [testPosts['1000'], testPosts['1002'], testPosts['1003']]);
assert.equal(now, previous);

// New posts, no changes to ones in ids
state = {
...state,
entities: {
...state.entities,
posts: {
...state.entities.posts,
posts: {
...state.entities.posts.posts
}
}
}
};

previous = now;
now = getPostsForIds(state, postIds);
assert.deepEqual(now, [testPosts['1000'], testPosts['1002'], testPosts['1003']]);
assert.equal(now, previous);

// New ids
postIds = ['1001', '1002', '1004'];

previous = now;
now = getPostsForIds(state, postIds);
assert.deepEqual(now, [testPosts['1001'], testPosts['1002'], testPosts['1004']]);
assert.notEqual(now, previous);

previous = now;
now = getPostsForIds(state, postIds);
assert.deepEqual(now, [testPosts['1001'], testPosts['1002'], testPosts['1004']]);
assert.equal(now, previous);

// New posts, changes to ones in ids
const newPost = {id: '1002', message: 'abcd'};
state = {
...state,
entities: {
...state.entities,
posts: {
...state.entities.posts,
posts: {
...state.entities.posts.posts,
[newPost.id]: newPost
}
}
}
};

previous = now;
now = getPostsForIds(state, postIds);
assert.deepEqual(now, [testPosts['1001'], newPost, testPosts['1004']]);
assert.notEqual(now, previous);

previous = now;
now = getPostsForIds(state, postIds);
assert.deepEqual(now, [testPosts['1001'], newPost, testPosts['1004']]);
assert.equal(now, previous);
});
});
});

0 comments on commit e087ff9

Please sign in to comment.