From fc15276c10bd7c60d5715bcea112ac0017d572ce Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 15 Jul 2020 09:55:38 -0400 Subject: [PATCH] Don't allow duplicate community names in API. #957 (#974) --- server/lemmy_db/src/community.rs | 4 ++-- server/src/api/community.rs | 13 ++++++++++++- ui/src/components/create-community.tsx | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/server/lemmy_db/src/community.rs b/server/lemmy_db/src/community.rs index 607520803..ef91eb849 100644 --- a/server/lemmy_db/src/community.rs +++ b/server/lemmy_db/src/community.rs @@ -88,10 +88,10 @@ impl Community { .first::(conn) } - pub fn read_from_actor_id(conn: &PgConnection, community_id: &str) -> Result { + pub fn read_from_actor_id(conn: &PgConnection, for_actor_id: &str) -> Result { use crate::schema::community::dsl::*; community - .filter(actor_id.eq(community_id)) + .filter(actor_id.eq(for_actor_id)) .first::(conn) } diff --git a/server/src/api/community.rs b/server/src/api/community.rs index 80d2d1253..fc5cb0e61 100644 --- a/server/src/api/community.rs +++ b/server/src/api/community.rs @@ -263,6 +263,17 @@ impl Perform for Oper { return Err(APIError::err("site_ban").into()); } + // Double check for duplicate community actor_ids + let actor_id = make_apub_endpoint(EndpointType::Community, &data.name).to_string(); + let actor_id_cloned = actor_id.to_owned(); + let community_dupe = blocking(pool, move |conn| { + Community::read_from_actor_id(conn, &actor_id_cloned) + }) + .await?; + if community_dupe.is_ok() { + return Err(APIError::err("community_already_exists").into()); + } + // When you create a community, make sure the user becomes a moderator and a follower let keypair = generate_actor_keypair()?; @@ -276,7 +287,7 @@ impl Perform for Oper { deleted: None, nsfw: data.nsfw, updated: None, - actor_id: make_apub_endpoint(EndpointType::Community, &data.name).to_string(), + actor_id, local: true, private_key: Some(keypair.private_key), public_key: Some(keypair.public_key), diff --git a/ui/src/components/create-community.tsx b/ui/src/components/create-community.tsx index 3a5d943d4..10d570984 100644 --- a/ui/src/components/create-community.tsx +++ b/ui/src/components/create-community.tsx @@ -70,7 +70,7 @@ export class CreateCommunity extends Component { console.log(msg); let res = wsJsonToRes(msg); if (msg.error) { - toast(i18n.t(msg.error), 'danger'); + // Toast errors are already handled by community-form return; } else if (res.op == UserOperation.GetSite) { let data = res.data as GetSiteResponse;