1
0
mirror of https://github.com/nerzhul/ocsms.git synced 2025-06-07 07:56:23 +00:00

Prepare api v2 calls to retrieve messages from the server

This commit is contained in:
Loic Blot 2016-05-12 23:05:58 +02:00
parent facbe2a7d7
commit d6818be568
3 changed files with 61 additions and 6 deletions

View File

@ -26,11 +26,16 @@ $application->registerRoutes($this, array('routes' => array(
array('name' => 'settings#set_messagelimit', 'url'=> '/set/msglimit', 'verb' => 'POST'), array('name' => 'settings#set_messagelimit', 'url'=> '/set/msglimit', 'verb' => 'POST'),
array('name' => 'settings#set_notification_state', 'url'=> '/set/notification_state', '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#get_api_version', 'url' => '/get/apiversion', 'verb' => 'GET'), // Android APIv1
array('name' => 'api#push', 'url' => '/push', 'verb' => 'POST'), // Android API array('name' => 'api#push', 'url' => '/push', 'verb' => 'POST'), // Android API
array('name' => 'api#replace', 'url' => '/replace', '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', '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_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#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
))); )));

View File

@ -13,6 +13,7 @@ namespace OCA\OcSms\Controller;
use \OCP\IRequest; use \OCP\IRequest;
use \OCP\AppFramework\Http;
use \OCP\AppFramework\Controller; use \OCP\AppFramework\Controller;
use \OCP\AppFramework\Http\JSONResponse; use \OCP\AppFramework\Http\JSONResponse;
@ -45,7 +46,7 @@ class ApiController extends Controller {
/** /**
* @NoAdminRequired * @NoAdminRequired
* @NoCSRFRequired * @NoCSRFRequired
* *
* This function is used by API v1 * This function is used by API v1
* Phone will compare its own message list with this * Phone will compare its own message list with this
* message list and send the missing messages * message list and send the missing messages
@ -59,7 +60,7 @@ class ApiController extends Controller {
/** /**
* @NoAdminRequired * @NoAdminRequired
* @NoCSRFRequired * @NoCSRFRequired
* *
* This function is used by API v2 * This function is used by API v2
* Phone will get this ID to push recent messages * Phone will get this ID to push recent messages
* This call will be used combined with retrieveAllIds * This call will be used combined with retrieveAllIds
@ -83,8 +84,7 @@ class ApiController extends Controller {
* @NoAdminRequired * @NoAdminRequired
* @NoCSRFRequired * @NoCSRFRequired
* *
* This function is used by API v2 * API v2
* Phone will get this list to generate a ListView
*/ */
public function getAllStoredPhoneNumbers () { public function getAllStoredPhoneNumbers () {
$phoneList = $this->smsMapper->getAllPhoneNumbers($this->userId); $phoneList = $this->smsMapper->getAllPhoneNumbers($this->userId);
@ -114,7 +114,42 @@ class ApiController extends Controller {
$this->smsMapper->writeToDB($this->userId, $smsDatas, true); $this->smsMapper->writeToDB($this->userId, $smsDatas, true);
return new JSONResponse(array("status" => true, "msg" => "OK")); 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) { private function checkPushStructure ($smsCount, $smsDatas) {
if ($smsCount != count($smsDatas)) { if ($smsCount != count($smsDatas)) {

View File

@ -157,6 +157,21 @@ class SmsMapper extends Mapper {
return $messageList; 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) { public function countMessagesForPhoneNumber ($userId, $phoneNumber, $country) {
$cnt = 0; $cnt = 0;
$phlst = $this->getAllPhoneNumbersForFPN ($userId, $phoneNumber, $country); $phlst = $this->getAllPhoneNumbersForFPN ($userId, $phoneNumber, $country);