From 9215d836de14e904e5acc8e364756a88eb96b056 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Tue, 9 Jul 2024 11:16:57 +0300 Subject: [PATCH] feat: Blacklist table Signed-off-by: Awiteb --- crates/oxidetalis_entities/src/blacklist.rs | 56 ++++++++++++++++ crates/oxidetalis_entities/src/lib.rs | 1 + crates/oxidetalis_entities/src/prelude.rs | 6 ++ crates/oxidetalis_entities/src/users.rs | 9 +++ .../src/create_blacklist_table.rs | 65 +++++++++++++++++++ crates/oxidetalis_migrations/src/lib.rs | 2 + 6 files changed, 139 insertions(+) create mode 100644 crates/oxidetalis_entities/src/blacklist.rs create mode 100644 crates/oxidetalis_migrations/src/create_blacklist_table.rs diff --git a/crates/oxidetalis_entities/src/blacklist.rs b/crates/oxidetalis_entities/src/blacklist.rs new file mode 100644 index 0000000..6d955a6 --- /dev/null +++ b/crates/oxidetalis_entities/src/blacklist.rs @@ -0,0 +1,56 @@ +// OxideTalis Messaging Protocol homeserver core implementation +// Copyright (c) 2024 OxideTalis Developers +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +use chrono::Utc; +use sea_orm::entity::prelude::*; + +use super::users::Entity as UserEntity; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "blacklist")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + pub user_id: i32, + /// Public key of the target + pub target: String, + pub blacklisted_at: chrono::DateTime, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "UserEntity", + from = "Column::UserId", + to = "super::users::Column::Id" + on_update = "NoAction", + on_delete = "Cascade" + )] + UserId, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::UserId.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/crates/oxidetalis_entities/src/lib.rs b/crates/oxidetalis_entities/src/lib.rs index 0fe9098..e758d98 100644 --- a/crates/oxidetalis_entities/src/lib.rs +++ b/crates/oxidetalis_entities/src/lib.rs @@ -19,6 +19,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. +pub mod blacklist; pub mod incoming_chat_requests; pub mod outgoing_chat_requests; pub mod prelude; diff --git a/crates/oxidetalis_entities/src/prelude.rs b/crates/oxidetalis_entities/src/prelude.rs index cd34ded..8d0b50e 100644 --- a/crates/oxidetalis_entities/src/prelude.rs +++ b/crates/oxidetalis_entities/src/prelude.rs @@ -33,6 +33,12 @@ pub use sea_orm::{ SqlErr, }; +pub use super::blacklist::{ + ActiveModel as BlacklistActiveModel, + Column as BlacklistColumn, + Entity as BlacklistEntity, + Model as BlacklistModel, +}; pub use super::incoming_chat_requests::{ ActiveModel as InChatRequestsActiveModel, Column as InChatRequestsColumn, diff --git a/crates/oxidetalis_entities/src/users.rs b/crates/oxidetalis_entities/src/users.rs index 24a88cd..9842826 100644 --- a/crates/oxidetalis_entities/src/users.rs +++ b/crates/oxidetalis_entities/src/users.rs @@ -21,6 +21,7 @@ use sea_orm::entity::prelude::*; +use super::blacklist::Entity as BlacklistEntity; use super::incoming_chat_requests::Entity as InChatRequestsEntity; use super::outgoing_chat_requests::Entity as OutChatRequestsEntity; @@ -39,6 +40,8 @@ pub enum Relation { InChatRequests, #[sea_orm(has_many = "OutChatRequestsEntity")] OutChatRequests, + #[sea_orm(has_many = "BlacklistEntity")] + Blacklist, } impl Related for Entity { @@ -53,4 +56,10 @@ impl Related for Entity { } } +impl Related for Entity { + fn to() -> RelationDef { + Relation::Blacklist.def() + } +} + impl ActiveModelBehavior for ActiveModel {} diff --git a/crates/oxidetalis_migrations/src/create_blacklist_table.rs b/crates/oxidetalis_migrations/src/create_blacklist_table.rs new file mode 100644 index 0000000..e459d2e --- /dev/null +++ b/crates/oxidetalis_migrations/src/create_blacklist_table.rs @@ -0,0 +1,65 @@ +// OxideTalis Messaging Protocol homeserver core implementation +// Copyright (c) 2024 OxideTalis Developers +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(Blacklist::Table) + .if_not_exists() + .col( + ColumnDef::new(Blacklist::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(Blacklist::UserId).integer().not_null()) + .col(ColumnDef::new(Blacklist::Target).string().not_null()) + .col(ColumnDef::new(Blacklist::Reason).string_len(400)) + .col( + ColumnDef::new(Blacklist::BlacklistedAt) + .timestamp_with_time_zone() + .not_null(), + ) + .to_owned(), + ) + .await + } +} + +#[derive(DeriveIden)] +enum Blacklist { + Table, + Id, + UserId, + /// Public key of the target + Target, + Reason, + BlacklistedAt, +} diff --git a/crates/oxidetalis_migrations/src/lib.rs b/crates/oxidetalis_migrations/src/lib.rs index 8a1009f..b50125c 100644 --- a/crates/oxidetalis_migrations/src/lib.rs +++ b/crates/oxidetalis_migrations/src/lib.rs @@ -21,6 +21,7 @@ pub use sea_orm_migration::prelude::*; +mod create_blacklist_table; mod create_incoming_chat_requests_table; mod create_outgoing_chat_requests_table; mod create_users_table; @@ -34,6 +35,7 @@ impl MigratorTrait for Migrator { Box::new(create_users_table::Migration), Box::new(create_incoming_chat_requests_table::Migration), Box::new(create_outgoing_chat_requests_table::Migration), + Box::new(create_blacklist_table::Migration), ] } }