From fc5e65aa82dee6acba52b47dc751fcc3c327d7c7 Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+studip@gmail.com>
Date: Tue, 13 Sep 2022 12:12:21 +0000
Subject: [PATCH] allow disabling of notifications for institute blubber
 threads, fixes #1021

Closes #1021

Merge request studip/studip!996
---
 lib/models/BlubberThread.php                | 25 ++++++++++++++++++++-
 resources/vue/components/BlubberThread.vue  |  2 +-
 templates/blubber/course_context.php        | 14 +-----------
 templates/blubber/disable-notifications.php | 13 +++++++++++
 templates/blubber/institute_context.php     |  1 +
 5 files changed, 40 insertions(+), 15 deletions(-)
 create mode 100644 templates/blubber/disable-notifications.php

diff --git a/lib/models/BlubberThread.php b/lib/models/BlubberThread.php
index 1ff28bea999..bc721e0f9b9 100644
--- a/lib/models/BlubberThread.php
+++ b/lib/models/BlubberThread.php
@@ -520,6 +520,7 @@ class BlubberThread extends SimpleORMap implements PrivacyObject
             $template = $GLOBALS['template_factory']->open('blubber/institute_context');
             $template->thread = $this;
             $template->institute = Institute::find($this['context_id']);
+            $template->unfollowed = !$this->isFollowedByUser();
             return $template;
         }
     }
@@ -848,7 +849,7 @@ class BlubberThread extends SimpleORMap implements PrivacyObject
                 $this->getLastVisit() ?: object_get_visit_threshold(),
                 $user_id
             ]),
-            'notifications' => $this->id === 'global' || ($this->context_type === 'course' && !$GLOBALS['perm']->have_perm('admin')),
+            'notifications' => $this->mayDisableNotifications(),
             'followed' => $this->isFollowedByUser(),
         ];
         $context_info = $this->getContextTemplate();
@@ -1041,4 +1042,26 @@ class BlubberThread extends SimpleORMap implements PrivacyObject
         }
         return $institut_ids;
     }
+
+    /**
+     * Returns whether the notifications for this thread may be disabled.
+     *
+     * @return bool
+     */
+    public function mayDisableNotifications(): bool
+    {
+        // Notifications may always be disabled for global blubber stream
+        if ($this->id === 'global') {
+            return true;
+        }
+
+        // Notifications may not be disabled outside of course and institute
+        // streams
+        if (!in_array($this->context_type, ['course', 'institute'])) {
+            return false;
+        }
+
+        // Only users with permission below admin may disable the notifications.
+        return !$GLOBALS['perm']->have_perm('admin');
+    }
 }
diff --git a/resources/vue/components/BlubberThread.vue b/resources/vue/components/BlubberThread.vue
index 5feb8bde17a..bb742feaa7d 100644
--- a/resources/vue/components/BlubberThread.vue
+++ b/resources/vue/components/BlubberThread.vue
@@ -10,7 +10,7 @@
                :class="{unfollowed: !thread_data.followed}"
                :title="$gettext('Benachrichtigungen für diese Konversation abstellen.')"
                :data-thread_id="thread_data.thread_posting.thread_id">
-                <StudipIcon shape="remove/notification2" :size="20" class="follow text-bottom"></StudipIcon>
+                <StudipIcon shape="decline" :size="20" class="follow text-bottom"></StudipIcon>
                 <StudipIcon shape="notification2" :size="20" class="unfollow text-bottom"></StudipIcon>
                 {{ $gettext('Benachrichtigungen aktiviert') }}
             </a>
diff --git a/templates/blubber/course_context.php b/templates/blubber/course_context.php
index f5aca3258af..206397a4186 100644
--- a/templates/blubber/course_context.php
+++ b/templates/blubber/course_context.php
@@ -58,16 +58,4 @@
     </div>
 </div>
 <?= $this->render_partial("blubber/_tagcloud") ?>
-<? if (!$GLOBALS['perm']->have_perm("admin")) : ?>
-    <div class="indented new_section">
-        <a href="#"
-           onClick="STUDIP.Blubber.followunfollow('<?= htmlReady($thread->id) ?>'); return false;"
-           class="followunfollow<?= $unfollowed ? " unfollowed" : "" ?>"
-           title="<?= _("Benachrichtigungen für diese Konversation abstellen.") ?>"
-           data-thread_id="<?= htmlReady($thread->id) ?>">
-            <?= Icon::create("notification2+remove")->asImg(20, ['class' => "follow text-bottom"]) ?>
-            <?= Icon::create("notification2")->asImg(20, ['class' => "unfollow text-bottom"]) ?>
-            <?= _("Benachrichtigungen aktiviert") ?>
-        </a>
-    </div>
-<? endif ?>
+<?= $this->render_partial('blubber/disable-notifications', compact('thread', 'unfollowed')) ?>
diff --git a/templates/blubber/disable-notifications.php b/templates/blubber/disable-notifications.php
new file mode 100644
index 00000000000..a3a63c40094
--- /dev/null
+++ b/templates/blubber/disable-notifications.php
@@ -0,0 +1,13 @@
+<? if (!$GLOBALS['perm']->have_perm("admin")) : ?>
+    <div class="indented new_section">
+        <a href="#"
+           onClick="STUDIP.Blubber.followunfollow('<?= htmlReady($thread->id) ?>'); return false;"
+           class="followunfollow<?= $unfollowed ? " unfollowed" : "" ?>"
+           title="<?= _("Benachrichtigungen für diese Konversation abstellen.") ?>"
+           data-thread_id="<?= htmlReady($thread->id) ?>">
+            <?= Icon::create("decline")->asImg(20, ['class' => "follow text-bottom"]) ?>
+            <?= Icon::create("notification2")->asImg(20, ['class' => "unfollow text-bottom"]) ?>
+            <?= _("Benachrichtigungen aktiviert") ?>
+        </a>
+    </div>
+<? endif ?>
diff --git a/templates/blubber/institute_context.php b/templates/blubber/institute_context.php
index 6e0fa291abc..130378e15b9 100644
--- a/templates/blubber/institute_context.php
+++ b/templates/blubber/institute_context.php
@@ -11,3 +11,4 @@
         </div>
     </div>
 </div>
+<?= $this->render_partial('blubber/disable-notifications', compact('thread', 'unfollowed')) ?>
-- 
GitLab