FIX: Remove social sharing icons from private contexts (#10213)

FIX: Remove social sharing icons from private contexts (#10213)

diff --git a/app/assets/javascripts/discourse/app/components/share-panel.js b/app/assets/javascripts/discourse/app/components/share-panel.js
index 5094854..74608db 100644
--- a/app/assets/javascripts/discourse/app/components/share-panel.js
+++ b/app/assets/javascripts/discourse/app/components/share-panel.js
@@ -14,9 +14,10 @@ export default Component.extend({
 
   topic: alias("panel.model.topic"),
 
-  @discourseComputed
-  sources() {
-    return Sharing.activeSources(this.siteSettings.share_links);
+  @discourseComputed("topic.isPrivateMessage")
+  sources(isPM) {
+    const privateContext = this.siteSettings.login_required || isPM;
+    return Sharing.activeSources(this.siteSettings.share_links, privateContext);
   },
 
   @discourseComputed("type", "topic.title")
diff --git a/app/assets/javascripts/discourse/app/components/share-popup.js b/app/assets/javascripts/discourse/app/components/share-popup.js
index a329da8..9564c29 100644
--- a/app/assets/javascripts/discourse/app/components/share-popup.js
+++ b/app/assets/javascripts/discourse/app/components/share-popup.js
@@ -14,9 +14,10 @@ export default Component.extend({
   link: null,
   visible: null,
 
-  @discourseComputed
-  sources() {
-    return Sharing.activeSources(this.siteSettings.share_links);
+  @discourseComputed("topic.isPrivateMessage")
+  sources(isPM) {
+    const privateContext = this.siteSettings.login_required || isPM;
+    return Sharing.activeSources(this.siteSettings.share_links, privateContext);
   },
 
   @discourseComputed("type", "postNumber")
diff --git a/app/assets/javascripts/discourse/app/initializers/sharing-sources.js b/app/assets/javascripts/discourse/app/initializers/sharing-sources.js
index b56e174..127a6e5 100644
--- a/app/assets/javascripts/discourse/app/initializers/sharing-sources.js
+++ b/app/assets/javascripts/discourse/app/initializers/sharing-sources.js
@@ -40,6 +40,7 @@ export default {
       id: "email",
       icon: "envelope-square",
       title: I18n.t("share.email"),
+      showInPrivateContext: true,
       generateUrl: function(link, title) {
         return (
           "mailto:?to=&subject=" +
diff --git a/app/assets/javascripts/discourse/app/lib/sharing.js b/app/assets/javascripts/discourse/app/lib/sharing.js
index 2a3b4d1..aa7b0a5 100644
--- a/app/assets/javascripts/discourse/app/lib/sharing.js
+++ b/app/assets/javascripts/discourse/app/lib/sharing.js
@@ -11,7 +11,7 @@
 
       // The icon that will be displayed, choose between icon name `icon` and custom HTML `htmlIcon`.
       // When both provided, prefer `icon`
-      icon: 'twitter-square'
+      icon: 'twitter-square',
       htmlIcon: '<img src="example.com/example.jpg">',
 
       // A callback for generating the remote link from the `link` and `title`
@@ -22,11 +22,14 @@
       // If provided, handle by custom javascript rather than default url open
       clickHandler: function(link, title){
         alert("Hello!")
-      }
+      },
 
       // If true, opens in a popup of `popupHeight` size. If false it's opened in a new tab
       shouldOpenInPopup: true,
-      popupHeight: 265
+      popupHeight: 265,
+
+      // If true, will show the sharing service in PMs and login_required sites
+      showInPrivateContext: false
     });
   `‍``
 **/
@@ -77,12 +80,16 @@ export default {
     }
   },
 
-  activeSources(linksSetting = "") {
-    return linksSetting
+  activeSources(linksSetting = "", privateContext = false) {
+    const sources = linksSetting
       .split("|")
       .concat(_customSharingIds)
       .map(s => _sources[s])
       .compact();
+
+    return privateContext
+      ? sources.filter(s => s.showInPrivateContext)
+      : sources;
   },
 
   _reset() {
diff --git a/test/javascripts/lib/sharing-test.js b/test/javascripts/lib/sharing-test.js
index 2a6f249..d55a72c 100644
--- a/test/javascripts/lib/sharing-test.js
+++ b/test/javascripts/lib/sharing-test.js
@@ -39,4 +39,29 @@ QUnit.test("addSharingId", assert => {
     1,
     "it adds sharing id to existing sharing settings"
   );
+
+  const privateContext = true;
+
+  Sharing.addSource({
+    id: "another-source"
+  });
+  Sharing.addSharingId("another-source");
+
+  assert.equal(
+    Sharing.activeSources(sharingSettings, privateContext).length,
+    0,
+    "it does not add a regular source to sources in a private context"
+  );
+
+  Sharing.addSource({
+    id: "a-private-friendly-source",
+    showInPrivateContext: true
+  });
+  Sharing.addSharingId("a-private-friendly-source");
+
+  assert.equal(
+    Sharing.activeSources(sharingSettings, privateContext).length,
+    1,
+    "it does not add a regular source to sources in a private context"
+  );
 });

GitHub sha: c02e3581

This commit appears in #10213 which was merged by pmusaraj.

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/social-share-component/89980/63