1
0
mirror of https://github.com/nerzhul/ocsms.git synced 2025-07-22 17:35:52 +00:00

APIv2 improvements

* prepare a table for messaging sending queue
* drop retrieveAllIdsWithStatus and related API call (unused and dropped from Android app devel version)
* reorganise api controller
This commit is contained in:
Loic Blot 2016-05-12 23:18:25 +02:00
parent d6818be568
commit 1b2a171b89
4 changed files with 91 additions and 80 deletions

View File

@ -104,6 +104,38 @@
</field>
</declaration>
</table>
<table>
<name>*dbprefix*ocsms_sendmessage_queue</name>
<declaration>
<field>
<name>id</name>
<type>integer</type>
<default>0</default>
<notnull>true</notnull>
<autoincrement>1</autoincrement>
<length>10</length>
<primary>true</primary>
</field>
<field>
<name>user_id</name>
<type>text</type>
<notnull>true</notnull>
<length>64</length>
</field>
<field>
<name>sms_address</name>
<type>text</type>
<notnull>true</notnull>
<length>64</length>
</field>
<field>
<name>sms_msg</name>
<type>text</type>
<notnull>true</notnull>
<length>2048</length>
</field>
</declaration>
</table>
<table>
<name>*dbprefix*ocsms_config</name>
<declaration>

View File

@ -31,11 +31,11 @@ $application->registerRoutes($this, array('routes' => array(
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
// API v2
array('name' => 'api#get_all_stored_phone_numbers', 'url' => '/api/v2/get/phones/numberlist', 'verb' => 'GET'), // Android APIv2
array('name' => 'api#get_all_stored_phone_numbers', 'url' => '/api/v2/phones/list', '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
array('name' => 'api#fetch_messages_to_send', 'url' => '/api/v2/messages/sendqueue', 'verb' => 'GET'), // Android APIv2
)));

View File

@ -71,26 +71,6 @@ class ApiController extends Controller {
return new JSONResponse(array("timestamp" => $ts));
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function retrieveAllIdsWithStatus () {
$smsList = $this->smsMapper->getAllIdsWithStatus($this->userId);
return new JSONResponse(array("smslist" => $smsList));
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* API v2
*/
public function getAllStoredPhoneNumbers () {
$phoneList = $this->smsMapper->getAllPhoneNumbers($this->userId);
return new JSONResponse(array("phoneList" => $phoneList));
}
/**
* @NoAdminRequired
* @NoCSRFRequired
@ -116,41 +96,6 @@ 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";
@ -201,4 +146,61 @@ class ApiController extends Controller {
}
return true;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* API v2
*/
public function getAllStoredPhoneNumbers () {
$phoneList = $this->smsMapper->getAllPhoneNumbers($this->userId);
return new JSONResponse(array("phoneList" => $phoneList));
}
/**
* @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()));
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* APIv2
*/
public function fetchMessagesToSend() {
// @TODO
return new JSONResponse(array("messages" => array()));
}
}

View File

@ -58,29 +58,6 @@ class SmsMapper extends Mapper {
return $smsList;
}
public function getAllIdsWithStatus ($userId) {
$query = \OCP\DB::prepare('SELECT sms_id, sms_type, sms_mailbox FROM ' .
'*PREFIX*ocsms_smsdatas WHERE user_id = ?');
$result = $query->execute(array($userId));
$smsList = array();
while($row = $result->fetchRow()) {
// This case may not arrive, but we test if the DB is consistent
if (!in_array($row["sms_mailbox"], SmsMapper::$mailboxNames)) {
continue;
}
$mbox = SmsMapper::$mailboxNames[$row["sms_mailbox"]];
if (!isset($smsList[$mbox])) {
$smsList[$mbox] = array();
}
if (!isset($smsList[$mbox][$row["sms_id"]])) {
$smsList[$mbox][$row["sms_id"]] = $row["sms_type"];
}
}
return $smsList;
}
public function getLastTimestamp ($userId) {
$query = \OCP\DB::prepare('SELECT max(sms_date) as mx FROM ' .
'*PREFIX*ocsms_smsdatas WHERE user_id = ?');