refactor: Refactor in_chat_requests table #35

Manually merged
awiteb merged 7 commits from awiteb/rename-chat-request-table-and-save-respnse-in-it into master 2024-07-30 10:39:04 +02:00 AGit
9 changed files with 38 additions and 41 deletions
Showing only changes of commit 0274444434 - Show all commits

View file

@ -14,7 +14,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://gnu.org/licenses/agpl-3.0>.
//! Database extension for the `in_chat_requests` table.
//! Database extension for the `incoming_chat` table.
use chrono::Utc;
use oxidetalis_core::types::PublicKey;
@ -23,8 +23,8 @@ use sea_orm::{sea_query::OnConflict, DatabaseConnection};
use crate::errors::ServerResult;
/// Extension trait for the `in_chat_requests` table.
pub trait InChatRequestsExt {
/// Extension trait for the `incoming_chat` table.
pub trait IncomingChatExt {
/// Save the chat request in the recipient table
async fn save_in_chat_request(
&self,
@ -33,26 +33,23 @@ pub trait InChatRequestsExt {
) -> ServerResult<()>;
}
impl InChatRequestsExt for DatabaseConnection {
impl IncomingChatExt for DatabaseConnection {
#[logcall::logcall]
async fn save_in_chat_request(
&self,
sender: &PublicKey,
recipient: &UserModel,
) -> ServerResult<()> {
InChatRequestsEntity::insert(InChatRequestsActiveModel {
IncomingChatEntity::insert(IncomingChatActiveModel {
recipient_id: Set(recipient.id),
sender: Set(*sender),
in_on: Set(Utc::now()),
..Default::default()
})
.on_conflict(
OnConflict::columns([
InChatRequestsColumn::RecipientId,
InChatRequestsColumn::Sender,
])
.do_nothing()
.to_owned(),
OnConflict::columns([IncomingChatColumn::RecipientId, IncomingChatColumn::Sender])
.do_nothing()
.to_owned(),
)
.exec(self)
.await?;

View file

@ -16,12 +16,12 @@
//! Database trait extensions.
mod in_chat_requests;
mod incoming_chat;
mod out_chat_requests;
mod user;
mod user_status;
pub use in_chat_requests::*;
pub use incoming_chat::*;
pub use out_chat_requests::*;
pub use user::*;
pub use user_status::*;

View file

@ -20,7 +20,7 @@ use oxidetalis_core::types::PublicKey;
use oxidetalis_entities::prelude::*;
use sea_orm::DatabaseConnection;
use crate::database::InChatRequestsExt;
use crate::database::IncomingChatExt;
use crate::errors::ServerError;
use crate::extensions::OnlineUsersExt;
use crate::{

View file

@ -19,7 +19,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//! Entity for `in_chat_requests` table
//! Entity for `incoming_chat` table
use chrono::Utc;
use oxidetalis_core::types::PublicKey;
@ -28,7 +28,7 @@ use sea_orm::entity::prelude::*;
use crate::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "in_chat_requests")]
#[sea_orm(table_name = "incoming_chat")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: UserId,

View file

@ -21,7 +21,7 @@
#![doc = include_str!("../README.md")]
pub mod incoming_chat_requests;
pub mod incoming_chat;
pub mod outgoing_chat_requests;
pub mod prelude;
pub mod users;

View file

@ -40,11 +40,11 @@ pub use sea_orm::{
/// User ID type
pub type UserId = i64;
pub use super::incoming_chat_requests::{
ActiveModel as InChatRequestsActiveModel,
Column as InChatRequestsColumn,
Entity as InChatRequestsEntity,
Model as InChatRequestsModel,
pub use super::incoming_chat::{
ActiveModel as IncomingChatActiveModel,
Column as IncomingChatColumn,
Entity as IncomingChatEntity,
Model as IncomingChatModel,
};
pub use super::outgoing_chat_requests::{
ActiveModel as OutChatRequestsActiveModel,

View file

@ -39,17 +39,17 @@ pub struct Model {
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "InChatRequestsEntity")]
InChatRequests,
#[sea_orm(has_many = "IncomingChatEntity")]
IncomingChat,
#[sea_orm(has_many = "OutChatRequestsEntity")]
OutChatRequests,
#[sea_orm(has_many = "UsersStatusEntity")]
UsersStatus,
}
impl Related<InChatRequestsEntity> for Entity {
impl Related<IncomingChatEntity> for Entity {
fn to() -> RelationDef {
Relation::InChatRequests.def()
Relation::IncomingChat.def()
}
}

View file

@ -19,8 +19,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//! Migration to create the `in_chat_requests` table, a table for incoming chat
//! requests from other users
//! Migration to create the `incoming_chat` table, a table for incoming chat
//! requests and responses from other users
use sea_orm_migration::prelude::*;
@ -35,31 +35,31 @@ impl MigrationTrait for Migration {
manager
.create_table(
Table::create()
.table(InChatRequests::Table)
.table(IncomingChat::Table)
.if_not_exists()
.col(
ColumnDef::new(InChatRequests::Id)
ColumnDef::new(IncomingChat::Id)
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(InChatRequests::RecipientId)
ColumnDef::new(IncomingChat::RecipientId)
.big_integer()
.not_null(),
)
.foreign_key(
ForeignKey::create()
.name("fk-in_chat_requests-users")
.from(InChatRequests::Table, InChatRequests::RecipientId)
.name("fk-incoming_chat-users")
.from(IncomingChat::Table, IncomingChat::RecipientId)
.to(Users::Table, Users::Id)
.on_update(ForeignKeyAction::NoAction)
.on_delete(ForeignKeyAction::Cascade),
)
.col(ColumnDef::new(InChatRequests::Sender).binary().not_null())
.col(ColumnDef::new(IncomingChat::Sender).binary().not_null())
.col(
ColumnDef::new(InChatRequests::InOn)
ColumnDef::new(IncomingChat::InOn)
.timestamp_with_time_zone()
.not_null(),
)
@ -71,9 +71,9 @@ impl MigrationTrait for Migration {
Index::create()
.if_not_exists()
.name("sep_request")
.table(InChatRequests::Table)
.col(InChatRequests::RecipientId)
.col(InChatRequests::Sender)
.table(IncomingChat::Table)
.col(IncomingChat::RecipientId)
.col(IncomingChat::Sender)
.unique()
.to_owned(),
)
@ -82,7 +82,7 @@ impl MigrationTrait for Migration {
}
#[derive(DeriveIden)]
enum InChatRequests {
enum IncomingChat {
Table,
Id,
RecipientId,

View file

@ -24,7 +24,7 @@
use sea_orm_migration::prelude::*;
pub use sea_orm_migration::MigratorTrait;
mod create_incoming_chat_requests_table;
mod create_incoming_chat_table;
mod create_outgoing_chat_requests_table;
mod create_users_status;
mod create_users_table;
@ -36,7 +36,7 @@ impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![
Box::new(create_users_table::Migration),
Box::new(create_incoming_chat_requests_table::Migration),
Box::new(create_incoming_chat_table::Migration),
Box::new(create_outgoing_chat_requests_table::Migration),
Box::new(create_users_status::Migration),
]