Using instance creation helper function.

This commit is contained in:
Dessalines 2022-10-14 12:07:54 -04:00
parent 2fcea21d96
commit cbd8eead41
31 changed files with 69 additions and 240 deletions

View file

@ -250,7 +250,7 @@ mod tests {
use lemmy_api_common::utils::check_validator_time;
use lemmy_db_schema::{
source::{
instance::{Instance, InstanceForm},
instance::Instance,
local_user::{LocalUser, LocalUserInsertForm},
person::{Person, PersonInsertForm},
secret::Secret,
@ -266,12 +266,7 @@ mod tests {
let secret = Secret::init(conn).unwrap();
let settings = &SETTINGS.to_owned();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let new_person = PersonInsertForm::builder()
.name("Gerry9812".into())

View file

@ -152,7 +152,7 @@ mod tests {
use lemmy_db_schema::{
source::{
community::Community,
instance::{Instance, InstanceForm},
instance::Instance,
person::{Person, PersonInsertForm},
site::Site,
},
@ -169,12 +169,7 @@ mod tests {
let community = parse_lemmy_community(&context).await;
let community_id = community.id;
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let old_mod = PersonInsertForm::builder()
.name("holly".into())

View file

@ -26,10 +26,9 @@ use lemmy_db_schema::{
source::{
actor_language::CommunityLanguage,
community::{Community, CommunityUpdateForm},
instance::{Instance, InstanceForm},
instance::Instance,
},
traits::{ApubActor, Crud},
utils::naive_now,
};
use lemmy_db_views_actor::structs::CommunityFollowerView;
use lemmy_utils::{
@ -150,14 +149,7 @@ impl ApubObject for ApubCommunity {
request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let domain = generate_domain_url(group.id.inner())?;
let instance_form = InstanceForm {
domain,
updated: Some(naive_now()),
};
let instance = blocking(context.pool(), move |conn| {
Instance::create(conn, &instance_form)
})
.await??;
let instance = blocking(context.pool(), move |conn| Instance::create(conn, &domain)).await??;
let form = Group::into_insert_form(group.clone(), instance.id);
let languages = LanguageTag::to_language_id_multiple(group.language, context.pool()).await?;

View file

@ -25,7 +25,7 @@ use lemmy_api_common::utils::blocking;
use lemmy_db_schema::{
source::{
actor_language::SiteLanguage,
instance::{Instance as DbInstance, InstanceForm},
instance::Instance as DbInstance,
site::{Site, SiteInsertForm},
},
traits::Crud,
@ -143,14 +143,7 @@ impl ApubObject for ApubSite {
_request_counter: &mut i32,
) -> Result<Self, LemmyError> {
let domain = generate_domain_url(apub.id.inner())?;
let instance_form = InstanceForm {
domain,
updated: Some(naive_now()),
};
let instance = blocking(data.pool(), move |conn| {
DbInstance::create(conn, &instance_form)
})
.await??;
let instance = blocking(data.pool(), move |conn| DbInstance::create(conn, &domain)).await??;
let site_form = SiteInsertForm {
name: apub.name.clone(),

View file

@ -23,7 +23,7 @@ use chrono::NaiveDateTime;
use lemmy_api_common::utils::blocking;
use lemmy_db_schema::{
source::{
instance::{Instance, InstanceForm},
instance::Instance,
person::{Person as DbPerson, PersonInsertForm, PersonUpdateForm},
},
traits::{ApubActor, Crud},
@ -161,14 +161,7 @@ impl ApubObject for ApubPerson {
) -> Result<ApubPerson, LemmyError> {
// TODO Maybe a better way to do this? Same for community and site from_apub
let domain = generate_domain_url(person.id.inner())?;
let instance_form = InstanceForm {
domain,
updated: Some(naive_now()),
};
let instance = blocking(context.pool(), move |conn| {
Instance::create(conn, &instance_form)
})
.await??;
let instance = blocking(context.pool(), move |conn| Instance::create(conn, &domain)).await??;
let person_form = PersonInsertForm {
name: person.preferred_username,

View file

@ -20,7 +20,7 @@ mod tests {
source::{
comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm},
community::{Community, CommunityInsertForm},
instance::{Instance, InstanceForm},
instance::Instance,
person::{Person, PersonInsertForm},
post::{Post, PostInsertForm},
},
@ -34,12 +34,7 @@ mod tests {
fn test_crud() {
let conn = &mut establish_unpooled_connection();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let new_person = PersonInsertForm::builder()
.name("thommy_comment_agg".into())

View file

@ -20,7 +20,7 @@ mod tests {
source::{
comment::{Comment, CommentInsertForm},
community::{Community, CommunityFollower, CommunityFollowerForm, CommunityInsertForm},
instance::{Instance, InstanceForm},
instance::Instance,
person::{Person, PersonInsertForm},
post::{Post, PostInsertForm},
},
@ -34,12 +34,7 @@ mod tests {
fn test_crud() {
let conn = &mut establish_unpooled_connection();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let new_person = PersonInsertForm::builder()
.name("thommy_community_agg".into())

View file

@ -16,7 +16,7 @@ mod tests {
source::{
comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm},
community::{Community, CommunityInsertForm},
instance::{Instance, InstanceForm},
instance::Instance,
person::{Person, PersonInsertForm},
post::{Post, PostInsertForm, PostLike, PostLikeForm},
},
@ -30,12 +30,7 @@ mod tests {
fn test_crud() {
let conn = &mut establish_unpooled_connection();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let new_person = PersonInsertForm::builder()
.name("thommy_user_agg".into())

View file

@ -16,7 +16,7 @@ mod tests {
source::{
comment::{Comment, CommentInsertForm},
community::{Community, CommunityInsertForm},
instance::{Instance, InstanceForm},
instance::Instance,
person::{Person, PersonInsertForm},
post::{Post, PostInsertForm, PostLike, PostLikeForm},
},
@ -30,12 +30,7 @@ mod tests {
fn test_crud() {
let conn = &mut establish_unpooled_connection();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let new_person = PersonInsertForm::builder()
.name("thommy_community_agg".into())

View file

@ -14,7 +14,7 @@ mod tests {
source::{
comment::{Comment, CommentInsertForm},
community::{Community, CommunityInsertForm},
instance::{Instance, InstanceForm},
instance::Instance,
person::{Person, PersonInsertForm},
post::{Post, PostInsertForm},
site::{Site, SiteInsertForm},
@ -29,12 +29,7 @@ mod tests {
fn test_crud() {
let conn = &mut establish_unpooled_connection();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let new_person = PersonInsertForm::builder()
.name("thommy_site_agg".into())

View file

@ -84,7 +84,7 @@ mod tests {
newtypes::DbUrl,
source::{
activity::{Activity, ActivityInsertForm},
instance::{Instance, InstanceForm},
instance::Instance,
person::{Person, PersonInsertForm},
},
utils::establish_unpooled_connection,
@ -98,12 +98,7 @@ mod tests {
fn test_crud() {
let conn = &mut establish_unpooled_connection();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let creator_form = PersonInsertForm::builder()
.name("activity_creator_ pm".into())

View file

@ -234,7 +234,7 @@ mod tests {
impls::actor_language::*,
source::{
community::{Community, CommunityInsertForm},
instance::{Instance, InstanceForm},
instance::Instance,
local_site::{LocalSite, LocalSiteInsertForm},
local_user::{LocalUser, LocalUserInsertForm},
person::{Person, PersonInsertForm},
@ -260,12 +260,7 @@ mod tests {
}
fn create_test_site(conn: &mut PgConnection) -> (Site, Instance) {
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let site_form = SiteInsertForm::builder()
.name("test site".to_string())

View file

@ -231,7 +231,7 @@ mod tests {
source::{
comment::*,
community::{Community, CommunityInsertForm},
instance::{Instance, InstanceForm},
instance::Instance,
person::{Person, PersonInsertForm},
post::*,
},
@ -246,12 +246,7 @@ mod tests {
fn test_crud() {
let conn = &mut establish_unpooled_connection();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let new_person = PersonInsertForm::builder()
.name("terry".into())

View file

@ -71,7 +71,7 @@ mod tests {
comment::*,
comment_reply::*,
community::{Community, CommunityInsertForm},
instance::{Instance, InstanceForm},
instance::Instance,
person::*,
post::*,
},
@ -85,12 +85,7 @@ mod tests {
fn test_crud() {
let conn = &mut establish_unpooled_connection();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let new_person = PersonInsertForm::builder()
.name("terrylake".into())

View file

@ -336,11 +336,7 @@ impl ApubActor for Community {
#[cfg(test)]
mod tests {
use crate::{
source::{
community::*,
instance::{Instance, InstanceForm},
person::*,
},
source::{community::*, instance::Instance, person::*},
traits::{Bannable, Crud, Followable, Joinable},
utils::establish_unpooled_connection,
};
@ -351,12 +347,7 @@ mod tests {
fn test_crud() {
let conn = &mut establish_unpooled_connection();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let new_person = PersonInsertForm::builder()
.name("bobbee".into())

View file

@ -2,7 +2,7 @@ use crate::{
schema::federation_allowlist,
source::{
federation_allowlist::{FederationAllowList, FederationAllowListForm},
instance::{Instance, InstanceForm},
instance::Instance,
},
};
use diesel::{dsl::*, result::Error, *};
@ -15,11 +15,7 @@ impl FederationAllowList {
for domain in list {
// Upsert all of these as instances
let instance_form = InstanceForm {
domain: domain.to_string(),
updated: None,
};
let instance = Instance::create(conn, &instance_form)?;
let instance = Instance::create(conn, &domain)?;
let form = FederationAllowListForm {
instance_id: instance.id,

View file

@ -2,7 +2,7 @@ use crate::{
schema::federation_blocklist,
source::{
federation_blocklist::{FederationBlockList, FederationBlockListForm},
instance::{Instance, InstanceForm},
instance::Instance,
},
};
use diesel::{dsl::*, result::Error, *};
@ -15,11 +15,7 @@ impl FederationBlockList {
for domain in list {
// Upsert all of these as instances
let instance_form = InstanceForm {
domain: domain.to_string(),
updated: None,
};
let instance = Instance::create(conn, &instance_form)?;
let instance = Instance::create(conn, &domain)?;
let form = FederationBlockListForm {
instance_id: instance.id,

View file

@ -2,11 +2,12 @@ use crate::{
newtypes::InstanceId,
schema::{federation_allowlist, federation_blocklist, instance},
source::instance::{Instance, InstanceForm},
utils::naive_now,
};
use diesel::{dsl::*, result::Error, *};
impl Instance {
pub fn create(conn: &mut PgConnection, form: &InstanceForm) -> Result<Self, Error> {
fn create_from_form(conn: &mut PgConnection, form: &InstanceForm) -> Result<Self, Error> {
// Do upsert on domain name conflict
insert_into(instance::table)
.values(form)
@ -15,6 +16,13 @@ impl Instance {
.set(form)
.get_result::<Self>(conn)
}
pub fn create(conn: &mut PgConnection, domain: &str) -> Result<Self, Error> {
let form = InstanceForm {
domain: domain.to_string(),
updated: Some(naive_now()),
};
Self::create_from_form(conn, &form)
}
pub fn delete(conn: &mut PgConnection, instance_id: InstanceId) -> Result<usize, Error> {
diesel::delete(instance::table.find(instance_id)).execute(conn)
}

View file

@ -395,14 +395,7 @@ impl Crud for AdminPurgeComment {
#[cfg(test)]
mod tests {
use crate::{
source::{
comment::*,
community::*,
instance::{Instance, InstanceForm},
moderator::*,
person::*,
post::*,
},
source::{comment::*, community::*, instance::Instance, moderator::*, person::*, post::*},
traits::Crud,
utils::establish_unpooled_connection,
};
@ -414,12 +407,7 @@ mod tests {
fn test_crud() {
let conn = &mut establish_unpooled_connection();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let new_mod = PersonInsertForm::builder()
.name("the mod".into())

View file

@ -75,7 +75,7 @@ fn bytes_to_hex(bytes: Vec<u8>) -> String {
mod tests {
use crate::{
source::{
instance::{Instance, InstanceForm},
instance::Instance,
local_user::{LocalUser, LocalUserInsertForm},
password_reset_request::PasswordResetRequest,
person::*,
@ -90,12 +90,7 @@ mod tests {
fn test_crud() {
let conn = &mut establish_unpooled_connection();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let new_person = PersonInsertForm::builder()
.name("thommy prw".into())

View file

@ -178,10 +178,7 @@ impl ApubActor for Person {
#[cfg(test)]
mod tests {
use crate::{
source::{
instance::{Instance, InstanceForm},
person::*,
},
source::{instance::Instance, person::*},
traits::Crud,
utils::establish_unpooled_connection,
};
@ -192,12 +189,7 @@ mod tests {
fn test_crud() {
let conn = &mut establish_unpooled_connection();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let new_person = PersonInsertForm::builder()
.name("holly".into())

View file

@ -75,7 +75,7 @@ mod tests {
source::{
comment::*,
community::{Community, CommunityInsertForm},
instance::{Instance, InstanceForm},
instance::Instance,
person::*,
person_mention::*,
post::*,
@ -90,12 +90,7 @@ mod tests {
fn test_crud() {
let conn = &mut establish_unpooled_connection();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let new_person = PersonInsertForm::builder()
.name("terrylake".into())

View file

@ -281,7 +281,7 @@ mod tests {
use crate::{
source::{
community::{Community, CommunityInsertForm},
instance::{Instance, InstanceForm},
instance::Instance,
person::*,
post::*,
},
@ -295,12 +295,7 @@ mod tests {
fn test_crud() {
let conn = &mut establish_unpooled_connection();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let new_person = PersonInsertForm::builder()
.name("jim".into())

View file

@ -83,11 +83,7 @@ impl DeleteableOrRemoveable for PrivateMessage {
#[cfg(test)]
mod tests {
use crate::{
source::{
instance::{Instance, InstanceForm},
person::*,
private_message::*,
},
source::{instance::Instance, person::*, private_message::*},
traits::Crud,
utils::establish_unpooled_connection,
};
@ -98,12 +94,7 @@ mod tests {
fn test_crud() {
let conn = &mut establish_unpooled_connection();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let creator_form = PersonInsertForm::builder()
.name("creator_pm".into())

View file

@ -290,14 +290,7 @@ mod tests {
use crate::comment_report_view::{CommentReportQuery, CommentReportView};
use lemmy_db_schema::{
aggregates::structs::CommentAggregates,
source::{
comment::*,
comment_report::*,
community::*,
instance::{Instance, InstanceForm},
person::*,
post::*,
},
source::{comment::*, comment_report::*, community::*, instance::Instance, person::*, post::*},
traits::{Crud, Joinable, Reportable},
utils::establish_unpooled_connection,
};
@ -308,12 +301,7 @@ mod tests {
fn test_crud() {
let conn = &mut establish_unpooled_connection();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let new_person = PersonInsertForm::builder()
.name("timmy_crv".into())

View file

@ -396,7 +396,7 @@ mod tests {
actor_language::LocalUserLanguage,
comment::*,
community::*,
instance::{Instance, InstanceForm},
instance::Instance,
language::Language,
local_user::LocalUserInsertForm,
person::*,
@ -422,12 +422,7 @@ mod tests {
}
fn init_data(conn: &mut PgConnection) -> Data {
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let new_person = PersonInsertForm::builder()
.name("timmy".into())

View file

@ -272,7 +272,7 @@ mod tests {
aggregates::structs::PostAggregates,
source::{
community::*,
instance::{Instance, InstanceForm},
instance::Instance,
person::*,
post::*,
post_report::{PostReport, PostReportForm},
@ -287,12 +287,7 @@ mod tests {
fn test_crud() {
let conn = &mut establish_unpooled_connection();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let new_person = PersonInsertForm::builder()
.name("timmy_prv".into())

View file

@ -457,7 +457,7 @@ mod tests {
actor_language::LocalUserLanguage,
community::*,
community_block::{CommunityBlock, CommunityBlockForm},
instance::{Instance, InstanceForm},
instance::Instance,
language::Language,
local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm},
person::*,
@ -482,12 +482,7 @@ mod tests {
}
fn init_data(conn: &mut PgConnection) -> Data {
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let person_name = "tegan".to_string();

View file

@ -148,7 +148,7 @@ mod tests {
use crate::private_message_report_view::PrivateMessageReportQuery;
use lemmy_db_schema::{
source::{
instance::{Instance, InstanceForm},
instance::Instance,
person::{Person, PersonInsertForm},
private_message::{PrivateMessage, PrivateMessageInsertForm},
private_message_report::{PrivateMessageReport, PrivateMessageReportForm},
@ -163,12 +163,7 @@ mod tests {
fn test_crud() {
let conn = &mut establish_unpooled_connection();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let new_person_1 = PersonInsertForm::builder()
.name("timmy_mrv".into())

View file

@ -157,7 +157,7 @@ mod tests {
};
use lemmy_db_schema::{
source::{
instance::{Instance, InstanceForm},
instance::Instance,
local_user::{LocalUser, LocalUserInsertForm, LocalUserSettings, LocalUserUpdateForm},
person::*,
registration_application::{
@ -176,12 +176,7 @@ mod tests {
fn test_crud() {
let conn = &mut establish_unpooled_connection();
let new_instance = InstanceForm {
domain: "my_domain.tld".into(),
updated: None,
};
let inserted_instance = Instance::create(conn, &new_instance).unwrap();
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
let timmy_person_form = PersonInsertForm::builder()
.name("timmy_rav".into())

View file

@ -17,7 +17,7 @@ use lemmy_db_schema::{
source::{
comment::{Comment, CommentUpdateForm},
community::{Community, CommunityUpdateForm},
instance::{Instance, InstanceForm},
instance::Instance,
local_site::{LocalSite, LocalSiteInsertForm},
local_site_rate_limit::{LocalSiteRateLimit, LocalSiteRateLimitInsertForm},
local_user::{LocalUser, LocalUserInsertForm},
@ -398,11 +398,7 @@ fn initialize_local_site_2022_10_10(
.expect("must have domain");
// Upsert this to the instance table
let instance_form = InstanceForm {
domain,
updated: None,
};
let instance = Instance::create(conn, &instance_form)?;
let instance = Instance::create(conn, &domain)?;
if let Some(setup) = &settings.setup {
let person_keypair = generate_actor_keypair()?;