feat: Chat request implementation #14
2 changed files with 24 additions and 9 deletions
|
@ -69,21 +69,23 @@ impl OutChatRequestsExt for DatabaseConnection {
|
|||
requester: &UserModel,
|
||||
recipient: &PublicKey,
|
||||
) -> ServerResult<()> {
|
||||
if self
|
||||
.have_chat_request_to(requester, recipient)
|
||||
.await?
|
||||
.is_some()
|
||||
{
|
||||
return Err(WsError::AlreadySendChatRequest.into());
|
||||
}
|
||||
OutChatRequestsActiveModel {
|
||||
if let Err(err) = (OutChatRequestsActiveModel {
|
||||
sender_id: Set(requester.id),
|
||||
recipient: Set(recipient.to_string()),
|
||||
out_on: Set(Utc::now()),
|
||||
..Default::default()
|
||||
}
|
||||
.save(self)
|
||||
.await?;
|
||||
.await)
|
||||
{
|
||||
match err.sql_err() {
|
||||
Some(SqlErr::UniqueConstraintViolation(_)) => {
|
||||
return Err(WsError::AlreadySendChatRequest.into());
|
||||
}
|
||||
_ => return Err(err.into()),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
@ -29,6 +29,19 @@ pub struct Migration;
|
|||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
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
|
||||
.create_table(
|
||||
Table::create()
|
||||
|
|
Loading…
Reference in a new issue