prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Permissions and warning for custom emoji overriding unicode emoji
  Only administrators/owners should be able to override unicode emoji
  
  1. If an administrator attempts to override a unicode emoji with a custom emoji, they should get a warning. #16937 attempts to fix this, but it is currently not working in production.
  
  We should also shorten the warning message and avoid referring to "unicode" to avoid confusing non-technical users:
  >**Override built-in emoji?**
  > Uploading a custom emoji with the name **<name>** will override the built-in **<name>** emoji. Continue?
  
  2. If a non-administrator attempts to override an emoji, show an error in the same style as the error for overriding custom emoji (screenshot below). Text: "Failed: An emoji with this name already exists. Only administrators can override built-in emoji."
  
  Error for overriding custom emoji:
  <img width="531" alt="Screen Shot 2021-06-15 at 2 30 38 PM" src="https://user-images.githubusercontent.com/2090066/122126418-915e9880-cde6-11eb-86f6-0a4338478739.png">
  
  Related issue: #18269
  [Related CZO thread](https://chat.zulip.org/#narrow/stream/2-general/topic/ok.20emoji)
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `zerver/views/realm_emoji.py`
  Content:
  ```
  1 from django.conf import settings
  2 from django.http import HttpRequest, HttpResponse
  3 from django.utils.translation import gettext as _
  4 
  5 from zerver.decorator import require_member_or_admin
  6 from zerver.lib.actions import check_add_realm_emoji, do_remove_realm_emoji
  7 from zerver.lib.emoji import check_emoji_admin, check_valid_emoji_name
  8 from zerver.lib.request import REQ, JsonableError, has_request_variables
  9 from zerver.lib.response import json_success
  10 from zerver.models import RealmEmoji, UserProfile
  11 
  12 
  13 def list_emoji(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
  14 
  15     # We don't call check_emoji_admin here because the list of realm
  16     # emoji is public.
  17     return json_success({"emoji": user_profile.realm.get_emoji()})
  18 
  19 
  20 @require_member_or_admin
  21 @has_request_variables
  22 def upload_emoji(
  23     request: HttpRequest, user_profile: UserProfile, emoji_name: str = REQ(path_only=True)
  24 ) -> HttpResponse:
  25     emoji_name = emoji_name.strip().replace(" ", "_")
  26     check_valid_emoji_name(emoji_name)
  27     check_emoji_admin(user_profile)
  28     if RealmEmoji.objects.filter(
  29         realm=user_profile.realm, name=emoji_name, deactivated=False
  30     ).exists():
  31         raise JsonableError(_("A custom emoji with this name already exists."))
  32     if len(request.FILES) != 1:
  33         raise JsonableError(_("You must upload exactly one file."))
  34     emoji_file = list(request.FILES.values())[0]
  35     if (settings.MAX_EMOJI_FILE_SIZE_MIB * 1024 * 1024) < emoji_file.size:
  36         raise JsonableError(
  37             _("Uploaded file is larger than the allowed limit of {} MiB").format(
  38                 settings.MAX_EMOJI_FILE_SIZE_MIB,
  39             )
  40         )
  41 
  42     realm_emoji = check_add_realm_emoji(user_profile.realm, emoji_name, user_profile, emoji_file)
  43     if realm_emoji is None:
  44         raise JsonableError(_("Image file upload failed."))
  45     return json_success()
  46 
  47 
  48 def delete_emoji(request: HttpRequest, user_profile: UserProfile, emoji_name: str) -> HttpResponse:
  49     if not RealmEmoji.objects.filter(
  50         realm=user_profile.realm, name=emoji_name, deactivated=False
  51     ).exists():
  52         raise JsonableError(_("Emoji '{}' does not exist").format(emoji_name))
  53     check_emoji_admin(user_profile, emoji_name)
  54     do_remove_realm_emoji(user_profile.realm, emoji_name)
  55     return json_success()
  56
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/zerver/views/realm_emoji.py b/zerver/views/realm_emoji.py
  --- a/zerver/views/realm_emoji.py
  +++ b/zerver/views/realm_emoji.py
  @@ -4,7 +4,7 @@
   
   from zerver.decorator import require_member_or_admin
   from zerver.lib.actions import check_add_realm_emoji, do_remove_realm_emoji
  -from zerver.lib.emoji import check_emoji_admin, check_valid_emoji_name
  +from zerver.lib.emoji import check_emoji_admin, check_valid_emoji_name, name_to_codepoint
   from zerver.lib.request import REQ, JsonableError, has_request_variables
   from zerver.lib.response import json_success
   from zerver.models import RealmEmoji, UserProfile
  @@ -23,6 +23,7 @@
       request: HttpRequest, user_profile: UserProfile, emoji_name: str = REQ(path_only=True)
   ) -> HttpResponse:
       emoji_name = emoji_name.strip().replace(" ", "_")
  +    valid_built_in_emoji = name_to_codepoint.keys()
       check_valid_emoji_name(emoji_name)
       check_emoji_admin(user_profile)
       if RealmEmoji.objects.filter(
  @@ -31,6 +32,9 @@
           raise JsonableError(_("A custom emoji with this name already exists."))
       if len(request.FILES) != 1:
           raise JsonableError(_("You must upload exactly one file."))
  +    if emoji_name in valid_built_in_emoji:
  +        if not user_profile.is_realm_admin:
  +            raise JsonableError(_("Only administrators can override built-in emoji."))
       emoji_file = list(request.FILES.values())[0]
       if (settings.MAX_EMOJI_FILE_SIZE_MIB * 1024 * 1024) < emoji_file.size:
           raise JsonableError(
  
verification_info: {"golden_diff": "diff --git a/zerver/views/realm_emoji.py b/zerver/views/realm_emoji.py\n--- a/zerver/views/realm_emoji.py\n+++ b/zerver/views/realm_emoji.py\n@@ -4,7 +4,7 @@\n \n from zerver.decorator import require_member_or_admin\n from zerver.lib.actions import check_add_realm_emoji, do_remove_realm_emoji\n-from zerver.lib.emoji import check_emoji_admin, check_valid_emoji_name\n+from zerver.lib.emoji import check_emoji_admin, check_valid_emoji_name, name_to_codepoint\n from zerver.lib.request import REQ, JsonableError, has_request_variables\n from zerver.lib.response import json_success\n from zerver.models import RealmEmoji, UserProfile\n@@ -23,6 +23,7 @@\n     request: HttpRequest, user_profile: UserProfile, emoji_name: str = REQ(path_only=True)\n ) -> HttpResponse:\n     emoji_name = emoji_name.strip().replace(\" \", \"_\")\n+    valid_built_in_emoji = name_to_codepoint.keys()\n     check_valid_emoji_name(emoji_name)\n     check_emoji_admin(user_profile)\n     if RealmEmoji.objects.filter(\n@@ -31,6 +32,9 @@\n         raise JsonableError(_(\"A custom emoji with this name already exists.\"))\n     if len(request.FILES) != 1:\n         raise JsonableError(_(\"You must upload exactly one file.\"))\n+    if emoji_name in valid_built_in_emoji:\n+        if not user_profile.is_realm_admin:\n+            raise JsonableError(_(\"Only administrators can override built-in emoji.\"))\n     emoji_file = list(request.FILES.values())[0]\n     if (settings.MAX_EMOJI_FILE_SIZE_MIB * 1024 * 1024) < emoji_file.size:\n         raise JsonableError(\n", "issue": "Permissions and warning for custom emoji overriding unicode emoji\nOnly administrators/owners should be able to override unicode emoji\r\n\r\n1. If an administrator attempts to override a unicode emoji with a custom emoji, they should get a warning. #16937 attempts to fix this, but it is currently not working in production.\r\n\r\nWe should also shorten the warning message and avoid referring to \"unicode\" to avoid confusing non-technical users:\r\n>**Override built-in emoji?**\r\n> Uploading a custom emoji with the name **<name>** will override the built-in **<name>** emoji. Continue?\r\n\r\n2. If a non-administrator attempts to override an emoji, show an error in the same style as the error for overriding custom emoji (screenshot below). Text: \"Failed: An emoji with this name already exists. Only administrators can override built-in emoji.\"\r\n\r\nError for overriding custom emoji:\r\n<img width=\"531\" alt=\"Screen Shot 2021-06-15 at 2 30 38 PM\" src=\"https://user-images.githubusercontent.com/2090066/122126418-915e9880-cde6-11eb-86f6-0a4338478739.png\">\r\n\r\nRelated issue: #18269\r\n[Related CZO thread](https://chat.zulip.org/#narrow/stream/2-general/topic/ok.20emoji)\r\n\n", "before_files": [{"content": "from django.conf import settings\nfrom django.http import HttpRequest, HttpResponse\nfrom django.utils.translation import gettext as _\n\nfrom zerver.decorator import require_member_or_admin\nfrom zerver.lib.actions import check_add_realm_emoji, do_remove_realm_emoji\nfrom zerver.lib.emoji import check_emoji_admin, check_valid_emoji_name\nfrom zerver.lib.request import REQ, JsonableError, has_request_variables\nfrom zerver.lib.response import json_success\nfrom zerver.models import RealmEmoji, UserProfile\n\n\ndef list_emoji(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:\n\n    # We don't call check_emoji_admin here because the list of realm\n    # emoji is public.\n    return json_success({\"emoji\": user_profile.realm.get_emoji()})\n\n\n@require_member_or_admin\n@has_request_variables\ndef upload_emoji(\n    request: HttpRequest, user_profile: UserProfile, emoji_name: str = REQ(path_only=True)\n) -> HttpResponse:\n    emoji_name = emoji_name.strip().replace(\" \", \"_\")\n    check_valid_emoji_name(emoji_name)\n    check_emoji_admin(user_profile)\n    if RealmEmoji.objects.filter(\n        realm=user_profile.realm, name=emoji_name, deactivated=False\n    ).exists():\n        raise JsonableError(_(\"A custom emoji with this name already exists.\"))\n    if len(request.FILES) != 1:\n        raise JsonableError(_(\"You must upload exactly one file.\"))\n    emoji_file = list(request.FILES.values())[0]\n    if (settings.MAX_EMOJI_FILE_SIZE_MIB * 1024 * 1024) < emoji_file.size:\n        raise JsonableError(\n            _(\"Uploaded file is larger than the allowed limit of {} MiB\").format(\n                settings.MAX_EMOJI_FILE_SIZE_MIB,\n            )\n        )\n\n    realm_emoji = check_add_realm_emoji(user_profile.realm, emoji_name, user_profile, emoji_file)\n    if realm_emoji is None:\n        raise JsonableError(_(\"Image file upload failed.\"))\n    return json_success()\n\n\ndef delete_emoji(request: HttpRequest, user_profile: UserProfile, emoji_name: str) -> HttpResponse:\n    if not RealmEmoji.objects.filter(\n        realm=user_profile.realm, name=emoji_name, deactivated=False\n    ).exists():\n        raise JsonableError(_(\"Emoji '{}' does not exist\").format(emoji_name))\n    check_emoji_admin(user_profile, emoji_name)\n    do_remove_realm_emoji(user_profile.realm, emoji_name)\n    return json_success()\n", "path": "zerver/views/realm_emoji.py"}], "after_files": [{"content": "from django.conf import settings\nfrom django.http import HttpRequest, HttpResponse\nfrom django.utils.translation import gettext as _\n\nfrom zerver.decorator import require_member_or_admin\nfrom zerver.lib.actions import check_add_realm_emoji, do_remove_realm_emoji\nfrom zerver.lib.emoji import check_emoji_admin, check_valid_emoji_name, name_to_codepoint\nfrom zerver.lib.request import REQ, JsonableError, has_request_variables\nfrom zerver.lib.response import json_success\nfrom zerver.models import RealmEmoji, UserProfile\n\n\ndef list_emoji(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:\n\n    # We don't call check_emoji_admin here because the list of realm\n    # emoji is public.\n    return json_success({\"emoji\": user_profile.realm.get_emoji()})\n\n\n@require_member_or_admin\n@has_request_variables\ndef upload_emoji(\n    request: HttpRequest, user_profile: UserProfile, emoji_name: str = REQ(path_only=True)\n) -> HttpResponse:\n    emoji_name = emoji_name.strip().replace(\" \", \"_\")\n    valid_built_in_emoji = name_to_codepoint.keys()\n    check_valid_emoji_name(emoji_name)\n    check_emoji_admin(user_profile)\n    if RealmEmoji.objects.filter(\n        realm=user_profile.realm, name=emoji_name, deactivated=False\n    ).exists():\n        raise JsonableError(_(\"A custom emoji with this name already exists.\"))\n    if len(request.FILES) != 1:\n        raise JsonableError(_(\"You must upload exactly one file.\"))\n    if emoji_name in valid_built_in_emoji:\n        if not user_profile.is_realm_admin:\n            raise JsonableError(_(\"Only administrators can override built-in emoji.\"))\n    emoji_file = list(request.FILES.values())[0]\n    if (settings.MAX_EMOJI_FILE_SIZE_MIB * 1024 * 1024) < emoji_file.size:\n        raise JsonableError(\n            _(\"Uploaded file is larger than the allowed limit of {} MiB\").format(\n                settings.MAX_EMOJI_FILE_SIZE_MIB,\n            )\n        )\n\n    realm_emoji = check_add_realm_emoji(user_profile.realm, emoji_name, user_profile, emoji_file)\n    if realm_emoji is None:\n        raise JsonableError(_(\"Image file upload failed.\"))\n    return json_success()\n\n\ndef delete_emoji(request: HttpRequest, user_profile: UserProfile, emoji_name: str) -> HttpResponse:\n    if not RealmEmoji.objects.filter(\n        realm=user_profile.realm, name=emoji_name, deactivated=False\n    ).exists():\n        raise JsonableError(_(\"Emoji '{}' does not exist\").format(emoji_name))\n    check_emoji_admin(user_profile, emoji_name)\n    do_remove_realm_emoji(user_profile.realm, emoji_name)\n    return json_success()\n", "path": "zerver/views/realm_emoji.py"}]}