Skip to content

Commit

Permalink
Adding diesel enums for SortType and ListingType (#2808)
Browse files Browse the repository at this point in the history
* Adding diesel enums for SortType and ListingType

- Uses diesel-derive-enum.
- Adds diesel.toml , so we can again use the auto-generated schema.rs
- Fixes a lot of DB null issues and column ordering issues.
- Fixes #1136
- Also replaces RegistrationMode boilerplate.

* Fixing unit tests 1.

* Remove comment line.

* Before patch.

* Before again.

* Using patch file to fix diesel_ltree issue with diesel.toml

* Adding some yalc ignores

* Fixing RegistrationMode enums

* Adding woodpecker diesel schema check.

* Try adding openssl 1.

* Try using diesel-cli image 1

* Try using diesel-cli image 2

* Try using diesel-cli image 3

* Try using diesel-cli image 4

* Try using diesel-cli image 5

* Try using diesel-cli image 6

* Try using diesel-cli image 7

* Try using diesel-cli image 8

* Try using diesel-cli image 9

* Try using diesel-cli image 10

* Try using diesel-cli image 11

* Try using diesel-cli image 12

* Try using diesel-cli image 13
  • Loading branch information
dessalines authored Apr 17, 2023
1 parent f5511cf commit d8722b6
Show file tree
Hide file tree
Showing 39 changed files with 834 additions and 638 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ query_testing/**/reports/*.json

# API tests
api_tests/node_modules
api_tests/.yalc
api_tests/yalc.lock

# pictrs data
pictrs/
10 changes: 10 additions & 0 deletions .woodpecker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ pipeline:
# when:
# platform: linux/amd64

check_diesel_schema:
image: jameshiew/diesel-cli
environment:
CARGO_HOME: .cargo
DATABASE_URL: postgres://lemmy:password@database:5432/lemmy
commands:
- diesel migration run
- diesel print-schema --config-file=diesel.toml > tmp.schema
- diff tmp.schema crates/db_schema/src/schema.rs

cargo_test:
image: *muslrust_image
environment:
Expand Down
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ sha2 = "0.10.6"
regex = "1.6.0"
once_cell = "1.15.0"
diesel-derive-newtype = "2.0.0-rc.0"
diesel-derive-enum = {version = "2.0.1", features = ["postgres"] }
strum = "0.24.1"
strum_macros = "0.24.3"
itertools = "0.10.5"
Expand Down
2 changes: 1 addition & 1 deletion api_tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"eslint": "^8.25.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "^27.0.6",
"lemmy-js-client": "0.17.2-rc.1",
"lemmy-js-client": "0.17.2-rc.6",
"node-fetch": "^2.6.1",
"prettier": "^2.7.1",
"ts-jest": "^27.0.3",
Expand Down
8 changes: 4 additions & 4 deletions api_tests/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,8 @@ export async function saveUserSettingsBio(api: API): Promise<LoginResponse> {
let form: SaveUserSettings = {
show_nsfw: true,
theme: "darkly",
default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
default_sort_type: SortType.Active,
default_listing_type: ListingType.All,
interface_language: "en",
show_avatars: true,
send_notifications_to_email: false,
Expand All @@ -634,8 +634,8 @@ export async function saveUserSettingsFederated(
let bio = "a changed bio";
let form: SaveUserSettings = {
show_nsfw: false,
default_sort_type: Object.keys(SortType).indexOf(SortType.Hot),
default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
default_sort_type: SortType.Hot,
default_listing_type: ListingType.All,
interface_language: "",
avatar,
banner,
Expand Down
7 changes: 2 additions & 5 deletions api_tests/src/user.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
jest.setTimeout(120000);
import { PersonViewSafe } from "lemmy-js-client";
import { PersonView } from "lemmy-js-client";

import {
alpha,
Expand All @@ -25,10 +25,7 @@ beforeAll(async () => {

let apShortname: string;

function assertUserFederation(
userOne?: PersonViewSafe,
userTwo?: PersonViewSafe
) {
function assertUserFederation(userOne?: PersonView, userTwo?: PersonView) {
expect(userOne?.person.name).toBe(userTwo?.person.name);
expect(userOne?.person.display_name).toBe(userTwo?.person.display_name);
expect(userOne?.person.bio).toBe(userTwo?.person.bio);
Expand Down
36 changes: 23 additions & 13 deletions api_tests/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,13 @@ convert-source-map@^1.4.0, convert-source-map@^1.6.0, convert-source-map@^1.7.0:
dependencies:
safe-buffer "~5.1.1"

cross-fetch@^3.1.5:
version "3.1.5"
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.1.5.tgz#e1389f44d9e7ba767907f7af8454787952ab534f"
integrity sha512-lvb1SBsI0Z7GDwmuid+mU3kWVBwTVUbe7S0H52yaaAdQOXq2YktTCZdlAcNKFzE6QtRz0snpw9bNiPeOIkkQvw==
dependencies:
node-fetch "2.6.7"

cross-spawn@^7.0.2, cross-spawn@^7.0.3:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
Expand Down Expand Up @@ -1555,6 +1562,15 @@ form-data@^3.0.0:
combined-stream "^1.0.8"
mime-types "^2.1.12"

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
Expand Down Expand Up @@ -2363,12 +2379,13 @@ kleur@^3.0.3:
resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e"
integrity sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==

lemmy-js-client@0.17.2-rc.1:
version "0.17.2-rc.1"
resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.17.2-rc.1.tgz#fe8d1508311bbf245acc98c2c3e47e2165a95b14"
integrity sha512-YrOXuCofgkqp28krmPTQZAfUWL5zEDA0sRJ0abKcgf/I8YYkYkUkPS9TOORN5Lv3bc8RAAz4+2/zLHqYL/Tnow==
lemmy-js-client@0.17.2-rc.6:
version "0.17.2-rc.6"
resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.17.2-rc.6.tgz#cd488bc30edf7b65a02b91f1bc295d1b958e9b86"
integrity sha512-/EdzpLJpYOq3ypCQA1MyI7sX0DmHUIA1ZUIda2XTCUUP7a5pltF7WHTicFrQ9j6JwCiFMTVkw6S/L8LzfD6cGA==
dependencies:
node-fetch "2.6.6"
cross-fetch "^3.1.5"
form-data "^4.0.0"

leven@^3.1.0:
version "3.1.0"
Expand Down Expand Up @@ -2503,14 +2520,7 @@ natural-compare@^1.4.0:
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
integrity sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==

node-fetch@2.6.6:
version "2.6.6"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.6.tgz#1751a7c01834e8e1697758732e9efb6eeadfaf89"
integrity sha512-Z8/6vRlTUChSdIgMa51jxQ4lrw/Jy5SOW10ObaA47/RElsAN2c5Pn8bTgFGWn/ibwzXTE8qwr1Yzx28vsecXEA==
dependencies:
whatwg-url "^5.0.0"

node-fetch@^2.6.1:
node-fetch@2.6.7, node-fetch@^2.6.1:
version "2.6.7"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==
Expand Down
1 change: 0 additions & 1 deletion crates/api/src/community/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ impl Perform for TransferCommunity {
mod_person_id: local_user_view.person.id,
other_person_id: data.person_id,
community_id: data.community_id,
removed: Some(false),
};

ModTransferCommunity::create(context.pool(), &form).await?;
Expand Down
7 changes: 3 additions & 4 deletions crates/api/src/local_user/change_password_after_reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use lemmy_api_common::{
person::{LoginResponse, PasswordChangeAfterReset},
utils::password_length_check,
};
use lemmy_db_schema::source::{
local_site::RegistrationMode,
local_user::LocalUser,
password_reset_request::PasswordResetRequest,
use lemmy_db_schema::{
source::{local_user::LocalUser, password_reset_request::PasswordResetRequest},
RegistrationMode,
};
use lemmy_db_views::structs::SiteView;
use lemmy_utils::{claims::Claims, error::LemmyError, ConnectionId};
Expand Down
5 changes: 3 additions & 2 deletions crates/api_common/src/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::sensitive::Sensitive;
use lemmy_db_schema::{
newtypes::{CommentReplyId, CommunityId, LanguageId, PersonId, PersonMentionId},
CommentSortType,
ListingType,
SortType,
};
use lemmy_db_views::structs::{CommentView, PostView};
Expand Down Expand Up @@ -55,8 +56,8 @@ pub struct SaveUserSettings {
pub show_nsfw: Option<bool>,
pub show_scores: Option<bool>,
pub theme: Option<String>,
pub default_sort_type: Option<i16>,
pub default_listing_type: Option<i16>,
pub default_sort_type: Option<SortType>,
pub default_listing_type: Option<ListingType>,
pub interface_language: Option<String>,
pub avatar: Option<String>,
pub banner: Option<String>,
Expand Down
12 changes: 4 additions & 8 deletions crates/api_common/src/site.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use crate::sensitive::Sensitive;
use lemmy_db_schema::{
newtypes::{CommentId, CommunityId, LanguageId, PersonId, PostId},
source::{
instance::Instance,
language::Language,
local_site::RegistrationMode,
tagline::Tagline,
},
source::{instance::Instance, language::Language, tagline::Tagline},
ListingType,
ModlogActionType,
RegistrationMode,
SearchType,
SortType,
};
Expand Down Expand Up @@ -128,7 +124,7 @@ pub struct CreateSite {
pub application_question: Option<String>,
pub private_instance: Option<bool>,
pub default_theme: Option<String>,
pub default_post_listing_type: Option<String>,
pub default_post_listing_type: Option<ListingType>,
pub legal_information: Option<String>,
pub application_email_admins: Option<bool>,
pub hide_modlog_mod_names: Option<bool>,
Expand Down Expand Up @@ -173,7 +169,7 @@ pub struct EditSite {
pub application_question: Option<String>,
pub private_instance: Option<bool>,
pub default_theme: Option<String>,
pub default_post_listing_type: Option<String>,
pub default_post_listing_type: Option<ListingType>,
pub legal_information: Option<String>,
pub application_email_admins: Option<bool>,
pub hide_modlog_mod_names: Option<bool>,
Expand Down
3 changes: 2 additions & 1 deletion crates/api_common/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use lemmy_db_schema::{
community::{Community, CommunityModerator, CommunityUpdateForm},
email_verification::{EmailVerification, EmailVerificationForm},
instance::Instance,
local_site::{LocalSite, RegistrationMode},
local_site::LocalSite,
local_site_rate_limit::LocalSiteRateLimit,
password_reset_request::PasswordResetRequest,
person::{Person, PersonUpdateForm},
Expand All @@ -20,6 +20,7 @@ use lemmy_db_schema::{
},
traits::{Crud, Readable},
utils::DbPool,
RegistrationMode,
};
use lemmy_db_views::{comment_view::CommentQuery, structs::LocalUserView};
use lemmy_db_views_actor::structs::{
Expand Down
2 changes: 1 addition & 1 deletion crates/api_crud/src/site/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl PerformCrud for CreateSite {
.application_question(application_question)
.private_instance(data.private_instance)
.default_theme(data.default_theme.clone())
.default_post_listing_type(data.default_post_listing_type.clone())
.default_post_listing_type(data.default_post_listing_type)
.legal_information(diesel_option_overwrite(&data.legal_information))
.application_email_admins(data.application_email_admins)
.hide_modlog_mod_names(data.hide_modlog_mod_names)
Expand Down
2 changes: 1 addition & 1 deletion crates/api_crud/src/site/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use lemmy_db_schema::source::local_site::RegistrationMode;
use lemmy_db_schema::RegistrationMode;
use lemmy_utils::error::LemmyError;

mod create;
Expand Down
11 changes: 5 additions & 6 deletions crates/api_crud/src/site/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use lemmy_db_schema::{
actor_language::SiteLanguage,
federation_allowlist::FederationAllowList,
federation_blocklist::FederationBlockList,
local_site::{LocalSite, LocalSiteUpdateForm, RegistrationMode},
local_site::{LocalSite, LocalSiteUpdateForm},
local_site_rate_limit::{LocalSiteRateLimit, LocalSiteRateLimitUpdateForm},
local_user::LocalUser,
site::{Site, SiteUpdateForm},
Expand All @@ -26,14 +26,14 @@ use lemmy_db_schema::{
traits::Crud,
utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now},
ListingType,
RegistrationMode,
};
use lemmy_db_views::structs::SiteView;
use lemmy_utils::{
error::LemmyError,
utils::{slurs::check_slurs_opt, validation::is_valid_body_field},
ConnectionId,
};
use std::str::FromStr;

#[async_trait::async_trait(?Send)]
impl PerformCrud for EditSite {
Expand Down Expand Up @@ -74,10 +74,9 @@ impl PerformCrud for EditSite {
.unwrap_or(local_site.registration_mode),
)?;

if let Some(default_post_listing_type) = &data.default_post_listing_type {
if let Some(listing_type) = &data.default_post_listing_type {
// only allow all or local as default listing types
let val = ListingType::from_str(default_post_listing_type);
if val != Ok(ListingType::All) && val != Ok(ListingType::Local) {
if listing_type != &ListingType::All && listing_type != &ListingType::Local {
return Err(LemmyError::from_message(
"invalid_default_post_listing_type",
));
Expand Down Expand Up @@ -113,7 +112,7 @@ impl PerformCrud for EditSite {
.application_question(application_question)
.private_instance(data.private_instance)
.default_theme(data.default_theme.clone())
.default_post_listing_type(data.default_post_listing_type.clone())
.default_post_listing_type(data.default_post_listing_type)
.legal_information(diesel_option_overwrite(&data.legal_information))
.application_email_admins(data.application_email_admins)
.hide_modlog_mod_names(data.hide_modlog_mod_names)
Expand Down
2 changes: 1 addition & 1 deletion crates/api_crud/src/user/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ use lemmy_api_common::{
use lemmy_db_schema::{
aggregates::structs::PersonAggregates,
source::{
local_site::RegistrationMode,
local_user::{LocalUser, LocalUserInsertForm},
person::{Person, PersonInsertForm},
registration_application::{RegistrationApplication, RegistrationApplicationInsertForm},
},
traits::Crud,
RegistrationMode,
};
use lemmy_db_views::structs::{LocalUserView, SiteView};
use lemmy_utils::{
Expand Down
5 changes: 1 addition & 4 deletions crates/apub/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use activitypub_federation::config::Data;
use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::{newtypes::CommunityId, source::local_site::LocalSite, ListingType};
use lemmy_utils::{error::LemmyError, ConnectionId};
use std::str::FromStr;

mod list_comments;
mod list_posts;
Expand Down Expand Up @@ -30,9 +29,7 @@ fn listing_type_with_default(
) -> Result<ListingType, LemmyError> {
// On frontpage use listing type from param or admin configured default
let listing_type = if community_id.is_none() {
type_.unwrap_or(ListingType::from_str(
&local_site.default_post_listing_type,
)?)
type_.unwrap_or(local_site.default_post_listing_type)
} else {
// inside of community show everything
ListingType::All
Expand Down
2 changes: 1 addition & 1 deletion crates/apub/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub(crate) async fn get_activity(
.into();
let activity = Activity::read_from_apub_id(context.pool(), &activity_id).await?;

let sensitive = activity.sensitive.unwrap_or(true);
let sensitive = activity.sensitive;
if !activity.local {
Err(err_object_not_local())
} else if sensitive {
Expand Down
3 changes: 2 additions & 1 deletion crates/db_schema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ path = "src/lib.rs"
doctest = false

[features]
full = ["diesel", "diesel-derive-newtype", "diesel_migrations", "bcrypt", "lemmy_utils",
full = ["diesel", "diesel-derive-newtype", "diesel-derive-enum", "diesel_migrations", "bcrypt", "lemmy_utils",
"activitypub_federation", "sha2", "regex", "once_cell", "serde_json", "diesel_ltree",
"diesel-async", "deadpool"]

Expand All @@ -30,6 +30,7 @@ lemmy_utils = { workspace = true, optional = true }
bcrypt = { workspace = true, optional = true }
diesel = { workspace = true, features = ["postgres","chrono", "serde_json"], optional = true }
diesel-derive-newtype = { workspace = true, optional = true }
diesel-derive-enum = { workspace = true, optional = true }
diesel_migrations = { workspace = true, optional = true }
diesel-async = { workspace = true, features = ["postgres", "deadpool"], optional = true }
sha2 = { workspace = true, optional = true }
Expand Down
Loading

0 comments on commit d8722b6

Please sign in to comment.