Use enum for registration mode setting (#2604)

* Use enum for registration mode setting

* fix tests
This commit is contained in:
Nutomic 2023-01-05 01:42:30 +00:00 committed by GitHub
parent 0630d214e3
commit ceff2ec686
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 186 additions and 72 deletions

View file

@ -18,7 +18,7 @@
"eslint": "^8.25.0", "eslint": "^8.25.0",
"eslint-plugin-prettier": "^4.0.0", "eslint-plugin-prettier": "^4.0.0",
"jest": "^27.0.6", "jest": "^27.0.6",
"lemmy-js-client": "0.17.0-rc.60", "lemmy-js-client": "0.17.0-rc.61",
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"prettier": "^2.7.1", "prettier": "^2.7.1",
"ts-jest": "^27.0.3", "ts-jest": "^27.0.3",

View file

@ -64,6 +64,7 @@ import {
GetCommentsResponse, GetCommentsResponse,
FeaturePost, FeaturePost,
PostFeatureType, PostFeatureType,
RegistrationMode,
} from "lemmy-js-client"; } from "lemmy-js-client";
export interface API { export interface API {
@ -145,7 +146,7 @@ export async function setupLogins() {
// Registration applications are now enabled by default, need to disable them // Registration applications are now enabled by default, need to disable them
let editSiteForm: EditSite = { let editSiteForm: EditSite = {
require_application: false, registration_mode: RegistrationMode.Open,
federation_debug: true, federation_debug: true,
rate_limit_message: 999, rate_limit_message: 999,
rate_limit_post: 999, rate_limit_post: 999,

View file

@ -2363,10 +2363,10 @@ kleur@^3.0.3:
resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e" resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e"
integrity sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w== integrity sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==
lemmy-js-client@0.17.0-rc.60: lemmy-js-client@0.17.0-rc.61:
version "0.17.0-rc.60" version "0.17.0-rc.61"
resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.17.0-rc.60.tgz#6cabac42d842eb1f152d230be018090050476614" resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.17.0-rc.61.tgz#c01e129a3d4c3483ecf337f1e4acf0ad91f9684f"
integrity sha512-Nl+DUBJde0KpKywNkX5wof9PDCmr6RuJlgukVfQRmW5rznYRKYnI1V+awf+9mUx1eNQ8jhnjVO+6XxOzMjloZA== integrity sha512-xauBCD5i4vlUEWqsTMIXLCXeIjAK7ivVIN3C/g+RMAM7mD3CTcRkDZUerwnvLipIfr7V/4iYLWZW0orBaiV1CQ==
dependencies: dependencies:
node-fetch "2.6.6" node-fetch "2.6.6"

View file

@ -6,6 +6,7 @@ use lemmy_api_common::{
utils::password_length_check, utils::password_length_check,
}; };
use lemmy_db_schema::source::{ use lemmy_db_schema::source::{
local_site::RegistrationMode,
local_user::LocalUser, local_user::LocalUser,
password_reset_request::PasswordResetRequest, password_reset_request::PasswordResetRequest,
}; };
@ -45,8 +46,9 @@ impl Perform for PasswordChangeAfterReset {
// Return the jwt if login is allowed // Return the jwt if login is allowed
let site_view = SiteView::read_local(context.pool()).await?; let site_view = SiteView::read_local(context.pool()).await?;
let jwt = let jwt = if site_view.local_site.registration_mode == RegistrationMode::RequireApplication
if site_view.local_site.require_application && !updated_local_user.accepted_application { && !updated_local_user.accepted_application
{
None None
} else { } else {
Some( Some(

View file

@ -1,7 +1,7 @@
use crate::sensitive::Sensitive; use crate::sensitive::Sensitive;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::{CommentId, CommunityId, LanguageId, PersonId, PostId}, newtypes::{CommentId, CommunityId, LanguageId, PersonId, PostId},
source::{language::Language, tagline::Tagline}, source::{language::Language, local_site::RegistrationMode, tagline::Tagline},
ListingType, ListingType,
ModlogActionType, ModlogActionType,
SearchType, SearchType,
@ -116,11 +116,9 @@ pub struct CreateSite {
pub icon: Option<String>, pub icon: Option<String>,
pub banner: Option<String>, pub banner: Option<String>,
pub enable_downvotes: Option<bool>, pub enable_downvotes: Option<bool>,
pub open_registration: Option<bool>,
pub enable_nsfw: Option<bool>, pub enable_nsfw: Option<bool>,
pub community_creation_admin_only: Option<bool>, pub community_creation_admin_only: Option<bool>,
pub require_email_verification: Option<bool>, pub require_email_verification: Option<bool>,
pub require_application: Option<bool>,
pub application_question: Option<String>, pub application_question: Option<String>,
pub private_instance: Option<bool>, pub private_instance: Option<bool>,
pub default_theme: Option<String>, pub default_theme: Option<String>,
@ -151,6 +149,7 @@ pub struct CreateSite {
pub allowed_instances: Option<Vec<String>>, pub allowed_instances: Option<Vec<String>>,
pub blocked_instances: Option<Vec<String>>, pub blocked_instances: Option<Vec<String>>,
pub taglines: Option<Vec<String>>, pub taglines: Option<Vec<String>>,
pub registration_mode: Option<RegistrationMode>,
pub auth: Sensitive<String>, pub auth: Sensitive<String>,
} }
@ -162,11 +161,9 @@ pub struct EditSite {
pub icon: Option<String>, pub icon: Option<String>,
pub banner: Option<String>, pub banner: Option<String>,
pub enable_downvotes: Option<bool>, pub enable_downvotes: Option<bool>,
pub open_registration: Option<bool>,
pub enable_nsfw: Option<bool>, pub enable_nsfw: Option<bool>,
pub community_creation_admin_only: Option<bool>, pub community_creation_admin_only: Option<bool>,
pub require_email_verification: Option<bool>, pub require_email_verification: Option<bool>,
pub require_application: Option<bool>,
pub application_question: Option<String>, pub application_question: Option<String>,
pub private_instance: Option<bool>, pub private_instance: Option<bool>,
pub default_theme: Option<String>, pub default_theme: Option<String>,
@ -197,6 +194,7 @@ pub struct EditSite {
pub allowed_instances: Option<Vec<String>>, pub allowed_instances: Option<Vec<String>>,
pub blocked_instances: Option<Vec<String>>, pub blocked_instances: Option<Vec<String>>,
pub taglines: Option<Vec<String>>, pub taglines: Option<Vec<String>>,
pub registration_mode: Option<RegistrationMode>,
pub auth: Sensitive<String>, pub auth: Sensitive<String>,
} }

View file

@ -9,7 +9,7 @@ use lemmy_db_schema::{
community::{Community, CommunityUpdateForm}, community::{Community, CommunityUpdateForm},
email_verification::{EmailVerification, EmailVerificationForm}, email_verification::{EmailVerification, EmailVerificationForm},
instance::Instance, instance::Instance,
local_site::LocalSite, local_site::{LocalSite, RegistrationMode},
local_site_rate_limit::LocalSiteRateLimit, local_site_rate_limit::LocalSiteRateLimit,
password_reset_request::PasswordResetRequest, password_reset_request::PasswordResetRequest,
person::{Person, PersonUpdateForm}, person::{Person, PersonUpdateForm},
@ -488,7 +488,7 @@ pub async fn check_registration_application(
local_site: &LocalSite, local_site: &LocalSite,
pool: &DbPool, pool: &DbPool,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
if local_site.require_application if local_site.registration_mode == RegistrationMode::RequireApplication
&& !local_user_view.local_user.accepted_application && !local_user_view.local_user.accepted_application
&& !local_user_view.person.admin && !local_user_view.person.admin
{ {

View file

@ -1,6 +1,5 @@
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::context::LemmyContext; use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::source::local_site::LocalSite;
use lemmy_utils::{error::LemmyError, ConnectionId}; use lemmy_utils::{error::LemmyError, ConnectionId};
mod comment; mod comment;
@ -20,18 +19,3 @@ pub trait PerformCrud {
websocket_id: Option<ConnectionId>, websocket_id: Option<ConnectionId>,
) -> Result<Self::Response, LemmyError>; ) -> Result<Self::Response, LemmyError>;
} }
/// Make sure if applications are required, that there is an application questionnaire
pub fn check_application_question(
application_question: &Option<Option<String>>,
local_site: &LocalSite,
require_application: &Option<bool>,
) -> Result<(), LemmyError> {
if require_application.unwrap_or(false)
&& (application_question == &Some(None)
|| (application_question.is_none() && local_site.application_question.is_none()))
{
return Err(LemmyError::from_message("application_question_required"));
}
Ok(())
}

View file

@ -1,4 +1,4 @@
use crate::{check_application_question, PerformCrud}; use crate::{site::check_application_question, PerformCrud};
use activitypub_federation::core::signatures::generate_actor_keypair; use activitypub_federation::core::signatures::generate_actor_keypair;
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{ use lemmy_api_common::{
@ -71,8 +71,9 @@ impl PerformCrud for CreateSite {
let application_question = diesel_option_overwrite(&data.application_question); let application_question = diesel_option_overwrite(&data.application_question);
check_application_question( check_application_question(
&application_question, &application_question,
&local_site, data
&data.require_application, .registration_mode
.unwrap_or(local_site.registration_mode),
)?; )?;
let actor_id: DbUrl = Url::parse(&context.settings().get_protocol_and_hostname())?.into(); let actor_id: DbUrl = Url::parse(&context.settings().get_protocol_and_hostname())?.into();
@ -99,11 +100,10 @@ impl PerformCrud for CreateSite {
// Set the site setup to true // Set the site setup to true
.site_setup(Some(true)) .site_setup(Some(true))
.enable_downvotes(data.enable_downvotes) .enable_downvotes(data.enable_downvotes)
.open_registration(data.open_registration) .registration_mode(data.registration_mode)
.enable_nsfw(data.enable_nsfw) .enable_nsfw(data.enable_nsfw)
.community_creation_admin_only(data.community_creation_admin_only) .community_creation_admin_only(data.community_creation_admin_only)
.require_email_verification(data.require_email_verification) .require_email_verification(data.require_email_verification)
.require_application(data.require_application)
.application_question(application_question) .application_question(application_question)
.private_instance(data.private_instance) .private_instance(data.private_instance)
.default_theme(data.default_theme.clone()) .default_theme(data.default_theme.clone())

View file

@ -1,3 +1,19 @@
use lemmy_db_schema::source::local_site::RegistrationMode;
use lemmy_utils::error::LemmyError;
mod create; mod create;
mod read; mod read;
mod update; mod update;
pub fn check_application_question(
application_question: &Option<Option<String>>,
registration_mode: RegistrationMode,
) -> Result<(), LemmyError> {
if registration_mode == RegistrationMode::RequireApplication
&& application_question.as_ref().unwrap_or(&None).is_none()
{
Err(LemmyError::from_message("application_question_required"))
} else {
Ok(())
}
}

View file

@ -1,4 +1,4 @@
use crate::{check_application_question, PerformCrud}; use crate::{site::check_application_question, PerformCrud};
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
@ -17,7 +17,7 @@ use lemmy_db_schema::{
actor_language::SiteLanguage, actor_language::SiteLanguage,
federation_allowlist::FederationAllowList, federation_allowlist::FederationAllowList,
federation_blocklist::FederationBlockList, federation_blocklist::FederationBlockList,
local_site::{LocalSite, LocalSiteUpdateForm}, local_site::{LocalSite, LocalSiteUpdateForm, RegistrationMode},
local_site_rate_limit::{LocalSiteRateLimit, LocalSiteRateLimitUpdateForm}, local_site_rate_limit::{LocalSiteRateLimit, LocalSiteRateLimitUpdateForm},
local_user::LocalUser, local_user::LocalUser,
site::{Site, SiteUpdateForm}, site::{Site, SiteUpdateForm},
@ -61,8 +61,9 @@ impl PerformCrud for EditSite {
let application_question = diesel_option_overwrite(&data.application_question); let application_question = diesel_option_overwrite(&data.application_question);
check_application_question( check_application_question(
&application_question, &application_question,
&local_site, data
&data.require_application, .registration_mode
.unwrap_or(local_site.registration_mode),
)?; )?;
if let Some(default_post_listing_type) = &data.default_post_listing_type { if let Some(default_post_listing_type) = &data.default_post_listing_type {
@ -99,11 +100,10 @@ impl PerformCrud for EditSite {
let local_site_form = LocalSiteUpdateForm::builder() let local_site_form = LocalSiteUpdateForm::builder()
.enable_downvotes(data.enable_downvotes) .enable_downvotes(data.enable_downvotes)
.open_registration(data.open_registration) .registration_mode(data.registration_mode)
.enable_nsfw(data.enable_nsfw) .enable_nsfw(data.enable_nsfw)
.community_creation_admin_only(data.community_creation_admin_only) .community_creation_admin_only(data.community_creation_admin_only)
.require_email_verification(data.require_email_verification) .require_email_verification(data.require_email_verification)
.require_application(data.require_application)
.application_question(application_question) .application_question(application_question)
.private_instance(data.private_instance) .private_instance(data.private_instance)
.default_theme(data.default_theme.clone()) .default_theme(data.default_theme.clone())
@ -155,11 +155,13 @@ impl PerformCrud for EditSite {
// will be able to log in. It really only wants this to be a requirement for NEW signups. // will be able to log in. It really only wants this to be a requirement for NEW signups.
// So if it was set from false, to true, you need to update all current users columns to be verified. // So if it was set from false, to true, you need to update all current users columns to be verified.
let old_require_application =
local_site.registration_mode == RegistrationMode::RequireApplication;
let new_require_application = update_local_site let new_require_application = update_local_site
.as_ref() .as_ref()
.map(|ols| ols.require_application) .map(|ols| ols.registration_mode == RegistrationMode::RequireApplication)
.unwrap_or(false); .unwrap_or(false);
if !local_site.require_application && new_require_application { if !old_require_application && new_require_application {
LocalUser::set_all_users_registration_applications_accepted(context.pool()) LocalUser::set_all_users_registration_applications_accepted(context.pool())
.await .await
.map_err(|e| LemmyError::from_error_message(e, "couldnt_set_all_registrations_accepted"))?; .map_err(|e| LemmyError::from_error_message(e, "couldnt_set_all_registrations_accepted"))?;

View file

@ -19,6 +19,7 @@ use lemmy_api_common::{
use lemmy_db_schema::{ use lemmy_db_schema::{
aggregates::structs::PersonAggregates, aggregates::structs::PersonAggregates,
source::{ source::{
local_site::RegistrationMode,
local_user::{LocalUser, LocalUserInsertForm}, local_user::{LocalUser, LocalUserInsertForm},
person::{Person, PersonInsertForm}, person::{Person, PersonInsertForm},
registration_application::{RegistrationApplication, RegistrationApplicationInsertForm}, registration_application::{RegistrationApplication, RegistrationApplicationInsertForm},
@ -47,8 +48,10 @@ impl PerformCrud for Register {
let site_view = SiteView::read_local(context.pool()).await?; let site_view = SiteView::read_local(context.pool()).await?;
let local_site = site_view.local_site; let local_site = site_view.local_site;
let require_registration_application =
local_site.registration_mode == RegistrationMode::RequireApplication;
if !local_site.open_registration { if local_site.registration_mode == RegistrationMode::Closed {
return Err(LemmyError::from_message("registration_closed")); return Err(LemmyError::from_message("registration_closed"));
} }
@ -59,7 +62,7 @@ impl PerformCrud for Register {
return Err(LemmyError::from_message("email_required")); return Err(LemmyError::from_message("email_required"));
} }
if local_site.site_setup && local_site.require_application && data.answer.is_none() { if local_site.site_setup && require_registration_application && data.answer.is_none() {
return Err(LemmyError::from_message( return Err(LemmyError::from_message(
"registration_application_answer_required", "registration_application_answer_required",
)); ));
@ -141,7 +144,7 @@ impl PerformCrud for Register {
} }
}; };
if local_site.site_setup && local_site.require_application { if local_site.site_setup && require_registration_application {
// Create the registration application // Create the registration application
let form = RegistrationApplicationInsertForm { let form = RegistrationApplicationInsertForm {
local_user_id: inserted_local_user.id, local_user_id: inserted_local_user.id,
@ -166,7 +169,7 @@ impl PerformCrud for Register {
// Log the user in directly if the site is not setup, or email verification and application aren't required // Log the user in directly if the site is not setup, or email verification and application aren't required
if !local_site.site_setup if !local_site.site_setup
|| (!local_site.require_application && !local_site.require_email_verification) || (!require_registration_application && !local_site.require_email_verification)
{ {
login_response.jwt = Some( login_response.jwt = Some(
Claims::jwt( Claims::jwt(
@ -195,7 +198,7 @@ impl PerformCrud for Register {
login_response.verify_email_sent = true; login_response.verify_email_sent = true;
} }
if local_site.require_application { if require_registration_application {
login_response.registration_created = true; login_response.registration_created = true;
} }
} }

View file

@ -1,10 +1,25 @@
use crate::{ use crate::{
schema::local_site::dsl::local_site, schema::local_site::dsl::local_site,
source::local_site::{LocalSite, LocalSiteInsertForm, LocalSiteUpdateForm}, source::local_site::{
LocalSite,
LocalSiteInsertForm,
LocalSiteUpdateForm,
RegistrationMode,
RegistrationModeType,
},
utils::{get_conn, DbPool}, utils::{get_conn, DbPool},
}; };
use diesel::{dsl::insert_into, result::Error}; use diesel::{
deserialize,
deserialize::FromSql,
dsl::insert_into,
pg::{Pg, PgValue},
result::Error,
serialize,
serialize::{IsNull, Output, ToSql},
};
use diesel_async::RunQueryDsl; use diesel_async::RunQueryDsl;
use std::io::Write;
impl LocalSite { impl LocalSite {
pub async fn create(pool: &DbPool, form: &LocalSiteInsertForm) -> Result<Self, Error> { pub async fn create(pool: &DbPool, form: &LocalSiteInsertForm) -> Result<Self, Error> {
@ -30,3 +45,25 @@ impl LocalSite {
diesel::delete(local_site).execute(conn).await diesel::delete(local_site).execute(conn).await
} }
} }
impl ToSql<RegistrationModeType, Pg> for RegistrationMode {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
match *self {
RegistrationMode::Closed => out.write_all(b"closed")?,
RegistrationMode::RequireApplication => out.write_all(b"require_application")?,
RegistrationMode::Open => out.write_all(b"open")?,
}
Ok(IsNull::No)
}
}
impl FromSql<RegistrationModeType, Pg> for RegistrationMode {
fn from_sql(bytes: PgValue<'_>) -> deserialize::Result<Self> {
match bytes.as_bytes() {
b"closed" => Ok(RegistrationMode::Closed),
b"require_application" => Ok(RegistrationMode::RequireApplication),
b"open" => Ok(RegistrationMode::Open),
_ => Err("Unrecognized enum variant".into()),
}
}
}

View file

@ -672,16 +672,17 @@ table! {
} }
table! { table! {
use crate::source::local_site::RegistrationModeType;
use diesel::sql_types::*;
local_site(id) { local_site(id) {
id -> Int4, id -> Int4,
site_id -> Int4, site_id -> Int4,
site_setup -> Bool, site_setup -> Bool,
enable_downvotes -> Bool, enable_downvotes -> Bool,
open_registration -> Bool,
enable_nsfw -> Bool, enable_nsfw -> Bool,
community_creation_admin_only -> Bool, community_creation_admin_only -> Bool,
require_email_verification -> Bool, require_email_verification -> Bool,
require_application -> Bool,
application_question -> Nullable<Text>, application_question -> Nullable<Text>,
private_instance -> Bool, private_instance -> Bool,
default_theme -> Text, default_theme -> Text,
@ -696,6 +697,7 @@ table! {
federation_worker_count -> Int4, federation_worker_count -> Int4,
captcha_enabled -> Bool, captcha_enabled -> Bool,
captcha_difficulty -> Text, captcha_difficulty -> Text,
registration_mode -> RegistrationModeType,
published -> Timestamp, published -> Timestamp,
updated -> Nullable<Timestamp>, updated -> Nullable<Timestamp>,
} }

View file

@ -13,11 +13,9 @@ pub struct LocalSite {
pub site_id: SiteId, pub site_id: SiteId,
pub site_setup: bool, pub site_setup: bool,
pub enable_downvotes: bool, pub enable_downvotes: bool,
pub open_registration: bool,
pub enable_nsfw: bool, pub enable_nsfw: bool,
pub community_creation_admin_only: bool, pub community_creation_admin_only: bool,
pub require_email_verification: bool, pub require_email_verification: bool,
pub require_application: bool,
pub application_question: Option<String>, pub application_question: Option<String>,
pub private_instance: bool, pub private_instance: bool,
pub default_theme: String, pub default_theme: String,
@ -32,6 +30,7 @@ pub struct LocalSite {
pub federation_worker_count: i32, pub federation_worker_count: i32,
pub captcha_enabled: bool, pub captcha_enabled: bool,
pub captcha_difficulty: String, pub captcha_difficulty: String,
pub registration_mode: RegistrationMode,
pub published: chrono::NaiveDateTime, pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>, pub updated: Option<chrono::NaiveDateTime>,
} }
@ -45,11 +44,9 @@ pub struct LocalSiteInsertForm {
pub site_id: SiteId, pub site_id: SiteId,
pub site_setup: Option<bool>, pub site_setup: Option<bool>,
pub enable_downvotes: Option<bool>, pub enable_downvotes: Option<bool>,
pub open_registration: Option<bool>,
pub enable_nsfw: Option<bool>, pub enable_nsfw: Option<bool>,
pub community_creation_admin_only: Option<bool>, pub community_creation_admin_only: Option<bool>,
pub require_email_verification: Option<bool>, pub require_email_verification: Option<bool>,
pub require_application: Option<bool>,
pub application_question: Option<String>, pub application_question: Option<String>,
pub private_instance: Option<bool>, pub private_instance: Option<bool>,
pub default_theme: Option<String>, pub default_theme: Option<String>,
@ -64,6 +61,7 @@ pub struct LocalSiteInsertForm {
pub federation_worker_count: Option<i32>, pub federation_worker_count: Option<i32>,
pub captcha_enabled: Option<bool>, pub captcha_enabled: Option<bool>,
pub captcha_difficulty: Option<String>, pub captcha_difficulty: Option<String>,
pub registration_mode: Option<RegistrationMode>,
} }
#[derive(Clone, TypedBuilder)] #[derive(Clone, TypedBuilder)]
@ -73,11 +71,9 @@ pub struct LocalSiteInsertForm {
pub struct LocalSiteUpdateForm { pub struct LocalSiteUpdateForm {
pub site_setup: Option<bool>, pub site_setup: Option<bool>,
pub enable_downvotes: Option<bool>, pub enable_downvotes: Option<bool>,
pub open_registration: Option<bool>,
pub enable_nsfw: Option<bool>, pub enable_nsfw: Option<bool>,
pub community_creation_admin_only: Option<bool>, pub community_creation_admin_only: Option<bool>,
pub require_email_verification: Option<bool>, pub require_email_verification: Option<bool>,
pub require_application: Option<bool>,
pub application_question: Option<Option<String>>, pub application_question: Option<Option<String>>,
pub private_instance: Option<bool>, pub private_instance: Option<bool>,
pub default_theme: Option<String>, pub default_theme: Option<String>,
@ -92,5 +88,21 @@ pub struct LocalSiteUpdateForm {
pub federation_worker_count: Option<i32>, pub federation_worker_count: Option<i32>,
pub captcha_enabled: Option<bool>, pub captcha_enabled: Option<bool>,
pub captcha_difficulty: Option<String>, pub captcha_difficulty: Option<String>,
pub registration_mode: Option<RegistrationMode>,
pub updated: Option<Option<chrono::NaiveDateTime>>, pub updated: Option<Option<chrono::NaiveDateTime>>,
} }
#[cfg(feature = "full")]
#[derive(SqlType)]
#[diesel(postgres_type(name = "registration_mode_enum"))]
pub struct RegistrationModeType;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "full", derive(FromSqlRow, AsExpression))]
#[cfg_attr(feature = "full", diesel(sql_type = RegistrationModeType))]
#[serde(rename_all = "lowercase")]
pub enum RegistrationMode {
Closed,
RequireApplication,
Open,
}

View file

@ -1,6 +1,7 @@
use actix_web::{error::ErrorBadRequest, web, Error, HttpResponse, Result}; use actix_web::{error::ErrorBadRequest, web, Error, HttpResponse, Result};
use anyhow::anyhow; use anyhow::anyhow;
use lemmy_api_common::context::LemmyContext; use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::source::local_site::RegistrationMode;
use lemmy_db_views::structs::SiteView; use lemmy_db_views::structs::SiteView;
use lemmy_utils::{error::LemmyError, version}; use lemmy_utils::{error::LemmyError, version};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -37,7 +38,7 @@ async fn node_info(context: web::Data<LemmyContext>) -> Result<HttpResponse, Err
} else { } else {
vec![] vec![]
}; };
let open_registrations = site_view.local_site.registration_mode == RegistrationMode::Open;
let json = NodeInfo { let json = NodeInfo {
version: "2.0".to_string(), version: "2.0".to_string(),
software: NodeInfoSoftware { software: NodeInfoSoftware {
@ -54,7 +55,7 @@ async fn node_info(context: web::Data<LemmyContext>) -> Result<HttpResponse, Err
local_posts: site_view.counts.posts, local_posts: site_view.counts.posts,
local_comments: site_view.counts.comments, local_comments: site_view.counts.comments,
}, },
open_registrations: site_view.local_site.open_registration, open_registrations,
}; };
Ok(HttpResponse::Ok().json(json)) Ok(HttpResponse::Ok().json(json))

View file

@ -0,0 +1,31 @@
-- add back old registration columns
alter table local_site add column open_registration boolean not null default true;
alter table local_site add column require_application boolean not null default true;
-- regenerate their values
with subquery as (
select registration_mode,
case
when registration_mode='closed' then false
else true
end
from local_site
)
update local_site
set open_registration = subquery.case
from subquery;
with subquery as (
select registration_mode,
case
when registration_mode='open' then false
else true
end
from local_site
)
update local_site
set require_application = subquery.case
from subquery;
-- drop new column and type
alter table local_site drop column registration_mode;
drop type registration_mode_enum;

View file

@ -0,0 +1,25 @@
-- create enum for registration modes
create type registration_mode_enum as enum
('closed', 'require_application', 'open');
-- use this enum for registration mode setting
alter table local_site add column
registration_mode registration_mode_enum not null default 'require_application';
-- generate registration mode value from previous settings
with subquery as (
select open_registration, require_application,
case
when open_registration=false then 'closed'::registration_mode_enum
when open_registration=true and require_application=true then 'require_application'
else 'open'
end
from local_site
)
update local_site
set registration_mode = subquery.case
from subquery;
-- drop old registration settings
alter table local_site drop column open_registration;
alter table local_site drop column require_application;