From a93c7d93eeae92609d8631ff1831f57e99b24fc0 Mon Sep 17 00:00:00 2001
From: Dessalines <tyhou13@gmx.com>
Date: Fri, 23 Apr 2021 23:55:51 -0400
Subject: [PATCH] Adding only admins can create communities. Fixes #268

---
 lemmy-translations                  |  2 +-
 package.json                        |  2 +-
 src/shared/components/navbar.tsx    | 26 +++++++++++++++++---------
 src/shared/components/site-form.tsx | 29 +++++++++++++++++++++++++++++
 yarn.lock                           |  8 ++++----
 5 files changed, 52 insertions(+), 15 deletions(-)

diff --git a/lemmy-translations b/lemmy-translations
index ef6c991..0cf2ad8 160000
--- a/lemmy-translations
+++ b/lemmy-translations
@@ -1 +1 @@
-Subproject commit ef6c991e345a880d98b165508f28793fef3f922a
+Subproject commit 0cf2ad832d07d6c0232b3c9c1958d5572303410e
diff --git a/package.json b/package.json
index 0153a92..25ce700 100644
--- a/package.json
+++ b/package.json
@@ -68,7 +68,7 @@
     "eslint-plugin-prettier": "^3.3.1",
     "husky": "^6.0.0",
     "iso-639-1": "^2.1.9",
-    "lemmy-js-client": "0.11.0-rc.11",
+    "lemmy-js-client": "0.11.0-rc.12",
     "lint-staged": "^10.5.4",
     "mini-css-extract-plugin": "^1.4.1",
     "node-fetch": "^2.6.1",
diff --git a/src/shared/components/navbar.tsx b/src/shared/components/navbar.tsx
index ff0f0ba..31f4545 100644
--- a/src/shared/components/navbar.tsx
+++ b/src/shared/components/navbar.tsx
@@ -247,15 +247,17 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
                   {i18n.t("create_post")}
                 </Link>
               </li>
-              <li class="nav-item">
-                <Link
-                  className="nav-link"
-                  to="/create_community"
-                  title={i18n.t("create_community")}
-                >
-                  {i18n.t("create_community")}
-                </Link>
-              </li>
+              {this.canCreateCommunity && (
+                <li class="nav-item">
+                  <Link
+                    className="nav-link"
+                    to="/create_community"
+                    title={i18n.t("create_community")}
+                  >
+                    {i18n.t("create_community")}
+                  </Link>
+                </li>
+              )}
               <li class="nav-item">
                 <a
                   className="nav-link"
@@ -524,6 +526,12 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
     );
   }
 
+  get canCreateCommunity(): boolean {
+    let adminOnly = this.props.site_res.site_view.site
+      .community_creation_admin_only;
+    return !adminOnly || this.canAdmin;
+  }
+
   requestNotificationPermission() {
     if (UserService.Instance.localUserView) {
       document.addEventListener("DOMContentLoaded", function () {
diff --git a/src/shared/components/site-form.tsx b/src/shared/components/site-form.tsx
index f68d793..c5a0fb9 100644
--- a/src/shared/components/site-form.tsx
+++ b/src/shared/components/site-form.tsx
@@ -58,6 +58,8 @@ export class SiteForm extends Component<SiteFormProps, SiteFormState> {
         enable_downvotes: this.props.site.enable_downvotes,
         open_registration: this.props.site.open_registration,
         enable_nsfw: this.props.site.enable_nsfw,
+        community_creation_admin_only: this.props.site
+          .community_creation_admin_only,
         icon: this.props.site.icon,
         banner: this.props.site.banner,
         auth: authField(),
@@ -231,6 +233,28 @@ export class SiteForm extends Component<SiteFormProps, SiteFormState> {
               </div>
             </div>
           </div>
+          <div class="form-group row">
+            <div class="col-12">
+              <div class="form-check">
+                <input
+                  class="form-check-input"
+                  id="create-site-community-creation-admin-only"
+                  type="checkbox"
+                  checked={this.state.siteForm.community_creation_admin_only}
+                  onChange={linkEvent(
+                    this,
+                    this.handleSiteCommunityCreationAdminOnly
+                  )}
+                />
+                <label
+                  class="form-check-label"
+                  htmlFor="create-site-community-creation-admin-only"
+                >
+                  {i18n.t("community_creation_admin_only")}
+                </label>
+              </div>
+            </div>
+          </div>
           <div class="form-group row">
             <div class="col-12">
               <button
@@ -302,6 +326,11 @@ export class SiteForm extends Component<SiteFormProps, SiteFormState> {
     i.setState(i.state);
   }
 
+  handleSiteCommunityCreationAdminOnly(i: SiteForm, event: any) {
+    i.state.siteForm.community_creation_admin_only = event.target.checked;
+    i.setState(i.state);
+  }
+
   handleSiteEnableDownvotesChange(i: SiteForm, event: any) {
     i.state.siteForm.enable_downvotes = event.target.checked;
     i.setState(i.state);
diff --git a/yarn.lock b/yarn.lock
index eba2f02..1fed995 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -5125,10 +5125,10 @@ lcid@^1.0.0:
   dependencies:
     invert-kv "^1.0.0"
 
-lemmy-js-client@0.11.0-rc.11:
-  version "0.11.0-rc.11"
-  resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.11.0-rc.11.tgz#f99be613d08d0c196a16a37f74fdaa3b196f0459"
-  integrity sha512-IuNzU4+324fhb77JCQ/a/te/gXhQWUvxvtfV3xxQWJ0vG7kWOuz5Rd7KsfSF1d6LbuN9kbO9ebjSRW800ytbsA==
+lemmy-js-client@0.11.0-rc.12:
+  version "0.11.0-rc.12"
+  resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.11.0-rc.12.tgz#b6f80f69425ae9699857f98640a3d5a9697955b1"
+  integrity sha512-tAYL6g+OgJcNy4SP0tbN6x/qyScAV5XPVyIYGGZd2FDfVd3VFDvfoesqGBOSryLhW9jDE6YSC09jhNLMyNtMCA==
 
 levn@^0.4.1:
   version "0.4.1"
-- 
2.44.1