FEATURE: allows published pages to be public (#10053)

FEATURE: allows published pages to be public (#10053)

diff --git a/app/assets/javascripts/discourse/app/controllers/publish-page.js b/app/assets/javascripts/discourse/app/controllers/publish-page.js
index f6c8646..58059d0 100644
--- a/app/assets/javascripts/discourse/app/controllers/publish-page.js
+++ b/app/assets/javascripts/discourse/app/controllers/publish-page.js
@@ -35,6 +35,7 @@ export default Controller.extend(ModalFunctionality, StateHelpers, {
       this.state === States.existing
     );
   }),
+
   showUnpublish: computed("state", function() {
     return this.state === States.existing || this.state === States.unpublishing;
   }),
@@ -95,7 +96,7 @@ export default Controller.extend(ModalFunctionality, StateHelpers, {
     this.set("state", States.saving);
 
     return this.publishedPage
-      .update({ slug: this.publishedPage.slug })
+      .update(this.publishedPage.getProperties("slug", "public"))
       .then(() => {
         this.set("state", States.existing);
         this.model.set("publishedPage", this.publishedPage);
@@ -110,11 +111,17 @@ export default Controller.extend(ModalFunctionality, StateHelpers, {
   startNew() {
     this.setProperties({
       state: States.new,
-      publishedPage: this.store.createRecord("published_page", {
-        id: this.model.id,
-        slug: this.model.slug
-      })
+      publishedPage: this.store.createRecord(
+        "published_page",
+        this.model.getProperties("id", "slug", "public")
+      )
     });
     this.checkSlug();
+  },
+
+  @action
+  onChangePublic(isPublic) {
+    this.publishedPage.set("public", isPublic);
+    this.publish();
   }
 });
diff --git a/app/assets/javascripts/discourse/app/templates/modal/publish-page.hbs b/app/assets/javascripts/discourse/app/templates/modal/publish-page.hbs
index 712682f..9cd9a56 100644
--- a/app/assets/javascripts/discourse/app/templates/modal/publish-page.hbs
+++ b/app/assets/javascripts/discourse/app/templates/modal/publish-page.hbs
@@ -6,8 +6,29 @@
       <p class="publish-description">{{i18n "topic.publish_page.description"}}</p>
 
       <form>
-        <label>{{i18n "topic.publish_page.slug"}}</label>
-        {{text-field value=publishedPage.slug onChange=(action "checkSlug") onChangeImmediate=(action "startCheckSlug") disabled=existing class="publish-slug"}}
+        <div class="controls">
+          <label>{{i18n "topic.publish_page.slug"}}</label>
+          {{text-field
+            value=publishedPage.slug
+            onChange=(action "checkSlug")
+            onChangeImmediate=(action "startCheckSlug")
+            disabled=existing
+            class="publish-slug"
+          }}
+        </div>
+
+        <div class="controls">
+          <label>{{i18n "topic.publish_page.public"}}</label>
+
+          <p class="description">
+            {{input
+              type="checkbox"
+              checked=publishedPage.public
+              click=(action "onChangePublic" value="target.checked")
+            }}
+            {{i18n "topic.publish_page.public_description"}}
+          </p>
+        </div>
       </form>
 
       <div class="publish-url">
@@ -40,14 +61,15 @@
 
 <div class="modal-footer">
   {{#if showUnpublish}}
-    {{d-button icon="times" label="close" action=(action "closeModal")}}
-
     {{d-button
       label="topic.publish_page.unpublish"
       icon="trash-alt"
       class="btn-danger"
       isLoading=unpublishing
-      action=(action "unpublish") }}
+      action=(action "unpublish")
+    }}
+
+    {{d-button class="close-publish-page" icon="times" label="close" action=(action "closeModal")}}
   {{else if unpublished}}
     {{d-button label="topic.publish_page.publishing_settings" action=(action "startNew")}}
   {{else}}
diff --git a/app/assets/javascripts/discourse/app/templates/topic.hbs b/app/assets/javascripts/discourse/app/templates/topic.hbs
index e6968f1..33f1179 100644
--- a/app/assets/javascripts/discourse/app/templates/topic.hbs
+++ b/app/assets/javascripts/discourse/app/templates/topic.hbs
@@ -90,6 +90,9 @@
       {{#if model.publishedPage}}
         <div class="published-page-notice">
           <div class="details">
+            {{#if model.publishedPage.public}}
+              <span class="is-public">{{i18n "topic.publish_page.public"}}</span>
+            {{/if}}
             {{i18n "topic.publish_page.topic_published"}}
             <div>
               <a href={{model.publishedPage.url}} target="_blank" rel="noopener noreferrer">{{model.publishedPage.url}}</a>
diff --git a/app/assets/stylesheets/common/base/modal.scss b/app/assets/stylesheets/common/base/modal.scss
index a15a7cf..4e1a794 100644
--- a/app/assets/stylesheets/common/base/modal.scss
+++ b/app/assets/stylesheets/common/base/modal.scss
@@ -689,24 +689,45 @@
   }
 }
 
-.publish-page-modal .modal-body {
-  p.publish-description {
-    margin-top: 0;
-  }
-  input.publish-slug {
-    width: 100%;
-  }
+.publish-page-modal {
+  .modal-body {
+    p.publish-description {
+      margin-top: 0;
+    }
+    input.publish-slug {
+      width: 100%;
+    }
 
-  .publish-url {
-    margin-bottom: 1em;
-    .example-url,
-    .invalid-slug {
-      font-weight: bold;
+    .publish-url {
+      margin-bottom: 1em;
+      .example-url,
+      .invalid-slug {
+        font-weight: bold;
+      }
+    }
+
+    .publish-slug:disabled {
+      cursor: not-allowed;
+    }
+
+    .controls {
+      margin-bottom: 1em;
+
+      .description {
+        margin: 0;
+        display: flex;
+        align-items: center;
+      }
     }
   }
 
-  .publish-slug:disabled {
-    cursor: not-allowed;
+  .modal-footer {
+    display: flex;
+
+    .close-publish-page {
+      margin-left: auto;
+      margin-right: 0;
+    }
   }
 }
 
diff --git a/app/assets/stylesheets/common/base/topic.scss b/app/assets/stylesheets/common/base/topic.scss
index 02b267a..eee0faf 100644
--- a/app/assets/stylesheets/common/base/topic.scss
+++ b/app/assets/stylesheets/common/base/topic.scss
@@ -305,4 +305,13 @@ a.topic-featured-link {
       2}
   );
   align-items: center;
+
+  .is-public {
+    padding: 0.25em 0.5em;
+    font-size: $font-down-2;
+    background: $tertiary;
+    color: $secondary;
+    border-radius: 3px;
+    text-transform: lowercase;
+  }
 }
diff --git a/app/controllers/published_pages_controller.rb b/app/controllers/published_pages_controller.rb
index 67d3fb8..f61a0b0 100644
--- a/app/controllers/published_pages_controller.rb
+++ b/app/controllers/published_pages_controller.rb
@@ -5,6 +5,7 @@ class PublishedPagesController < ApplicationController
   skip_before_action :preload_json
   skip_before_action :check_xhr, :verify_authenticity_token, only: [:show]
   before_action :ensure_publish_enabled
+  before_action :redirect_to_login_if_required, except: [:show]
 
   def show
     params.require(:slug)
@@ -12,7 +13,22 @@ class PublishedPagesController < ApplicationController
     pp = PublishedPage.find_by(slug: params[:slug])
     raise Discourse::NotFound unless pp
 
-    guardian.ensure_can_see!(pp.topic)
+    return if enforce_login_required!
+
+    if !pp.public
+      begin
+        guardian.ensure_can_see!(pp.topic)
+      rescue Discourse::InvalidAccess => e
+        return rescue_discourse_actions(
+          :invalid_access,
+          403,
+          include_ember: false,
+          custom_message: e.custom_message,
+          group: e.group
+        )
+      end
+    end
+
     @topic = pp.topic
     @canonical_url = @topic.url
 
@@ -37,7 +53,15 @@ class PublishedPagesController < ApplicationController
   end
 
   def upsert
-    result, pp = PublishedPage.publish!(current_user, fetch_topic, params[:published_page][:slug].strip)
+    pp_params = params.require(:published_page)
+
+    result, pp = PublishedPage.publish!(
+      current_user,
+      fetch_topic,
+      pp_params[:slug].strip,
+      pp_params.permit(:public)
+    )
+
     json_result(pp, serializer: PublishedPageSerializer) { result }
   end
 
@@ -68,4 +92,13 @@ private

[... diff too long, it was truncated ...]

GitHub sha: 9da3a7f4

1 Like

This commit appears in #10053 which was merged by jjaffeux.