@@ -15,14 +15,14 @@ const maxFileAgeInDays = 90;
1515var router = express . Router ( ) ;
1616
1717router . get ( '/' , async function ( req , res , next ) {
18- const lensId = req ? .query ?. uid || false ;
18+ const lensId = req . query ?. uid || false ;
1919 if ( ! lensId ) {
2020 return res . json ( { } ) ;
2121 }
2222
2323 if ( Util . isLensId ( lensId ) ) {
2424 const unlock = await DB . getLensUnlock ( lensId ) ;
25- if ( unlock && unlock [ 0 ] ) {
25+ if ( unlock ?. [ 0 ] ) {
2626 if ( unlock [ 0 ] . lens_id && unlock [ 0 ] . lens_url ) {
2727 // trigger re-download to catch missing files automatically
2828 await Util . downloadUnlock ( unlock [ 0 ] . lens_id , unlock [ 0 ] . lens_url ) ;
@@ -41,9 +41,9 @@ router.get('/', async function (req, res, next) {
4141 }
4242 }
4343
44- const cacheUnlock = await getCacheUnlockByLensId ( lensId ) ;
45- if ( cacheUnlock ) {
46- return res . json ( cacheUnlock ) ;
44+ const webUnlock = await getWebUnlockByLensId ( lensId ) ;
45+ if ( webUnlock ) {
46+ return res . json ( webUnlock ) ;
4747 }
4848
4949 console . info ( `[Info] 😕 This lens cannot currently be activated: ${ lensId } ` ) ;
@@ -67,38 +67,26 @@ async function getRemoteUnlockByLensId(lensId) {
6767 return null ;
6868}
6969
70- async function getCacheUnlockByLensId ( lensId ) {
70+ async function getWebUnlockByLensId ( lensId ) {
7171 try {
7272 if ( useWebSource ) {
7373 if ( Cache . Top . has ( lensId ) ) {
74- const topLens = Cache . Top . get ( lensId ) ;
75- return unlockLens ( topLens ) ;
74+ const topLens = await unlockWebLens ( Cache . Top . get ( lensId ) ) ;
75+ if ( topLens ) {
76+ return topLens ;
77+ }
7678 }
7779
7880 if ( Cache . Search . has ( lensId ) ) {
79- let lens = Cache . Search . get ( lensId ) ;
80- if ( ! lens ?. uuid ) {
81- const dbLens = await DB . getSingleLens ( lensId ) ;
82- lens = ( dbLens ?. [ 0 ] ) ? Util . mergeLens ( dbLens , lens ) : lens ;
83- }
84-
85- if ( lens ?. uuid && ! Util . isLensId ( lensId ) ) {
86- const webLens = await Web . getLensByHash ( lens . uuid ) ;
87- lens = ( webLens ) ? Util . mergeLens ( webLens , lens ) : lens ;
88- }
89-
90- if ( lens ?. uuid && ! lens ?. lens_url ) {
91- const webLens = await Web . getUnlockByHash ( lens . uuid ) ;
92- lens = ( webLens ) ? Util . mergeLens ( webLens , lens ) : lens ;
81+ const searchLens = await unlockWebLens ( Cache . Search . get ( lensId ) ) ;
82+ if ( searchLens ) {
83+ return searchLens ;
9384 }
85+ }
9486
95- if ( lens ?. lens_id && ! Util . isLensId ( lens . lens_id ) && Util . isLensId ( lens . unlockable_id ) ) {
96- lens . lens_id = lens . unlockable_id ;
97- }
98-
99- Cache . Search . set ( lensId , lens ) ;
100-
101- return unlockLens ( lens ) ;
87+ const lens = await DB . getSingleLens ( lensId ) ;
88+ if ( lens ?. [ 0 ] ) {
89+ return await unlockWebLens ( lens [ 0 ] ) ;
10290 }
10391 }
10492 } catch ( e ) {
@@ -108,10 +96,30 @@ async function getCacheUnlockByLensId(lensId) {
10896 return null ;
10997}
11098
111- function unlockLens ( lens ) {
99+ async function unlockWebLens ( lens ) {
100+ if ( lens ?. unlockable_id && ! lens ?. uuid ) {
101+ const dbLens = await DB . getSingleLens ( lens . unlockable_id ) ;
102+ lens = ( dbLens ?. [ 0 ] ) ? Util . mergeLens ( dbLens [ 0 ] , lens ) : lens ;
103+ }
104+
105+ if ( lens ?. uuid && ! Util . isLensId ( lens . unlockable_id ) ) {
106+ const webLens = await Web . getLensByHash ( lens . uuid ) ;
107+ lens = ( webLens ) ? Util . mergeLens ( webLens , lens ) : lens ;
108+ }
109+
110+ if ( lens ?. uuid && ! lens ?. lens_url ) {
111+ const webLens = await Web . getUnlockByHash ( lens . uuid ) ;
112+ lens = ( webLens ) ? Util . mergeLens ( webLens , lens ) : lens ;
113+ }
114+
115+ if ( ! Util . isLensId ( lens ?. lens_id ) && Util . isLensId ( lens ?. unlockable_id ) ) {
116+ lens . lens_id = lens . unlockable_id ;
117+ }
118+
112119 if ( lens ?. lens_url && Util . isLensId ( lens . unlockable_id ) && Util . isLensId ( lens . lens_id ) ) {
113- DB . insertLens ( lens ) ;
114- DB . insertUnlock ( lens ) ;
120+ await DB . insertLens ( lens ) ;
121+ await DB . insertUnlock ( lens ) ;
122+
115123 return lens ;
116124 }
117125
0 commit comments