@@ -126,7 +126,7 @@ const Torrent = ({ token, torrent = {}, userId, userRole, uid }) => {
126126 const [ comments , setComments ] = useState ( torrent . comments ) ;
127127 const [ isFreeleech , setIsFreeleech ] = useState ( torrent . freeleech ) ;
128128 const [ hasGroup , setHasGroup ] = useState ( ! ! torrent . group ) ;
129- const [ bookmarked , setBookmarked ] = useState ( torrent . fetchedBy . bookmarked ) ;
129+ const [ bookmarked , setBookmarked ] = useState ( torrent . fetchedBy ? .bookmarked ) ;
130130
131131 const { addNotification } = useContext ( NotificationContext ) ;
132132 const { setLoading } = useContext ( LoadingContext ) ;
@@ -477,9 +477,20 @@ const Torrent = ({ token, torrent = {}, userId, userRole, uid }) => {
477477 ) }
478478 </ Text >
479479 < Box display = "flex" alignItems = "center" ml = { 3 } >
480- < Button onClick = { handleBookmark } variant = "secondary" px = "10px" mr = { 3 } >
481- { bookmarked ? < Bookmark size = { 18 } /> : < BookmarkEmpty size = { 18 } /> }
482- </ Button >
480+ { ! ! userId && (
481+ < Button
482+ onClick = { handleBookmark }
483+ variant = "secondary"
484+ px = "10px"
485+ mr = { 3 }
486+ >
487+ { bookmarked ? (
488+ < Bookmark size = { 18 } />
489+ ) : (
490+ < BookmarkEmpty size = { 18 } />
491+ ) }
492+ </ Button >
493+ ) }
483494 { ( userRole === "admin" || userId === torrent . uploadedBy . _id ) && (
484495 < >
485496 < Button
@@ -503,13 +514,19 @@ const Torrent = ({ token, torrent = {}, userId, userRole, uid }) => {
503514 { isFreeleech ? "Unset" : "Set" } freeleech
504515 </ Button >
505516 ) }
506- < Button
507- as = "a"
508- href = { `${ SQ_API_URL } /torrent/download/${ torrent . infoHash } /${ uid } ` }
509- target = "_blank"
510- >
511- Download .torrent
512- </ Button >
517+ { ! ! userId ? (
518+ < Button
519+ as = "a"
520+ href = { `${ SQ_API_URL } /torrent/download/${ torrent . infoHash } /${ uid } ` }
521+ target = "_blank"
522+ >
523+ Download .torrent
524+ </ Button >
525+ ) : (
526+ < Link href = "/login" passHref >
527+ < Button as = "a" > Log in to download</ Button >
528+ </ Link >
529+ ) }
513530 </ Box >
514531 </ Box >
515532 < Info
@@ -629,29 +646,43 @@ const Torrent = ({ token, torrent = {}, userId, userRole, uid }) => {
629646 display = "flex"
630647 borderBottom = "1px solid"
631648 borderColor = "border"
632- pb = { 5 }
649+ pb = { userId ? 5 : 0 }
633650 mb = { 5 }
634651 >
635- < Button onClick = { ( ) => handleVote ( "up" ) } variant = "noBackground" mr = { 2 } >
636- < Text icon = { Like } iconColor = { userVote === "up" ? "green" : undefined } >
637- { votes . up || 0 }
638- </ Text >
639- </ Button >
640- < Button
641- onClick = { ( ) => handleVote ( "down" ) }
642- variant = "noBackground"
643- mr = { 2 }
644- >
645- < Text
646- icon = { Dislike }
647- iconColor = { userVote === "down" ? "red" : undefined }
648- >
649- { votes . down || 0 }
650- </ Text >
651- </ Button >
652- < Button onClick = { ( ) => setShowReportModal ( true ) } variant = "noBackground" >
653- Report
654- </ Button >
652+ { ! ! userId && (
653+ < >
654+ < Button
655+ onClick = { ( ) => handleVote ( "up" ) }
656+ variant = "noBackground"
657+ mr = { 2 }
658+ >
659+ < Text
660+ icon = { Like }
661+ iconColor = { userVote === "up" ? "green" : undefined }
662+ >
663+ { votes . up || 0 }
664+ </ Text >
665+ </ Button >
666+ < Button
667+ onClick = { ( ) => handleVote ( "down" ) }
668+ variant = "noBackground"
669+ mr = { 2 }
670+ >
671+ < Text
672+ icon = { Dislike }
673+ iconColor = { userVote === "down" ? "red" : undefined }
674+ >
675+ { votes . down || 0 }
676+ </ Text >
677+ </ Button >
678+ < Button
679+ onClick = { ( ) => setShowReportModal ( true ) }
680+ variant = "noBackground"
681+ >
682+ Report
683+ </ Button >
684+ </ >
685+ ) }
655686 </ Box >
656687 < Box borderBottom = "1px solid" borderColor = "border" pb = { 5 } mb = { 5 } >
657688 < Box
@@ -674,11 +705,13 @@ const Torrent = ({ token, torrent = {}, userId, userRole, uid }) => {
674705 Remove this torrent
675706 </ Button >
676707 ) }
677- < Link href = { `/upload?groupWith=${ torrent . infoHash } ` } passHref >
678- < Button as = "a" ml = { 3 } >
679- Add a torrent
680- </ Button >
681- </ Link >
708+ { ! ! userId && (
709+ < Link href = { `/upload?groupWith=${ torrent . infoHash } ` } passHref >
710+ < Button as = "a" ml = { 3 } >
711+ Add a torrent
712+ </ Button >
713+ </ Link >
714+ ) }
682715 </ Box >
683716 </ Box >
684717 { torrent . groupTorrents . length && hasGroup ? (
@@ -693,15 +726,16 @@ const Torrent = ({ token, torrent = {}, userId, userRole, uid }) => {
693726 < Text as = "h2" mb = { 4 } >
694727 Comments
695728 </ Text >
696- < form onSubmit = { handleComment } >
729+ < form onSubmit = { userId ? handleComment : undefined } >
697730 < Input
698731 ref = { commentInputRef }
699732 name = "comment"
700733 label = "Post a comment"
701734 rows = "5"
735+ disabled = { ! userId }
702736 mb = { 4 }
703737 />
704- < Button display = "block" ml = "auto" >
738+ < Button disabled = { ! userId } display = "block" ml = "auto" >
705739 Post
706740 </ Button >
707741 </ form >
@@ -780,15 +814,23 @@ const Torrent = ({ token, torrent = {}, userId, userRole, uid }) => {
780814} ;
781815
782816export const getServerSideProps = withAuthServerSideProps (
783- async ( { token, userId, fetchHeaders, query : { infoHash } } ) => {
784- if ( ! token ) return { props : { } } ;
817+ async ( {
818+ token,
819+ userId,
820+ fetchHeaders,
821+ isPublicAccess,
822+ query : { infoHash } ,
823+ } ) => {
824+ if ( ! token && ! isPublicAccess ) return { props : { } } ;
785825
786826 const {
787827 publicRuntimeConfig : { SQ_API_URL } ,
788828 serverRuntimeConfig : { SQ_JWT_SECRET } ,
789829 } = getConfig ( ) ;
790830
791- const { id, role } = jwt . verify ( token , SQ_JWT_SECRET ) ;
831+ const { id, role } = token
832+ ? jwt . verify ( token , SQ_JWT_SECRET )
833+ : { id : null , role : null } ;
792834
793835 try {
794836 const torrentRes = await fetch ( `${ SQ_API_URL } /torrent/info/${ infoHash } ` , {
@@ -810,7 +852,8 @@ export const getServerSideProps = withAuthServerSideProps(
810852 if ( e === "banned" ) throw "banned" ;
811853 return { props : { } } ;
812854 }
813- }
855+ } ,
856+ true
814857) ;
815858
816859export default Torrent ;
0 commit comments