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 get;
pub mod list;
pub mod unread_count;

View File

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

View File

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

View File

@ -148,6 +148,12 @@ pub struct LocalSiteId(i32);
/// The custom emoji id.
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")]
#[derive(Serialize, Deserialize)]
#[serde(remote = "Ltree")]

View File

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

View File

@ -11,12 +11,18 @@ use diesel::{
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
aliases,
newtypes::{PersonId, RegistrationApplicationId},
schema::{local_user, person, registration_application},
utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
};
enum ReadBy {
Id(RegistrationApplicationId),
Person(PersonId),
}
fn queries<'a>() -> Queries<
impl ReadFn<'a, RegistrationApplicationView, i32>,
impl ReadFn<'a, RegistrationApplicationView, ReadBy>,
impl ListFn<'a, RegistrationApplicationView, RegistrationApplicationQuery>,
> {
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 {
all_joins(
registration_application::table
.find(registration_application_id)
.into_boxed(),
)
.first(&mut conn)
.await
let read = move |mut conn: DbConn<'a>, search: ReadBy| async move {
let mut query = all_joins(registration_application::table.into_boxed());
query = match search {
ReadBy::Id(id) => query.filter(registration_application::id.eq(id)),
ReadBy::Person(person_id) => query.filter(person::id.eq(person_id)),
};
query.first(&mut conn).await
};
let list = move |mut conn: DbConn<'a>, options: RegistrationApplicationQuery| async move {
@ -76,11 +83,17 @@ fn queries<'a>() -> Queries<
impl RegistrationApplicationView {
pub async fn read(
pool: &mut DbPool<'_>,
registration_application_id: i32,
id: RegistrationApplicationId,
) -> 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
pub async fn get_unread_count(
pool: &mut DbPool<'_>,

View File

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