Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
568 changes: 0 additions & 568 deletions application/src/service/account.rs

This file was deleted.

106 changes: 106 additions & 0 deletions application/src/service/account/create.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use crate::signing_key::CreateSigningKeyUseCase;
use crate::transfer::account::{AccountDto, CreateAccountDto};
use adapter::crypto::DependOnSigningKeyGenerator;
use adapter::processor::account::{
AccountCommandProcessor, CreateAccountParam, DependOnAccountCommandProcessor,
};
use adapter::processor::profile::{
CreateProfileParam, DependOnProfileCommandProcessor, ProfileCommandProcessor,
};
use kernel::interfaces::config::DependOnPublicBaseUrl;
use kernel::interfaces::crypto::{DependOnPasswordProvider, SigningAlgorithm};
use kernel::interfaces::database::DatabaseConnection;
use kernel::interfaces::permission::{
AccountRelation, DependOnPermissionWriter, PermissionWriter, RelationTarget,
};
use kernel::interfaces::repository::DependOnSigningKeyRepository;
use kernel::prelude::entity::{
AccountIsBot, AccountName, AuthAccountId, Nanoid, Profile, ProfileDisplayName,
};
use kernel::KernelError;
use std::future::Future;

pub trait CreateAccountUseCase:
'static
+ Sync
+ Send
+ DependOnAccountCommandProcessor
+ DependOnProfileCommandProcessor
+ DependOnPasswordProvider
+ DependOnSigningKeyGenerator
+ DependOnPermissionWriter
+ DependOnSigningKeyRepository
+ DependOnPublicBaseUrl
{
fn create_account(
&self,
auth_account_id: AuthAccountId,
dto: CreateAccountDto,
) -> impl Future<Output = error_stack::Result<AccountDto, KernelError>> + Send {
async move {
let mut transaction = self.database_connection().get_executor().await?;

let account_name = AccountName::new(dto.name);
let account_is_bot = AccountIsBot::new(dto.is_bot);

let display_name = ProfileDisplayName::new(account_name.as_ref().to_string());

let account = self
.account_command_processor()
.create(
&mut transaction,
CreateAccountParam {
name: account_name,
is_bot: account_is_bot,
auth_account_id: auth_account_id.clone(),
},
)
.await?;

self.profile_command_processor()
.create(
&mut transaction,
CreateProfileParam {
account_id: account.id().clone(),
display_name: Some(display_name),
summary: None,
icon: None,
banner: None,
nano_id: Nanoid::<Profile>::default(),
},
)
.await?;

self.permission_writer()
.create_relation(
&RelationTarget::Account {
account_id: account.id().clone(),
relation: AccountRelation::Owner,
},
&auth_account_id,
)
.await?;

self.create(
account.id().clone(),
account.nanoid(),
SigningAlgorithm::Rsa2048,
)
.await?;

Ok(AccountDto::from(account))
}
}
}

impl<T> CreateAccountUseCase for T where
T: 'static
+ DependOnAccountCommandProcessor
+ DependOnProfileCommandProcessor
+ DependOnPasswordProvider
+ DependOnSigningKeyGenerator
+ DependOnPermissionWriter
+ DependOnSigningKeyRepository
+ DependOnPublicBaseUrl
{
}
86 changes: 86 additions & 0 deletions application/src/service/account/deactivate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use super::rehydrate::rehydrate_account;
use crate::permission::{account_deactivate, check_permission};
use adapter::processor::account::{
AccountCommandProcessor, AccountQueryProcessor, DependOnAccountCommandProcessor,
DependOnAccountQueryProcessor,
};
use error_stack::Report;
use kernel::interfaces::database::DatabaseConnection;
use kernel::interfaces::event_store::DependOnAccountEventStore;
use kernel::interfaces::permission::{
AccountRelation, DependOnPermissionChecker, DependOnPermissionWriter, PermissionWriter,
RelationTarget,
};
use kernel::prelude::entity::{Account, AuthAccountId, Nanoid};
use kernel::KernelError;
use std::future::Future;

pub trait DeactivateAccountUseCase:
'static
+ Sync
+ Send
+ DependOnAccountCommandProcessor
+ DependOnAccountQueryProcessor
+ DependOnAccountEventStore
+ DependOnPermissionChecker
+ DependOnPermissionWriter
{
fn deactivate_account(
&self,
auth_account_id: &AuthAccountId,
account_id: String,
) -> impl Future<Output = error_stack::Result<(), KernelError>> + Send {
async move {
let mut transaction = self.database_connection().get_executor().await?;

let nanoid = Nanoid::<Account>::new(account_id);
let projection = self
.account_query_processor()
.find_by_nanoid(&mut transaction, &nanoid)
.await?
.ok_or_else(|| {
Report::new(KernelError::NotFound).attach_printable(format!(
"Account not found with nanoid: {}",
nanoid.as_ref()
))
})?;

check_permission(self, auth_account_id, &account_deactivate(projection.id())).await?;

let account_id = projection.id().clone();
let (_account, current_version) =
rehydrate_account(self, &mut transaction, &account_id).await?;
self.account_command_processor()
.deactivate(&mut transaction, account_id.clone(), current_version)
.await?;

for relation in [
AccountRelation::Owner,
AccountRelation::Editor,
AccountRelation::Signer,
] {
self.permission_writer()
.delete_relation(
&RelationTarget::Account {
account_id: account_id.clone(),
relation,
},
auth_account_id,
)
.await?;
}

Ok(())
}
}
}

impl<T> DeactivateAccountUseCase for T where
T: 'static
+ DependOnAccountCommandProcessor
+ DependOnAccountQueryProcessor
+ DependOnAccountEventStore
+ DependOnPermissionChecker
+ DependOnPermissionWriter
{
}
13 changes: 13 additions & 0 deletions application/src/service/account/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mod create;
mod deactivate;
mod moderation;
mod read;
mod rehydrate;
mod update;

pub use create::CreateAccountUseCase;
pub use deactivate::DeactivateAccountUseCase;
pub use moderation::{BanAccountUseCase, SuspendAccountUseCase, UnsuspendAccountUseCase};
pub use read::GetAccountUseCase;
pub(crate) use rehydrate::rehydrate_account;
pub use update::UpdateAccountUseCase;
Loading