]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/lib.rs
Use same table join code for both read and list functions (#3663)
[lemmy.git] / crates / db_schema / src / lib.rs
index 3c8abaf79ff9bcec369fbbad08e19be4defac197..e3232c402e3feb42ab4ddfbb87700eee47d9f822 100644 (file)
+#![recursion_limit = "256"]
+
+#[cfg(feature = "full")]
 #[macro_use]
 extern crate diesel;
-
+#[cfg(feature = "full")]
 #[macro_use]
 extern crate diesel_derive_newtype;
 
-use chrono::NaiveDateTime;
-use diesel::{
-  backend::Backend,
-  deserialize::FromSql,
-  serialize::{Output, ToSql},
-  sql_types::Text,
-};
-use serde::{Deserialize, Serialize};
-use std::{
-  fmt,
-  fmt::{Display, Formatter},
-  io::Write,
-};
-use url::Url;
-
-pub mod schema;
-pub mod source;
+#[cfg(feature = "full")]
+#[macro_use]
+extern crate diesel_derive_enum;
 
-#[derive(
-  Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
-)]
-pub struct PostId(pub i32);
+// this is used in tests
+#[cfg(feature = "full")]
+#[macro_use]
+extern crate diesel_migrations;
 
-impl fmt::Display for PostId {
-  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-    write!(f, "{}", self.0)
-  }
+#[cfg(feature = "full")]
+#[macro_use]
+extern crate async_trait;
+
+pub mod aggregates;
+#[cfg(feature = "full")]
+pub mod impls;
+pub mod newtypes;
+#[cfg(feature = "full")]
+#[rustfmt::skip]
+#[allow(clippy::wildcard_imports)]
+pub mod schema;
+#[cfg(feature = "full")]
+pub mod aliases {
+  use crate::schema::person;
+  diesel::alias!(person as person1: Person1, person as person2: Person2);
 }
+pub mod source;
+#[cfg(feature = "full")]
+pub mod traits;
+#[cfg(feature = "full")]
+pub mod utils;
 
-#[derive(
-  Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
+use serde::{Deserialize, Serialize};
+use strum_macros::{Display, EnumString};
+#[cfg(feature = "full")]
+use ts_rs::TS;
+
+#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
+#[cfg_attr(feature = "full", derive(DbEnum, TS))]
+#[cfg_attr(
+  feature = "full",
+  ExistingTypePath = "crate::schema::sql_types::SortTypeEnum"
 )]
-pub struct PersonId(pub i32);
-
-#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
-pub struct CommentId(pub i32);
-
-impl fmt::Display for CommentId {
-  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-    write!(f, "{}", self.0)
-  }
+#[cfg_attr(feature = "full", DbValueStyle = "verbatim")]
+#[cfg_attr(feature = "full", ts(export))]
+/// The post sort types. See here for descriptions: https://join-lemmy.org/docs/en/users/03-votes-and-ranking.html
+pub enum SortType {
+  Active,
+  Hot,
+  New,
+  Old,
+  TopDay,
+  TopWeek,
+  TopMonth,
+  TopYear,
+  TopAll,
+  MostComments,
+  NewComments,
+  TopHour,
+  TopSixHour,
+  TopTwelveHour,
+  TopThreeMonths,
+  TopSixMonths,
+  TopNineMonths,
+  Controversial,
 }
 
-#[derive(
-  Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
-)]
-pub struct CommunityId(pub i32);
-
-#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
-pub struct LocalUserId(pub i32);
-
-#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
-pub struct PrivateMessageId(i32);
-
-impl fmt::Display for PrivateMessageId {
-  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-    write!(f, "{}", self.0)
-  }
+#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy)]
+#[cfg_attr(feature = "full", derive(TS))]
+#[cfg_attr(feature = "full", ts(export))]
+/// The comment sort types. See here for descriptions: https://join-lemmy.org/docs/en/users/03-votes-and-ranking.html
+pub enum CommentSortType {
+  Hot,
+  Top,
+  New,
+  Old,
+  Controversial,
 }
 
