use crate::structs::RegistrationApplicationView;
-use diesel::{dsl::count, result::Error, *};
+use diesel::{
+ dsl::count,
+ result::Error,
+ ExpressionMethods,
+ JoinOnDsl,
+ NullableExpressionMethods,
+ QueryDsl,
+};
+use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
schema::{local_user, person, registration_application},
source::{
- local_user::{LocalUser, LocalUserSettings},
- person::{Person, PersonSafe},
+ local_user::LocalUser,
+ person::Person,
registration_application::RegistrationApplication,
},
- traits::{ToSafe, ToSafeSettings, ViewToVec},
- utils::limit_and_offset,
+ traits::JoinView,
+ utils::{get_conn, limit_and_offset, DbPool},
};
use typed_builder::TypedBuilder;
-type RegistrationApplicationViewTuple = (
- RegistrationApplication,
- LocalUserSettings,
- PersonSafe,
- Option<PersonSafe>,
-);
+type RegistrationApplicationViewTuple =
+ (RegistrationApplication, LocalUser, Person, Option<Person>);
impl RegistrationApplicationView {
- pub fn read(conn: &mut PgConnection, registration_application_id: i32) -> Result<Self, Error> {
+ pub async fn read(pool: &DbPool, registration_application_id: i32) -> Result<Self, Error> {
+ let conn = &mut get_conn(pool).await?;
let person_alias_1 = diesel::alias!(person as person1);
let (registration_application, creator_local_user, creator, admin) =
.order_by(registration_application::published.desc())
.select((
registration_application::all_columns,
- LocalUser::safe_settings_columns_tuple(),
- Person::safe_columns_tuple(),
- person_alias_1
- .fields(Person::safe_columns_tuple())
- .nullable(),
+ local_user::all_columns,
+ person::all_columns,
+ person_alias_1.fields(person::all_columns).nullable(),
))
- .first::<RegistrationApplicationViewTuple>(conn)?;
+ .first::<RegistrationApplicationViewTuple>(conn)
+ .await?;
Ok(RegistrationApplicationView {
registration_application,
}
/// Returns the current unread registration_application count
- pub fn get_unread_count(
- conn: &mut PgConnection,
- verified_email_only: bool,
- ) -> Result<i64, Error> {
+ pub async fn get_unread_count(pool: &DbPool, verified_email_only: bool) -> Result<i64, Error> {
+ let conn = &mut get_conn(pool).await?;
let person_alias_1 = diesel::alias!(person as person1);
let mut query = registration_application::table
query
.select(count(registration_application::id))
.first::<i64>(conn)
+ .await
}
}
#[builder(field_defaults(default))]
pub struct RegistrationApplicationQuery<'a> {
#[builder(!default)]
- conn: &'a mut PgConnection,
+ pool: &'a DbPool,
unread_only: Option<bool>,
verified_email_only: Option<bool>,
page: Option<i64>,
}
impl<'a> RegistrationApplicationQuery<'a> {
- pub fn list(self) -> Result<Vec<RegistrationApplicationView>, Error> {
+ pub async fn list(self) -> Result<Vec<RegistrationApplicationView>, Error> {
+ let conn = &mut get_conn(self.pool).await?;
let person_alias_1 = diesel::alias!(person as person1);
let mut query = registration_application::table
.order_by(registration_application::published.desc())
.select((
registration_application::all_columns,
- LocalUser::safe_settings_columns_tuple(),
- Person::safe_columns_tuple(),
- person_alias_1
- .fields(Person::safe_columns_tuple())
- .nullable(),
+ local_user::all_columns,
+ person::all_columns,
+ person_alias_1.fields(person::all_columns).nullable(),
))
.into_boxed();
.offset(offset)
.order_by(registration_application::published.desc());
- let res = query.load::<RegistrationApplicationViewTuple>(self.conn)?;
+ let res = query.load::<RegistrationApplicationViewTuple>(conn).await?;
- Ok(RegistrationApplicationView::from_tuple_to_vec(res))
+ Ok(
+ res
+ .into_iter()
+ .map(RegistrationApplicationView::from_tuple)
+ .collect(),
+ )
}
}
-impl ViewToVec for RegistrationApplicationView {
- type DbTuple = RegistrationApplicationViewTuple;
- fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
- items
- .into_iter()
- .map(|a| Self {
- registration_application: a.0,
- creator_local_user: a.1,
- creator: a.2,
- admin: a.3,
- })
- .collect::<Vec<Self>>()
+impl JoinView for RegistrationApplicationView {
+ type JoinTuple = RegistrationApplicationViewTuple;
+ fn from_tuple(a: Self::JoinTuple) -> Self {
+ Self {
+ registration_application: a.0,
+ creator_local_user: a.1,
+ creator: a.2,
+ admin: a.3,
+ }
}
}
use lemmy_db_schema::{
source::{
instance::Instance,
- local_user::{LocalUser, LocalUserInsertForm, LocalUserSettings, LocalUserUpdateForm},
- person::*,
+ local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm},
+ person::{Person, PersonInsertForm},
registration_application::{
RegistrationApplication,
RegistrationApplicationInsertForm,
},
},
traits::Crud,
- utils::establish_unpooled_connection,
+ utils::build_db_pool_for_tests,
};
use serial_test::serial;
- #[test]
+ #[tokio::test]
#[serial]
- fn test_crud() {
- let conn = &mut establish_unpooled_connection();
+ async fn test_crud() {
+ let pool = &build_db_pool_for_tests().await;
- let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
+ let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
+ .await
+ .unwrap();
let timmy_person_form = PersonInsertForm::builder()
.name("timmy_rav".into())
.instance_id(inserted_instance.id)
.build();
- let inserted_timmy_person = Person::create(conn, &timmy_person_form).unwrap();
+ let inserted_timmy_person = Person::create(pool, &timmy_person_form).await.unwrap();
let timmy_local_user_form = LocalUserInsertForm::builder()
.person_id(inserted_timmy_person.id)
.password_encrypted("nada".to_string())
.build();
- let _inserted_timmy_local_user = LocalUser::create(conn, &timmy_local_user_form).unwrap();
+ let _inserted_timmy_local_user = LocalUser::create(pool, &timmy_local_user_form)
+ .await
+ .unwrap();
let sara_person_form = PersonInsertForm::builder()
.name("sara_rav".into())
.instance_id(inserted_instance.id)
.build();
- let inserted_sara_person = Person::create(conn, &sara_person_form).unwrap();
+ let inserted_sara_person = Person::create(pool, &sara_person_form).await.unwrap();
let sara_local_user_form = LocalUserInsertForm::builder()
.person_id(inserted_sara_person.id)
.password_encrypted("nada".to_string())
.build();
- let inserted_sara_local_user = LocalUser::create(conn, &sara_local_user_form).unwrap();
+ let inserted_sara_local_user = LocalUser::create(pool, &sara_local_user_form)
+ .await
+ .unwrap();
// Sara creates an application
let sara_app_form = RegistrationApplicationInsertForm {
answer: "LET ME IIIIINN".to_string(),
};
- let sara_app = RegistrationApplication::create(conn, &sara_app_form).unwrap();
+ let sara_app = RegistrationApplication::create(pool, &sara_app_form)
+ .await
+ .unwrap();
- let read_sara_app_view = RegistrationApplicationView::read(conn, sara_app.id).unwrap();
+ let read_sara_app_view = RegistrationApplicationView::read(pool, sara_app.id)
+ .await
+ .unwrap();
let jess_person_form = PersonInsertForm::builder()
.name("jess_rav".into())
.instance_id(inserted_instance.id)
.build();
- let inserted_jess_person = Person::create(conn, &jess_person_form).unwrap();
+ let inserted_jess_person = Person::create(pool, &jess_person_form).await.unwrap();
let jess_local_user_form = LocalUserInsertForm::builder()
.person_id(inserted_jess_person.id)
.password_encrypted("nada".to_string())
.build();
- let inserted_jess_local_user = LocalUser::create(conn, &jess_local_user_form).unwrap();
+ let inserted_jess_local_user = LocalUser::create(pool, &jess_local_user_form)
+ .await
+ .unwrap();
// Sara creates an application
let jess_app_form = RegistrationApplicationInsertForm {
answer: "LET ME IIIIINN".to_string(),
};
- let jess_app = RegistrationApplication::create(conn, &jess_app_form).unwrap();
+ let jess_app = RegistrationApplication::create(pool, &jess_app_form)
+ .await
+ .unwrap();
- let read_jess_app_view = RegistrationApplicationView::read(conn, jess_app.id).unwrap();
+ let read_jess_app_view = RegistrationApplicationView::read(pool, jess_app.id)
+ .await
+ .unwrap();
let mut expected_sara_app_view = RegistrationApplicationView {
- registration_application: sara_app.to_owned(),
- creator_local_user: LocalUserSettings {
+ registration_application: sara_app.clone(),
+ creator_local_user: LocalUser {
id: inserted_sara_local_user.id,
person_id: inserted_sara_local_user.person_id,
email: inserted_sara_local_user.email,
show_new_post_notifs: inserted_sara_local_user.show_new_post_notifs,
email_verified: inserted_sara_local_user.email_verified,
accepted_application: inserted_sara_local_user.accepted_application,
+ password_encrypted: inserted_sara_local_user.password_encrypted,
},
- creator: PersonSafe {
+ creator: Person {
id: inserted_sara_person.id,
- name: inserted_sara_person.name.to_owned(),
+ name: inserted_sara_person.name.clone(),
display_name: None,
published: inserted_sara_person.published,
avatar: None,
- actor_id: inserted_sara_person.actor_id.to_owned(),
+ actor_id: inserted_sara_person.actor_id.clone(),
local: true,
banned: false,
ban_expires: None,
bio: None,
banner: None,
updated: None,
- inbox_url: inserted_sara_person.inbox_url.to_owned(),
+ inbox_url: inserted_sara_person.inbox_url.clone(),
shared_inbox_url: None,
matrix_user_id: None,
instance_id: inserted_instance.id,
+ private_key: inserted_sara_person.private_key,
+ public_key: inserted_sara_person.public_key,
+ last_refreshed_at: inserted_sara_person.last_refreshed_at,
},
admin: None,
};
// Do a batch read of the applications
let apps = RegistrationApplicationQuery::builder()
- .conn(conn)
+ .pool(pool)
.unread_only(Some(true))
.build()
.list()
+ .await
.unwrap();
assert_eq!(
apps,
- [
- read_jess_app_view.to_owned(),
- expected_sara_app_view.to_owned()
- ]
+ [read_jess_app_view.clone(), expected_sara_app_view.clone()]
);
// Make sure the counts are correct
- let unread_count = RegistrationApplicationView::get_unread_count(conn, false).unwrap();
+ let unread_count = RegistrationApplicationView::get_unread_count(pool, false)
+ .await
+ .unwrap();
assert_eq!(unread_count, 2);
// Approve the application
deny_reason: None,
};
- RegistrationApplication::update(conn, sara_app.id, &approve_form).unwrap();
+ RegistrationApplication::update(pool, sara_app.id, &approve_form)
+ .await
+ .unwrap();
// Update the local_user row
let approve_local_user_form = LocalUserUpdateForm::builder()
.accepted_application(Some(true))
.build();
- LocalUser::update(conn, inserted_sara_local_user.id, &approve_local_user_form).unwrap();
+ LocalUser::update(pool, inserted_sara_local_user.id, &approve_local_user_form)
+ .await
+ .unwrap();
- let read_sara_app_view_after_approve =
- RegistrationApplicationView::read(conn, sara_app.id).unwrap();
+ let read_sara_app_view_after_approve = RegistrationApplicationView::read(pool, sara_app.id)
+ .await
+ .unwrap();
// Make sure the columns changed
expected_sara_app_view
.accepted_application = true;
expected_sara_app_view.registration_application.admin_id = Some(inserted_timmy_person.id);
- expected_sara_app_view.admin = Some(PersonSafe {
+ expected_sara_app_view.admin = Some(Person {
id: inserted_timmy_person.id,
- name: inserted_timmy_person.name.to_owned(),
+ name: inserted_timmy_person.name.clone(),
display_name: None,
published: inserted_timmy_person.published,
avatar: None,
- actor_id: inserted_timmy_person.actor_id.to_owned(),
+ actor_id: inserted_timmy_person.actor_id.clone(),
local: true,
banned: false,
ban_expires: None,
bio: None,
banner: None,
updated: None,
- inbox_url: inserted_timmy_person.inbox_url.to_owned(),
+ inbox_url: inserted_timmy_person.inbox_url.clone(),
shared_inbox_url: None,
matrix_user_id: None,
instance_id: inserted_instance.id,
+ private_key: inserted_timmy_person.private_key,
+ public_key: inserted_timmy_person.public_key,
+ last_refreshed_at: inserted_timmy_person.last_refreshed_at,
});
assert_eq!(read_sara_app_view_after_approve, expected_sara_app_view);
// Do a batch read of apps again
// It should show only jessicas which is unresolved
let apps_after_resolve = RegistrationApplicationQuery::builder()
- .conn(conn)
+ .pool(pool)
.unread_only(Some(true))
.build()
.list()
+ .await
.unwrap();
assert_eq!(apps_after_resolve, vec![read_jess_app_view]);
// Make sure the counts are correct
- let unread_count_after_approve =
- RegistrationApplicationView::get_unread_count(conn, false).unwrap();
+ let unread_count_after_approve = RegistrationApplicationView::get_unread_count(pool, false)
+ .await
+ .unwrap();
assert_eq!(unread_count_after_approve, 1);
// Make sure the not undenied_only has all the apps
let all_apps = RegistrationApplicationQuery::builder()
- .conn(conn)
+ .pool(pool)
.build()
.list()
+ .await
.unwrap();
assert_eq!(all_apps.len(), 2);
- Person::delete(conn, inserted_timmy_person.id).unwrap();
- Person::delete(conn, inserted_sara_person.id).unwrap();
- Person::delete(conn, inserted_jess_person.id).unwrap();
- Instance::delete(conn, inserted_instance.id).unwrap();
+ Person::delete(pool, inserted_timmy_person.id)
+ .await
+ .unwrap();
+ Person::delete(pool, inserted_sara_person.id).await.unwrap();
+ Person::delete(pool, inserted_jess_person.id).await.unwrap();
+ Instance::delete(pool, inserted_instance.id).await.unwrap();
}
}