Browse Source

improvements in private threads participants handling

Rafał Pitoń 8 years ago
parent
commit
ee71258638

+ 13 - 8
misago/threads/participants.py

@@ -70,12 +70,15 @@ def add_participant(request, thread, user):
     """
     add_participants(request, thread, [user])
 
-    record_event(request, thread, 'added_participant', {
-        'user': {
-            'username': user.username,
-            'url': user.get_absolute_url(),
-        }
-    })
+    if request.user == user:
+        record_event(request, thread, 'entered_thread')
+    else:
+        record_event(request, thread, 'added_participant', {
+            'user': {
+                'username': user.username,
+                'url': user.get_absolute_url(),
+            }
+        })
 
 
 def add_participants(request, thread, users):
@@ -94,8 +97,10 @@ def add_participants(request, thread, users):
 
     emails = []
     for user in users:
-        emails.append(build_noticiation_email(request, thread, user))
-    send_messages(emails)
+        if user != request.user:
+            emails.append(build_noticiation_email(request, thread, user))
+    if emails:
+        send_messages(emails)
 
 
 def build_noticiation_email(request, thread, user):

+ 8 - 8
misago/threads/permissions/privatethreads.py

@@ -220,16 +220,16 @@ can_change_owner = return_boolean(allow_change_owner)
 
 
 def allow_add_participants(user, target):
-    if user.acl['can_moderate_private_threads']:
-        return
+    is_moderator = user.acl['can_moderate_private_threads']
 
-    if not target.participant or not target.participant.is_owner:
-        raise PermissionDenied(
-            _("You have to be thread owner to add new participants to it."))
+    if not is_moderator:
+        if not target.participant or not target.participant.is_owner:
+            raise PermissionDenied(
+                _("You have to be thread owner to add new participants to it."))
 
-    if target.is_closed:
-        raise PermissionDenied(
-            _("Only moderators can add participants to closed threads."))
+        if target.is_closed:
+            raise PermissionDenied(
+                _("Only moderators can add participants to closed threads."))
 
     max_participants = user.acl['max_private_thread_participants']
     current_participants = len(target.participants_list) - 1

+ 10 - 17
misago/threads/tests/test_privatethread_patch_api.py

@@ -132,35 +132,28 @@ class PrivateThreadAddParticipantApiTests(PrivateThreadPatchApiTestCase):
         self.assertIn(self.user.username, email.subject)
         self.assertIn(self.thread.title, email.subject)
 
-    def test_add_user_too_many_users_moderator(self):
-        """moderators bypass users limit"""
-        ThreadParticipant.objects.set_owner(self.thread, self.user)
+    def test_add_user_to_other_user_thread_moderator(self):
+        """moderators can add users to other users threads"""
+        ThreadParticipant.objects.set_owner(self.thread, self.other_user)
+
+        self.thread.has_reported_posts = True
+        self.thread.save()
 
         override_acl(self.user, {
             'can_moderate_private_threads': 1
         })
 
-        User = get_user_model()
-        for i in range(self.user.acl['max_private_thread_participants']):
-            user = User.objects.create_user(
-                'User{}'.format(i), 'user{}@example.com'.format(i), 'Pass.123')
-            ThreadParticipant.objects.add_participants(self.thread, [user])
-
         response = self.patch(self.api_link, [
-            {'op': 'add', 'path': 'participants', 'value': self.other_user.username}
+            {'op': 'add', 'path': 'participants', 'value': self.user.username}
         ])
 
         # event was set on thread
         event = self.thread.post_set.order_by('id').last()
         self.assertTrue(event.is_event)
-        self.assertTrue(event.event_type, 'added_participant')
+        self.assertTrue(event.event_type, 'entered_thread')
 
-        # notification about new private thread was sent to other user
-        self.assertEqual(len(mail.outbox), 1)
-        email = mail.outbox[-1]
-
-        self.assertIn(self.user.username, email.subject)
-        self.assertIn(self.thread.title, email.subject)
+        # notification about new private thread wasn't send because we invited ourselves
+        self.assertEqual(len(mail.outbox), 0)
 
     def test_add_user_to_closed_moderator(self):
         """moderators can add users to closed threads"""