1
0
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:
Loic Blot 2018-05-17 10:27:06 +02:00
parent e63237283f
commit ed380fd09e
No known key found for this signature in database
GPG Key ID: EFAA458E8C153987
3 changed files with 80 additions and 3 deletions

25
db/Conversation.php Normal file
View 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;
}
}

View File

@ -11,6 +11,7 @@
namespace OCA\OcSms\Db;
use lib\UUIDGenerator;
use \OCP\IDBConnection;
use \OCP\AppFramework\Db\Mapper;
@ -309,13 +310,33 @@ class SmsMapper extends Mapper {
}
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 @@ class SmsMapper extends Mapper {
'(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();

26
lib/UUIDGenerator.php Normal file
View 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))
);
}
}