Skip to content

Commit ed380fd

Browse files
committed
Prepare to write conversation objects to database
1 parent e632372 commit ed380fd

3 files changed

Lines changed: 80 additions & 3 deletions

File tree

db/Conversation.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Nextcloud - Phone Sync
4+
*
5+
* This file is licensed under the Affero General Public License version 3 or
6+
* later. See the COPYING file.
7+
*
8+
* @author Loic Blot <loic.blot@unix-experience.fr>
9+
* @copyright Loic Blot 2014-2018
10+
*/
11+
12+
namespace OCA\OcSms\Db;
13+
14+
15+
class Conversation {
16+
public $id;
17+
public $userId;
18+
public $phoneNumber;
19+
20+
public function __construct($userId, $phoneNumber) {
21+
$this->userId = $userId;
22+
$this->phoneNumber = $phoneNumber;
23+
$id = null;
24+
}
25+
}

db/smsmapper.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace OCA\OcSms\Db;
1313

14+
use lib\UUIDGenerator;
1415
use \OCP\IDBConnection;
1516

1617
use \OCP\AppFramework\Db\Mapper;
@@ -309,13 +310,33 @@ public function getNewMessagesCountForAllPhonesNumbers($userId, $lastDate) {
309310
}
310311

311312
private function getConversationForUserAndPhone($userId, $phoneNumber) {
313+
$qb = $this->db->getQueryBuilder();
314+
312315
$qb->select('id')
313316
->from('ocsms_conversations')
314317
->where($qb->expr()->andX(
315318
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId)),
316319
$qb->expr()->in('phone_number', $qb->createNamedParameter($phoneNumber))
317-
);
320+
));
318321
$result = $qb->execute();
322+
323+
if ($row = $result->fetch()) {
324+
$conversation = new Conversation($userId, $phoneNumber);
325+
$conversation->id = $row["id"];
326+
return $conversation;
327+
}
328+
return null;
329+
}
330+
331+
private function registerConversation($userId, $phoneNumber) {
332+
$qb = $this->db->getQueryBuilder();
333+
$qb->insert('ocsms_conversations')
334+
->values([
335+
'id' => $qb->createNamedParameter(UUIDGenerator::generate()),
336+
'user_id' => $qb->createNamedParameter($userId),
337+
'phone_number' => $qb->createNamedParameter($phoneNumber),
338+
])
339+
->execute();
319340
}
320341

321342
public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) {
@@ -351,14 +372,19 @@ public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false)
351372
'(user_id, added, lastmodified, sms_flags, sms_date, sms_id,' .
352373
'sms_address, sms_msg, sms_mailbox, sms_type) VALUES ' .
353374
'(?,?,?,?,?,?,?,?,?,?)');
354-
$result = $query->execute(array(
375+
$query->execute(array(
355376
$userId, $now, $now, $smsFlags,
356377
$sms["date"], (int) $sms["_id"],
357378
$sms["address"], $sms["body"], (int) $sms["mbox"],
358379
(int) $sms["type"]
359380
));
360381

361-
$this->getConversationForUserAndPhone($userId, $sms["address"]);
382+
/*
383+
$conversation = $this->getConversationForUserAndPhone($userId, $sms["address"]);
384+
if ($conversation === null) {
385+
$this->registerConversation($userId, $sms["address"]);
386+
}
387+
*/
362388
}
363389

364390
$this->db->commit();

lib/UUIDGenerator.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Nextcloud - Phone Sync
4+
*
5+
* This file is licensed under the Affero General Public License version 3 or
6+
* later. See the COPYING file.
7+
*
8+
* @author Loic Blot <loic.blot@unix-experience.fr>
9+
* @copyright Loic Blot 2014-2018
10+
*/
11+
12+
namespace lib;
13+
14+
15+
class UUIDGenerator {
16+
public static function generate() {
17+
if (function_exists('com_create_guid') === true) {
18+
return trim(com_create_guid(), '{}');
19+
}
20+
21+
return strtolower(sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X',
22+
mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151),
23+
mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535))
24+
);
25+
}
26+
}

0 commit comments

Comments
 (0)