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:
parent
facbe2a7d7
commit
d6818be568
@ -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
|
||||
)));
|
||||
|
@ -13,6 +13,7 @@ namespace OCA\OcSms\Controller;
|
||||
|
||||
|
||||
use \OCP\IRequest;
|
||||
use \OCP\AppFramework\Http;
|
||||
use \OCP\AppFramework\Controller;
|
||||
use \OCP\AppFramework\Http\JSONResponse;
|
||||
|
||||
@ -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);
|
||||
@ -116,6 +116,41 @@ class ApiController extends Controller {
|
||||
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)) {
|
||||
$this->errorMsg = "Error: sms count invalid";
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user