From 7bd0ea3af3262e8e5c82834cffbbda5303f699d1 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Thu, 17 May 2018 09:21:55 +0200 Subject: [PATCH 1/4] Prepare conversation scheme --- appinfo/database.xml | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/appinfo/database.xml b/appinfo/database.xml index dd63cc99..f3d71df0 100644 --- a/appinfo/database.xml +++ b/appinfo/database.xml @@ -169,6 +169,32 @@ + + *dbprefix*ocsms_conversations + + + id + text + true + 64 + + + sms_address + text + true + 512 + + + smsdata_conversations_id_address + + id + + + sms_address + + + +
*dbprefix*ocsms_sendmessage_queue From e63237283f4a3959bf9a80db27caa78c2f57ac52 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Thu, 17 May 2018 09:31:55 +0200 Subject: [PATCH 2/4] Prepare to get conversation number on write --- appinfo/database.xml | 11 +++++++++-- db/smsmapper.php | 12 +++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/appinfo/database.xml b/appinfo/database.xml index f3d71df0..faa2de05 100644 --- a/appinfo/database.xml +++ b/appinfo/database.xml @@ -177,15 +177,22 @@ text true 64 + true - sms_address + user_id + text + true + 64 + + + phone_number text true 512 - smsdata_conversations_id_address + smsdata_conversations_user_id_address id diff --git a/db/smsmapper.php b/db/smsmapper.php index 19ab5e2b..ec7e073c 100644 --- a/db/smsmapper.php +++ b/db/smsmapper.php @@ -308,6 +308,16 @@ public function getNewMessagesCountForAllPhonesNumbers($userId, $lastDate) { return $phoneList; } + private function getConversationForUserAndPhone($userId, $phoneNumber) { + $qb->select('id') + ->from('ocsms_conversations') + ->where($qb->expr()->andX( + $qb->expr()->eq('user_id', $qb->createNamedParameter($userId)), + $qb->expr()->in('phone_number', $qb->createNamedParameter($phoneNumber)) + ); + $result = $qb->execute(); + } + public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) { $this->db->beginTransaction(); $qb = $this->db->getQueryBuilder(); @@ -348,7 +358,7 @@ public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) (int) $sms["type"] )); - + $this->getConversationForUserAndPhone($userId, $sms["address"]); } $this->db->commit(); From ed380fd09ec2f5c5a8cfc3921a8214da3f188447 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Thu, 17 May 2018 10:27:06 +0200 Subject: [PATCH 3/4] Prepare to write conversation objects to database --- db/Conversation.php | 25 +++++++++++++++++++++++++ db/smsmapper.php | 32 +++++++++++++++++++++++++++++--- lib/UUIDGenerator.php | 26 ++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 db/Conversation.php create mode 100644 lib/UUIDGenerator.php diff --git a/db/Conversation.php b/db/Conversation.php new file mode 100644 index 00000000..0fb83def --- /dev/null +++ b/db/Conversation.php @@ -0,0 +1,25 @@ + + * @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; + } +} \ No newline at end of file diff --git a/db/smsmapper.php b/db/smsmapper.php index ec7e073c..b624d909 100644 --- a/db/smsmapper.php +++ b/db/smsmapper.php @@ -11,6 +11,7 @@ namespace OCA\OcSms\Db; +use lib\UUIDGenerator; use \OCP\IDBConnection; use \OCP\AppFramework\Db\Mapper; @@ -309,13 +310,33 @@ public function getNewMessagesCountForAllPhonesNumbers($userId, $lastDate) { } private function getConversationForUserAndPhone($userId, $phoneNumber) { + $qb = $this->db->getQueryBuilder(); + $qb->select('id') ->from('ocsms_conversations') ->where($qb->expr()->andX( $qb->expr()->eq('user_id', $qb->createNamedParameter($userId)), $qb->expr()->in('phone_number', $qb->createNamedParameter($phoneNumber)) - ); + )); $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) { @@ -351,14 +372,19 @@ public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) '(user_id, added, lastmodified, sms_flags, sms_date, sms_id,' . 'sms_address, sms_msg, sms_mailbox, sms_type) VALUES ' . '(?,?,?,?,?,?,?,?,?,?)'); - $result = $query->execute(array( + $query->execute(array( $userId, $now, $now, $smsFlags, $sms["date"], (int) $sms["_id"], $sms["address"], $sms["body"], (int) $sms["mbox"], (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(); diff --git a/lib/UUIDGenerator.php b/lib/UUIDGenerator.php new file mode 100644 index 00000000..03e779df --- /dev/null +++ b/lib/UUIDGenerator.php @@ -0,0 +1,26 @@ + + * @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)) + ); + } +} \ No newline at end of file From 68971b1d3322286127f692435a36f4b96a7cac33 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Thu, 17 May 2018 13:02:03 +0200 Subject: [PATCH 4/4] Register conversations & add configMapper to smsmapper to use the phone number formatter --- appinfo/ocsmsapp.php | 10 ++++++---- controller/smscontroller.php | 3 ++- db/smsmapper.php | 13 ++++++++----- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/appinfo/ocsmsapp.php b/appinfo/ocsmsapp.php index 75682081..6e7db621 100644 --- a/appinfo/ocsmsapp.php +++ b/appinfo/ocsmsapp.php @@ -33,6 +33,7 @@ class OcSmsApp extends App { /** * OcSmsApp constructor. * @param array $urlParams + * @throws \OCP\AppFramework\QueryException */ public function __construct (array $urlParams=array()) { parent::__construct('ocsms', $urlParams); @@ -60,7 +61,7 @@ public function __construct (array $urlParams=array()) { }); $container->registerService('Sms', function(IContainer $c) use ($server) { - return new Sms($server->getDb()); + return new Sms(); }); $container->registerService('ConversationStateMapper', function(IContainer $c) use ($server) { @@ -70,7 +71,8 @@ public function __construct (array $urlParams=array()) { $container->registerService('SmsMapper', function(IContainer $c) use ($server) { return new SmsMapper( $server->getDatabaseConnection(), - $c->query('ConversationStateMapper') + $c->query('ConversationStateMapper'), + $c->query('ConfigMapper') ); }); @@ -117,10 +119,10 @@ public function __construct (array $urlParams=array()) { /** * Migration services */ - $container->registerService('OCA\OcSms\Migration\FixConversationReadStates', function ($c) { + $container->registerService('OCA\OcSms\Migration\FixConversationReadStates', function (IContainer $c) use($server) { return new FixConversationReadStates( $c->query('ConversationStateMapper'), - $c->getServer()->getUserManager() + $server->getUserManager() ); }); } diff --git a/controller/smscontroller.php b/controller/smscontroller.php index e03456bd..57f0e72f 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -42,9 +42,10 @@ class SmsController extends Controller { * @param IRequest $request * @param $userId * @param SmsMapper $mapper + * @param ConversationStateMapper $cmapper * @param ConfigMapper $cfgMapper * @param IContactsManager $contactsManager - * @param $urlGenerator + * @param IURLGenerator $urlGenerator */ public function __construct ($appName, IRequest $request, $userId, SmsMapper $mapper, ConversationStateMapper $cmapper, diff --git a/db/smsmapper.php b/db/smsmapper.php index b624d909..e730c7d5 100644 --- a/db/smsmapper.php +++ b/db/smsmapper.php @@ -33,10 +33,12 @@ class SmsMapper extends Mapper { 6 => "queued" ); private $convStateMapper; + private $configMapper; - public function __construct (IDBConnection $db, ConversationStateMapper $cmapper) { + public function __construct (IDBConnection $db, ConversationStateMapper $cmapper, ConfigMapper $configMapper) { parent::__construct($db, 'ocsms_smsdatas'); $this->convStateMapper = $cmapper; + $this->configMapper = $configMapper; } public function getAllIds ($userId) { @@ -379,12 +381,13 @@ public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) (int) $sms["type"] )); - /* - $conversation = $this->getConversationForUserAndPhone($userId, $sms["address"]); + $configuredCountry = $this->configMapper->getCountry(); + $fmtPhoneNumber = PhoneNumberFormatter::format($configuredCountry, $sms["address"]); + + $conversation = $this->getConversationForUserAndPhone($userId, $fmtPhoneNumber); if ($conversation === null) { - $this->registerConversation($userId, $sms["address"]); + $this->registerConversation($userId, $fmtPhoneNumber); } - */ } $this->db->commit();