@@ -2,6 +2,7 @@ const constants = require('../../common/constants');
22
33const User = require ( '../../model/user' ) ;
44const Bookmark = require ( '../../model/bookmark' ) ;
5+ const Note = require ( '../../model/note' ) ;
56
67const ValidationError = require ( '../../error/validation.error' ) ;
78const NotFoundError = require ( '../../error/not-found.error' ) ;
@@ -347,7 +348,7 @@ let getUsedTagsForPrivateBookmarks = async function (userId) {
347348 return usedTags ;
348349} ;
349350
350- let getPinnedBookmarks = async function ( userId , page , limit ) {
351+ let getPinnedResources = async function ( userId , page , limit ) {
351352 const userData = await User . findOne ( {
352353 userId : userId ,
353354 } ) ;
@@ -358,14 +359,22 @@ let getPinnedBookmarks = async function (userId, page, limit) {
358359 ( page - 1 ) * limit ,
359360 ( page - 1 ) * limit + limit
360361 ) ;
361- const bookmarks = await Bookmark . find ( { _id : { $in : pinnedRangeIds } } ) ;
362- //we need to order the bookmarks to correspond the one in the userData.pinned array
363- const orderedBookmarksAsInPinned = bookmarks . sort ( function ( a , b ) {
364- return pinnedRangeIds . indexOf ( a . _id ) - pinnedRangeIds . indexOf ( b . _id ) ;
362+ // Pinned entries can be either bookmarks or notes; look both collections up
363+ const [ bookmarks , notes ] = await Promise . all ( [
364+ Bookmark . find ( { _id : { $in : pinnedRangeIds } } ) ,
365+ Note . find ( { _id : { $in : pinnedRangeIds } } ) ,
366+ ] ) ;
367+ const pinnedResources = [ ...bookmarks , ...notes ] ;
368+ //we need to order the resources to correspond the one in the userData.pinned array
369+ const orderedResourcesAsInPinned = pinnedResources . sort ( function ( a , b ) {
370+ return (
371+ pinnedRangeIds . indexOf ( a . _id . toString ( ) ) -
372+ pinnedRangeIds . indexOf ( b . _id . toString ( ) )
373+ ) ;
365374 } ) ;
366375
367- return orderedBookmarksAsInPinned . filter (
368- ( bookmark ) => bookmark !== undefined
376+ return orderedResourcesAsInPinned . filter (
377+ ( resource ) => resource !== undefined
369378 ) ;
370379 }
371380} ;
@@ -408,16 +417,22 @@ let getBookmarksFromHistory = async function (userId, page, limit) {
408417 ( page - 1 ) * limit ,
409418 ( page - 1 ) * limit + limit
410419 ) ;
411- const bookmarks = await Bookmark . find ( { _id : { $in : historyRangeIds } } ) ;
420+ // History entries can be either bookmarks or notes; look both collections up
421+ const [ bookmarks , notes ] = await Promise . all ( [
422+ Bookmark . find ( { _id : { $in : historyRangeIds } } ) ,
423+ Note . find ( { _id : { $in : historyRangeIds } } ) ,
424+ ] ) ;
425+ const historyResources = [ ...bookmarks , ...notes ] ;
412426
413- //we need to order the bookmarks to correspond the one in the userData.history array
414- const orderedBookmarksAsInHistory = bookmarks . sort ( function ( a , b ) {
415- return historyRangeIds . indexOf ( a . _id ) - historyRangeIds . indexOf ( b . _id ) ;
427+ //we need to order the resources to correspond the one in the userData.history array
428+ const orderedResourcesAsInHistory = historyResources . sort ( function ( a , b ) {
429+ return (
430+ historyRangeIds . indexOf ( a . _id . toString ( ) ) -
431+ historyRangeIds . indexOf ( b . _id . toString ( ) )
432+ ) ;
416433 } ) ;
417434
418- //check for "potentially" deleted bookmarks via "delete all private for tag"
419- //return orderedBookmarksAsInHistory.filter(bookmark => bookmark !== undefined);
420- return orderedBookmarksAsInHistory ;
435+ return orderedResourcesAsInHistory ;
421436 }
422437} ;
423438
@@ -428,16 +443,25 @@ let getAllBookmarksFromHistory = async function (userId) {
428443 if ( ! userData ) {
429444 throw new NotFoundError ( `User data NOT_FOUND for userId: ${ userId } ` ) ;
430445 } else {
431- const bookmarks = await Bookmark . find ( { _id : { $in : userData . history } } ) ;
446+ // History entries can be either bookmarks or notes; look both collections up
447+ const [ bookmarks , notes ] = await Promise . all ( [
448+ Bookmark . find ( { _id : { $in : userData . history } } ) ,
449+ Note . find ( { _id : { $in : userData . history } } ) ,
450+ ] ) ;
451+ const historyResources = [ ...bookmarks , ...notes ] ;
432452
433- //we need to order the bookmarks to correspond the one in the userData.history array
434- const allBookmarksOrderedFromHistory = bookmarks . sort ( function ( a , b ) {
435- return userData . history . indexOf ( a . _id ) - userData . history . indexOf ( b . _id ) ;
453+ //we need to order the resources to correspond the one in the userData.history array
454+ const allResourcesOrderedFromHistory = historyResources . sort ( function (
455+ a ,
456+ b
457+ ) {
458+ return (
459+ userData . history . indexOf ( a . _id . toString ( ) ) -
460+ userData . history . indexOf ( b . _id . toString ( ) )
461+ ) ;
436462 } ) ;
437463
438- //check for "potentially" deleted bookmarks via "delete all private for tag"
439- //return orderedBookmarksAsInHistory.filter(bookmark => bookmark !== undefined);
440- return allBookmarksOrderedFromHistory ;
464+ return allResourcesOrderedFromHistory ;
441465 }
442466} ;
443467
@@ -695,7 +719,7 @@ module.exports = {
695719 getLikedBookmarks : getLikedBookmarks ,
696720 getUsedTagsForPublicBookmarks : getUsedTagsForPublicBookmarks ,
697721 getUsedTagsForPrivateBookmarks : getUsedTagsForPrivateBookmarks ,
698- getPinnedBookmarks : getPinnedBookmarks ,
722+ getPinnedResources : getPinnedResources ,
699723 getFavoriteBookmarks : getFavoriteBookmarks ,
700724 getBookmarksFromHistory : getBookmarksFromHistory ,
701725 getAllBookmarksFromHistory : getAllBookmarksFromHistory ,
0 commit comments