@@ -15,28 +15,35 @@ const maxFileAgeInDays = 90;
1515var router = express . Router ( ) ;
1616
1717router . get ( '/' , async function ( req , res , next ) {
18- const lensId = req ?. query ?. uid && parseInt ( req . query . uid ) || false ;
18+ const lensId = req ?. query ?. uid || false ;
1919 if ( ! lensId ) {
2020 return res . json ( { } ) ;
2121 }
2222
23- const unlock = await DB . getLensUnlock ( lensId ) ;
24- if ( unlock && unlock [ 0 ] ) {
25- if ( unlock [ 0 ] . lens_id && unlock [ 0 ] . lens_url ) {
26- // trigger re-download to catch missing files automatically
27- await Util . downloadUnlock ( unlock [ 0 ] . lens_id , unlock [ 0 ] . lens_url ) ;
23+ if ( Util . isLensId ( lensId ) ) {
24+ const unlock = await DB . getLensUnlock ( lensId ) ;
25+ if ( unlock && unlock [ 0 ] ) {
26+ if ( unlock [ 0 ] . lens_id && unlock [ 0 ] . lens_url ) {
27+ // trigger re-download to catch missing files automatically
28+ await Util . downloadUnlock ( unlock [ 0 ] . lens_id , unlock [ 0 ] . lens_url ) ;
2829
29- wayback . saveOutdatedUrl ( unlock [ 0 ] . lens_url , maxFileAgeInDays ) ;
30+ wayback . saveOutdatedUrl ( unlock [ 0 ] . lens_url , maxFileAgeInDays ) ;
3031
31- return res . json ( Util . modifyResponseURLs ( unlock [ 0 ] ) ) ;
32- } else {
33- console . warn ( `[Warning] Unlock Download URL is missing: ${ lensId } ` ) ;
32+ return res . json ( Util . modifyResponseURLs ( unlock [ 0 ] ) ) ;
33+ } else {
34+ console . warn ( `[Warning] Unlock Download URL is missing: ${ lensId } ` ) ;
35+ }
36+ }
37+
38+ const remoteUnlock = await getRemoteUnlockByLensId ( lensId ) ;
39+ if ( remoteUnlock ) {
40+ return res . json ( remoteUnlock ) ;
3441 }
3542 }
3643
37- const remoteUnlock = await getRemoteUnlockByLensId ( lensId ) ;
38- if ( remoteUnlock ) {
39- return res . json ( remoteUnlock ) ;
44+ const cacheUnlock = await getCacheUnlockByLensId ( lensId ) ;
45+ if ( cacheUnlock ) {
46+ return res . json ( cacheUnlock ) ;
4047 }
4148
4249 console . info ( `[Info] 😕 This lens cannot currently be activated: ${ lensId } ` ) ;
@@ -53,32 +60,45 @@ async function getRemoteUnlockByLensId(lensId) {
5360 return unlock ;
5461 }
5562 }
63+ } catch ( e ) {
64+ console . error ( e ) ;
65+ }
5666
67+ return null ;
68+ }
69+
70+ async function getCacheUnlockByLensId ( lensId ) {
71+ try {
5772 if ( useWebSource ) {
5873 if ( Cache . Top . has ( lensId ) ) {
5974 const topLens = Cache . Top . get ( lensId ) ;
60- DB . insertLens ( topLens ) ;
61- DB . insertUnlock ( topLens ) ;
62- return topLens ;
75+ return unlockLens ( topLens ) ;
6376 }
6477
6578 if ( Cache . Search . has ( lensId ) ) {
6679 let lens = Cache . Search . get ( lensId ) ;
6780 if ( ! lens ?. uuid ) {
6881 const dbLens = await DB . getSingleLens ( lensId ) ;
69- lens = dbLens ?. [ 0 ] ? { ...lens , ...dbLens } : lens ;
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 ;
7088 }
7189
7290 if ( lens ?. uuid && ! lens ?. lens_url ) {
7391 const webLens = await Web . getUnlockByHash ( lens . uuid ) ;
74- lens = webLens ? { ... webLens , ... lens } : lens ;
92+ lens = ( webLens ) ? Util . mergeLens ( webLens , lens ) : lens ;
7593 }
7694
77- if ( lens ?. lens_url ) {
78- DB . insertLens ( lens ) ;
79- DB . insertUnlock ( lens ) ;
80- return lens ;
95+ if ( lens ?. lens_id && ! Util . isLensId ( lens . lens_id ) && Util . isLensId ( lens . unlockable_id ) ) {
96+ lens . lens_id = lens . unlockable_id ;
8197 }
98+
99+ Cache . Search . set ( lensId , lens ) ;
100+
101+ return unlockLens ( lens ) ;
82102 }
83103 }
84104 } catch ( e ) {
@@ -88,4 +108,14 @@ async function getRemoteUnlockByLensId(lensId) {
88108 return null ;
89109}
90110
111+ function unlockLens ( lens ) {
112+ if ( lens ?. lens_url && Util . isLensId ( lens . unlockable_id ) && Util . isLensId ( lens . lens_id ) ) {
113+ DB . insertLens ( lens ) ;
114+ DB . insertUnlock ( lens ) ;
115+ return lens ;
116+ }
117+
118+ return null ;
119+ }
120+
91121export default router ;
0 commit comments