mirror of
https://github.com/nerzhul/ocsms.git
synced 2025-06-07 16:06:15 +00:00
Conversation Mapper addition + migration to proper scheme
This commit is contained in:
parent
42dbd2c3bf
commit
26df3a53a4
@ -201,6 +201,38 @@
|
|||||||
</field>
|
</field>
|
||||||
</declaration>
|
</declaration>
|
||||||
</table>
|
</table>
|
||||||
|
<table>
|
||||||
|
<name>*dbprefix*ocsms_conversation_read_states</name>
|
||||||
|
<declaration>
|
||||||
|
<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>64</length>
|
||||||
|
</field>
|
||||||
|
<field>
|
||||||
|
<name>int_date</name>
|
||||||
|
<type>integer</type>
|
||||||
|
<length>32</length>
|
||||||
|
<notnull>true</notnull>
|
||||||
|
</field>
|
||||||
|
<index>
|
||||||
|
<name>sms_conversation_rs_pkey</name>
|
||||||
|
<field>
|
||||||
|
<name>user_id</name>
|
||||||
|
</field>
|
||||||
|
<field>
|
||||||
|
<name>phone_number</name>
|
||||||
|
</field>
|
||||||
|
</index>
|
||||||
|
</declaration>
|
||||||
|
</table>
|
||||||
<table>
|
<table>
|
||||||
<name>*dbprefix*ocsms_config</name>
|
<name>*dbprefix*ocsms_config</name>
|
||||||
<declaration>
|
<declaration>
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
<description>A app to sync SMS with your ownCloud</description>
|
<description>A app to sync SMS with your ownCloud</description>
|
||||||
<licence>agpl</licence>
|
<licence>agpl</licence>
|
||||||
<author>Loic Blot</author>
|
<author>Loic Blot</author>
|
||||||
<version>1.9.0</version>
|
<version>1.10.0</version>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<owncloud min-version="8.1" max-version="9.2" />
|
<owncloud min-version="8.1" max-version="9.2" />
|
||||||
<nextcloud min-version="9.0" />
|
<nextcloud min-version="9.0" />
|
||||||
@ -17,4 +17,10 @@
|
|||||||
<website>https://github.com/nerzhul/ocsms</website>
|
<website>https://github.com/nerzhul/ocsms</website>
|
||||||
<bugs>https://github.com/nerzhul/ocsms/issues</bugs>
|
<bugs>https://github.com/nerzhul/ocsms/issues</bugs>
|
||||||
<repository type="git">https://github.com/nerzhul/ocsms</repository>
|
<repository type="git">https://github.com/nerzhul/ocsms</repository>
|
||||||
|
|
||||||
|
<repair-steps>
|
||||||
|
<post-migration>
|
||||||
|
<step>OCA\OcSms\Migration\FixConversationReadStates</step>
|
||||||
|
</post-migration>
|
||||||
|
</repair-steps>
|
||||||
</info>
|
</info>
|
||||||
|
@ -22,9 +22,12 @@ use \OCA\OcSms\Controller\SmsController;
|
|||||||
|
|
||||||
use \OCA\OcSms\Db\Sms;
|
use \OCA\OcSms\Db\Sms;
|
||||||
use \OCA\OcSms\Db\SmsMapper;
|
use \OCA\OcSms\Db\SmsMapper;
|
||||||
|
use \OCA\OcSms\Db\ConversationStateMapper;
|
||||||
|
|
||||||
use \OCA\OcSms\Db\ConfigMapper;
|
use \OCA\OcSms\Db\ConfigMapper;
|
||||||
|
|
||||||
|
use \OCA\OcSms\Migration\FixConversationReadStates;
|
||||||
|
|
||||||
class OcSmsApp extends App {
|
class OcSmsApp extends App {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -55,8 +58,15 @@ class OcSmsApp extends App {
|
|||||||
return new Sms($server->getDb());
|
return new Sms($server->getDb());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$container->registerService('ConversationStateMapper', function(IContainer $c) use ($server) {
|
||||||
|
return new ConversationStateMapper($server->getDb());
|
||||||
|
});
|
||||||
|
|
||||||
$container->registerService('SmsMapper', function(IContainer $c) use ($server) {
|
$container->registerService('SmsMapper', function(IContainer $c) use ($server) {
|
||||||
return new SmsMapper($server->getDb());
|
return new SmsMapper(
|
||||||
|
$server->getDb(),
|
||||||
|
$c->query('ConversationStateMapper')
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -83,6 +93,7 @@ class OcSmsApp extends App {
|
|||||||
$c->query('Request'),
|
$c->query('Request'),
|
||||||
$c->query('UserId'),
|
$c->query('UserId'),
|
||||||
$c->query('SmsMapper'),
|
$c->query('SmsMapper'),
|
||||||
|
$c->query('ConversationStateMapper'),
|
||||||
$c->query('ConfigMapper'),
|
$c->query('ConfigMapper'),
|
||||||
$server->getContactsManager(),
|
$server->getContactsManager(),
|
||||||
$server->getURLGenerator()
|
$server->getURLGenerator()
|
||||||
@ -97,5 +108,15 @@ class OcSmsApp extends App {
|
|||||||
$c->query('SmsMapper')
|
$c->query('SmsMapper')
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Migration services
|
||||||
|
*/
|
||||||
|
$container->registerService('OCA\OcSms\Migration\FixConversationReadStates', function ($c) {
|
||||||
|
return new FixConversationReadStates(
|
||||||
|
$c->query('ConversationStateMapper'),
|
||||||
|
$c->getServer()->getUserManager()
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ use \OCP\AppFramework\Http;
|
|||||||
|
|
||||||
use \OCA\OcSms\Db\ConfigMapper;
|
use \OCA\OcSms\Db\ConfigMapper;
|
||||||
use \OCA\OcSms\Db\SmsMapper;
|
use \OCA\OcSms\Db\SmsMapper;
|
||||||
|
use \OCA\OcSms\Db\ConversationStateMapper;
|
||||||
|
|
||||||
use \OCA\OcSms\Lib\ContactCache;
|
use \OCA\OcSms\Lib\ContactCache;
|
||||||
use \OCA\OcSms\Lib\PhoneNumberFormatter;
|
use \OCA\OcSms\Lib\PhoneNumberFormatter;
|
||||||
@ -31,6 +32,7 @@ class SmsController extends Controller {
|
|||||||
private $userId;
|
private $userId;
|
||||||
private $configMapper;
|
private $configMapper;
|
||||||
private $smsMapper;
|
private $smsMapper;
|
||||||
|
private $convStateMapper;
|
||||||
private $urlGenerator;
|
private $urlGenerator;
|
||||||
private $contactCache;
|
private $contactCache;
|
||||||
|
|
||||||
@ -44,11 +46,14 @@ class SmsController extends Controller {
|
|||||||
* @param IContactsManager $contactsManager
|
* @param IContactsManager $contactsManager
|
||||||
* @param $urlGenerator
|
* @param $urlGenerator
|
||||||
*/
|
*/
|
||||||
public function __construct ($appName, IRequest $request, $userId, SmsMapper $mapper, ConfigMapper $cfgMapper,
|
public function __construct ($appName, IRequest $request, $userId,
|
||||||
|
SmsMapper $mapper, ConversationStateMapper $cmapper,
|
||||||
|
ConfigMapper $cfgMapper,
|
||||||
IContactsManager $contactsManager, IURLGenerator $urlGenerator) {
|
IContactsManager $contactsManager, IURLGenerator $urlGenerator) {
|
||||||
parent::__construct($appName, $request);
|
parent::__construct($appName, $request);
|
||||||
$this->userId = $userId;
|
$this->userId = $userId;
|
||||||
$this->smsMapper = $mapper;
|
$this->smsMapper = $mapper;
|
||||||
|
$this->convStateMapper = $cmapper;
|
||||||
$this->configMapper = $cfgMapper;
|
$this->configMapper = $cfgMapper;
|
||||||
$this->urlGenerator = $urlGenerator;
|
$this->urlGenerator = $urlGenerator;
|
||||||
$this->contactCache = new ContactCache($cfgMapper, $contactsManager);
|
$this->contactCache = new ContactCache($cfgMapper, $contactsManager);
|
||||||
@ -103,7 +108,7 @@ class SmsController extends Controller {
|
|||||||
$contacts[$number] = $fmtPN;
|
$contacts[$number] = $fmtPN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$lastRead = $this->smsMapper->getLastReadDate($this->userId);
|
$lastRead = $this->convStateMapper->getLast($this->userId);
|
||||||
$ocversion = \OCP\Util::getVersion();
|
$ocversion = \OCP\Util::getVersion();
|
||||||
$photoversion = 1;
|
$photoversion = 1;
|
||||||
if (version_compare($ocversion[0].".".$ocversion[1].".".$ocversion[2], "9.0.0", ">=")) {
|
if (version_compare($ocversion[0].".".$ocversion[1].".".$ocversion[2], "9.0.0", ">=")) {
|
||||||
@ -160,7 +165,7 @@ class SmsController extends Controller {
|
|||||||
if (count($messages) > 0) {
|
if (count($messages) > 0) {
|
||||||
$maxDate = max(array_keys($messages));
|
$maxDate = max(array_keys($messages));
|
||||||
for ($i=0;$i<count($phoneNumbers);$i++) {
|
for ($i=0;$i<count($phoneNumbers);$i++) {
|
||||||
$this->smsMapper->setLastReadDate($this->userId, $phoneNumbers[$i], $maxDate);
|
$this->convStateMapper->setLast($this->userId, $phoneNumbers[$i], $maxDate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
88
db/conversationstatemapper.php
Normal file
88
db/conversationstatemapper.php
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* ownCloud - ocsms
|
||||||
|
*
|
||||||
|
* 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-2016
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace OCA\OcSms\Db;
|
||||||
|
|
||||||
|
use \OCP\IDb;
|
||||||
|
|
||||||
|
use \OCP\AppFramework\Db\Mapper;
|
||||||
|
|
||||||
|
use \OCA\OcSms\AppInfo\OcSmsApp;
|
||||||
|
use \OCA\OcSms\Lib\PhoneNumberFormatter;
|
||||||
|
|
||||||
|
class ConversationStateMapper extends Mapper {
|
||||||
|
public function __construct (IDb $db) {
|
||||||
|
parent::__construct($db, 'ocsms_smsdatas');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getLast ($userId) {
|
||||||
|
$sql = 'SELECT MAX(int_date) as mx FROM ' .
|
||||||
|
'*PREFIX*ocsms_conversation_read_states WHERE user_id = ?';
|
||||||
|
|
||||||
|
$query = \OCP\DB::prepare($sql);
|
||||||
|
$result = $query->execute(array($userId));
|
||||||
|
|
||||||
|
if ($row = $result->fetchRow()) {
|
||||||
|
return $row["mx"];
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getLastForPhoneNumber ($userId, $phoneNumber) {
|
||||||
|
$sql = 'SELECT MAX(int_date) as mx FROM ' .
|
||||||
|
'*PREFIX*ocsms_conversation_read_states WHERE user_id = ? AND phone_number = ?';
|
||||||
|
|
||||||
|
$query = \OCP\DB::prepare($sql);
|
||||||
|
$result = $query->execute(array($userId, $phoneNumber));
|
||||||
|
|
||||||
|
if ($row = $result->fetchRow()) {
|
||||||
|
return $row["mx"];
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setLast ($userId, $phoneNumber, $lastDate) {
|
||||||
|
\OCP\DB::beginTransaction();
|
||||||
|
$query = \OCP\DB::prepare('DELETE FROM *PREFIX*ocsms_conversation_read_states ' .
|
||||||
|
'WHERE user_id = ? AND phone_number = ?');
|
||||||
|
$query->execute(array($userId, $phoneNumber));
|
||||||
|
|
||||||
|
$query = \OCP\DB::prepare('INSERT INTO *PREFIX*ocsms_conversation_read_states' .
|
||||||
|
'(user_id, phone_number, int_date) VALUES ' .
|
||||||
|
'(?,?,?)');
|
||||||
|
$query->execute(array($userId, $phoneNumber, $lastDate));
|
||||||
|
\OCP\DB::commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Migration steps
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function migrate () {
|
||||||
|
$sql = 'SELECT user_id, datakey, datavalue FROM ' .
|
||||||
|
'*PREFIX*ocsms_user_datas WHERE datakey LIKE \'lastReadDate-%\'';
|
||||||
|
|
||||||
|
$query = \OCP\DB::prepare($sql);
|
||||||
|
$result = $query->execute(array());
|
||||||
|
|
||||||
|
while ($row = $result->fetchRow()) {
|
||||||
|
$pn = preg_replace("#lastReadDate[-]#", "", $row["datakey"]);
|
||||||
|
$this->setLast($row["user_id"], $pn, $row["datavalue"]);
|
||||||
|
};
|
||||||
|
|
||||||
|
$query = \OCP\DB::prepare("DELETE FROM *PREFIX*ocsms_user_datas WHERE datakey LIKE 'lastReadDate-%'");
|
||||||
|
$query->execute(array());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
@ -17,6 +17,7 @@ use \OCP\AppFramework\Db\Mapper;
|
|||||||
|
|
||||||
use \OCA\OcSms\AppInfo\OcSmsApp;
|
use \OCA\OcSms\AppInfo\OcSmsApp;
|
||||||
use \OCA\OcSms\Lib\PhoneNumberFormatter;
|
use \OCA\OcSms\Lib\PhoneNumberFormatter;
|
||||||
|
use \OCA\OcSms\Db\ConversationStateMapper;
|
||||||
|
|
||||||
class SmsMapper extends Mapper {
|
class SmsMapper extends Mapper {
|
||||||
/*
|
/*
|
||||||
@ -30,9 +31,11 @@ class SmsMapper extends Mapper {
|
|||||||
4 => "outbox", 5 => "failed",
|
4 => "outbox", 5 => "failed",
|
||||||
6 => "queued"
|
6 => "queued"
|
||||||
);
|
);
|
||||||
|
private $convStateMapper;
|
||||||
|
|
||||||
public function __construct (IDb $db) {
|
public function __construct (IDb $db, ConversationStateMapper $cmapper) {
|
||||||
parent::__construct($db, 'ocsms_smsdatas');
|
parent::__construct($db, 'ocsms_smsdatas');
|
||||||
|
$this->convStateMapper = $cmapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAllIds ($userId) {
|
public function getAllIds ($userId) {
|
||||||
@ -235,7 +238,7 @@ class SmsMapper extends Mapper {
|
|||||||
$phoneList = array();
|
$phoneList = array();
|
||||||
while ($row = $result->fetchRow()) {
|
while ($row = $result->fetchRow()) {
|
||||||
$phoneNumber = preg_replace("#[ ]#", "", $row["sms_address"]);
|
$phoneNumber = preg_replace("#[ ]#", "", $row["sms_address"]);
|
||||||
if ($this->getLastReadDateForPhoneNumber($userId, $phoneNumber) < $lastDate) {
|
if ($this->convStateMapper->getLastForPhoneNumber($userId, $phoneNumber) < $lastDate) {
|
||||||
if (!array_key_exists($phoneNumber, $phoneList)) {
|
if (!array_key_exists($phoneNumber, $phoneList)) {
|
||||||
$phoneList[$phoneNumber] = $row["ct"];
|
$phoneList[$phoneNumber] = $row["ct"];
|
||||||
}
|
}
|
||||||
@ -247,47 +250,6 @@ class SmsMapper extends Mapper {
|
|||||||
return $phoneList;
|
return $phoneList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getLastReadDate ($userId) {
|
|
||||||
$sql = 'SELECT MAX(datavalue) as mx FROM ' .
|
|
||||||
'*PREFIX*ocsms_user_datas WHERE user_id = ?';
|
|
||||||
|
|
||||||
$query = \OCP\DB::prepare($sql);
|
|
||||||
$result = $query->execute(array($userId));
|
|
||||||
|
|
||||||
if ($row = $result->fetchRow()) {
|
|
||||||
return $row["mx"];
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getLastReadDateForPhoneNumber ($userId, $phoneNumber) {
|
|
||||||
$sql = 'SELECT MAX(datavalue) as mx FROM ' .
|
|
||||||
'*PREFIX*ocsms_user_datas WHERE user_id = ? AND datakey = ?';
|
|
||||||
|
|
||||||
$query = \OCP\DB::prepare($sql);
|
|
||||||
$result = $query->execute(array($userId, 'lastReadDate-' . $phoneNumber));
|
|
||||||
|
|
||||||
if ($row = $result->fetchRow()) {
|
|
||||||
return $row["mx"];
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setLastReadDate ($userId, $phoneNumber, $lastDate) {
|
|
||||||
\OCP\DB::beginTransaction();
|
|
||||||
$query = \OCP\DB::prepare('DELETE FROM *PREFIX*ocsms_user_datas ' .
|
|
||||||
'WHERE user_id = ? AND datakey = ?');
|
|
||||||
$query->execute(array($userId, 'lastReadDate-' . $phoneNumber));
|
|
||||||
|
|
||||||
$query = \OCP\DB::prepare('INSERT INTO *PREFIX*ocsms_user_datas' .
|
|
||||||
'(user_id, datakey, datavalue) VALUES ' .
|
|
||||||
'(?,?,?)');
|
|
||||||
$query->execute(array($userId, 'lastReadDate-' . $phoneNumber, $lastDate));
|
|
||||||
\OCP\DB::commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) {
|
public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) {
|
||||||
\OCP\DB::beginTransaction();
|
\OCP\DB::beginTransaction();
|
||||||
|
|
||||||
|
54
lib/migration/fixconversationreadstates.php
Normal file
54
lib/migration/fixconversationreadstates.php
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* ownCloud - ocsms
|
||||||
|
*
|
||||||
|
* 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-2016
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
namespace OCA\OcSms\Migration;
|
||||||
|
|
||||||
|
use OCP\IUser;
|
||||||
|
use OCP\IUserManager;
|
||||||
|
use OCP\Migration\IOutput;
|
||||||
|
use OCP\Migration\IRepairStep;
|
||||||
|
|
||||||
|
use \OCA\OcSms\Db\ConversationStateMapper;
|
||||||
|
|
||||||
|
class FixConversationReadStates implements IRepairStep {
|
||||||
|
|
||||||
|
private $userManager;
|
||||||
|
private $convStateMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FixConversationReadStates constructor.
|
||||||
|
*
|
||||||
|
* @param IUserManager $userManager
|
||||||
|
*/
|
||||||
|
public function __construct(ConversationStateMapper $mapper, IUserManager $userManager) {
|
||||||
|
$this->userManager = $userManager;
|
||||||
|
$this->convStateMapper = $mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
public function getName() {
|
||||||
|
return 'Migrate legacy conversation reading states';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
public function run(IOutput $output) {
|
||||||
|
|
||||||
|
$output->startProgress();
|
||||||
|
$output->advance(1, "Migrate states");
|
||||||
|
$this->convStateMapper->migrate();
|
||||||
|
$output->finishProgress();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user