Skip to content

Commit

Permalink
CustomContent: Add withinDescendant parameter to API
Browse files Browse the repository at this point in the history
This patch adds a new optional parameter to the getContentByFilter API
call to be able to retrieve all descendant nodes using the MPTT model
fields.

With this new parameter it's possible to filter all the nodes below a
specific topic node, useful when combining with other filters like kind
or tags.
  • Loading branch information
danigm committed Dec 2, 2021
1 parent 35665d0 commit 4385631
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
// called in mainClient.js
fetchContentCollection(message) {
const { options } = message;
const { kinds, onlyContent, onlyTopics } = options;
const { kinds, onlyContent, onlyTopics, withinDescendant } = options;
if (onlyContent && onlyTopics) {
const err = new Error('onlyContent and onlyTopics can not be used at the same time');
Expand All @@ -149,17 +149,25 @@
// limit to channel, defaults to true
const limitToChannel = 'limitToChannel' in options ? options.limitToChannel : true;
const getParams = {
ids: options.ids,
authors: options.authors,
tags: options.tags,
parent: options.parent === 'self' ? this.topic.id : options.parent,
channel_id: limitToChannel ? this.topic.channel_id : undefined,
max_results: options.maxResults ? options.maxResults : 50,
kind: kind,
kind_in: kinds,
};
if (withinDescendant) {
getParams.tree_id = withinDescendant.tree_id;
getParams.lft__gt = withinDescendant.lft;
getParams.rght__lt = withinDescendant.rght;
}
return ContentNodeResource.fetchCollection({
getParams: {
ids: options.ids,
authors: options.authors,
tags: options.tags,
parent: options.parent === 'self' ? this.topic.id : options.parent,
channel_id: limitToChannel ? this.topic.channel_id : undefined,
max_results: options.maxResults ? options.maxResults : 50,
kind: kind,
kind_in: kinds,
},
getParams,
})
.then(contentNodes => {
return createReturnMsg({
Expand Down
8 changes: 8 additions & 0 deletions packages/hashi/src/kolibri.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ import { events, nameSpace, DataTypes } from './hashiBase';
* if depth recursion was not deep enough
*/

/**
* Type definition for MPTTNode
* @typedef {Object} MPTTNode
* @property {number} tree_id - tree node id
* @property {number} lft - left tree node edge id
* @property {number} rght - right tree node edge id
/**
* Type definition for pagination more object
* @typedef {Object} MoreObject
Expand Down Expand Up @@ -123,6 +130,7 @@ export default class Kolibri extends BaseShim {
* @param {boolean} options.onlyContent - set to true to query only content nodes
* @param {boolean} [options.limitToChannel=true] - true to limit the
* results to the topic channel
* @param {MPTTNode} options.withinDescendant - Object to filter nodes descendant of this one
* @return {Promise<PageResult>} - a Promise that resolves to an array of ContentNodes
*/
getContentByFilter(options) {
Expand Down

0 comments on commit 4385631

Please sign in to comment.