mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
Hotfix: moving pinned items (#12935)
* increase checks for moving pinned items - fixes #10406 * allow to move official pinneditems * using common object instead of method import
This commit is contained in:
@@ -1716,7 +1716,7 @@ api.movePinnedItem = {
|
||||
|
||||
const { user } = res.locals;
|
||||
const { path } = req.params;
|
||||
const position = Number(req.params.position);
|
||||
let position = Number(req.params.position);
|
||||
|
||||
// If something has been added or removed from the inAppRewards, we need
|
||||
// to reset pinnedItemsOrder to have the correct length. Since inAppRewards
|
||||
@@ -1727,17 +1727,28 @@ api.movePinnedItem = {
|
||||
user.pinnedItemsOrder = currentPinnedItems.map(item => item.path);
|
||||
}
|
||||
|
||||
const officialItems = common.getOfficialPinnedItems(user);
|
||||
|
||||
const itemExistInPinnedArray = user.pinnedItems.findIndex(item => item.path === path);
|
||||
const itemExistInOfficialItems = officialItems.findIndex(item => item.path === path);
|
||||
|
||||
if (itemExistInPinnedArray === -1 && itemExistInOfficialItems === -1) {
|
||||
throw new BadRequest(res.t('wrongItemPath', { path }, req.language));
|
||||
}
|
||||
|
||||
// Adjust the order
|
||||
const currentIndex = user.pinnedItemsOrder.findIndex(item => item === path);
|
||||
const currentPinnedItemPath = user.pinnedItemsOrder[currentIndex];
|
||||
|
||||
if (currentIndex === -1) {
|
||||
throw new BadRequest(res.t('wrongItemPath', { path }, req.language));
|
||||
if (currentIndex !== -1) {
|
||||
// Remove the one we will move
|
||||
user.pinnedItemsOrder.splice(currentIndex, 1);
|
||||
} else {
|
||||
// usually the array would be already fixed by the inAppRewards call
|
||||
// but it seems something didn't work out
|
||||
position = Math.min(position, user.pinnedItemsOrder.length - 1);
|
||||
}
|
||||
|
||||
// Remove the one we will move
|
||||
user.pinnedItemsOrder.splice(currentIndex, 1);
|
||||
|
||||
// reinsert the item in position (or just at the end)
|
||||
if (position === -1) {
|
||||
user.pinnedItemsOrder.push(currentPinnedItemPath);
|
||||
|
||||
Reference in New Issue
Block a user