🐛 Fixes in bot

This commit is contained in:
2025-08-23 14:20:21 +08:00
parent 5d7429a416
commit f759b19bcb
4 changed files with 48 additions and 23 deletions

View File

@@ -64,7 +64,7 @@ public class BotAccountController(
public string? Name { get; set; } = string.Empty;
[MaxLength(256)] public string? Nick { get; set; } = string.Empty;
[Required] [MaxLength(1024)] public string Slug { get; set; } = string.Empty;
[MaxLength(128)] public string? Language { get; set; }
@@ -146,13 +146,17 @@ public class BotAccountController(
if (project is null)
return NotFound("Project not found or you don't have access");
var now = SystemClock.Instance.GetCurrentInstant();
var accountId = Guid.NewGuid();
var account = new Account()
{
Id = accountId.ToString(),
Name = createRequest.Name,
Nick = createRequest.Nick,
Language = createRequest.Language,
Profile = new AccountProfile()
{
Id = Guid.NewGuid().ToString(),
Bio = createRequest.Bio,
Gender = createRequest.Gender,
FirstName = createRequest.FirstName,
@@ -162,14 +166,23 @@ public class BotAccountController(
Pronouns = createRequest.Pronouns,
Location = createRequest.Location,
Birthday = createRequest.Birthday?.ToTimestamp(),
Picture = new CloudFile() { Id = createRequest.PictureId },
Background = new CloudFile() { Id = createRequest.BackgroundId }
}
AccountId = accountId.ToString(),
CreatedAt = now.ToTimestamp(),
UpdatedAt = now.ToTimestamp()
},
CreatedAt = now.ToTimestamp(),
UpdatedAt = now.ToTimestamp()
};
try
{
var bot = await botService.CreateBotAsync(project, createRequest.Slug, account);
var bot = await botService.CreateBotAsync(
project,
createRequest.Slug,
account,
createRequest.PictureId,
createRequest.BackgroundId
);
return Ok(bot);
}
catch (Exception ex)
@@ -220,15 +233,14 @@ public class BotAccountController(
if (request.Pronouns is not null) botAccount.Profile.Pronouns = request.Pronouns;
if (request.Location is not null) botAccount.Profile.Location = request.Location;
if (request.Birthday is not null) botAccount.Profile.Birthday = request.Birthday?.ToTimestamp();
if (request.PictureId is not null) botAccount.Profile.Picture = new CloudFile() { Id = request.PictureId };
if (request.BackgroundId is not null)
botAccount.Profile.Background = new CloudFile() { Id = request.BackgroundId };
try
{
var updatedBot = await botService.UpdateBotAsync(
bot,
botAccount,
request.PictureId,
request.BackgroundId,
request.Slug,
request.IsActive
);

View File

@@ -28,23 +28,30 @@ public class BotAccountService(
.ToListAsync();
}
public async Task<BotAccount> CreateBotAsync(DevProject project, string slug, Account account)
public async Task<BotAccount> CreateBotAsync(
DevProject project,
string slug,
Account account,
string? pictureId,
string? backgroundId
)
{
// First, check if a bot with this slug already exists in this project
var existingBot = await db.BotAccounts
.FirstOrDefaultAsync(b => b.ProjectId == project.Id && b.Slug == slug);
if (existingBot != null)
{
throw new InvalidOperationException("A bot with this slug already exists in this project.");
}
try
{
var automatedId = Guid.NewGuid();
var createRequest = new CreateBotAccountRequest
{
AutomatedId = Guid.NewGuid().ToString(),
Account = account
AutomatedId = automatedId.ToString(),
Account = account,
PictureId = pictureId,
BackgroundId = backgroundId
};
var createResponse = await accountReceiver.CreateBotAccountAsync(createRequest);
@@ -53,7 +60,7 @@ public class BotAccountService(
// Then create the local bot account
var bot = new BotAccount
{
Id = Guid.Parse(botAccount.AutomatedId),
Id = automatedId,
Slug = slug,
ProjectId = project.Id,
Project = project,
@@ -82,8 +89,13 @@ public class BotAccountService(
}
}
public async Task<BotAccount> UpdateBotAsync(BotAccount bot, Account account, string? slug = null,
bool? isActive = null)
public async Task<BotAccount> UpdateBotAsync(BotAccount bot,
Account account,
string? pictureId,
string? backgroundId,
string? slug = null,
bool? isActive = null
)
{
var updated = false;
if (slug != null && bot.Slug != slug)
@@ -106,7 +118,9 @@ public class BotAccountService(
var updateRequest = new UpdateBotAccountRequest
{
AutomatedId = bot.Id.ToString(),
Account = account
Account = account,
PictureId = pictureId,
BackgroundId = backgroundId
};
var updateResponse = await accountReceiver.UpdateBotAccountAsync(updateRequest);
@@ -151,7 +165,7 @@ public class BotAccountService(
db.BotAccounts.Remove(bot);
await db.SaveChangesAsync();
}
public async Task<BotAccount?> LoadBotAccountAsync(BotAccount bot) =>
(await LoadBotsAccountAsync([bot])).FirstOrDefault();

View File

@@ -82,11 +82,10 @@ public class Account : ModelBase
: null,
CreatedAt = proto.CreatedAt.ToInstant(),
UpdatedAt = proto.UpdatedAt.ToInstant(),
AutomatedId = proto.AutomatedId is not null ? Guid.Parse(proto.AutomatedId) : null
AutomatedId = proto.AutomatedId is not null ? Guid.Parse(proto.AutomatedId) : null,
Profile = AccountProfile.FromProtoValue(proto.Profile)
};
account.Profile = AccountProfile.FromProtoValue(proto.Profile);
foreach (var contactProto in proto.Contacts)
account.Contacts.Add(AccountContact.FromProtoValue(contactProto));

View File

@@ -33,7 +33,7 @@ message Account {
google.protobuf.Timestamp created_at = 14;
google.protobuf.Timestamp updated_at = 15;
optional string automated_id = 17;
google.protobuf.StringValue automated_id = 17;
}
// Enum for status attitude