@@ -307,6 +307,32 @@ export const fetchTorrent = (tracker) => async (req, res, next) => {
307307 } ,
308308 } ,
309309 { $unwind : { path : "$uploadedBy" , preserveNullAndEmptyArrays : true } } ,
310+ {
311+ $lookup : {
312+ from : "users" ,
313+ as : "fetchedBy" ,
314+ let : { torrentId : "$_id" } ,
315+ pipeline : [
316+ { $match : { $expr : { $eq : [ "$_id" , req . userId ] } } } ,
317+ {
318+ $project : {
319+ bookmarks : 1 ,
320+ } ,
321+ } ,
322+ {
323+ $addFields : {
324+ bookmarked : { $in : [ "$$torrentId" , "$bookmarks" ] } ,
325+ } ,
326+ } ,
327+ {
328+ $project : {
329+ bookmarked : 1 ,
330+ } ,
331+ } ,
332+ ] ,
333+ } ,
334+ } ,
335+ { $unwind : { path : "$fetchedBy" , preserveNullAndEmptyArrays : true } } ,
310336 {
311337 $lookup : {
312338 from : "comments" ,
@@ -428,6 +454,7 @@ export const getTorrentsPage = async ({
428454 category,
429455 source,
430456 tag,
457+ uploadedBy,
431458 userId,
432459 tracker,
433460} ) => {
@@ -485,11 +512,11 @@ export const getTorrentsPage = async ({
485512 } ,
486513 ]
487514 : [ ] ) ,
488- ...( userId
515+ ...( uploadedBy
489516 ? [
490517 {
491518 $match : {
492- uploadedBy : userId ,
519+ uploadedBy,
493520 } ,
494521 } ,
495522 ]
@@ -525,6 +552,32 @@ export const getTorrentsPage = async ({
525552 preserveNullAndEmptyArrays : true ,
526553 } ,
527554 } ,
555+ {
556+ $lookup : {
557+ from : "users" ,
558+ as : "fetchedBy" ,
559+ let : { torrentId : "$_id" } ,
560+ pipeline : [
561+ { $match : { $expr : { $eq : [ "$_id" , userId ] } } } ,
562+ {
563+ $project : {
564+ bookmarks : 1 ,
565+ } ,
566+ } ,
567+ {
568+ $addFields : {
569+ bookmarked : { $in : [ "$$torrentId" , "$bookmarks" ] } ,
570+ } ,
571+ } ,
572+ {
573+ $project : {
574+ bookmarked : 1 ,
575+ } ,
576+ } ,
577+ ] ,
578+ } ,
579+ } ,
580+ { $unwind : { path : "$fetchedBy" , preserveNullAndEmptyArrays : true } } ,
528581 ] ) ;
529582
530583 const [ count ] = await Torrent . aggregate ( [
@@ -592,7 +645,11 @@ export const listLatest = (tracker) => async (req, res, next) => {
592645 count = parseInt ( count ) || 25 ;
593646 count = Math . min ( count , 100 ) ;
594647 try {
595- const { torrents } = await getTorrentsPage ( { limit : count , tracker } ) ;
648+ const { torrents } = await getTorrentsPage ( {
649+ limit : count ,
650+ userId : req . userId ,
651+ tracker,
652+ } ) ;
596653 res . json ( torrents ) ;
597654 } catch ( e ) {
598655 next ( e ) ;
@@ -608,6 +665,7 @@ export const searchTorrents = (tracker) => async (req, res, next) => {
608665 category,
609666 source,
610667 tag,
668+ userId : req . userId ,
611669 tracker,
612670 } ) ;
613671 res . json ( torrents ) ;
@@ -734,3 +792,29 @@ export const toggleFreeleech = async (req, res, next) => {
734792 next ( e ) ;
735793 }
736794} ;
795+
796+ export const toggleBookmark = async ( req , res , next ) => {
797+ const { infoHash } = req . params ;
798+ try {
799+ const torrent = await Torrent . findOne ( { infoHash } ) . lean ( ) ;
800+
801+ if ( ! torrent ) {
802+ res . status ( 404 ) . send ( "Torrent could not be found" ) ;
803+ return ;
804+ }
805+
806+ const user = await User . findOne ( { _id : req . userId } ) . lean ( ) ;
807+
808+ const isBookmarked = ( await user . bookmarks ?. length )
809+ ? user . bookmarks . map ( ( b ) => b . toString ( ) ) . includes ( torrent . _id . toString ( ) )
810+ : false ;
811+
812+ await User . findOneAndUpdate (
813+ { _id : req . userId } ,
814+ { [ isBookmarked ? "$pull" : "$addToSet" ] : { bookmarks : torrent . _id } }
815+ ) ;
816+ res . sendStatus ( 200 ) ;
817+ } catch ( e ) {
818+ next ( e ) ;
819+ }
820+ } ;
0 commit comments