feat: Chat request implementation #14

Manually merged
awiteb merged 55 commits from awiteb/chat-request-and-response into master 2024-07-18 14:21:39 +02:00 AGit
2 changed files with 24 additions and 9 deletions
Showing only changes of commit 9c1ba97791 - Show all commits

View file

@ -69,21 +69,23 @@ impl OutChatRequestsExt for DatabaseConnection {
requester: &UserModel, requester: &UserModel,
recipient: &PublicKey, recipient: &PublicKey,
) -> ServerResult<()> { ) -> ServerResult<()> {
if self if let Err(err) = (OutChatRequestsActiveModel {
.have_chat_request_to(requester, recipient)
.await?
.is_some()
{
return Err(WsError::AlreadySendChatRequest.into());
}
OutChatRequestsActiveModel {
sender_id: Set(requester.id), sender_id: Set(requester.id),
recipient: Set(recipient.to_string()), recipient: Set(recipient.to_string()),
out_on: Set(Utc::now()), out_on: Set(Utc::now()),
..Default::default() ..Default::default()
} }
.save(self) .save(self)
.await?; .await)
{
match err.sql_err() {
Some(SqlErr::UniqueConstraintViolation(_)) => {
return Err(WsError::AlreadySendChatRequest.into());
}
_ => return Err(err.into()),
}
}
Ok(()) Ok(())
} }

View file

@ -29,6 +29,19 @@ pub struct Migration;
#[async_trait::async_trait] #[async_trait::async_trait]
impl MigrationTrait for Migration { impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_index(
Index::create()
.if_not_exists()
.name("sep_request")
.table(OutChatRequests::Table)
.col(OutChatRequests::SenderId)
.col(OutChatRequests::Recipient)
.unique()
.to_owned(),
)
.await?;
manager manager
.create_table( .create_table(
Table::create() Table::create()