FEATURE: allow users to opt out from notifications

FEATURE: allow users to opt out from notifications

Some users find code review approval notifications annoying, allow them to opt out.

This can be disabled from the pref->notification panel for the user

diff --git a/assets/javascripts/discourse/connectors/user-preferences-notifications/notify-code-review.js.es6 b/assets/javascripts/discourse/connectors/user-preferences-notifications/notify-code-review.js.es6
new file mode 100644
index 0000000..b449c60
--- /dev/null
+++ b/assets/javascripts/discourse/connectors/user-preferences-notifications/notify-code-review.js.es6
@@ -0,0 +1,6 @@
+export default {
+  //setupComponent(args, component) {},
+  shouldRender(args, component) {
+    return component.currentUser && component.currentUser.admin;
+  },
+};
diff --git a/assets/javascripts/discourse/initializers/init-code-review.js.es6 b/assets/javascripts/discourse/initializers/init-code-review.js.es6
index 67169a3..ba9feda 100644
--- a/assets/javascripts/discourse/initializers/init-code-review.js.es6
+++ b/assets/javascripts/discourse/initializers/init-code-review.js.es6
@@ -39,6 +39,13 @@ function initialize(api) {
     }),
   });
 
+  api.modifyClass("controller:preferences/notifications", {
+    init() {
+      this._super(...arguments);
+      this.saveAttrNames.push("custom_fields");
+    },
+  });
+
   function allowSkip(currentUser, topic, siteSettings) {
     return allowApprove(currentUser, topic, siteSettings);
   }
diff --git a/assets/javascripts/discourse/templates/connectors/user-preferences-notifications/notify-code-review.hbs b/assets/javascripts/discourse/templates/connectors/user-preferences-notifications/notify-code-review.hbs
new file mode 100644
index 0000000..48b52d2
--- /dev/null
+++ b/assets/javascripts/discourse/templates/connectors/user-preferences-notifications/notify-code-review.hbs
@@ -0,0 +1,8 @@
+<div class="control-group">
+  <label class="control-label">{{i18n "code_review.title"}}</label>
+  <div class="controls">
+    {{preference-checkbox
+          labelKey="code_review.notify_on_approval"
+          checked=model.custom_fields.notify_on_code_reviews}}
+  </div>
+</div>
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 4a880e2..951beeb 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -29,3 +29,4 @@ en:
       github_webhooks: "Github Webhooks"
       configure_webhooks: "Configure Webhooks"
       configure_webhook: "Configure Webhook"
+      notify_on_approval: "Notify on code review approvals"
diff --git a/lib/discourse_code_review/state/commit_approval.rb b/lib/discourse_code_review/state/commit_approval.rb
index 2c68ecd..7e37410 100644
--- a/lib/discourse_code_review/state/commit_approval.rb
+++ b/lib/discourse_code_review/state/commit_approval.rb
@@ -139,6 +139,17 @@ module DiscourseCodeReview::State::CommitApproval
     end
 
     def send_approved_notification(topic, post)
+      if !topic.user
+        return
+      end
+
+      notify = topic.user.custom_fields[DiscourseCodeReview::NOTIFY_REVIEW_CUSTOM_FIELD]
+
+      # can be nil as well
+      if notify == false
+        return
+      end
+
       Notification.transaction do
         destroyed_notifications =
           topic.user.notifications
diff --git a/plugin.rb b/plugin.rb
index 16a1518..7626ad2 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -50,6 +50,7 @@ after_initialize do
 
   module ::DiscourseCodeReview
     PluginName = 'discourse-code-review'
+    NOTIFY_REVIEW_CUSTOM_FIELD = 'notify_on_code_reviews'
 
     class APIUserError < StandardError
     end
@@ -162,6 +163,16 @@ after_initialize do
     end
   end
 
+  # TODO Drop after Discourse 2.6.0 release
+  register_editable_user_custom_field(DiscourseCodeReview::NOTIFY_REVIEW_CUSTOM_FIELD)
+  if respond_to?(:allow_staff_user_custom_field)
+    allow_staff_user_custom_field(DiscourseCodeReview::NOTIFY_REVIEW_CUSTOM_FIELD)
+  else
+    whitelist_staff_user_custom_field(DiscourseCodeReview::NOTIFY_REVIEW_CUSTOM_FIELD)
+  end
+
+  User.register_custom_field_type(DiscourseCodeReview::NOTIFY_REVIEW_CUSTOM_FIELD, :boolean)
+
   require File.expand_path("../app/controllers/discourse_code_review/code_review_controller.rb", __FILE__)
   require File.expand_path("../app/controllers/discourse_code_review/organizations_controller.rb", __FILE__)
   require File.expand_path("../app/controllers/discourse_code_review/repos_controller.rb", __FILE__)

GitHub sha: ed69e785

This comment should be removed.

Sorry, sorted it out in the following commit

On Wed, 23 Sep 2020 at 12:44 am, Robin Ward notifications@github.com wrote:

This comment should be removed.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/discourse/discourse-code-review/commit/ed69e78576bd6e4ea30293449468e085054a3bd7#commitcomment-42626755, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAABIXIGISW3ZQKW4YK45CLSHCZ53ANCNFSM4RV3YXBQ .

1 Like