From d6818be568ba9be09d386a40af12bb57e0e7ab65 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Thu, 12 May 2016 23:05:58 +0200 Subject: [PATCH] Prepare api v2 calls to retrieve messages from the server --- appinfo/routes.php | 7 +++++- controller/apicontroller.php | 45 ++++++++++++++++++++++++++++++++---- db/smsmapper.php | 15 ++++++++++++ 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index e45319f..260502f 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -26,11 +26,16 @@ $application->registerRoutes($this, array('routes' => array( array('name' => 'settings#set_messagelimit', 'url'=> '/set/msglimit', 'verb' => 'POST'), array('name' => 'settings#set_notification_state', 'url'=> '/set/notification_state', 'verb' => 'POST'), + // Android API v1 doesn't have a version in the URL, be careful array('name' => 'api#get_api_version', 'url' => '/get/apiversion', 'verb' => 'GET'), // Android APIv1 array('name' => 'api#push', 'url' => '/push', 'verb' => 'POST'), // Android API array('name' => 'api#replace', 'url' => '/replace', 'verb' => 'POST'), // Android API array('name' => 'api#retrieve_all_ids', 'url' => '/get/smsidlist', 'verb' => 'GET'), // Android APIv1 array('name' => 'api#retrieve_all_ids_with_status', 'url' => '/get/smsidstate', 'verb' => 'GET'), // Android APIv1 array('name' => 'api#retrieve_last_timestamp', 'url' => '/get/lastmsgtime', 'verb' => 'GET'), // Android APIv1 - array('name' => 'api#get_all_stored_phone_numbers', 'url' => 'get/phones/numberlist', 'verb' => 'GET'), // Android APIv2 + + // API v2 + array('name' => 'api#get_all_stored_phone_numbers', 'url' => '/api/v2/get/phones/numberlist', 'verb' => 'GET'), // Android APIv2 + array('name' => 'api#fetch_messages', 'url' => '/api/v2/messages/{start}/{limit}', 'verb' => 'GET'), // Android APIv2 + array('name' => 'api#fetch_messages_for_number', 'url' => '/api/v2/messages/{phonenumber}/{start}/{limit}', 'verb' => 'GET'), // Android APIv2 ))); diff --git a/controller/apicontroller.php b/controller/apicontroller.php index 29ecf12..1f3ae59 100644 --- a/controller/apicontroller.php +++ b/controller/apicontroller.php @@ -13,6 +13,7 @@ namespace OCA\OcSms\Controller; use \OCP\IRequest; +use \OCP\AppFramework\Http; use \OCP\AppFramework\Controller; use \OCP\AppFramework\Http\JSONResponse; @@ -45,7 +46,7 @@ class ApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * + * * This function is used by API v1 * Phone will compare its own message list with this * message list and send the missing messages @@ -59,7 +60,7 @@ class ApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * + * * This function is used by API v2 * Phone will get this ID to push recent messages * This call will be used combined with retrieveAllIds @@ -83,8 +84,7 @@ class ApiController extends Controller { * @NoAdminRequired * @NoCSRFRequired * - * This function is used by API v2 - * Phone will get this list to generate a ListView + * API v2 */ public function getAllStoredPhoneNumbers () { $phoneList = $this->smsMapper->getAllPhoneNumbers($this->userId); @@ -114,7 +114,42 @@ class ApiController extends Controller { $this->smsMapper->writeToDB($this->userId, $smsDatas, true); return new JSONResponse(array("status" => true, "msg" => "OK")); - } + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * + * APIv2 + */ + public function fetchMessages($start, $limit) { + if (!is_numeric($start) || !is_numeric($limit) || $start < 0 || $limit <= 0) { + return new JSONResponse(array("msg" => "Invalid request"), \OCP\AppFramework\Http::STATUS_BAD_REQUEST); + } + + // Limit messages per fetch to prevent phone garbage collecting due to too many datas + if ($limit > 500) { + return new JSONResponse(array("msg" => "Too many messages requested"), 413); + } + + $messages = $this->smsMapper->getMessages($this->userId, $start, $limit); + return new JSONResponse(array("messages" => $messages)); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * + * APIv2 + */ + public function fetchMessagesForNumber($phoneNumber, $start, $limit) { + if (!is_numeric($start) || !is_numeric($limit) || $start < 0 || $limit <= 0) { + return new JSONResponse(array("msg" => "Invalid request"), \OCP\AppFramework\Http::STATUS_BAD_REQUEST); + } + + // @TODO because multiple phone numbers can be same number with different formatting + return new JSONResponse(array("messages" => array())); + } private function checkPushStructure ($smsCount, $smsDatas) { if ($smsCount != count($smsDatas)) { diff --git a/db/smsmapper.php b/db/smsmapper.php index 96f1851..145f7d5 100644 --- a/db/smsmapper.php +++ b/db/smsmapper.php @@ -157,6 +157,21 @@ class SmsMapper extends Mapper { return $messageList; } + public function getMessages ($userId, $start, $limit) { + $messageList = array(); + + $query = \OCP\DB::prepare('SELECT sms_date, sms_msg, sms_type FROM ' . + '*PREFIX*ocsms_smsdatas WHERE user_id = ? LIMIT ?,?'); + $result = $query->execute(array($userId, $start, $limit)); + while ($row = $result->fetchRow()) { + $messageList[$row["sms_date"]] = array( + "msg" => $row["sms_msg"], + "type" => $row["sms_type"] + ); + } + return $messageList; + } + public function countMessagesForPhoneNumber ($userId, $phoneNumber, $country) { $cnt = 0; $phlst = $this->getAllPhoneNumbersForFPN ($userId, $phoneNumber, $country);