1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2014, Tobia De Koninck hey--at--ledfan.be |
4
|
|
|
* This file is licensed under the AGPL version 3 or later. |
5
|
|
|
* See the COPYING file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace OCA\Chat\OCH\Db; |
9
|
|
|
|
10
|
|
|
use Doctrine\DBAL\Sharding\SQLAzure\SQLAzureFederationsSynchronizer; |
11
|
|
|
use \OCP\AppFramework\Db\Mapper; |
12
|
|
|
use \OCP\IDb; |
13
|
|
|
use \OCP\AppFramework\Db\Entity; |
14
|
|
|
use \OCP\AppFramework\Db\DoesNotExistException; |
15
|
|
|
|
16
|
|
|
class UserMapper extends Mapper { |
17
|
|
|
|
18
|
|
|
private $userOnlineTable = '*PREFIX*chat_och_users_online'; |
|
|
|
|
19
|
|
|
|
20
|
|
|
public function __construct(IDb $api) { |
21
|
|
|
parent::__construct($api, 'chat_och_users_in_conversation'); |
22
|
|
|
$this->table = '*PREFIX*chat_och_users_in_conversation'; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function findSessionsByConversation($conversationId){ |
26
|
|
|
$sql = <<<SQL |
27
|
|
|
SELECT |
28
|
|
|
$this->table.user, |
29
|
|
|
$this->userOnlineTable.session_id, |
30
|
|
|
$this->table.conversation_id, |
31
|
|
|
$this->table.joined, |
32
|
|
|
$this->userOnlineTable.id |
33
|
|
|
FROM |
34
|
|
|
$this->table |
35
|
|
|
INNER JOIN |
36
|
|
|
$this->userOnlineTable |
37
|
|
|
ON |
38
|
|
|
$this->table.user = $this->userOnlineTable.user |
39
|
|
|
AND |
40
|
|
|
$this->table.conversation_id = ? |
41
|
|
|
SQL; |
42
|
|
|
$result = $this->findEntities($sql, array($conversationId)); |
43
|
|
|
return $result; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function findByUser($user){ |
47
|
|
|
$sql = <<<SQL |
48
|
|
|
SELECT |
49
|
|
|
* |
50
|
|
|
FROM |
51
|
|
|
$this->table |
52
|
|
|
WHERE |
53
|
|
|
`user` = ? |
54
|
|
|
SQL; |
55
|
|
|
$result = $this->findEntities($sql, array($user)); |
56
|
|
|
return $result; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function findConvsIdByUser($user){ |
60
|
|
|
$sql = 'SELECT conversation_id FROM `' . $this->getTableName() . '` ' . |
61
|
|
|
'WHERE `user` = ? '; |
62
|
|
|
|
63
|
|
|
$result = $this->execute($sql, array($user)); |
64
|
|
|
|
65
|
|
|
$ids = array(); |
66
|
|
|
while($row = $result->fetch()){ |
67
|
|
|
array_push($ids, $row['conversation_id']); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$ids = array_unique($ids); |
71
|
|
|
return $ids; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
public function findUsersInConv($id){ |
76
|
|
|
$sql = 'SELECT `user` FROM `' . $this->getTableName() . '` ' . |
77
|
|
|
'WHERE `conversation_id` = ? '; |
78
|
|
|
|
79
|
|
|
$result = $this->execute($sql, array($id)); |
80
|
|
|
|
81
|
|
|
$users = array(); |
82
|
|
|
while($row = $result->fetch()){ |
83
|
|
|
array_push($users, $row['user']); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$users = array_unique($users); |
87
|
|
|
return $users; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function insertUnique(Entity $entity){ |
91
|
|
|
// First check $entity is already in DB |
92
|
|
|
$sql = <<<SQL |
93
|
|
|
SELECT |
94
|
|
|
`user` |
95
|
|
|
FROM |
96
|
|
|
`*PREFIX*chat_och_users_in_conversation` |
97
|
|
|
WHERE |
98
|
|
|
`user` = ? |
99
|
|
|
AND |
100
|
|
|
`conversation_id` = ? |
101
|
|
|
SQL; |
102
|
|
|
try { |
103
|
|
|
$result = $this->findOneQuery($sql, array($entity->getUser(), $entity->getConversationId())); |
|
|
|
|
104
|
|
|
// The user already joined the conv -> nothing to do |
105
|
|
|
} catch (\Exception $exception) { |
106
|
|
|
// The user isn't in this conversation -> add it |
107
|
|
|
$sql = <<<SQL |
108
|
|
|
INSERT |
109
|
|
|
INTO `*PREFIX*chat_och_users_in_conversation` |
110
|
|
|
( |
111
|
|
|
`user`, |
112
|
|
|
`conversation_id`, |
113
|
|
|
`joined` |
114
|
|
|
) VALUES ( |
115
|
|
|
?, |
116
|
|
|
?, |
117
|
|
|
? |
118
|
|
|
) |
119
|
|
|
SQL; |
120
|
|
|
$this->execute($sql, array( |
121
|
|
|
$entity->getUser(), |
122
|
|
|
$entity->getConversationId(), |
123
|
|
|
$entity->getJoined() |
124
|
|
|
)); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function findAll(){ |
129
|
|
|
$sql = 'SELECT * FROM `' . $this->getTableName() . '`'; |
130
|
|
|
return $this->findEntities($sql, array()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |
This check marks private properties in classes that are never used. Those properties can be removed.