Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions appinfo/database.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,39 @@
</index>
</declaration>
</table>
<table>
<name>*dbprefix*ocsms_conversations</name>
<declaration>
<field>
<name>id</name>
<type>text</type>
<notnull>true</notnull>
<length>64</length>
<primary>true</primary>
</field>
<field>
<name>user_id</name>
<type>text</type>
<notnull>true</notnull>
<length>64</length>
</field>
<field>
<name>phone_number</name>
<type>text</type>
<notnull>true</notnull>
<length>512</length>
</field>
<index>
<name>smsdata_conversations_user_id_address</name>
<field>
<name>id</name>
</field>
<field>
<name>sms_address</name>
</field>
</index>
</declaration>
</table>
<table>
<name>*dbprefix*ocsms_sendmessage_queue</name>
<declaration>
Expand Down
10 changes: 6 additions & 4 deletions appinfo/ocsmsapp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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')
);
});

Expand Down Expand Up @@ -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()
);
});
}
Expand Down
3 changes: 2 additions & 1 deletion controller/smscontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions db/Conversation.php
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
* @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;
}
}
43 changes: 41 additions & 2 deletions db/smsmapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace OCA\OcSms\Db;

use lib\UUIDGenerator;
use \OCP\IDBConnection;

use \OCP\AppFramework\Db\Mapper;
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
26 changes: 26 additions & 0 deletions lib/UUIDGenerator.php
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
* @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))
);
}
}