Skip to content

Commit b46b812

Browse files
committed
change pool and entity type to int
Signed-off-by: Lukas Schaefer <[email protected]>
1 parent 896f0a7 commit b46b812

File tree

11 files changed

+67
-51
lines changed

11 files changed

+67
-51
lines changed

lib/Db/QuotaRule.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
* @method void setAmount(int $amount)
2222
* @method int getPriority()
2323
* @method void setPriority(int $priority)
24-
* @method bool getPool()
25-
* @method void setPool(bool $pool)
24+
* @method int getPool()
25+
* @method void setPool(int $pool)
2626
*/
2727
class QuotaRule extends Entity implements JsonSerializable {
2828
/** @var int */
@@ -31,14 +31,14 @@ class QuotaRule extends Entity implements JsonSerializable {
3131
protected $amount;
3232
/** @var int */
3333
protected $priority;
34-
/** @var bool */
34+
/** @var int */
3535
protected $pool;
3636

3737
public function __construct() {
3838
$this->addType('type', Types::INTEGER);
3939
$this->addType('amount', Types::INTEGER);
4040
$this->addType('priority', Types::INTEGER);
41-
$this->addType('pool', Types::BOOLEAN);
41+
$this->addType('pool', Types::INTEGER);
4242
}
4343

4444
#[ReturnTypeWillChange]

lib/Db/QuotaRuleMapper.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ public function getRule(int $quotaType, string $userId, array $groups): QuotaRul
5757
)->andWhere(
5858
$qb->expr()->orX(
5959
$qb->expr()->andX(
60-
$qb->expr()->eq('u.entity_type', $qb->createNamedParameter('user', IQueryBuilder::PARAM_STR)),
60+
$qb->expr()->eq('u.entity_type', $qb->createNamedParameter(EntityType::USER->value, IQueryBuilder::PARAM_INT)),
6161
$qb->expr()->eq('u.entity_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
6262
),
6363
$qb->expr()->andX(
64-
$qb->expr()->eq('u.entity_type', $qb->createNamedParameter('group', IQueryBuilder::PARAM_STR)),
64+
$qb->expr()->eq('u.entity_type', $qb->createNamedParameter(EntityType::GROUP->value, IQueryBuilder::PARAM_INT)),
6565
$qb->expr()->in('u.entity_id', $qb->createNamedParameter($groups, IQueryBuilder::PARAM_STR_ARRAY))
6666
),
6767

@@ -76,11 +76,11 @@ public function getRule(int $quotaType, string $userId, array $groups): QuotaRul
7676
* @param int $quotaType
7777
* @param int $amount
7878
* @param int $priority
79-
* @param bool $pool
79+
* @param int $pool
8080
* @return int
8181
* @throws Exception
8282
*/
83-
public function addRule(int $quotaType, int $amount, int $priority, bool $pool): int {
83+
public function addRule(int $quotaType, int $amount, int $priority, int $pool): int {
8484
$qb = $this->db->getQueryBuilder();
8585

8686
$qb->insert($this->getTableName())
@@ -89,7 +89,7 @@ public function addRule(int $quotaType, int $amount, int $priority, bool $pool):
8989
'type' => $qb->createNamedParameter($quotaType, IQueryBuilder::PARAM_INT),
9090
'amount' => $qb->createNamedParameter($amount, IQueryBuilder::PARAM_INT),
9191
'priority' => $qb->createNamedParameter($priority, IQueryBuilder::PARAM_INT),
92-
'pool' => $qb->createNamedParameter($pool, IQueryBuilder::PARAM_BOOL)
92+
'pool' => $qb->createNamedParameter($pool, IQueryBuilder::PARAM_INT)
9393
]
9494
);
9595
$qb->executeStatement();
@@ -100,18 +100,18 @@ public function addRule(int $quotaType, int $amount, int $priority, bool $pool):
100100
* @param int $quotaType
101101
* @param int $amount
102102
* @param int $priority
103-
* @param bool $pool
103+
* @param int $pool
104104
* @return void
105105
* @throws Exception
106106
*/
107-
public function updateRule(int $id, int $quotaType, int $amount, int $priority, bool $pool): void {
107+
public function updateRule(int $id, int $quotaType, int $amount, int $priority, int $pool): void {
108108
$qb = $this->db->getQueryBuilder();
109109

110110
$qb->update($this->getTableName())
111111
->set('type', $qb->createNamedParameter($quotaType, IQueryBuilder::PARAM_INT))
112112
->set('amount', $qb->createNamedParameter($amount, IQueryBuilder::PARAM_INT))
113113
->set('priority', $qb->createNamedParameter($priority, IQueryBuilder::PARAM_INT))
114-
->set('pool', $qb->createNamedParameter($pool, IQueryBuilder::PARAM_BOOL))
114+
->set('pool', $qb->createNamedParameter($pool, IQueryBuilder::PARAM_INT))
115115
->where(
116116
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
117117
);

lib/Db/QuotaUser.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@
1515
/**
1616
* @method int getRuleId()
1717
* @method void setRuleId(int $ruleId)
18-
* @method string getEntityType()
19-
* @method void setEntityType(string $entityType)
18+
* @method int getEntityType()
19+
* @method void setEntityType(int $entityType)
2020
* @method string getEntityId()
2121
* @method void setEntityId(string $entityId)
2222
*/
2323
class QuotaUser extends Entity implements JsonSerializable {
2424
/** @var int */
2525
protected $ruleId;
26-
/** @var string */
26+
/** @var int */
2727
protected $entityType;
2828
/** @var string */
2929
protected $entityId;
3030

3131
public function __construct() {
3232
$this->addType('rule_id', Types::INTEGER);
33-
$this->addType('entity_type', Types::STRING);
33+
$this->addType('entity_type', Types::INTEGER);
3434
$this->addType('entity_id', Types::STRING);
3535
}
3636

lib/Db/QuotaUserMapper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
use OCP\DB\QueryBuilder\IQueryBuilder;
1313
use OCP\IDBConnection;
1414

15+
enum EntityType: int {
16+
case USER = 0;
17+
case GROUP = 1;
18+
}
19+
1520
/**
1621
* @extends QBMapper<QuotaUser>
1722
*/

lib/Migration/Version030800Date20250812122830.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
4646
$table->addColumn('priority', Types::INTEGER, [
4747
'notnull' => true
4848
]);
49-
$table->addColumn('pool', Types::BOOLEAN, [
50-
'notnull' => false,
51-
'default' => false,
49+
$table->addColumn('pool', Types::INTEGER, [
50+
'notnull' => true
5251
]);
5352
$table->setPrimaryKey(['id']);
5453
$table->addIndex(['type'], 'oai_rule_type');
@@ -62,7 +61,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
6261
$table->addColumn('rule_id', Types::BIGINT, [
6362
'notnull' => true,
6463
]);
65-
$table->addColumn('entity_type', Types::STRING, [
64+
$table->addColumn('entity_type', Types::INTEGER, [
6665
'notnull' => true,
6766
]);
6867
$table->addColumn('entity_id', Types::STRING, [

lib/Service/OpenAiSettingsService.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,9 @@ public function setQuotaPeriod(array $quotaPeriod): void {
681681
if (!isset($quotaPeriod['length']) || !is_int($quotaPeriod['length'])) {
682682
throw new Exception('Invalid quota period length');
683683
}
684-
$quotaPeriod['length'] = max(1, $quotaPeriod['length']);
684+
if ($quotaPeriod['length'] < 1) {
685+
throw new Exception('Invalid quota period length');
686+
}
685687
if (!isset($quotaPeriod['unit']) || !is_string($quotaPeriod['unit'])) {
686688
throw new Exception('Invalid quota period unit');
687689
}
@@ -690,8 +692,12 @@ public function setQuotaPeriod(array $quotaPeriod): void {
690692
if (!isset($quotaPeriod['day']) || !is_int($quotaPeriod['day'])) {
691693
throw new Exception('Invalid quota period day');
692694
}
693-
$quotaPeriod['day'] = max(1, $quotaPeriod['day']);
694-
$quotaPeriod['day'] = min($quotaPeriod['day'], 28);
695+
if ($quotaPeriod['day'] < 1) {
696+
throw new Exception('Invalid quota period day');
697+
}
698+
if ($quotaPeriod['day'] > 28) {
699+
throw new Exception('Invalid quota period day');
700+
}
695701
} elseif ($quotaPeriod['unit'] !== 'day') {
696702
throw new Exception('Invalid quota period unit');
697703
}

lib/Service/QuotaRuleService.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Exception;
1111
use OCA\OpenAi\AppInfo\Application;
12+
use OCA\OpenAi\Db\EntityType;
1213
use OCA\OpenAi\Db\QuotaRuleMapper;
1314
use OCA\OpenAi\Db\QuotaUserMapper;
1415
use OCP\AppFramework\Db\DoesNotExistException;
@@ -77,7 +78,7 @@ public function getRules(): array {
7778
$result = $rule->jsonSerialize();
7879
$result['entities'] = array_map(static function ($u) use ($userManager) {
7980
$displayName = $u->getEntityId();
80-
if ($u->getEntityType() === 'user') {
81+
if ($u->getEntityType() === EntityType::USER->value) {
8182
$user = $userManager->get($u->getEntityId());
8283
$displayName = $user->getDisplayName();
8384
}
@@ -97,7 +98,7 @@ public function getRules(): array {
9798
* @throws Exception
9899
*/
99100
public function addRule(): array {
100-
$id = $this->quotaRuleMapper->addRule(0, 0, 0, false);
101+
$id = $this->quotaRuleMapper->addRule(0, 0, 0, 0);
101102
$this->clearCache();
102103
return [
103104
'id' => $id,
@@ -170,11 +171,7 @@ private function validateEntities(array $entities) {
170171
$this->logger->warning('Invalid entity', $e);
171172
throw new Exception('Invalid entity');
172173
}
173-
$validTypes = [
174-
'user',
175-
'group',
176-
];
177-
if (!isset($e['entity_type'], $e['entity_id']) || !in_array($e['entity_type'], $validTypes) || !is_string($e['entity_id'])) {
174+
if (!isset($e['entity_type'], $e['entity_id']) || EntityType::tryFrom($e['entity_type']) === null || !is_string($e['entity_id'])) {
178175
$this->logger->warning('Invalid entity', $e);
179176
throw new Exception('Invalid entity');
180177
}

src/components/QuotaPeriodPicker.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
<NcInputField
1717
id="quota"
1818
:model-value="value.length"
19+
:min="1"
1920
class="input"
2021
type="number"
21-
@update:model-value="update('length', $event)" />
22+
@update:model-value="$event >= 1 && update('length', $event)" />
2223
<NcSelect
2324
:model-value="unitOptionName"
2425
:options="unitOptions"
@@ -31,9 +32,11 @@
3132
<NcInputField
3233
id="quota"
3334
:model-value="value?.day ?? 1"
35+
:min="1"
36+
:max="28"
3437
class="input"
3538
type="number"
36-
@update:model-value="update('day', $event)" />
39+
@update:model-value="$event >= 1 && $event <= 28 && update('day', $event)" />
3740
</div>
3841
<NcNoteCard type="success">
3942
{{ resetText }}

src/components/Rules/MultiselectWho.vue

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
<template #option="option">
2323
<div class="select-suggestion">
2424
<NcAvatar
25-
v-if="option.entity_type === 'user'"
25+
v-if="option.entity_type === ENTITY_TYPES.user"
2626
:user="option.entity_id"
2727
:show-user-status="false" />
2828
<NcAvatar
29-
v-else-if="option.entity_type === 'group'"
29+
v-else-if="option.entity_type === ENTITY_TYPES.group"
3030
:display-name="option.display_name"
3131
:is-no-user="true"
3232
:disable-tooltip="true"
@@ -44,11 +44,11 @@
4444
</template>
4545
<template #selected-option="option">
4646
<NcAvatar
47-
v-if="option.entity_type === 'user'"
47+
v-if="option.entity_type === ENTITY_TYPES.user"
4848
:user="option.entity_id"
4949
:show-user-status="false" />
5050
<NcAvatar
51-
v-else-if="option.entity_type === 'group'"
51+
v-else-if="option.entity_type === ENTITY_TYPES.group"
5252
:display-name="option.display_name"
5353
:is-no-user="true"
5454
:disable-tooltip="true"
@@ -81,9 +81,10 @@ import axios from '@nextcloud/axios'
8181
import NcAvatar from '@nextcloud/vue/components/NcAvatar'
8282
import NcSelect from '@nextcloud/vue/components/NcSelect'
8383
84-
const typeIconClass = {
85-
user: 'icon-user',
86-
group: 'icon-group',
84+
const typeIconClass = ['icon-user', 'icon-group']
85+
const ENTITY_TYPES = {
86+
user: 0,
87+
group: 1,
8788
}
8889
8990
export default {
@@ -118,6 +119,7 @@ export default {
118119
suggestions: [],
119120
query: '',
120121
currentUser: getCurrentUser(),
122+
ENTITY_TYPES,
121123
}
122124
},
123125
@@ -128,15 +130,15 @@ export default {
128130
.filter((s) => {
129131
return (
130132
s.source === 'users'
131-
&& !this.value.find((u) => u.entity_type === 'user' && u.entity_id === s.id)
133+
&& !this.value.find((u) => u.entity_type === ENTITY_TYPES.user && u.entity_id === s.id)
132134
)
133135
})
134136
.map((s) => {
135137
return {
136138
entity_id: s.id,
137-
entity_type: 'user',
139+
entity_type: ENTITY_TYPES.user,
138140
display_name: s.label,
139-
id: 'user-' + s.id,
141+
id: ENTITY_TYPES.user + '-' + s.id,
140142
}
141143
})
142144
@@ -149,14 +151,14 @@ export default {
149151
if (
150152
lowerCurrent.match(lowerQuery)
151153
&& !this.value.find(
152-
(u) => u.entity_type === 'user' && u.entity_id === this.currentUser.uid,
154+
(u) => u.entity_type === ENTITY_TYPES.user && u.entity_id === this.currentUser.uid,
153155
)
154156
) {
155157
result.push({
156158
entity_id: this.currentUser.uid,
157-
entity_type: 'user',
159+
entity_type: ENTITY_TYPES.user,
158160
display_name: this.currentUser.displayName,
159-
id: 'user-' + this.currentUser.uid,
161+
id: ENTITY_TYPES.user + '-' + this.currentUser.uid,
160162
})
161163
}
162164
}
@@ -167,15 +169,15 @@ export default {
167169
.filter((s) => {
168170
return (
169171
s.source === 'groups'
170-
&& !this.value.find((u) => u.entity_type === 'group' && u.entity_id === s.id)
172+
&& !this.value.find((u) => u.entity_type === ENTITY_TYPES.group && u.entity_id === s.id)
171173
)
172174
})
173175
.map((s) => {
174176
return {
175177
entity_id: s.id,
176-
entity_type: 'group',
178+
entity_type: ENTITY_TYPES.group,
177179
display_name: s.label,
178-
id: 'group-' + s.id,
180+
id: ENTITY_TYPES.group + '-' + s.id,
179181
}
180182
})
181183
result.push(...groups)

src/components/Rules/QuotaRules.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ export default {
4646
4747
data() {
4848
return {
49-
state: loadState('integration_openai', 'rules', []),
49+
state: loadState('integration_openai', 'rules', []).map((rule) => {
50+
rule.pool = rule.pool === 1
51+
return rule
52+
}),
5053
}
5154
},
5255

0 commit comments

Comments
 (0)