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