Don't allow duplicate community names in API. #957

This commit is contained in:
Dessalines 2020-07-14 16:05:23 -04:00
parent 2c0928efe0
commit fcdd1fd0f1
3 changed files with 15 additions and 4 deletions

View file

@ -88,10 +88,10 @@ impl Community {
.first::<Self>(conn) .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::*; use crate::schema::community::dsl::*;
community community
.filter(actor_id.eq(community_id)) .filter(actor_id.eq(for_actor_id))
.first::<Self>(conn) .first::<Self>(conn)
} }

View file

@ -264,6 +264,17 @@ impl Perform for Oper<CreateCommunity> {
return Err(APIError::err("site_ban").into()); 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 // When you create a community, make sure the user becomes a moderator and a follower
let keypair = generate_actor_keypair()?; let keypair = generate_actor_keypair()?;
@ -277,7 +288,7 @@ impl Perform for Oper<CreateCommunity> {
deleted: None, deleted: None,
nsfw: data.nsfw, nsfw: data.nsfw,
updated: None, updated: None,
actor_id: make_apub_endpoint(EndpointType::Community, &data.name).to_string(), actor_id,
local: true, local: true,
private_key: Some(keypair.private_key), private_key: Some(keypair.private_key),
public_key: Some(keypair.public_key), public_key: Some(keypair.public_key),

View file

@ -70,7 +70,7 @@ export class CreateCommunity extends Component<any, CreateCommunityState> {
console.log(msg); console.log(msg);
let res = wsJsonToRes(msg); let res = wsJsonToRes(msg);
if (msg.error) { if (msg.error) {
toast(i18n.t(msg.error), 'danger'); // Toast errors are already handled by community-form
return; return;
} else if (res.op == UserOperation.GetSite) { } else if (res.op == UserOperation.GetSite) {
let data = res.data as GetSiteResponse; let data = res.data as GetSiteResponse;