mirror of
https://github.com/nerzhul/ocsms.git
synced 2025-06-07 16:06:15 +00:00
Prepare to write conversation objects to database
This commit is contained in:
parent
e63237283f
commit
ed380fd09e
25
db/Conversation.php
Normal file
25
db/Conversation.php
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Nextcloud - Phone Sync
|
||||||
|
*
|
||||||
|
* This file is licensed under the Affero General Public License version 3 or
|
||||||
|
* later. See the COPYING file.
|
||||||
|
*
|
||||||
|
* @author Loic Blot <loic.blot@unix-experience.fr>
|
||||||
|
* @copyright Loic Blot 2014-2018
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace OCA\OcSms\Db;
|
||||||
|
|
||||||
|
|
||||||
|
class Conversation {
|
||||||
|
public $id;
|
||||||
|
public $userId;
|
||||||
|
public $phoneNumber;
|
||||||
|
|
||||||
|
public function __construct($userId, $phoneNumber) {
|
||||||
|
$this->userId = $userId;
|
||||||
|
$this->phoneNumber = $phoneNumber;
|
||||||
|
$id = null;
|
||||||
|
}
|
||||||
|
}
|
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
namespace OCA\OcSms\Db;
|
namespace OCA\OcSms\Db;
|
||||||
|
|
||||||
|
use lib\UUIDGenerator;
|
||||||
use \OCP\IDBConnection;
|
use \OCP\IDBConnection;
|
||||||
|
|
||||||
use \OCP\AppFramework\Db\Mapper;
|
use \OCP\AppFramework\Db\Mapper;
|
||||||
@ -309,13 +310,33 @@ class SmsMapper extends Mapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function getConversationForUserAndPhone($userId, $phoneNumber) {
|
private function getConversationForUserAndPhone($userId, $phoneNumber) {
|
||||||
|
$qb = $this->db->getQueryBuilder();
|
||||||
|
|
||||||
$qb->select('id')
|
$qb->select('id')
|
||||||
->from('ocsms_conversations')
|
->from('ocsms_conversations')
|
||||||
->where($qb->expr()->andX(
|
->where($qb->expr()->andX(
|
||||||
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId)),
|
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId)),
|
||||||
$qb->expr()->in('phone_number', $qb->createNamedParameter($phoneNumber))
|
$qb->expr()->in('phone_number', $qb->createNamedParameter($phoneNumber))
|
||||||
);
|
));
|
||||||
$result = $qb->execute();
|
$result = $qb->execute();
|
||||||
|
|
||||||
|
if ($row = $result->fetch()) {
|
||||||
|
$conversation = new Conversation($userId, $phoneNumber);
|
||||||
|
$conversation->id = $row["id"];
|
||||||
|
return $conversation;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function registerConversation($userId, $phoneNumber) {
|
||||||
|
$qb = $this->db->getQueryBuilder();
|
||||||
|
$qb->insert('ocsms_conversations')
|
||||||
|
->values([
|
||||||
|
'id' => $qb->createNamedParameter(UUIDGenerator::generate()),
|
||||||
|
'user_id' => $qb->createNamedParameter($userId),
|
||||||
|
'phone_number' => $qb->createNamedParameter($phoneNumber),
|
||||||
|
])
|
||||||
|
->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) {
|
public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) {
|
||||||
@ -351,14 +372,19 @@ class SmsMapper extends Mapper {
|
|||||||
'(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 ' .
|
||||||
'(?,?,?,?,?,?,?,?,?,?)');
|
'(?,?,?,?,?,?,?,?,?,?)');
|
||||||
$result = $query->execute(array(
|
$query->execute(array(
|
||||||
$userId, $now, $now, $smsFlags,
|
$userId, $now, $now, $smsFlags,
|
||||||
$sms["date"], (int) $sms["_id"],
|
$sms["date"], (int) $sms["_id"],
|
||||||
$sms["address"], $sms["body"], (int) $sms["mbox"],
|
$sms["address"], $sms["body"], (int) $sms["mbox"],
|
||||||
(int) $sms["type"]
|
(int) $sms["type"]
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->getConversationForUserAndPhone($userId, $sms["address"]);
|
/*
|
||||||
|
$conversation = $this->getConversationForUserAndPhone($userId, $sms["address"]);
|
||||||
|
if ($conversation === null) {
|
||||||
|
$this->registerConversation($userId, $sms["address"]);
|
||||||
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->db->commit();
|
$this->db->commit();
|
||||||
|
26
lib/UUIDGenerator.php
Normal file
26
lib/UUIDGenerator.php
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Nextcloud - Phone Sync
|
||||||
|
*
|
||||||
|
* This file is licensed under the Affero General Public License version 3 or
|
||||||
|
* later. See the COPYING file.
|
||||||
|
*
|
||||||
|
* @author Loic Blot <loic.blot@unix-experience.fr>
|
||||||
|
* @copyright Loic Blot 2014-2018
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace lib;
|
||||||
|
|
||||||
|
|
||||||
|
class UUIDGenerator {
|
||||||
|
public static function generate() {
|
||||||
|
if (function_exists('com_create_guid') === true) {
|
||||||
|
return trim(com_create_guid(), '{}');
|
||||||
|
}
|
||||||
|
|
||||||
|
return strtolower(sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X',
|
||||||
|
mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151),
|
||||||
|
mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user