Skip to content

Commit 3c84d67

Browse files
committed
feat: Replace SQL Server with PostgreSQL
feat: Add support for Redis
1 parent e83cd4d commit 3c84d67

29 files changed

+633
-452
lines changed

.vscode/extensions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"recommendations": [
3+
"bradlc.vscode-tailwindcss",
34
"csharpier.csharpier-vscode",
4-
"bradlc.vscode-tailwindcss"
5+
"ms-dotnettools.csdevkit"
56
]
67
}

compose.yaml

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,53 @@ services:
1010
PORT:
1111
ASPNETCORE_ENVIRONMENT: Production
1212
ASPNETCORE_HTTP_PORTS: 80
13-
CONNECTIONSTRINGS__DEFAULT: "Data Source=db;Initial Catalog=snao;User ID=sa;Password=${MSSQL_SA_PASSWORD};Trust Server Certificate=True"
13+
CONNECTIONSTRINGS__DEFAULT: # "Server=db;Database=${POSTGRES_DB};Username=${POSTGRES_USER};Password=${POSTGRES_PASSWORD}"
14+
# CONNECTIONSTRINGS__REDIS: "redis:6379"
1415
TELEGRAMBOT__BOTTOKEN:
15-
TELEGRAMBOT__SERVERADDRESS: http://local-bot-api-server:8081
16-
TELEGRAMBOT__WEBHOOKURL: http://app
16+
# TELEGRAMBOT__SERVERADDRESS: http://local-bot-api-server:8081
17+
# TELEGRAMBOT__WEBHOOKURL: http://app
1718
TELEGRAMBOT__SECRETTOKEN:
1819
GENERAL__APPLICATIONURL:
1920
GENERAL__SUPPORTCHATINVITATIONLINK:
20-
GENERAL__FILESPATH: /files:/opt/tba/${TELEGRAMBOT__BOTTOKEN}
21+
# GENERAL__FILESPATH: /files:/opt/tba/${TELEGRAMBOT__BOTTOKEN}
2122
GENERAL__FFMPEGPATH:
2223
ports:
2324
- ${PORT:-80}:80
24-
volumes:
25-
- "files:/files"
26-
depends_on:
27-
- db
28-
- local-bot-api-server
25+
# volumes:
26+
# - "files:/files"
27+
# depends_on:
28+
# - db
29+
# - redis
30+
# - local-bot-api-server
2931

30-
db:
31-
image: mcr.microsoft.com/azure-sql-edge:latest
32-
environment:
33-
ACCEPT_EULA: Y
34-
MSSQL_PID: Premium
35-
MSSQL_SA_PASSWORD:
36-
volumes:
37-
- data:/var/opt/mssql
32+
# db:
33+
# image: postgres:alpine
34+
# environment:
35+
# POSTGRES_USER: snao_user
36+
# POSTGRES_DB: snao
37+
# POSTGRES_PASSWORD:
38+
# POSTGRES_HOST_AUTH_METHOD: scram-sha-256
39+
# volumes:
40+
# - data:/var/lib/postgresql/data
3841

39-
local-bot-api-server:
40-
build: local-server
41-
environment:
42-
TELEGRAM_API_ID:
43-
TELEGRAM_API_HASH:
44-
TELEGRAMBOT__BOTTOKEN:
45-
volumes:
46-
- type: volume
47-
source: files
48-
target: /opt/tba/${TELEGRAMBOT__BOTTOKEN}
42+
# local-bot-api-server:
43+
# build: local-server
44+
# environment:
45+
# TELEGRAM_API_ID:
46+
# TELEGRAM_API_HASH:
47+
# TELEGRAMBOT__BOTTOKEN:
48+
# volumes:
49+
# - type: volume
50+
# source: files
51+
# target: /opt/tba/${TELEGRAMBOT__BOTTOKEN}
52+
53+
# redis:
54+
# image: redis:alpine
55+
# environment:
56+
# REDIS_ARGS: "--maxmemory 512mb --maxmemory-policy allkeys-lru"
57+
# ports:
58+
# - ${REDIS_PORT:-6379}:6379
4959

