diff --git a/appinfo/database.xml b/appinfo/database.xml
index dd63cc99..faa2de05 100644
--- a/appinfo/database.xml
+++ b/appinfo/database.xml
@@ -169,6 +169,39 @@
+
+ *dbprefix*ocsms_conversations
+
+
+ id
+ text
+ true
+ 64
+ true
+
+
+ user_id
+ text
+ true
+ 64
+
+
+ phone_number
+ text
+ true
+ 512
+
+
+ smsdata_conversations_user_id_address
+
+ id
+
+
+ sms_address
+
+
+
+
*dbprefix*ocsms_sendmessage_queue
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/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 19ab5e2b..e730c7d5 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;
@@ -32,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) {
@@ -308,6 +311,36 @@ public function getNewMessagesCountForAllPhonesNumbers($userId, $lastDate) {
return $phoneList;
}
+ 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) {
$this->db->beginTransaction();
$qb = $this->db->getQueryBuilder();
@@ -341,14 +374,20 @@ 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"]
));
+ $configuredCountry = $this->configMapper->getCountry();
+ $fmtPhoneNumber = PhoneNumberFormatter::format($configuredCountry, $sms["address"]);
+ $conversation = $this->getConversationForUserAndPhone($userId, $fmtPhoneNumber);
+ if ($conversation === null) {
+ $this->registerConversation($userId, $fmtPhoneNumber);
+ }
}
$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