]> Untitled Git - lemmy.git/commitdiff
Don't allow duplicate community names in API. #957 (#974)
authorDessalines <dessalines@users.noreply.github.com>
Wed, 15 Jul 2020 13:55:38 +0000 (09:55 -0400)
committerGitHub <noreply@github.com>
Wed, 15 Jul 2020 13:55:38 +0000 (09:55 -0400)
server/lemmy_db/src/community.rs
server/src/api/community.rs
ui/src/components/create-community.tsx

index 607520803b7aa2621a2f10d2e997b4ceb57f1716..ef91eb849bd8f4636816db8154bd458531391971 100644 (file)
@@ -88,10 +88,10 @@ impl Community {
       .first::<Self>(conn)
   }
 
-  pub fn read_from_actor_id(conn: &PgConnection, community_id: &str) -> Result<Self, Error> {
+  pub fn read_from_actor_id(conn: &PgConnection, for_actor_id: &str) -> Result<Self, Error> {
     use crate::schema::community::dsl::*;
     community
-      .filter(actor_id.eq(community_id))
+      .filter(actor_id.eq(for_actor_id))
       .first::<Self>(conn)
   }
 
index 80d2d12532f701540be2bba609b55e9f4c8f5df0..fc5cb0e61d6775347b33286c51b855821165448b 100644 (file)
@@ -263,6 +263,17 @@ impl Perform for Oper<CreateCommunity> {
       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<CreateCommunity> {
       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),
index 3a5d943d4ac64bc81c90056f4958e8860631e103..10d57098420abfe645551fe6b4f92af6d2911dcc 100644 (file)
@@ -70,7 +70,7 @@ export class CreateCommunity extends Component<any, CreateCommunityState> {
     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;