lemmy/crates/db_schema/src/traits.rs

183 lines
4.3 KiB
Rust
Raw Normal View History

2022-11-09 10:05:00 +00:00
use crate::{
newtypes::{CommunityId, DbUrl, PersonId},
utils::DbPool,
};
use diesel::result::Error;
2021-10-16 13:33:38 +00:00
2022-11-09 10:05:00 +00:00
#[async_trait]
2021-10-16 13:33:38 +00:00
pub trait Crud {
type InsertForm;
type UpdateForm;
2021-10-16 13:33:38 +00:00
type IdType;
2022-11-09 10:05:00 +00:00
async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error>
2021-10-16 13:33:38 +00:00
where
Self: Sized;
2022-11-09 10:05:00 +00:00
async fn read(pool: &DbPool, id: Self::IdType) -> Result<Self, Error>
2021-10-16 13:33:38 +00:00
where
Self: Sized;
/// when you want to null out a column, you have to send Some(None)), since sending None means you just don't want to update that column.
2022-11-09 10:05:00 +00:00
async fn update(pool: &DbPool, id: Self::IdType, form: &Self::UpdateForm) -> Result<Self, Error>
2021-10-16 13:33:38 +00:00
where
Self: Sized;
2022-11-09 10:05:00 +00:00
async fn delete(_pool: &DbPool, _id: Self::IdType) -> Result<usize, Error>
2021-10-16 13:33:38 +00:00
where
Self: Sized,
2022-11-09 10:05:00 +00:00
Self::IdType: Send,
2021-10-16 13:33:38 +00:00
{
2022-11-09 10:05:00 +00:00
async { Err(Error::NotFound) }.await
2021-10-16 13:33:38 +00:00
}
}
2022-11-09 10:05:00 +00:00
#[async_trait]
2021-10-16 13:33:38 +00:00
pub trait Followable {
type Form;
2022-11-09 10:05:00 +00:00
async fn follow(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
2021-10-16 13:33:38 +00:00
where
Self: Sized;
2022-11-09 10:05:00 +00:00
async fn follow_accepted(
pool: &DbPool,
2021-10-16 13:33:38 +00:00
community_id: CommunityId,
person_id: PersonId,
) -> Result<Self, Error>
where
Self: Sized;
2022-11-09 10:05:00 +00:00
async fn unfollow(pool: &DbPool, form: &Self::Form) -> Result<usize, Error>
2021-10-16 13:33:38 +00:00
where
Self: Sized;
}
2022-11-09 10:05:00 +00:00
#[async_trait]
2021-10-16 13:33:38 +00:00
pub trait Joinable {
type Form;
2022-11-09 10:05:00 +00:00
async fn join(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
2021-10-16 13:33:38 +00:00
where
Self: Sized;
2022-11-09 10:05:00 +00:00
async fn leave(pool: &DbPool, form: &Self::Form) -> Result<usize, Error>
2021-10-16 13:33:38 +00:00
where
Self: Sized;
}
2022-11-09 10:05:00 +00:00
#[async_trait]
2021-10-16 13:33:38 +00:00
pub trait Likeable {
type Form;
type IdType;
2022-11-09 10:05:00 +00:00
async fn like(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
2021-10-16 13:33:38 +00:00
where
Self: Sized;
2022-11-09 10:05:00 +00:00
async fn remove(
pool: &DbPool,
2021-10-16 13:33:38 +00:00
person_id: PersonId,
item_id: Self::IdType,
) -> Result<usize, Error>
where
Self: Sized;
}
2022-11-09 10:05:00 +00:00
#[async_trait]
2021-10-16 13:33:38 +00:00
pub trait Bannable {
type Form;
2022-11-09 10:05:00 +00:00
async fn ban(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
2021-10-16 13:33:38 +00:00
where
Self: Sized;
2022-11-09 10:05:00 +00:00
async fn unban(pool: &DbPool, form: &Self::Form) -> Result<usize, Error>
2021-10-16 13:33:38 +00:00
where
Self: Sized;
}
2022-11-09 10:05:00 +00:00
#[async_trait]
2021-10-16 13:33:38 +00:00
pub trait Saveable {
type Form;
2022-11-09 10:05:00 +00:00
async fn save(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
2021-10-16 13:33:38 +00:00
where
Self: Sized;
2022-11-09 10:05:00 +00:00
async fn unsave(pool: &DbPool, form: &Self::Form) -> Result<usize, Error>
2021-10-16 13:33:38 +00:00
where
Self: Sized;
}
2022-11-09 10:05:00 +00:00
#[async_trait]
2021-10-16 13:33:38 +00:00
pub trait Blockable {
type Form;
2022-11-09 10:05:00 +00:00
async fn block(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
2021-10-16 13:33:38 +00:00
where
Self: Sized;
2022-11-09 10:05:00 +00:00
async fn unblock(pool: &DbPool, form: &Self::Form) -> Result<usize, Error>
2021-10-16 13:33:38 +00:00
where
Self: Sized;
}
2022-11-09 10:05:00 +00:00
#[async_trait]
2021-10-16 13:33:38 +00:00
pub trait Readable {
type Form;
2022-11-09 10:05:00 +00:00
async fn mark_as_read(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
2021-10-16 13:33:38 +00:00
where
Self: Sized;
2022-11-09 10:05:00 +00:00
async fn mark_as_unread(pool: &DbPool, form: &Self::Form) -> Result<usize, Error>
2021-10-16 13:33:38 +00:00
where
Self: Sized;
}
2022-11-09 10:05:00 +00:00
#[async_trait]
2021-10-16 13:33:38 +00:00
pub trait Reportable {
type Form;
type IdType;
2022-11-09 10:05:00 +00:00
async fn report(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
2021-10-16 13:33:38 +00:00
where
Self: Sized;
2022-11-09 10:05:00 +00:00
async fn resolve(
pool: &DbPool,
2021-10-16 13:33:38 +00:00
report_id: Self::IdType,
resolver_id: PersonId,
) -> Result<usize, Error>
where
Self: Sized;
2022-11-09 10:05:00 +00:00
async fn unresolve(
pool: &DbPool,
2021-10-16 13:33:38 +00:00
report_id: Self::IdType,
resolver_id: PersonId,
) -> Result<usize, Error>
where
Self: Sized;
}
pub trait ToSafe {
type SafeColumns;
fn safe_columns_tuple() -> Self::SafeColumns;
}
pub trait ToSafeSettings {
type SafeSettingsColumns;
fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns;
}
pub trait ViewToVec {
type DbTuple;
fn from_tuple_to_vec(tuple: Vec<Self::DbTuple>) -> Vec<Self>
where
Self: Sized;
}
2022-11-09 10:05:00 +00:00
#[async_trait]
pub trait ApubActor {
// TODO: this should be in a trait ApubObject (and implemented for Post, Comment, PrivateMessage as well)
2022-11-09 10:05:00 +00:00
async fn read_from_apub_id(pool: &DbPool, object_id: &DbUrl) -> Result<Option<Self>, Error>
where
Self: Sized;
/// - actor_name is the name of the community or user to read.
/// - include_deleted, if true, will return communities or users that were deleted/removed
2022-11-09 10:05:00 +00:00
async fn read_from_name(
pool: &DbPool,
actor_name: &str,
include_deleted: bool,
) -> Result<Self, Error>
where
Self: Sized;
2022-11-09 10:05:00 +00:00
async fn read_from_name_and_domain(
pool: &DbPool,
actor_name: &str,
protocol_domain: &str,
) -> Result<Self, Error>
where
Self: Sized;
}