From 3d0fdc59a0b81b5dda5dd2c06e7daf06f7d72b98 Mon Sep 17 00:00:00 2001 From: "Louis Wicket (wil)" Date: Tue, 20 Sep 2022 14:39:56 +0000 Subject: [PATCH] [IMP] mail, im_livechat: remove conditions from sort MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit makes all sorts unconditional, which would allow us to get rid of the functions that wrap the sorting rules and use arrays directly instead. This will allow us to lighten the syntax and to perform some optimizations in subsequent PRs. Task-2992581. closes odoo/odoo#100895 X-original-commit: 4d15ca0f6883fb089e8d7514c4e639b86cedbd45 Signed-off-by: Alexandre Kühn (aku) Signed-off-by: Louis Wicket (wil) --- .../src/models/discuss_sidebar_category.js | 16 ++- .../src/models/discuss_sidebar_category.js | 97 +++++++++++-------- 2 files changed, 67 insertions(+), 46 deletions(-) diff --git a/addons/im_livechat/static/src/models/discuss_sidebar_category.js b/addons/im_livechat/static/src/models/discuss_sidebar_category.js index efaa41556c4..92b20c7a45e 100644 --- a/addons/im_livechat/static/src/models/discuss_sidebar_category.js +++ b/addons/im_livechat/static/src/models/discuss_sidebar_category.js @@ -14,6 +14,14 @@ addFields('DiscussSidebarCategory', { }); patchFields('DiscussSidebarCategory', { + categoryItemsOrderedByLastAction: { + compute() { + if (this.discussAsLivechat) { + return this.categoryItems; + } + return this._super(); + }, + }, isServerOpen: { compute() { // there is no server state for non-users (guests) @@ -37,18 +45,18 @@ patchFields('DiscussSidebarCategory', { return this._super(); }, }, - serverStateKey: { + orderedCategoryItems: { compute() { if (this.discussAsLivechat) { - return 'is_discuss_sidebar_category_livechat_open'; + return this.categoryItemsOrderedByLastAction; } return this._super(); }, }, - sortComputeMethod: { + serverStateKey: { compute() { if (this.discussAsLivechat) { - return 'last_action'; + return 'is_discuss_sidebar_category_livechat_open'; } return this._super(); }, diff --git a/addons/mail/static/src/models/discuss_sidebar_category.js b/addons/mail/static/src/models/discuss_sidebar_category.js index 8542271cf95..82976a5c92b 100644 --- a/addons/mail/static/src/models/discuss_sidebar_category.js +++ b/addons/mail/static/src/models/discuss_sidebar_category.js @@ -168,6 +168,49 @@ registerModel({ }, default: '', }), + /** + * Determines the discuss sidebar category items that are displayed by + * this discuss sidebar category. + */ + categoryItems: many('DiscussSidebarCategoryItem', { + inverse: 'category', + }), + categoryItemsOrderedByLastAction: many('DiscussSidebarCategoryItem', { + compute() { + if (this.discussAsChat) { + return this.categoryItems; + } + // clear if the value is not going to be used, so that it avoids + // sorting the items for nothing + return clear(); + }, + sort() { + return [ + ['truthy-first', 'thread'], + ['truthy-first', 'thread.lastInterestDateTime'], + ['most-recent-first', 'thread.lastInterestDateTime'], + ['greater-first', 'channel.id'], + ]; + }, + }), + categoryItemsOrderedByName: many('DiscussSidebarCategoryItem', { + compute() { + if (this.discussAsChannel) { + return this.categoryItems; + } + // clear if the value is not going to be used, so that it avoids + // sorting the items for nothing + return clear(); + }, + sort() { + return [ + ['truthy-first', 'thread'], + ['truthy-first', 'thread.displayName'], + ['case-insensitive-asc', 'thread.displayName'], + ['smaller-first', 'channel.id'], + ]; + }, + }), /** * The title text in UI for command `add` */ @@ -183,31 +226,6 @@ registerModel({ }, default: '', }), - /** - * Determines the discuss sidebar category items that are displayed by - * this discuss sidebar category. - */ - categoryItems: many('DiscussSidebarCategoryItem', { - inverse: 'category', - sort() { - switch (this.sortComputeMethod) { - case 'name': - return [ - ['truthy-first', 'thread'], - ['truthy-first', 'thread.displayName'], - ['case-insensitive-asc', 'thread.displayName'], - ['smaller-first', 'channel.id'], - ]; - case 'last_action': - return [ - ['truthy-first', 'thread'], - ['truthy-first', 'thread.lastInterestDateTime'], - ['most-recent-first', 'thread.lastInterestDateTime'], - ['greater-first', 'channel.id'], - ]; - } - }, - }), /** * States the total amount of unread/action-needed threads in this * category. @@ -231,7 +249,7 @@ registerModel({ */ filteredCategoryItems: many('DiscussSidebarCategoryItem', { compute() { - let categoryItems = this.categoryItems; + let categoryItems = this.orderedCategoryItems; const searchValue = this.messaging.discuss.sidebarQuickSearchValue; if (searchValue) { const qsVal = searchValue.toLowerCase(); @@ -343,6 +361,17 @@ registerModel({ return clear(); }, }), + orderedCategoryItems: many('DiscussSidebarCategoryItem', { + compute() { + if (this.discussAsChannel) { + return this.categoryItemsOrderedByName; + } + if (this.discussAsChat) { + return this.categoryItemsOrderedByLastAction; + } + return clear(); + }, + }), /** * The key used in the server side for the category state */ @@ -357,22 +386,6 @@ registerModel({ return clear(); }, }), - /** - * Determines the sorting method of channels in this category. - * Must be one of: 'name', 'last_action'. - */ - sortComputeMethod: attr({ - compute() { - if (this.discussAsChannel) { - return 'name'; - } - if (this.discussAsChat) { - return 'last_action'; - } - return clear(); - }, - required: true, - }), /** * Channel type which is supported by the category. */