50-
volumes:
51-
files:
52-
data:
60+
# volumes:
61+
# data:
62+
# files:

src/Core/Application/Commands/ApiKeyCommand.cs

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) 2024 Quetzal Rivera.
22
// Licensed under the GNU General Public License v3.0, See LICENCE in the project root for license information.
33

4-
using Microsoft.Extensions.Caching.Memory;
54
using SauceNAO.Application.Models;
65
using SauceNAO.Application.Services;
76
using SauceNAO.Domain.Entities.SauceAggregate;
@@ -66,18 +65,15 @@ CancellationToken cancellationToken
6665
if (message.Chat.Type == ChatTypes.Private)
6766
{
6867
// Send a message indicating that the bot is processing the command.
69-
client.SendChatAction(
70-
message.Chat.Id,
71-
ChatActions.Typing
72-
);
68+
client.SendChatAction(message.Chat.Id, ChatActions.Typing);
7369

7470
var action = args.FirstOrDefault() ?? "help";
7571
switch (action)
7672
{
7773
case ADD_VALUE:
7874

7975
// Initialize the user state.
80-
var initialData = new Dictionary<string, object?>()
76+
var initialData = new Dictionary<string, string?>()
8177
{
8278
{ ACTION_PARAM_NAME, ADD_VALUE },
8379
{ NAME_PARAM_NAME, args.ElementAtOrDefault(1) },
@@ -88,8 +84,11 @@ CancellationToken cancellationToken
8884
// If the third argument is provided, change it to a boolean.
8985
if (initialData[IS_PUBLIC_PARAM_NAME] is string isPublic)
9086
{
91-
initialData[IS_PUBLIC_PARAM_NAME] =
92-
isPublic == "true" || isPublic == "--public";
87+
initialData[IS_PUBLIC_PARAM_NAME] = (
88+
isPublic == "true" || isPublic == "--public"
89+
)
90+
.ToString()
91+
.ToLowerInvariant();
9392
}
9493

9594
return this.InitializeStateAsync(initialData, message, cancellationToken);
@@ -105,10 +104,10 @@ CancellationToken cancellationToken
105104
}
106105

107106
return this.InitializeStateAsync(
108-
new Dictionary<string, object?>
107+
new Dictionary<string, string?>
109108
{
110109
{ ACTION_PARAM_NAME, DELETE_VALUE },
111-
{ NAME_PARAM_NAME, args[1] }
110+
{ NAME_PARAM_NAME, args[1] },
112111
},
113112
message,
114113
cancellationToken
@@ -133,9 +132,9 @@ CancellationToken cancellationToken
133132
replyParameters: new ReplyParameters
134133
{
135134
MessageId = message.MessageId,
136-
AllowSendingWithoutReply = true
135+
AllowSendingWithoutReply = true,
137136
},
138-
linkPreviewOptions: new LinkPreviewOptions { IsDisabled = true, },
137+
linkPreviewOptions: new LinkPreviewOptions { IsDisabled = true },
139138
cancellationToken: cancellationToken
140139
);
141140

@@ -153,9 +152,9 @@ CancellationToken cancellationToken
153152
replyParameters: new ReplyParameters
154153
{
155154
MessageId = message.MessageId,
156-
AllowSendingWithoutReply = true
155+
AllowSendingWithoutReply = true,
157156
},
158-
linkPreviewOptions: new LinkPreviewOptions { IsDisabled = true, },
157+
linkPreviewOptions: new LinkPreviewOptions { IsDisabled = true },
159158
cancellationToken: cancellationToken
160159
);
161160
}
@@ -167,9 +166,9 @@ CancellationToken cancellationToken
167166
replyParameters: new ReplyParameters
168167
{
169168
MessageId = message.MessageId,
170-
AllowSendingWithoutReply = true
169+
AllowSendingWithoutReply = true,
171170
},
172-
linkPreviewOptions: new LinkPreviewOptions { IsDisabled = true, },
171+
linkPreviewOptions: new LinkPreviewOptions { IsDisabled = true },
173172
cancellationToken: cancellationToken
174173
);
175174
default:
@@ -180,9 +179,9 @@ CancellationToken cancellationToken
180179
replyParameters: new ReplyParameters
181180
{
182181
MessageId = message.MessageId,
183-
AllowSendingWithoutReply = true
182+
AllowSendingWithoutReply = true,
184183
},
185-
linkPreviewOptions: new LinkPreviewOptions { IsDisabled = true, },
184+
linkPreviewOptions: new LinkPreviewOptions { IsDisabled = true },
186185
cancellationToken: cancellationToken
187186
);
188187
}
@@ -192,7 +191,7 @@ CancellationToken cancellationToken
192191
}
193192

