Don't overload the server with killer queries.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Sat, 26 Feb 2011 08:09:35 +0000 (09:09 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Sat, 26 Feb 2011 08:09:35 +0000 (09:09 +0100)
Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
htdocs/javascript/xorg.js
modules/api.php

index 704fed1..bc822ba 100644 (file)
@@ -907,8 +907,9 @@ function sendTestEmail(token, hruid)
 
             function formatProfile(profile) {
                 var data = $.tmpl('quickMinifiche', profile);
-                data.find('a').popWin(840, 600).click(function() { $popup.hide(); });
-                data.click(data.find('a').click());
+                data.find('a').popWin(840, 600).click(function() {
+                    $popup.hide();
+                });
                 return data;
             }
 
@@ -919,7 +920,11 @@ function sendTestEmail(token, hruid)
             function doUpdatePopup()
             {
                 var quick = $(this).val();
-                if (disabled) {
+                if (query !== null) {
+                    query.abort();
+                }
+                if (disabled || quick.length < 3) {
+                    previous = quick;
                     $popup.hide();
                     return true;
                 }
@@ -927,14 +932,10 @@ function sendTestEmail(token, hruid)
                     $popup.show();
                     return true;
                 }
-                if (query !== null) {
-                    query.abort();
-                    query = null;
-                }
                 query = $.xapi(url, $.extend({ 'quick': quick }, args), function(data) {
                     query = null;
                     $popup.empty();
-                    if (data.profile_count > 10) {
+                    if (data.profile_count > 10 || data.profile_count < 0) {
                         $popup.hide();
                         return;
                     }
@@ -948,7 +949,7 @@ function sendTestEmail(token, hruid)
                 setTimeout(function() {
                     updatePopup = doUpdatePopup;
                     if (pending) {
-                        updatePopup();
+                        updatePopup.call($this.get(0));
                     }
                     pending = false;
                 }, 500);
index 5caedbf..ecdd645 100644 (file)
@@ -56,6 +56,11 @@ class ApiModule extends PlModule
             $page->trigError('Malformed search query');
             return PL_BAD_REQUEST;
         }
+        if (strlen(trim($payload['quick'])) < 3) {
+            $page->jsonAssign('profile_count', -1);
+            $page->jsonAssign('profiles', array());
+            return PL_JSON;
+        }
         Env::set('quick', $payload['quick']);
         foreach (array('with_soundex', 'exact') as $key) {
             if (isset($payload[$key])) {