Add the ability to fetch a registration application by person_id. (#4913)

* Add the ability to fetch a registration application by person_id.

- Fixes #4908

* Cleaning up PR.
This commit is contained in:
Dessalines 2024-07-23 19:01:24 -04:00 committed by GitHub
parent 32b73193df
commit 9738d87f38
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 87 additions and 17 deletions

View file

@ -0,0 +1,28 @@
use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{
context::LemmyContext,
site::{GetRegistrationApplication, RegistrationApplicationResponse},
utils::is_admin,
};
use lemmy_db_views::structs::{LocalUserView, RegistrationApplicationView};
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
/// Lists registration applications, filterable by undenied only.
pub async fn get_registration_application(
data: Query<GetRegistrationApplication>,
context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> LemmyResult<Json<RegistrationApplicationResponse>> {
// Make sure user is an admin
is_admin(&local_user_view)?;
// Read the view
let registration_application =
RegistrationApplicationView::read_by_person(&mut context.pool(), data.person_id)
.await?
.ok_or(LemmyErrorType::CouldntFindRegistrationApplication)?;
Ok(Json(RegistrationApplicationResponse {
registration_application,
}))
}

View file

@ -1,3 +1,4 @@
pub mod approve; pub mod approve;
pub mod get;
pub mod list; pub mod list;
pub mod unread_count; pub mod unread_count;

View file

@ -1,7 +1,15 @@
use crate::federate_retry_sleep_duration; use crate::federate_retry_sleep_duration;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::{CommentId, CommunityId, InstanceId, LanguageId, PersonId, PostId}, newtypes::{
CommentId,
CommunityId,
InstanceId,
LanguageId,
PersonId,
PostId,
RegistrationApplicationId,
},
source::{ source::{
federation_queue_state::FederationQueueState, federation_queue_state::FederationQueueState,
instance::Instance, instance::Instance,
@ -440,13 +448,22 @@ pub struct ListRegistrationApplicationsResponse {
pub registration_applications: Vec<RegistrationApplicationView>, pub registration_applications: Vec<RegistrationApplicationView>,
} }
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// Gets a registration application for a person
pub struct GetRegistrationApplication {
pub person_id: PersonId,
}
#[skip_serializing_none] #[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq, Eq, Hash)] #[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(TS))] #[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))] #[cfg_attr(feature = "full", ts(export))]
/// Approves a registration application. /// Approves a registration application.
pub struct ApproveRegistrationApplication { pub struct ApproveRegistrationApplication {
pub id: i32, pub id: RegistrationApplicationId,
pub approve: bool, pub approve: bool,
pub deny_reason: Option<String>, pub deny_reason: Option<String>,
} }

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
diesel::OptionalExtension, diesel::OptionalExtension,
newtypes::LocalUserId, newtypes::{LocalUserId, RegistrationApplicationId},
schema::registration_application::dsl::{local_user_id, registration_application}, schema::registration_application::dsl::{local_user_id, registration_application},
source::registration_application::{ source::registration_application::{
RegistrationApplication, RegistrationApplication,
@ -17,7 +17,7 @@ use diesel_async::RunQueryDsl;
impl Crud for RegistrationApplication { impl Crud for RegistrationApplication {
type InsertForm = RegistrationApplicationInsertForm; type InsertForm = RegistrationApplicationInsertForm;
type UpdateForm = RegistrationApplicationUpdateForm; type UpdateForm = RegistrationApplicationUpdateForm;
type IdType = i32; type IdType = RegistrationApplicationId;
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> { async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;

View file

@ -148,6 +148,12 @@ pub struct LocalSiteId(i32);
/// The custom emoji id. /// The custom emoji id.
pub struct CustomEmojiId(i32); pub struct CustomEmojiId(i32);
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "full", derive(DieselNewType, TS))]
#[cfg_attr(feature = "full", ts(export))]
/// The registration application id.
pub struct RegistrationApplicationId(i32);
#[cfg(feature = "full")] #[cfg(feature = "full")]
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
#[serde(remote = "Ltree")] #[serde(remote = "Ltree")]

View file

@ -1,4 +1,4 @@
use crate::newtypes::{LocalUserId, PersonId}; use crate::newtypes::{LocalUserId, PersonId, RegistrationApplicationId};
#[cfg(feature = "full")] #[cfg(feature = "full")]
use crate::schema::registration_application; use crate::schema::registration_application;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
@ -15,7 +15,7 @@ use ts_rs::TS;
#[cfg_attr(feature = "full", ts(export))] #[cfg_attr(feature = "full", ts(export))]
/// A registration application. /// A registration application.
pub struct RegistrationApplication { pub struct RegistrationApplication {
pub id: i32, pub id: RegistrationApplicationId,
pub local_user_id: LocalUserId, pub local_user_id: LocalUserId,
pub answer: String, pub answer: String,
pub admin_id: Option<PersonId>, pub admin_id: Option<PersonId>,

View file

@ -11,12 +11,18 @@ use diesel::{
use diesel_async::RunQueryDsl; use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
aliases, aliases,
newtypes::{PersonId, RegistrationApplicationId},
schema::{local_user, person, registration_application}, schema::{local_user, person, registration_application},
utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
}; };
enum ReadBy {
Id(RegistrationApplicationId),
Person(PersonId),
}
fn queries<'a>() -> Queries< fn queries<'a>() -> Queries<
impl ReadFn<'a, RegistrationApplicationView, i32>, impl ReadFn<'a, RegistrationApplicationView, ReadBy>,
impl ListFn<'a, RegistrationApplicationView, RegistrationApplicationQuery>, impl ListFn<'a, RegistrationApplicationView, RegistrationApplicationQuery>,
> { > {
let all_joins = |query: registration_application::BoxedQuery<'a, Pg>| { let all_joins = |query: registration_application::BoxedQuery<'a, Pg>| {
@ -36,14 +42,15 @@ fn queries<'a>() -> Queries<
)) ))
}; };
let read = move |mut conn: DbConn<'a>, registration_application_id: i32| async move { let read = move |mut conn: DbConn<'a>, search: ReadBy| async move {
all_joins( let mut query = all_joins(registration_application::table.into_boxed());
registration_application::table
.find(registration_application_id) query = match search {
.into_boxed(), ReadBy::Id(id) => query.filter(registration_application::id.eq(id)),
) ReadBy::Person(person_id) => query.filter(person::id.eq(person_id)),
.first(&mut conn) };
.await
query.first(&mut conn).await
}; };
let list = move |mut conn: DbConn<'a>, options: RegistrationApplicationQuery| async move { let list = move |mut conn: DbConn<'a>, options: RegistrationApplicationQuery| async move {
@ -76,11 +83,17 @@ fn queries<'a>() -> Queries<
impl RegistrationApplicationView { impl RegistrationApplicationView {
pub async fn read( pub async fn read(
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
registration_application_id: i32, id: RegistrationApplicationId,
) -> Result<Option<Self>, Error> { ) -> Result<Option<Self>, Error> {
queries().read(pool, registration_application_id).await queries().read(pool, ReadBy::Id(id)).await
} }
pub async fn read_by_person(
pool: &mut DbPool<'_>,
person_id: PersonId,
) -> Result<Option<Self>, Error> {
queries().read(pool, ReadBy::Person(person_id)).await
}
/// Returns the current unread registration_application count /// Returns the current unread registration_application count
pub async fn get_unread_count( pub async fn get_unread_count(
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,

View file

@ -82,6 +82,7 @@ use lemmy_api::{
}, },
registration_applications::{ registration_applications::{
approve::approve_registration_application, approve::approve_registration_application,
get::get_registration_application,
list::list_registration_applications, list::list_registration_applications,
unread_count::get_unread_registration_application_count, unread_count::get_unread_registration_application_count,
}, },
@ -361,6 +362,10 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
"/registration_application/approve", "/registration_application/approve",
web::put().to(approve_registration_application), web::put().to(approve_registration_application),
) )
.route(
"/registration_application",
web::get().to(get_registration_application),
)
.route("/list_all_media", web::get().to(list_all_media)) .route("/list_all_media", web::get().to(list_all_media))
.service( .service(
web::scope("/purge") web::scope("/purge")