mirror of
https://github.com/nerzhul/ocsms.git
synced 2025-06-08 00:16:24 +00:00
Add new function to fetch all SMS friends
This commit is contained in:
parent
3f712180fb
commit
169302516b
@ -5,6 +5,6 @@
|
|||||||
<description>Owncloud SMS app</description>
|
<description>Owncloud SMS app</description>
|
||||||
<licence>AGPL</licence>
|
<licence>AGPL</licence>
|
||||||
<author>Loic Blot</author>
|
<author>Loic Blot</author>
|
||||||
<version>0.1.7</version>
|
<version>0.1.8</version>
|
||||||
<requiremin>7</requiremin>
|
<requiremin>7</requiremin>
|
||||||
</info>
|
</info>
|
||||||
|
@ -23,7 +23,7 @@ class SmsController extends Controller {
|
|||||||
private $userId;
|
private $userId;
|
||||||
private $smsMapper;
|
private $smsMapper;
|
||||||
private $errorMsg;
|
private $errorMsg;
|
||||||
|
|
||||||
public function __construct ($appName, IRequest $request, $userId, SmsMapper $mapper){
|
public function __construct ($appName, IRequest $request, $userId, SmsMapper $mapper){
|
||||||
parent::__construct($appName, $request);
|
parent::__construct($appName, $request);
|
||||||
$this->userId = $userId;
|
$this->userId = $userId;
|
||||||
@ -35,7 +35,9 @@ class SmsController extends Controller {
|
|||||||
* @NoCSRFRequired
|
* @NoCSRFRequired
|
||||||
*/
|
*/
|
||||||
public function index () {
|
public function index () {
|
||||||
$params = array('user' => $this->userId);
|
$params = array('user' => $this->userId,
|
||||||
|
"PNLConversations" => $this->smsMapper->getAllPeersPhoneNumbers($this->userId)
|
||||||
|
);
|
||||||
return new TemplateResponse($this->appName, 'main', $params);
|
return new TemplateResponse($this->appName, 'main', $params);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,7 +49,7 @@ class SmsController extends Controller {
|
|||||||
$smsList = $this->smsMapper->getAllIds($this->userId);
|
$smsList = $this->smsMapper->getAllIds($this->userId);
|
||||||
return new JSONResponse(array("smslist" => $smsList));
|
return new JSONResponse(array("smslist" => $smsList));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @NoAdminRequired
|
* @NoAdminRequired
|
||||||
*/
|
*/
|
||||||
@ -59,7 +61,7 @@ class SmsController extends Controller {
|
|||||||
$this->smsMapper->writeToDB($this->userId, $smsDatas);
|
$this->smsMapper->writeToDB($this->userId, $smsDatas);
|
||||||
return new JSONResponse(array("status" => true, "msg" => "OK"));
|
return new JSONResponse(array("status" => true, "msg" => "OK"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @NoAdminRequired
|
* @NoAdminRequired
|
||||||
*/
|
*/
|
||||||
@ -71,7 +73,7 @@ class SmsController 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"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function checkPushStructure ($smsCount, $smsDatas) {
|
private function checkPushStructure ($smsCount, $smsDatas) {
|
||||||
if ($smsCount != count($smsDatas)) {
|
if ($smsCount != count($smsDatas)) {
|
||||||
$this->errorMsg = "Error: sms count invalid";
|
$this->errorMsg = "Error: sms count invalid";
|
||||||
|
@ -25,19 +25,19 @@ class SmsMapper extends Mapper {
|
|||||||
public function __construct (IDb $db) {
|
public function __construct (IDb $db) {
|
||||||
parent::__construct($db, 'ocsms_smsdatas');
|
parent::__construct($db, 'ocsms_smsdatas');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAllIds ($userId) {
|
public function getAllIds ($userId) {
|
||||||
$query = \OC_DB::prepare('SELECT sms_id, sms_mailbox FROM ' .
|
$query = \OC_DB::prepare('SELECT sms_id, sms_mailbox FROM ' .
|
||||||
'*PREFIX*ocsms_smsdatas WHERE user_id = ?');
|
'*PREFIX*ocsms_smsdatas WHERE user_id = ?');
|
||||||
$result = $query->execute(array($userId));
|
$result = $query->execute(array($userId));
|
||||||
|
|
||||||
$smsList = array();
|
$smsList = array();
|
||||||
while($row = $result->fetchRow()) {
|
while($row = $result->fetchRow()) {
|
||||||
$mbox = SmsMapper::$mailboxNames[$row["sms_mailbox"]];
|
$mbox = SmsMapper::$mailboxNames[$row["sms_mailbox"]];
|
||||||
if (!isset($smsList[$mbox])) {
|
if (!isset($smsList[$mbox])) {
|
||||||
$smsList[$mbox] = array();
|
$smsList[$mbox] = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!in_array($row["sms_id"], $smsList[$mbox])) {
|
if (!in_array($row["sms_id"], $smsList[$mbox])) {
|
||||||
array_push($smsList[$mbox], $row["sms_id"]);
|
array_push($smsList[$mbox], $row["sms_id"]);
|
||||||
}
|
}
|
||||||
@ -45,21 +45,36 @@ class SmsMapper extends Mapper {
|
|||||||
return $smsList;
|
return $smsList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getAllPeersPhoneNumbers ($userId) {
|
||||||
|
$query = \OC_DB::prepare('SELECT sms_address FROM ' .
|
||||||
|
'*PREFIX*ocsms_smsdatas WHERE user_id = ? AND sms_mailbox IN (?,?)');
|
||||||
|
$result = $query->execute(array($userId, 0, 1));
|
||||||
|
|
||||||
|
$phoneList = array();
|
||||||
|
while($row = $result->fetchRow()) {
|
||||||
|
if (!in_array($row["sms_id"], $phoneList)) {
|
||||||
|
array_push($phoneList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort($phoneList);
|
||||||
|
return $phoneList;
|
||||||
|
}
|
||||||
|
|
||||||
public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) {
|
public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) {
|
||||||
\OCP\DB::beginTransaction();
|
\OCP\DB::beginTransaction();
|
||||||
|
|
||||||
if ($purgeAllSmsBeforeInsert === true) {
|
if ($purgeAllSmsBeforeInsert === true) {
|
||||||
$query = \OC_DB::prepare('DELETE FROM *PREFIX*ocsms_smsdatas ' .
|
$query = \OC_DB::prepare('DELETE FROM *PREFIX*ocsms_smsdatas ' .
|
||||||
'WHERE user_id = ?');
|
'WHERE user_id = ?');
|
||||||
$result = $query->execute(array($userId));
|
$result = $query->execute(array($userId));
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($smsList as $sms) {
|
foreach ($smsList as $sms) {
|
||||||
$smsFlags = sprintf("%s%s",
|
$smsFlags = sprintf("%s%s",
|
||||||
$sms["read"] === "true" ? "1" : "0",
|
$sms["read"] === "true" ? "1" : "0",
|
||||||
$sms["seen"] === "true" ? "1" : "0"
|
$sms["seen"] === "true" ? "1" : "0"
|
||||||
);
|
);
|
||||||
|
|
||||||
// Only delete if we haven't purged the DB
|
// Only delete if we haven't purged the DB
|
||||||
if ($purgeAllSmsBeforeInsert === false) {
|
if ($purgeAllSmsBeforeInsert === false) {
|
||||||
// Remove previous record
|
// Remove previous record
|
||||||
@ -70,7 +85,7 @@ class SmsMapper extends Mapper {
|
|||||||
$userId, (int) $sms["_id"]
|
$userId, (int) $sms["_id"]
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
$query = \OC_DB::prepare('INSERT INTO *PREFIX*ocsms_smsdatas ' .
|
$query = \OC_DB::prepare('INSERT INTO *PREFIX*ocsms_smsdatas ' .
|
||||||
'(user_id, added, lastmodified, sms_flags, sms_date, sms_id,' .
|
'(user_id, added, lastmodified, sms_flags, sms_date, sms_id,' .
|
||||||
'sms_address, sms_msg, sms_mailbox, sms_type) VALUES ' .
|
'sms_address, sms_msg, sms_mailbox, sms_type) VALUES ' .
|
||||||
@ -81,10 +96,10 @@ class SmsMapper extends Mapper {
|
|||||||
$sms["address"], $sms["body"], (int) $sms["mbox"],
|
$sms["address"], $sms["body"], (int) $sms["mbox"],
|
||||||
(int) $sms["type"]
|
(int) $sms["type"]
|
||||||
));
|
));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
\OCP\DB::commit();
|
\OCP\DB::commit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user