Przeglądaj źródła

ensure code can be tested, handle async indexedDB, don't let user add same URL more then once

El RIDO 6 lat temu
rodzic
commit
ea93c474ea
4 zmienionych plików z 50 dodań i 22 usunięć
  1. 42 16
      js/privatebin.js
  2. 6 4
      js/test/Memory.js
  3. 1 1
      tpl/bootstrap.php
  4. 1 1
      tpl/page.php

+ 42 - 16
js/privatebin.js

@@ -4336,9 +4336,27 @@ jQuery.PrivateBin = (function($, RawDeflate) {
     const Memory = (function (document, window) {
         const me = {};
 
-        let urls = [],
+        let pastes = [],
             db;
 
+        /**
+         * checks if the given URL was already memorized
+         *
+         * @name   Memory.isInMemory
+         * @private
+         * @function
+         * @param  {string} pasteUrl
+         * @return {bool}
+         */
+        function isInMemory(pasteUrl)
+        {
+            return pastes.filter(
+                function(memorizedPaste) {
+                    return memorizedPaste.url === pasteUrl;
+                }
+            ).length > 0;
+        }
+
         /**
          * called after successfully connecting to the indexedDB
          *
@@ -4352,10 +4370,13 @@ jQuery.PrivateBin = (function($, RawDeflate) {
             memory.openCursor().onsuccess = function(e) {
                 let cursor = e.target.result;
                 if (cursor) {
-                    urls.push(cursor.value.url);
+                    pastes.push(cursor.value);
                     cursor.continue();
                 } else {
                     me.refreshList();
+                    if (isInMemory(window.location.href)) {
+                        $('#rememberbutton').addClass('disabled');
+                    }
                 }
             };
         }
@@ -4370,12 +4391,8 @@ jQuery.PrivateBin = (function($, RawDeflate) {
          */
         me.add = function(pasteUrl)
         {
-            if (!window.indexedDB || !db) {
-                return false;
-            }
             const url = new URL(pasteUrl);
-            const memory = db.transaction('pastes', 'readwrite').objectStore('pastes');
-            const request = memory.add({
+            const newPaste = {
                 'https': url.protocol == 'https:',
                 'service': url.hostname + url.pathname,
                 'pasteid': url.search.replace(/^\?+/, ''),
@@ -4383,11 +4400,17 @@ jQuery.PrivateBin = (function($, RawDeflate) {
                 // we store the full URL as it may contain additonal information
                 // required to open the paste, like port, username and password
                 'url': pasteUrl
-            });
-            request.onsuccess = function(e) {
-                urls.push(pasteUrl);
-                me.refreshList();
             };
+            // don't add an already memorized paste
+            if (isInMemory(pasteUrl)) {
+                return false;
+            }
+            pastes.push(newPaste);
+            if (!window.indexedDB || !db) {
+                return false;
+            }
+            const memory = db.transaction('pastes', 'readwrite').objectStore('pastes');
+            const request = memory.add(newPaste);
             return true;
         };
 
@@ -4400,11 +4423,14 @@ jQuery.PrivateBin = (function($, RawDeflate) {
         me.refreshList = function()
         {
             const $tbody = $('#sidebar-wrapper table tbody')[0];
+            if (!$tbody) {
+                return;
+            }
             $tbody.textContent = '';
-            urls.forEach(function(url) {
+            pastes.forEach(function(paste) {
                 const row  = document.createElement('tr'),
                       cell = document.createElement('td');
-                cell.textContent = url;
+                cell.textContent = paste.url;
                 row.appendChild(cell);
                 $tbody.appendChild(row);
             });
@@ -4420,7 +4446,7 @@ jQuery.PrivateBin = (function($, RawDeflate) {
          */
         me.init = function()
         {
-            urls = [];
+            pastes = [];
             if (!window.indexedDB) {
                 $('#menu-toggle').hide();
                 return;
@@ -4455,8 +4481,8 @@ jQuery.PrivateBin = (function($, RawDeflate) {
             });
 
             $('#rememberbutton').on('click', function(e) {
-                me.add(window.location.href);
-                $('#menu-toggle').click();
+                if (me.add(window.location.href))
+                    $('#menu-toggle').click();
             });
         };
 

+ 6 - 4
js/test/Memory.js

@@ -2,7 +2,7 @@
 const common = require('../common');
 
 describe('Memory', function () {
-    describe('add', function () {
+    describe('add & refreshList', function () {
         this.timeout(30000);
 
         jsc.property(
@@ -24,8 +24,10 @@ describe('Memory', function () {
                 // clear cache, then the first cell will match what we add
                 $.PrivateBin.Memory.init();
                 $.PrivateBin.Memory.add(expected);
+                $.PrivateBin.Memory.refreshList();
+                const result = $('#sidebar-wrapper table tbody tr td')[0].textContent;
                 clean();
-                return true;
+                return result === expected;
             }
         );
     });
@@ -35,8 +37,8 @@ describe('Memory', function () {
             'enables toggling the memory sidebar',
             function() {
                 $('body').html(
-                    '<main><div id="sidebar-wrapper"><table><tbody></tbody>' +
-                    '</table></div><button id="menu-toggle"></button></main>'
+                    '<main><div id="sidebar-wrapper"></div>' +
+                    '<button id="menu-toggle"></button></main>'
                 );
                 assert.ok(!$('main').hasClass('toggled'));
 

+ 1 - 1
tpl/bootstrap.php

@@ -72,7 +72,7 @@ endif;
 ?>
 		<script type="text/javascript" data-cfasync="false" src="js/purify-2.0.8.js" integrity="sha512-QwcEKGuEmKtMguCO9pqNtUtZqq9b/tJ8gNr5qhY8hykq3zKTlDOvpZAmf6Rs8yH35Bz1ZdctUjj2qEWxT5aXCg==" crossorigin="anonymous"></script>
 		<script type="text/javascript" data-cfasync="false" src="js/legacy.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-LYos+qXHIRqFf5ZPNphvtTB0cgzHUizu2wwcOwcwz/VIpRv9lpcBgPYz4uq6jx0INwCAj6Fbnl5HoKiLufS2jg==" crossorigin="anonymous"></script>
-		<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-pydUuue2tfUH/5qy4OcIQtlWnWthLBNh4AqXjDlWoEoanFG10LUDEIdr6H7MIrW1RfxJtReq8qoJRTSRRCo9vg==" crossorigin="anonymous"></script>
+		<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-+gTE0A/WPe61SJOCwyY3Xbo7Hyfs8fhnYn4OLiqBJpW6qnWVa/o0ycwwZ2iH6fM8XgpF4fFExMswE45cyLS/AA==" crossorigin="anonymous"></script>
 		<!-- icon -->
 		<link rel="apple-touch-icon" href="img/apple-touch-icon.png?<?php echo rawurlencode($VERSION); ?>" sizes="180x180" />
 		<link rel="icon" type="image/png" href="img/favicon-32x32.png?<?php echo rawurlencode($VERSION); ?>" sizes="32x32" />

+ 1 - 1
tpl/page.php

@@ -50,7 +50,7 @@ endif;
 ?>
 		<script type="text/javascript" data-cfasync="false" src="js/purify-2.0.8.js" integrity="sha512-QwcEKGuEmKtMguCO9pqNtUtZqq9b/tJ8gNr5qhY8hykq3zKTlDOvpZAmf6Rs8yH35Bz1ZdctUjj2qEWxT5aXCg==" crossorigin="anonymous"></script>
 		<script type="text/javascript" data-cfasync="false" src="js/legacy.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-LYos+qXHIRqFf5ZPNphvtTB0cgzHUizu2wwcOwcwz/VIpRv9lpcBgPYz4uq6jx0INwCAj6Fbnl5HoKiLufS2jg==" crossorigin="anonymous"></script>
-		<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-pydUuue2tfUH/5qy4OcIQtlWnWthLBNh4AqXjDlWoEoanFG10LUDEIdr6H7MIrW1RfxJtReq8qoJRTSRRCo9vg==" crossorigin="anonymous"></script>
+		<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-+gTE0A/WPe61SJOCwyY3Xbo7Hyfs8fhnYn4OLiqBJpW6qnWVa/o0ycwwZ2iH6fM8XgpF4fFExMswE45cyLS/AA==" crossorigin="anonymous"></script>
 		<!-- icon -->
 		<link rel="apple-touch-icon" href="img/apple-touch-icon.png?<?php echo rawurlencode($VERSION); ?>" sizes="180x180" />
 		<link rel="icon" type="image/png" href="img/favicon-32x32.png?<?php echo rawurlencode($VERSION); ?>" sizes="32x32" />