194193
protected async Task InitializeStateAsync(
195-
IDictionary<string, object?> initialData,
194+
IDictionary<string, string?> initialData,
196195
Message message,
197196
CancellationToken cancellationToken
198197
)
@@ -222,9 +221,9 @@ CancellationToken cancellationToken
222221
{
223222
case ADD_VALUE:
224223
{
225-
var name = (string?)userState.Data[NAME_PARAM_NAME];
226-
var apikey = (string?)userState.Data[API_KEY_PARAM_NAME];
227-
var isPublic = (bool?)userState.Data[IS_PUBLIC_PARAM_NAME];
224+
var name = userState.Data[NAME_PARAM_NAME];
225+
var apikey = userState.Data[API_KEY_PARAM_NAME];
226+
var isPublic = userState.Data[IS_PUBLIC_PARAM_NAME];
228227

229228
// If the name for the apikey is not defined, try to get it from the message.
230229
if (string.IsNullOrEmpty(name))
@@ -239,7 +238,7 @@ await client.SendMessageAsync(
239238
replyParameters: new ReplyParameters
240239
{
241240
MessageId = message.MessageId,
242-
AllowSendingWithoutReply = true
241+
AllowSendingWithoutReply = true,
243242
},
244243
cancellationToken: cancellationToken
245244
);
@@ -263,7 +262,7 @@ await client.SendMessageAsync(
263262
replyParameters: new ReplyParameters
264263
{
265264
MessageId = message.MessageId,
266-
AllowSendingWithoutReply = true
265+
AllowSendingWithoutReply = true,
267266
},
268267
cancellationToken: cancellationToken
269268
);
@@ -298,7 +297,7 @@ await client.SendMessageAsync(
298297
replyParameters: new ReplyParameters
299298
{
300299
MessageId = message.MessageId,
301-
AllowSendingWithoutReply = true
300+
AllowSendingWithoutReply = true,
302301
},
303302
cancellationToken: cancellationToken
304303
);
@@ -313,7 +312,7 @@ await client.SendMessageAsync(
313312
replyParameters: new ReplyParameters
314313
{
315314
MessageId = message.MessageId,
316-
AllowSendingWithoutReply = true
315+
AllowSendingWithoutReply = true,
317316
},
318317
cancellationToken: cancellationToken
319318
);
@@ -336,8 +335,9 @@ await client.SendMessageAsync(
336335
replyParameters: new ReplyParameters
337336
{
338337
MessageId = message.MessageId,
339-
AllowSendingWithoutReply = true
338+
AllowSendingWithoutReply = true,
340339
},
340+
replyMarkup: new ReplyKeyboardRemove(),
341341
cancellationToken: cancellationToken
342342
);
343343
// Remove the user state.
@@ -354,7 +354,7 @@ await client.SendMessageAsync(
354354
replyParameters: new ReplyParameters
355355
{
356356
MessageId = message.MessageId,
357-
AllowSendingWithoutReply = true
357+
AllowSendingWithoutReply = true,
358358
},
359359
cancellationToken: cancellationToken
360360
);
@@ -365,6 +365,8 @@ await client.SendMessageAsync(
365365

366366
// Back up the apikey in the user state if it was not defined before.
367367
userState.Data[API_KEY_PARAM_NAME] ??= apikey;
368+
// Back up the user state.
369+
stateManager.CreateOrUpdateState(userState);
368370

369371
// If the user has not decided if share the apikey, check it their response or ask again.
370372
if (isPublic is null)
@@ -382,11 +384,11 @@ await client.SendMessageAsync(
382384
replyParameters: new ReplyParameters
383385
{
384386
MessageId = message.MessageId,
385-
AllowSendingWithoutReply = true
387+
AllowSendingWithoutReply = true,
386388
},
387389
replyMarkup: new ReplyKeyboardMarkup(keyboard)
388390
{
389-
ResizeKeyboard = true
391+
ResizeKeyboard = true,
390392
},
391393
cancellationToken: cancellationToken
392394
);
@@ -395,8 +397,10 @@ await client.SendMessageAsync(
395397
// Otherwise, try to parse the user response.
396398
else
397399
{
398-
isPublic = userResponse == this.YesLabel;
399-
if (isPublic == false && userResponse != this.NoLabel)
400+
isPublic = (userResponse == this.YesLabel)
401+
.ToString()
402+
.ToLowerInvariant();
403+
if (isPublic == "false" && userResponse != this.NoLabel)
400404
{
401405
userResponse = null;
402406
goto noResponse;
@@ -419,21 +423,21 @@ await client.SendMessageAsync(
419423
replyParameters: new ReplyParameters
420424
{
421425
MessageId = message.MessageId,
422-
AllowSendingWithoutReply = true
426+
AllowSendingWithoutReply = true,
423427
},
424428
replyMarkup: new ReplyKeyboardRemove(),
425429
cancellationToken: cancellationToken
426430
);
427431
// Save the apikey in the database.
428432
this.User.ApiKeys.Add(
429-
new SauceApiKey(name, apikey) { IsPublic = isPublic == true }
433+
new SauceApiKey(name, apikey) { IsPublic = isPublic == "true" }
430434
);
431435
await userRepository.UpdateAsync(this.User, cancellationToken);
432436
}
433437
break;
434438
case DELETE_VALUE:
435439
{
436-
var name = (string)userState.Data[NAME_PARAM_NAME]!;
440+
var name = userState.Data[NAME_PARAM_NAME]!;
437441
// If the user has not responded, ask.
438442
if (string.IsNullOrEmpty(message.Text))
439443
{
@@ -447,11 +451,11 @@ await client.SendMessageAsync(
447451
replyParameters: new ReplyParameters
448452
{
449453
MessageId = message.MessageId,
450-
AllowSendingWithoutReply = true
454+
AllowSendingWithoutReply = true,
451455
},
452456
replyMarkup: new ReplyKeyboardMarkup(keyboard)
453457
{
454-
ResizeKeyboard = true
458+
ResizeKeyboard = true,
455459
},
456460
cancellationToken: cancellationToken
457461
);
@@ -473,7 +477,7 @@ await client.SendMessageAsync(
473477
replyParameters: new ReplyParameters
474478
{
475479
MessageId = message.MessageId,
476-
AllowSendingWithoutReply = true
480+
AllowSendingWithoutReply = true,
477481
},
478482
replyMarkup: new ReplyKeyboardRemove(),
479483
cancellationToken: cancellationToken
@@ -492,7 +496,7 @@ await client.SendMessageAsync(
492496
replyParameters: new ReplyParameters
493497
{
494498
MessageId = message.MessageId,
495-
AllowSendingWithoutReply = true
499+
AllowSendingWithoutReply = true,
496500
},
497501
replyMarkup: new ReplyKeyboardRemove(),
498502
cancellationToken: cancellationToken

0 commit comments

Comments
 (0)