lemmy/crates/db_views/src/registration_application_view.rs

410 lines
13 KiB
Rust
Raw Normal View History

use crate::structs::RegistrationApplicationView;
2022-11-09 10:05:00 +00:00
use diesel::{
dsl::count,
pg::Pg,
2022-11-09 10:05:00 +00:00
result::Error,
ExpressionMethods,
JoinOnDsl,
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
aliases,
schema::{local_user, person, registration_application},
utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
};
fn queries<'a>() -> Queries<
impl ReadFn<'a, RegistrationApplicationView, i32>,
impl ListFn<'a, RegistrationApplicationView, RegistrationApplicationQuery>,
> {
let all_joins = |query: registration_application::BoxedQuery<'a, Pg>| {
query
.inner_join(local_user::table.on(registration_application::local_user_id.eq(local_user::id)))
.inner_join(person::table.on(local_user::person_id.eq(person::id)))
.left_join(
aliases::person1
.on(registration_application::admin_id.eq(aliases::person1.field(person::id).nullable())),
)
.order_by(registration_application::published.desc())
.select((
registration_application::all_columns,
local_user::all_columns,
person::all_columns,
aliases::person1.fields(person::all_columns).nullable(),
))
};
let read = move |mut conn: DbConn<'a>, registration_application_id: i32| async move {
all_joins(
registration_application::table
.find(registration_application_id)
.into_boxed(),
)
.first::<RegistrationApplicationView>(&mut conn)
.await
};
let list = move |mut conn: DbConn<'a>, options: RegistrationApplicationQuery| async move {
let mut query = all_joins(registration_application::table.into_boxed());
// If viewing all applications, order by newest, but if viewing unresolved only, show the oldest first (FIFO)
if options.unread_only {
query = query
.filter(registration_application::admin_id.is_null())
.order_by(registration_application::published.asc());
} else {
query = query.order_by(registration_application::published.desc());
}
if options.verified_email_only {
query = query.filter(local_user::email_verified.eq(true))
}
let (limit, offset) = limit_and_offset(options.page, options.limit)?;
query = query.limit(limit).offset(offset);
query.load::<RegistrationApplicationView>(&mut conn).await
};
Queries::new(read, list)
}
impl RegistrationApplicationView {
Make functions work with both connection and pool (#3420) * a lot * merge * Fix stuff broken by merge * Get rid of repetitive `&mut *context.conn().await?` * Add blank lines under each line with `conn =` * Fix style mistakes (partial) * Revert "Fix style mistakes (partial)" This reverts commit 48a033b87f4fdc1ce14ff86cc019e1c703cd2741. * Revert "Add blank lines under each line with `conn =`" This reverts commit 773a6d3beba2cf89eac75913078b40c4f5190dd4. * Revert "Get rid of repetitive `&mut *context.conn().await?`" This reverts commit d2c6263ea13710177d49b2791278db5ad115fca5. * Use DbConn for CaptchaAnswer methods * DbConn trait * Remove more `&mut *` * Fix stuff * Re-run CI * try to make ci start * fix * fix * Fix api_common::utils * Fix apub::activities::block * Fix apub::api::resolve_object * Fix some things * Revert "Fix some things" This reverts commit 2bf8574bc8333d8d34ca542d61a0a5b50039c24d. * Revert "Fix apub::api::resolve_object" This reverts commit 3e4059aabbe485b2ff060bdeced8ef958ff62832. * Revert "Fix apub::activities::block" This reverts commit 3b02389abd780a7b1b8a2c89e26febdaa6a12159. * Revert "Fix api_common::utils" This reverts commit 7dc73de613a5618fa57eb06450f3699bbcb41254. * Revert "Revert "Fix api_common::utils"" This reverts commit f740f115e5457e83e53cc223e48196a2c47a9975. * Revert "Revert "Fix apub::activities::block"" This reverts commit 2ee206af7c885c10092cf209bf4a5b1d60327866. * Revert "Revert "Fix apub::api::resolve_object"" This reverts commit 96ed8bf2e9dcadae760743929498312334e23d2e. * Fix fetch_local_site_data * Fix get_comment_parent_creator * Remove unused perma deleted text * Fix routes::feeds * Fix lib.rs * Update lib.rs * rerun ci * Attempt to create custom GetConn and RunQueryDsl traits * Start over * Add GetConn trait * aaaa * Revert "aaaa" This reverts commit acc9ca1aed10c39efdd91cefece066e035a1fe80. * Revert "Revert "aaaa"" This reverts commit 443a2a00a56d152bb7eb429efd0d29a78e21b163. * still aaaaaaaaaaaaa * Return to earlier thing Revert "Add GetConn trait" This reverts commit ab4e94aea5bd9d34cbcddf017339131047e75344. * Try to use DbPool enum * Revert "Try to use DbPool enum" This reverts commit e4d1712646a52006b865a1fbe0dcf79976fdb027. * DbConn and DbPool enums (db_schema only fails to compile for tests) * fmt * Make functions take `&mut DbPool<'_>` and make db_schema tests compile * Add try_join_with_pool macro and run fix-clippy on more crates * Fix some errors * I did it * Remove function variants that take connection * rerun ci * rerun ci * rerun ci
2023-07-11 13:09:59 +00:00
pub async fn read(
pool: &mut DbPool<'_>,
registration_application_id: i32,
) -> Result<Self, Error> {
queries().read(pool, registration_application_id).await
}
/// Returns the current unread registration_application count
Make functions work with both connection and pool (#3420) * a lot * merge * Fix stuff broken by merge * Get rid of repetitive `&mut *context.conn().await?` * Add blank lines under each line with `conn =` * Fix style mistakes (partial) * Revert "Fix style mistakes (partial)" This reverts commit 48a033b87f4fdc1ce14ff86cc019e1c703cd2741. * Revert "Add blank lines under each line with `conn =`" This reverts commit 773a6d3beba2cf89eac75913078b40c4f5190dd4. * Revert "Get rid of repetitive `&mut *context.conn().await?`" This reverts commit d2c6263ea13710177d49b2791278db5ad115fca5. * Use DbConn for CaptchaAnswer methods * DbConn trait * Remove more `&mut *` * Fix stuff * Re-run CI * try to make ci start * fix * fix * Fix api_common::utils * Fix apub::activities::block * Fix apub::api::resolve_object * Fix some things * Revert "Fix some things" This reverts commit 2bf8574bc8333d8d34ca542d61a0a5b50039c24d. * Revert "Fix apub::api::resolve_object" This reverts commit 3e4059aabbe485b2ff060bdeced8ef958ff62832. * Revert "Fix apub::activities::block" This reverts commit 3b02389abd780a7b1b8a2c89e26febdaa6a12159. * Revert "Fix api_common::utils" This reverts commit 7dc73de613a5618fa57eb06450f3699bbcb41254. * Revert "Revert "Fix api_common::utils"" This reverts commit f740f115e5457e83e53cc223e48196a2c47a9975. * Revert "Revert "Fix apub::activities::block"" This reverts commit 2ee206af7c885c10092cf209bf4a5b1d60327866. * Revert "Revert "Fix apub::api::resolve_object"" This reverts commit 96ed8bf2e9dcadae760743929498312334e23d2e. * Fix fetch_local_site_data * Fix get_comment_parent_creator * Remove unused perma deleted text * Fix routes::feeds * Fix lib.rs * Update lib.rs * rerun ci * Attempt to create custom GetConn and RunQueryDsl traits * Start over * Add GetConn trait * aaaa * Revert "aaaa" This reverts commit acc9ca1aed10c39efdd91cefece066e035a1fe80. * Revert "Revert "aaaa"" This reverts commit 443a2a00a56d152bb7eb429efd0d29a78e21b163. * still aaaaaaaaaaaaa * Return to earlier thing Revert "Add GetConn trait" This reverts commit ab4e94aea5bd9d34cbcddf017339131047e75344. * Try to use DbPool enum * Revert "Try to use DbPool enum" This reverts commit e4d1712646a52006b865a1fbe0dcf79976fdb027. * DbConn and DbPool enums (db_schema only fails to compile for tests) * fmt * Make functions take `&mut DbPool<'_>` and make db_schema tests compile * Add try_join_with_pool macro and run fix-clippy on more crates * Fix some errors * I did it * Remove function variants that take connection * rerun ci * rerun ci * rerun ci
2023-07-11 13:09:59 +00:00
pub async fn get_unread_count(
pool: &mut DbPool<'_>,
verified_email_only: bool,
) -> Result<i64, Error> {
2022-11-09 10:05:00 +00:00
let conn = &mut get_conn(pool).await?;
let person_alias_1 = diesel::alias!(person as person1);
let mut query = registration_application::table
.inner_join(local_user::table.on(registration_application::local_user_id.eq(local_user::id)))
.inner_join(person::table.on(local_user::person_id.eq(person::id)))
.left_join(
person_alias_1
.on(registration_application::admin_id.eq(person_alias_1.field(person::id).nullable())),
)
.filter(registration_application::admin_id.is_null())
.into_boxed();
if verified_email_only {
query = query.filter(local_user::email_verified.eq(true))
}
query
.select(count(registration_application::id))
.first::<i64>(conn)
2022-11-09 10:05:00 +00:00
.await
}
}
#[derive(Default)]
pub struct RegistrationApplicationQuery {
pub unread_only: bool,
pub verified_email_only: bool,
pub page: Option<i64>,
pub limit: Option<i64>,
}
impl RegistrationApplicationQuery {
pub async fn list(
self,
pool: &mut DbPool<'_>,
) -> Result<Vec<RegistrationApplicationView>, Error> {
queries().list(pool, self).await
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
#![allow(clippy::indexing_slicing)]
use crate::registration_application_view::{
RegistrationApplicationQuery,
RegistrationApplicationView,
};
use lemmy_db_schema::{
source::{
instance::Instance,
local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm},
person::{Person, PersonInsertForm},
registration_application::{
RegistrationApplication,
RegistrationApplicationInsertForm,
RegistrationApplicationUpdateForm,
},
},
traits::Crud,
2022-11-09 10:05:00 +00:00
utils::build_db_pool_for_tests,
};
use serial_test::serial;
2022-11-09 10:05:00 +00:00
#[tokio::test]
#[serial]
2022-11-09 10:05:00 +00:00
async fn test_crud() {
let pool = &build_db_pool_for_tests().await;
Make functions work with both connection and pool (#3420) * a lot * merge * Fix stuff broken by merge * Get rid of repetitive `&mut *context.conn().await?` * Add blank lines under each line with `conn =` * Fix style mistakes (partial) * Revert "Fix style mistakes (partial)" This reverts commit 48a033b87f4fdc1ce14ff86cc019e1c703cd2741. * Revert "Add blank lines under each line with `conn =`" This reverts commit 773a6d3beba2cf89eac75913078b40c4f5190dd4. * Revert "Get rid of repetitive `&mut *context.conn().await?`" This reverts commit d2c6263ea13710177d49b2791278db5ad115fca5. * Use DbConn for CaptchaAnswer methods * DbConn trait * Remove more `&mut *` * Fix stuff * Re-run CI * try to make ci start * fix * fix * Fix api_common::utils * Fix apub::activities::block * Fix apub::api::resolve_object * Fix some things * Revert "Fix some things" This reverts commit 2bf8574bc8333d8d34ca542d61a0a5b50039c24d. * Revert "Fix apub::api::resolve_object" This reverts commit 3e4059aabbe485b2ff060bdeced8ef958ff62832. * Revert "Fix apub::activities::block" This reverts commit 3b02389abd780a7b1b8a2c89e26febdaa6a12159. * Revert "Fix api_common::utils" This reverts commit 7dc73de613a5618fa57eb06450f3699bbcb41254. * Revert "Revert "Fix api_common::utils"" This reverts commit f740f115e5457e83e53cc223e48196a2c47a9975. * Revert "Revert "Fix apub::activities::block"" This reverts commit 2ee206af7c885c10092cf209bf4a5b1d60327866. * Revert "Revert "Fix apub::api::resolve_object"" This reverts commit 96ed8bf2e9dcadae760743929498312334e23d2e. * Fix fetch_local_site_data * Fix get_comment_parent_creator * Remove unused perma deleted text * Fix routes::feeds * Fix lib.rs * Update lib.rs * rerun ci * Attempt to create custom GetConn and RunQueryDsl traits * Start over * Add GetConn trait * aaaa * Revert "aaaa" This reverts commit acc9ca1aed10c39efdd91cefece066e035a1fe80. * Revert "Revert "aaaa"" This reverts commit 443a2a00a56d152bb7eb429efd0d29a78e21b163. * still aaaaaaaaaaaaa * Return to earlier thing Revert "Add GetConn trait" This reverts commit ab4e94aea5bd9d34cbcddf017339131047e75344. * Try to use DbPool enum * Revert "Try to use DbPool enum" This reverts commit e4d1712646a52006b865a1fbe0dcf79976fdb027. * DbConn and DbPool enums (db_schema only fails to compile for tests) * fmt * Make functions take `&mut DbPool<'_>` and make db_schema tests compile * Add try_join_with_pool macro and run fix-clippy on more crates * Fix some errors * I did it * Remove function variants that take connection * rerun ci * rerun ci * rerun ci
2023-07-11 13:09:59 +00:00
let pool = &mut pool.into();
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())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
2022-11-09 10:05:00 +00:00
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())
.admin(Some(true))
.build();
2022-11-09 10:05:00 +00:00
let _inserted_timmy_local_user = LocalUser::create(pool, &timmy_local_user_form)
.await
.unwrap();
let sara_person_form = PersonInsertForm::builder()
.name("sara_rav".into())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
2022-11-09 10:05:00 +00:00
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();
2022-11-09 10:05:00 +00:00
let inserted_sara_local_user = LocalUser::create(pool, &sara_local_user_form)
.await
.unwrap();
// Sara creates an application
let sara_app_form = RegistrationApplicationInsertForm {
local_user_id: inserted_sara_local_user.id,
answer: "LET ME IIIIINN".to_string(),
};
2022-11-09 10:05:00 +00:00
let sara_app = RegistrationApplication::create(pool, &sara_app_form)
.await
.unwrap();
2022-11-09 10:05:00 +00:00
let read_sara_app_view = RegistrationApplicationView::read(pool, sara_app.id)
.await
.unwrap();
let jess_person_form = PersonInsertForm::builder()
.name("jess_rav".into())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
2022-11-09 10:05:00 +00:00
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();
2022-11-09 10:05:00 +00:00
let inserted_jess_local_user = LocalUser::create(pool, &jess_local_user_form)
.await
.unwrap();
// Sara creates an application
let jess_app_form = RegistrationApplicationInsertForm {
local_user_id: inserted_jess_local_user.id,
answer: "LET ME IIIIINN".to_string(),
};
2022-11-09 10:05:00 +00:00
let jess_app = RegistrationApplication::create(pool, &jess_app_form)
.await
.unwrap();
2022-11-09 10:05:00 +00:00
let read_jess_app_view = RegistrationApplicationView::read(pool, jess_app.id)
.await
.unwrap();
let mut expected_sara_app_view = RegistrationApplicationView {
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_nsfw: inserted_sara_local_user.show_nsfw,
auto_expand: inserted_sara_local_user.auto_expand,
blur_nsfw: inserted_sara_local_user.blur_nsfw,
theme: inserted_sara_local_user.theme,
default_sort_type: inserted_sara_local_user.default_sort_type,
default_listing_type: inserted_sara_local_user.default_listing_type,
interface_language: inserted_sara_local_user.interface_language,
show_avatars: inserted_sara_local_user.show_avatars,
send_notifications_to_email: inserted_sara_local_user.send_notifications_to_email,
show_bot_accounts: inserted_sara_local_user.show_bot_accounts,
show_scores: inserted_sara_local_user.show_scores,
show_read_posts: inserted_sara_local_user.show_read_posts,
email_verified: inserted_sara_local_user.email_verified,
accepted_application: inserted_sara_local_user.accepted_application,
totp_2fa_secret: inserted_sara_local_user.totp_2fa_secret,
password_encrypted: inserted_sara_local_user.password_encrypted,
open_links_in_new_tab: inserted_sara_local_user.open_links_in_new_tab,
infinite_scroll_enabled: inserted_sara_local_user.infinite_scroll_enabled,
admin: false,
post_listing_mode: inserted_sara_local_user.post_listing_mode,
totp_2fa_enabled: inserted_sara_local_user.totp_2fa_enabled,
enable_keyboard_navigation: inserted_sara_local_user.enable_keyboard_navigation,
enable_animated_images: inserted_sara_local_user.enable_animated_images,
collapse_bot_comments: inserted_sara_local_user.collapse_bot_comments,
},
creator: Person {
id: inserted_sara_person.id,
name: inserted_sara_person.name.clone(),
display_name: None,
published: inserted_sara_person.published,
avatar: None,
actor_id: inserted_sara_person.actor_id.clone(),
local: true,
banned: false,
ban_expires: None,
deleted: false,
bot_account: false,
bio: None,
banner: None,
updated: None,
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,
};
assert_eq!(read_sara_app_view, expected_sara_app_view);
// Do a batch read of the applications
let apps = RegistrationApplicationQuery {
unread_only: (true),
..Default::default()
}
.list(pool)
.await
.unwrap();
assert_eq!(
apps,
[expected_sara_app_view.clone(), read_jess_app_view.clone()]
);
// Make sure the counts are correct
2022-11-09 10:05:00 +00:00
let unread_count = RegistrationApplicationView::get_unread_count(pool, false)
.await
.unwrap();
assert_eq!(unread_count, 2);
// Approve the application
let approve_form = RegistrationApplicationUpdateForm {
admin_id: Some(Some(inserted_timmy_person.id)),
deny_reason: None,
};
2022-11-09 10:05:00 +00:00
RegistrationApplication::update(pool, sara_app.id, &approve_form)
.await
.unwrap();
// Update the local_user row
Replace TypedBuilder with Default in update forms (#3814) * Update comment.rs * Update community.rs * Update local_site.rs * Update local_site_rate_limit.rs * Update local_user.rs * Update person.rs * Update comment.rs * Update community.rs * Update local_site.rs * Update local_site_rate_limit.rs * Update local_user.rs * Update post.rs * Update private_message.rs * Update site.rs * Update post.rs * Update person.rs * Update private_message.rs * Update comment.rs * Update create.rs * Update leave_admin.rs * Update update.rs * Update remove.rs * Update add_admin.rs * Update verify_email.rs * Update mod.rs * Update mod.rs * Update undo_delete.rs * Update undo_delete.rs * Update utils.rs * Update feature.rs * Update delete.rs * Update lock.rs * Update create.rs * Update approve.rs * Update update.rs * Update lock_page.rs * Update block_user.rs * Update delete.rs * Update undo_block_user.rs * Update collection_remove.rs * Update post.rs * Update hide.rs * Update person.rs * Update remove.rs * Update post_view.rs * Update create.rs * Update remove.rs * Update collection_add.rs * Update community.rs * Update update.rs * Update post_aggregates.rs * Update update.rs * Update comment.rs * Update code_migrations.rs * Update registration_application_view.rs * Update update.rs * Update ban_person.rs * Update community.rs * Update delete.rs * Update delete.rs * Update delete.rs * Update person_aggregates.rs * Update save_settings.rs * Update distinguish.rs * Update mark_read.rs * Update site_aggregates.rs * Update create.rs * Fix * rerun ci * Update comment.rs * rerun ci * Update create.rs * Update create.rs * Update post_view.rs * rerun ci * Update undo_delete.rs * rerun ci
2023-08-08 09:41:41 +00:00
let approve_local_user_form = LocalUserUpdateForm {
accepted_application: Some(true),
..Default::default()
};
2022-11-09 10:05:00 +00:00
LocalUser::update(pool, inserted_sara_local_user.id, &approve_local_user_form)
.await
.unwrap();
2022-11-09 10:05:00 +00:00
let read_sara_app_view_after_approve = RegistrationApplicationView::read(pool, sara_app.id)
.await
.unwrap();
// Make sure the columns changed
expected_sara_app_view
.creator_local_user
.accepted_application = true;
expected_sara_app_view.registration_application.admin_id = Some(inserted_timmy_person.id);
expected_sara_app_view.admin = Some(Person {
id: inserted_timmy_person.id,
name: inserted_timmy_person.name.clone(),
display_name: None,
published: inserted_timmy_person.published,
avatar: None,
actor_id: inserted_timmy_person.actor_id.clone(),
local: true,
banned: false,
ban_expires: None,
deleted: false,
bot_account: false,
bio: None,
banner: None,
updated: None,
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 {
unread_only: (true),
..Default::default()
}
.list(pool)
.await
.unwrap();
assert_eq!(apps_after_resolve, vec![read_jess_app_view]);
// Make sure the counts are correct
2022-11-09 10:05:00 +00:00
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::default()
.list(pool)
2022-11-09 10:05:00 +00:00
.await
.unwrap();
assert_eq!(all_apps.len(), 2);
2022-11-09 10:05:00 +00:00
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();
}
}