✨ Account bot basis
This commit is contained in:
@@ -20,6 +20,9 @@ public class Account : ModelBase
|
||||
[MaxLength(32)] public string Language { get; set; } = string.Empty;
|
||||
public Instant? ActivatedAt { get; set; }
|
||||
public bool IsSuperuser { get; set; } = false;
|
||||
|
||||
// The ID is the BotAccount ID in the DysonNetwork.Develop
|
||||
public Guid? AutomatedId { get; set; }
|
||||
|
||||
public AccountProfile Profile { get; set; } = null!;
|
||||
public ICollection<AccountContact> Contacts { get; set; } = new List<AccountContact>();
|
||||
|
@@ -178,6 +178,29 @@ public class AccountService(
|
||||
);
|
||||
}
|
||||
|
||||
public async Task<Account> CreateBotAccount(Account account, Guid automatedId)
|
||||
{
|
||||
var dupeAutomateCount = await db.Accounts.Where(a => a.AutomatedId == automatedId).CountAsync();
|
||||
if (dupeAutomateCount > 0)
|
||||
throw new InvalidOperationException("Automated ID has already been used.");
|
||||
|
||||
var dupeNameCount = await db.Accounts.Where(a => a.Name == account.Name).CountAsync();
|
||||
if (dupeNameCount > 0)
|
||||
throw new InvalidOperationException("Account name has already been taken.");
|
||||
|
||||
account.AutomatedId = automatedId;
|
||||
account.ActivatedAt = SystemClock.Instance.GetCurrentInstant();
|
||||
account.IsSuperuser = false;
|
||||
db.Accounts.Add(account);
|
||||
await db.SaveChangesAsync();
|
||||
return account;
|
||||
}
|
||||
|
||||
public async Task<Account?> GetBotAccount(Guid automatedId)
|
||||
{
|
||||
return await db.Accounts.FirstOrDefaultAsync(a => a.AutomatedId == automatedId);
|
||||
}
|
||||
|
||||
public async Task RequestAccountDeletion(Account account)
|
||||
{
|
||||
var spell = await spells.CreateMagicSpell(
|
||||
@@ -665,21 +688,13 @@ public class AccountService(
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The maintenance method for server administrator.
|
||||
/// To check every user has an account profile and to create them if it isn't having one.
|
||||
/// </summary>
|
||||
public async Task EnsureAccountProfileCreated()
|
||||
public async Task DeleteAccount(Account account)
|
||||
{
|
||||
var accountsId = await db.Accounts.Select(a => a.Id).ToListAsync();
|
||||
var existingId = await db.AccountProfiles.Select(p => p.AccountId).ToListAsync();
|
||||
var missingId = accountsId.Except(existingId).ToList();
|
||||
|
||||
if (missingId.Count != 0)
|
||||
{
|
||||
var newProfiles = missingId.Select(id => new AccountProfile { Id = Guid.NewGuid(), AccountId = id })
|
||||
.ToList();
|
||||
await db.BulkInsertAsync(newProfiles);
|
||||
}
|
||||
await db.AuthSessions
|
||||
.Where(s => s.AccountId == account.Id)
|
||||
.ExecuteDeleteAsync();
|
||||
|
||||
db.Accounts.Remove(account);
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
}
|
77
DysonNetwork.Pass/Account/BotAccountReceiverGrpc.cs
Normal file
77
DysonNetwork.Pass/Account/BotAccountReceiverGrpc.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using DysonNetwork.Shared.Proto;
|
||||
using Grpc.Core;
|
||||
using NodaTime;
|
||||
using NodaTime.Serialization.Protobuf;
|
||||
|
||||
namespace DysonNetwork.Pass.Account;
|
||||
|
||||
public class BotAccountReceiverGrpc(AppDatabase db, AccountService accounts)
|
||||
: BotAccountReceiverService.BotAccountReceiverServiceBase
|
||||
{
|
||||
public override async Task<CreateBotAccountResponse> CreateBotAccount(
|
||||
CreateBotAccountRequest request,
|
||||
ServerCallContext context
|
||||
)
|
||||
{
|
||||
var account = Account.FromProtoValue(request.Account);
|
||||
account = await accounts.CreateBotAccount(account, Guid.Parse(request.AutomatedId));
|
||||
|
||||
return new CreateBotAccountResponse
|
||||
{
|
||||
Bot = new BotAccount
|
||||
{
|
||||
Account = account.ToProtoValue(),
|
||||
AutomatedId = account.Id.ToString(),
|
||||
CreatedAt = account.CreatedAt.ToTimestamp(),
|
||||
UpdatedAt = account.UpdatedAt.ToTimestamp(),
|
||||
IsActive = true
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public override async Task<UpdateBotAccountResponse> UpdateBotAccount(
|
||||
UpdateBotAccountRequest request,
|
||||
ServerCallContext context
|
||||
)
|
||||
{
|
||||
var automatedId = Guid.Parse(request.AutomatedId);
|
||||
var account = await accounts.GetBotAccount(automatedId);
|
||||
if (account is null)
|
||||
throw new RpcException(new Grpc.Core.Status(StatusCode.NotFound, "Account not found"));
|
||||
|
||||
account.Name = request.Account.Name;
|
||||
account.Nick = request.Account.Nick;
|
||||
account.Profile = AccountProfile.FromProtoValue(request.Account.Profile);
|
||||
account.Language = request.Account.Language;
|
||||
|
||||
db.Accounts.Update(account);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
return new UpdateBotAccountResponse
|
||||
{
|
||||
Bot = new BotAccount
|
||||
{
|
||||
Account = account.ToProtoValue(),
|
||||
AutomatedId = account.Id.ToString(),
|
||||
CreatedAt = account.CreatedAt.ToTimestamp(),
|
||||
UpdatedAt = account.UpdatedAt.ToTimestamp(),
|
||||
IsActive = true
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public override async Task<DeleteBotAccountResponse> DeleteBotAccount(
|
||||
DeleteBotAccountRequest request,
|
||||
ServerCallContext context
|
||||
)
|
||||
{
|
||||
var automatedId = Guid.Parse(request.AutomatedId);
|
||||
var account = await accounts.GetBotAccount(automatedId);
|
||||
if (account is null)
|
||||
throw new RpcException(new Grpc.Core.Status(StatusCode.NotFound, "Account not found"));
|
||||
|
||||
await accounts.DeleteAccount(account);
|
||||
|
||||
return new DeleteBotAccountResponse();
|
||||
}
|
||||
}
|
@@ -1,5 +1,4 @@
|
||||
using DysonNetwork.Pass;
|
||||
using DysonNetwork.Pass.Auth;
|
||||
using DysonNetwork.Pass.Pages.Data;
|
||||
using DysonNetwork.Pass.Startup;
|
||||
using DysonNetwork.Shared.Http;
|
||||
|
@@ -76,6 +76,7 @@ public static class ApplicationConfiguration
|
||||
app.MapGrpcService<AuthServiceGrpc>();
|
||||
app.MapGrpcService<ActionLogServiceGrpc>();
|
||||
app.MapGrpcService<PermissionServiceGrpc>();
|
||||
app.MapGrpcService<BotAccountReceiverGrpc>();
|
||||
|
||||
return app;
|
||||
}
|
||||
|
Reference in New Issue
Block a user