diff --git a/appinfo/routes.php b/appinfo/routes.php index 953ea24..69176ef 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -21,5 +21,6 @@ $application->registerRoutes($this, array('routes' => array( array('name' => 'sms#retrieve_all_ids_with_status', 'url' => '/get/smsidstate', 'verb' => 'GET'), array('name' => 'sms#retrieve_all_peers', 'url' => '/get/peerlist', 'verb' => 'GET'), array('name' => 'sms#get_conversation', 'url' => '/get/conversation', 'verb' => 'GET'), + array('name' => 'sms#check_new_messages', 'url' => '/get/new_messages', 'verb' => 'GET'), array('name' => 'sms#get_api_version', 'url' => '/get/apiversion', 'verb' => 'GET'), ))); diff --git a/controller/smscontroller.php b/controller/smscontroller.php index 9e14c52..11e4d2d 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -170,6 +170,28 @@ class SmsController extends Controller { "phoneNumbers" => $phoneNumbers, "msgCount" => $msgCount)); } + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function checkNewMessages($lastDate) { + $phoneList = $this->smsMapper->getNewMessagesCountForAllPhonesNumbers($this->userId, $lastDate); + $contactsSrc = $this->app->getContacts(); + $contacts = array(); + + $countPhone = count($phoneList); + foreach ($phoneList as $number => $ts) { + $fmtPN = preg_replace("#[ ]#","/", $number); + if (isset($contactsSrc[$fmtPN])) { + $fmtPN2 = preg_replace("#\/#","", $fmtPN); + $contacts[$fmtPN] = $contactsSrc[$fmtPN]; + $contacts[$fmtPN2] = $contactsSrc[$fmtPN]; + } + } + + return new JSONResponse(array("phonelist" => $phoneList, "contacts" => $contacts)); + } + /** * @NoAdminRequired */ diff --git a/db/smsmapper.php b/db/smsmapper.php index 79a6281..c3535fe 100644 --- a/db/smsmapper.php +++ b/db/smsmapper.php @@ -133,6 +133,23 @@ class SmsMapper extends Mapper { return $phoneList; } + public function getNewMessagesCountForAllPhonesNumbers($userId, $lastDate) { + $sql = 'SELECT sms_address,count(sms_date) as ct FROM ' . + '*PREFIX*ocsms_smsdatas WHERE user_id = ? AND sms_mailbox IN (?,?) ' . + 'AND sms_date > ? GROUP BY sms_address'; + + $query = \OCP\DB::prepare($sql); + $result = $query->execute(array($userId, 0, 1, $lastDate)); + + $phoneList = array(); + while ($row = $result->fetchRow()) { + $phoneNumber = preg_replace("#[ ]#", "/", $row["sms_address"]); + if (!in_array($phoneNumber, $phoneList)) { + $phoneList[$phoneNumber] = $row["ct"]; + } + } + return $phoneList; + } public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) { \OCP\DB::beginTransaction(); diff --git a/js/script.js b/js/script.js index a5e28cc..2f3c65f 100644 --- a/js/script.js +++ b/js/script.js @@ -63,6 +63,53 @@ var refreshConversation = function() { ); }; +var checkNewMessages = function() { + $.getJSON(OC.generateUrl('/apps/ocsms/get/new_messages'), + { 'lastDate': lastMsgDate }, + function(jsondata, status) { + var peerListBuf = ''; + var bufferedContacts = []; + + $.each(jsondata['phonelist'], function(id, val) { + var fn, peerLabel, idxVal; + idxVal = id.replace(/\//g,' '); + idxVal2 = idxVal.replace('/ /g',''); + if (typeof jsondata['contacts'][id] == 'undefined') { + fn = ''; + peerLabel = id; + } + else { + fn = jsondata['contacts'][id]; + peerLabel = fn; + } + if ($.inArray(peerLabel, bufferedContacts) == -1) { + $("a[mailbox-label='" + peerLabel + "']").remove(); + peerListBuf = '