Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
attachments: Add is_web_public field.
Clara Dantas05bf72a75cf2
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.pyindex e045c104c3c..9bef3d9387c 100644--- a/zerver/lib/actions.py+++ b/zerver/lib/actions.py@@ -5602,8 +5602,11 @@ def do_claim_attachments(message: Message, potential_path_ids: List[str]) -> boofor path_id in potential_path_ids:user_profile = message.senderis_message_realm_public = False+ is_message_web_public = Falseif message.is_stream_message():- is_message_realm_public = Stream.objects.get(id=message.recipient.type_id).is_public()+ stream = Stream.objects.get(id=message.recipient.type_id)+ is_message_realm_public = stream.is_public()+ is_message_web_public = stream.is_web_publicif not validate_attachment_request(user_profile, path_id):# Technically, there are 2 cases here:@@ -5622,7 +5625,10 @@ def do_claim_attachments(message: Message, potential_path_ids: List[str]) -> boocontinueclaimed = True- attachment = claim_attachment(user_profile, path_id, message, is_message_realm_public)+ attachment = claim_attachment(user_profile,+ path_id, message,+ is_message_realm_public,+ is_message_web_public)notify_attachment_update(user_profile, "update", attachment.to_dict())return claimeddiff --git a/zerver/lib/upload.py b/zerver/lib/upload.pyindex 7dbd550f5ae..18346830f99 100644--- a/zerver/lib/upload.py+++ b/zerver/lib/upload.py@@ -879,9 +879,11 @@ def upload_message_file(uploaded_file_name: str, uploaded_file_size: int,def claim_attachment(user_profile: UserProfile,path_id: str,message: Message,- is_message_realm_public: bool) -> Attachment:+ is_message_realm_public: bool,+ is_message_web_public: bool=False) -> Attachment:attachment = Attachment.objects.get(path_id=path_id)attachment.messages.add(message)+ attachment.is_web_public = attachment.is_web_public or is_message_web_publicattachment.is_realm_public = attachment.is_realm_public or is_message_realm_publicattachment.save()return attachmentdiff --git a/zerver/migrations/0300_add_attachment_is_web_public.py b/zerver/migrations/0300_add_attachment_is_web_public.pynew file mode 100644index 00000000000..eceae1f729f--- /dev/null+++ b/zerver/migrations/0300_add_attachment_is_web_public.py@@ -0,0 +1,23 @@+# Generated by Django 2.2.14 on 2020-07-31 21:15++from django.db import migrations, models+++class Migration(migrations.Migration):++ dependencies = [+ ('zerver', '0299_subscription_role'),+ ]++ operations = [+ migrations.AddField(+ model_name='archivedattachment',+ name='is_web_public',+ field=models.BooleanField(default=False),+ ),+ migrations.AddField(+ model_name='attachment',+ name='is_web_public',+ field=models.BooleanField(default=False),+ ),+ ]diff --git a/zerver/models.py b/zerver/models.pyindex b4728417635..3da326e1f38 100644--- a/zerver/models.py+++ b/zerver/models.py@@ -2141,12 +2141,18 @@ class AbstractAttachment(models.Model):# Size of the uploaded file, in bytessize: int = models.IntegerField()+ # The two fields below lets us avoid looking up the corresponding+ # messages/streams to check permissions before serving these files.+# Whether this attachment has been posted to a public stream, and# thus should be available to all non-guest users in the# organization (even if they weren't a recipient of a message- # linking to it). This lets us avoid looking up the corresponding- # messages/streams to check permissions before serving these files.+ # linking to it).is_realm_public: bool = models.BooleanField(default=False)+ # Whether this attachment has been posted to a web-public stream,+ # and thus should be available to everyone on the internet, even+ # if the person isn't logged in.+ is_web_public: bool = models.BooleanField(default=False)class Meta:abstract = Truediff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.pyindex c98247147d1..c6006e9a02a 100644--- a/zerver/tests/test_upload.py+++ b/zerver/tests/test_upload.py@@ -403,18 +403,28 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):and 'but lacks permission' in warn_log.output[0])self.assertEqual(Attachment.objects.get(path_id=d1_path_id).messages.count(), 1)self.assertFalse(Attachment.objects.get(path_id=d1_path_id).is_realm_public)+ self.assertFalse(Attachment.objects.get(path_id=d1_path_id).is_web_public)# Then, have the owner PM it to another user, giving that other user access.body = f"Second message ...[zulip.txt](http://{host}/user_uploads/" + d1_path_id + ")"self.send_personal_message(self.example_user("hamlet"), self.example_user("othello"), body)self.assertEqual(Attachment.objects.get(path_id=d1_path_id).messages.count(), 2)self.assertFalse(Attachment.objects.get(path_id=d1_path_id).is_realm_public)+ self.assertFalse(Attachment.objects.get(path_id=d1_path_id).is_web_public)# Then, have that new recipient user publish it.body = f"Third message ...[zulip.txt](http://{host}/user_uploads/" + d1_path_id + ")"self.send_stream_message(self.example_user("othello"), "Denmark", body, "test")self.assertEqual(Attachment.objects.get(path_id=d1_path_id).messages.count(), 3)self.assertTrue(Attachment.objects.get(path_id=d1_path_id).is_realm_public)+ self.assertFalse(Attachment.objects.get(path_id=d1_path_id).is_web_public)++ # Finally send to Rome, the web-public stream, and confirm it's now web-public+ body = f"Fourth message ...[zulip.txt](http://{host}/user_uploads/" + d1_path_id + ")"+ self.send_stream_message(self.example_user("othello"), "Rome", body, "test")+ self.assertEqual(Attachment.objects.get(path_id=d1_path_id).messages.count(), 4)+ self.assertTrue(Attachment.objects.get(path_id=d1_path_id).is_realm_public)+ self.assertTrue(Attachment.objects.get(path_id=d1_path_id).is_web_public)def test_check_attachment_reference_update(self) -> None:f1 = StringIO("file1")