-#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
-pub struct PersonMentionId(i32);
-
-#[repr(transparent)]
-#[derive(Clone, PartialEq, Serialize, Deserialize, Debug, AsExpression, FromSqlRow)]
-#[sql_type = "Text"]
-pub struct DbUrl(Url);
-
-impl<DB: Backend> ToSql<Text, DB> for DbUrl
-where
-  String: ToSql<Text, DB>,
-{
-  fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> diesel::serialize::Result {
-    self.0.to_string().to_sql(out)
-  }
+#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy)]
+#[cfg_attr(feature = "full", derive(TS))]
+#[cfg_attr(feature = "full", ts(export))]
+/// The person sort types. See here for descriptions: https://join-lemmy.org/docs/en/users/03-votes-and-ranking.html
+pub enum PersonSortType {
+  New,
+  Old,
+  MostComments,
+  CommentScore,
+  PostScore,
+  PostCount,
 }
 
-impl<DB: Backend> FromSql<Text, DB> for DbUrl
-where
-  String: FromSql<Text, DB>,
-{
-  fn from_sql(bytes: Option<&DB::RawValue>) -> diesel::deserialize::Result<Self> {
-    let str = String::from_sql(bytes)?;
-    Ok(DbUrl(Url::parse(&str)?))
-  }
+#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
+#[cfg_attr(feature = "full", derive(DbEnum, TS))]
+#[cfg_attr(
+  feature = "full",
+  ExistingTypePath = "crate::schema::sql_types::ListingTypeEnum"
+)]
+#[cfg_attr(feature = "full", DbValueStyle = "verbatim")]
+#[cfg_attr(feature = "full", ts(export))]
+/// A listing type for post and comment list fetches.
+pub enum ListingType {
+  /// Content from your own site, as well as all connected / federated sites.
+  All,
+  /// Content from your site only.
+  Local,
+  /// Content only from communities you've subscribed to.
+  Subscribed,
 }
 
-impl DbUrl {
-  // TODO: remove this method and just use into()
-  pub fn into_inner(self) -> Url {
-    self.0
-  }
+#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
+#[cfg_attr(feature = "full", derive(DbEnum, TS))]
+#[cfg_attr(
+  feature = "full",
+  ExistingTypePath = "crate::schema::sql_types::RegistrationModeEnum"
+)]
+#[cfg_attr(feature = "full", DbValueStyle = "verbatim")]
+#[cfg_attr(feature = "full", ts(export))]
+/// The registration mode for your site. Determines what happens after a user signs up.
+pub enum RegistrationMode {
+  /// Closed to public.
+  Closed,
+  /// Open, but pending approval of a registration application.
+  RequireApplication,
+  /// Open to all.
+  Open,
 }
 
-impl Display for DbUrl {
-  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
-    self.to_owned().0.fmt(f)
-  }
+#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy)]
+#[cfg_attr(feature = "full", derive(TS))]
+#[cfg_attr(feature = "full", ts(export))]
+/// The type of content returned from a search.
+pub enum SearchType {
+  All,
+  Comments,
+  Posts,
+  Communities,
+  Users,
+  Url,
 }
 
-impl From<DbUrl> for Url {
-  fn from(url: DbUrl) -> Self {
-    url.0
-  }
+#[derive(EnumString, Display, Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Copy)]
+#[cfg_attr(feature = "full", derive(TS))]
+#[cfg_attr(feature = "full", ts(export))]
+/// A type / status for a community subscribe.
+pub enum SubscribedType {
+  Subscribed,
+  NotSubscribed,
+  Pending,
 }
 
-impl From<Url> for DbUrl {
-  fn from(url: Url) -> Self {
-    DbUrl(url)
-  }
+#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
+#[cfg_attr(feature = "full", derive(TS))]
+#[cfg_attr(feature = "full", ts(export))]
+/// A list of possible types for the various modlog actions.
+pub enum ModlogActionType {
+  All,
+  ModRemovePost,
+  ModLockPost,
+  ModFeaturePost,
+  ModRemoveComment,
+  ModRemoveCommunity,
+  ModBanFromCommunity,
+  ModAddCommunity,
+  ModTransferCommunity,
+  ModAdd,
+  ModBan,
+  ModHideCommunity,
+  AdminPurgePerson,
+  AdminPurgeCommunity,
+  AdminPurgePost,
+  AdminPurgeComment,
 }
 
-// TODO: can probably move this back to lemmy_db_queries
-pub fn naive_now() -> NaiveDateTime {
-  chrono::prelude::Utc::now().naive_utc()
+#[derive(
+  EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq,
+)]
+#[cfg_attr(feature = "full", derive(TS))]
+#[cfg_attr(feature = "full", ts(export))]
+/// The feature type for a post.
+pub enum PostFeatureType {
+  #[default]
+  /// Features to the top of your site.
+  Local,
+  /// Features to the top of the community.
+  Community,
 }