-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathSessionMapper.php
More file actions
188 lines (163 loc) · 6.13 KB
/
SessionMapper.php
File metadata and controls
188 lines (163 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Assistant\Db\ChattyLLM;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
/**
* @extends QBMapper<Session>
*/
class SessionMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'assistant_chat_sns', Session::class);
}
/**
* @param string $userId
* @param integer $sessionId
* @return boolean
* @throws \OCP\DB\Exception
*/
public function exists(string $userId, int $sessionId): bool {
$qb = $this->db->getQueryBuilder();
$qb->select('id')
->from($this->getTableName())
->where($qb->expr()->eq('id', $qb->createPositionalParameter($sessionId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId, IQueryBuilder::PARAM_STR)));
try {
return $this->findEntity($qb) !== null;
} catch (\OCP\AppFramework\Db\DoesNotExistException $e) {
return false;
} catch (\OCP\AppFramework\Db\MultipleObjectsReturnedException $e) {
return true;
}
}
/**
* @param string $userId
* @param int $sessionId
* @return Session
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws Exception
*/
public function getUserSession(string $userId, int $sessionId): Session {
$qb = $this->db->getQueryBuilder();
$qb->select(Session::$columns)
->from($this->getTableName())
->where($qb->expr()->eq('id', $qb->createPositionalParameter($sessionId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId, IQueryBuilder::PARAM_STR)));
return $this->findEntity($qb);
}
/**
* @param string $userId
* @return array
* @throws \OCP\DB\Exception
*/
public function getUserSessions(string $userId): array {
$qb = $this->db->getQueryBuilder();
$qb->select(Session::$columns)
->from($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId, IQueryBuilder::PARAM_STR)))
->orderBy('timestamp', 'DESC');
return $this->findEntities($qb);
}
/**
* @return array<Session>
* @throws \OCP\DB\Exception
*/
public function getRememberedUserSessions(string $userId, int $limit = 0): array {
$qb = $this->db->getQueryBuilder();
$qb->select(Session::$columns)
->from($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId, IQueryBuilder::PARAM_STR)))
->andWhere($qb->expr()->eq('is_remembered', $qb->createPositionalParameter(1, IQueryBuilder::PARAM_INT)))
->orderBy('timestamp', 'DESC');
if ($limit > 0) {
$qb->setMaxResults($limit);
}
return $this->findEntities($qb);
}
/**
* @return array<Session>
* @throws \OCP\DB\Exception
*/
public function getRememberedUserSessionsWithOutdatedSummaries(string $userId, int $limit): array {
$qb = $this->db->getQueryBuilder();
$qb->select(Session::$columns)
->from($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId, IQueryBuilder::PARAM_STR)))
->andWhere($qb->expr()->eq('is_remembered', $qb->createPositionalParameter(1, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('is_summary_up_to_date', $qb->createPositionalParameter(0, IQueryBuilder::PARAM_INT)))
->setMaxResults($limit)
->orderBy('timestamp', 'DESC');
return $this->findEntities($qb);
}
/**
* @return array<Session>
* @throws \OCP\DB\Exception
*/
public function getRememberedSessionsWithOutdatedSummaries(int $limit): array {
$qb = $this->db->getQueryBuilder();
$qb->select(Session::$columns)
->from($this->getTableName())
->where($qb->expr()->eq('is_remembered', $qb->createPositionalParameter(1, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('is_summary_up_to_date', $qb->createPositionalParameter(0, IQueryBuilder::PARAM_INT)))
->setMaxResults($limit)
->orderBy('timestamp', 'DESC');
return $this->findEntities($qb);
}
/**
* @return array<Session>
* @throws \OCP\DB\Exception
*/
public function getRememberedUserSessionsWithoutSummaries(string $userId, int $limit): array {
$qb = $this->db->getQueryBuilder();
$qb->select(Session::$columns)
->from($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId, IQueryBuilder::PARAM_STR)))
->andWhere($qb->expr()->eq('is_remembered', $qb->createPositionalParameter(1, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->isNull('summary'))
->setMaxResults($limit)
->orderBy('timestamp', 'DESC');
return $this->findEntities($qb);
}
/**
* @param string $userId
* @param integer $sessionId
* @param string $title
* @throws \OCP\DB\Exception
* @throws \RuntimeException
*/
public function updateSessionTitle(string $userId, int $sessionId, string $title) {
$qb = $this->db->getQueryBuilder();
$qb->update($this->getTableName())
->set('title', $qb->createPositionalParameter($title, IQueryBuilder::PARAM_STR))
->where($qb->expr()->eq('id', $qb->createPositionalParameter($sessionId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId, IQueryBuilder::PARAM_STR)));
$qb->executeStatement();
}
/**
* @param string $userId
* @param integer $sessionId
* @throws \OCP\DB\Exception
* @throws \RuntimeException
*/
public function deleteSession(string $userId, int $sessionId) {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->where($qb->expr()->eq('id', $qb->createPositionalParameter($sessionId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId, IQueryBuilder::PARAM_STR)));
$qb->executeStatement();
}
public function updateSessionIsRemembered(?string $userId, int $sessionId, bool $is_remembered) {
$session = $this->getUserSession($userId, $sessionId);
$session->setIsRemembered($is_remembered ? 1 : 0);
$this->update($session);
}
}