Skip to content

Commit 74f12d1

Browse files
committed
Prepare to write conversation objects to database
1 parent 4477d62 commit 74f12d1

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

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 <[email protected]>
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;
@@ -311,13 +312,33 @@ public function getNewMessagesCountForAllPhonesNumbers($userId, $lastDate) {
311312
}
312313

313314
private function getConversationForUserAndPhone($userId, $phoneNumber) {
315+
$qb = $this->db->getQueryBuilder();
316+
314317
$qb->select('id')
315318
->from('ocsms_conversations')
316319
->where($qb->expr()->andX(
317320
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId)),
318321
$qb->expr()->in('phone_number', $qb->createNamedParameter($phoneNumber))
319-
);
322+
));
320323
$result = $qb->execute();
324+
325+
if ($row = $result->fetch()) {
326+
$conversation = new Conversation($userId, $phoneNumber);
327+
$conversation->id = $row["id"];
328+
return $conversation;
329+
}
330+
return null;
331+
}
332+
333+
private function registerConversation($userId, $phoneNumber) {
334+
$qb = $this->db->getQueryBuilder();
335+
$qb->insert('ocsms_conversations')
336+
->values([
337+
'id' => $qb->createNamedParameter(UUIDGenerator::generate()),
338+
'user_id' => $qb->createNamedParameter($userId),
339+
'phone_number' => $qb->createNamedParameter($phoneNumber),
340+
])
341+
->execute();
321342
}
322343

323344
public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) {
@@ -355,14 +376,19 @@ public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false)
355376
'(user_id, added, lastmodified, sms_flags, sms_date, sms_id,' .
356377
'sms_address, sms_msg, sms_mailbox, sms_type) VALUES ' .
357378
'(?,?,?,?,?,?,?,?,?,?)');
358-
$result = $query->execute(array(
379+
$query->execute(array(
359380
$userId, $now, $now, $smsFlags,
360381
$sms["date"], (int) $sms["_id"],
361382
$sms["address"], $sms["body"], (int) $sms["mbox"],
362383
(int) $sms["type"]
363384
));
364385

365-
$this->getConversationForUserAndPhone($userId, $sms["address"]);
386+
/*
387+
$conversation = $this->getConversationForUserAndPhone($userId, $sms["address"]);
388+
if ($conversation === null) {
389+
$this->registerConversation($userId, $sms["address"]);
390+
}
391+
*/
366392
}
367393

368394
$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 <[email protected]>
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)