Repository: fossasia/open-event-server

Issue:
Event Room Chat: Renaming Event name shows no room exists
**Describe the bug**
<!-- A clear and concise description of what the bug is. -->

**To Reproduce**
Steps to reproduce the behavior:
1. Go to 'event page' after renaming the event
2. Click on 'side panel - chat'
4. See error

![Screenshot from 2021-04-12 22-34-19](https://user-images.githubusercontent.com/61330148/114433382-620d8e80-9bdf-11eb-9ec0-9fcf43412fd8.png)

**Additional Context**
https://developer.rocket.chat/api/rest-api/methods/groups/rename


Discussion:

Resolving patch:
diff --git a/app/api/chat/rocket_chat.py b/app/api/chat/rocket_chat.py
--- a/app/api/chat/rocket_chat.py
+++ b/app/api/chat/rocket_chat.py
@@ -206,3 +206,32 @@ def get_rocket_chat_token(user: User, event: Event = None):
 
     rocket_chat = RocketChat(api_url)
     return rocket_chat.get_token(user, event)
+
+
+def rename_rocketchat_room(event: Event):
+    settings = get_settings()
+    if not event.chat_room_id or not (api_url := settings['rocket_chat_url']):
+        return
+
+    rocket_chat = RocketChat(api_url)
+    bot = rocket_chat.check_or_create_bot()
+    data = rocket_chat.get_token(bot)
+
+    bot_token = data['token']
+    bot_id = data['res']['data']['userId']
+
+    res = requests.post(
+        rocket_chat.api_url + '/api/v1/groups.rename',
+        json=dict(
+            name=event.chat_room_name,
+            roomId=event.chat_room_id,
+        ),
+        headers={
+            'X-Auth-Token': bot_token,
+            'X-User-Id': bot_id,
+        },
+    )
+
+    if not res.status_code == 200:
+        logger.error('Error while changing room name : %s', res.json())
+        raise RocketChatException('Error while changing room name', response=res)
diff --git a/app/api/events.py b/app/api/events.py
--- a/app/api/events.py
+++ b/app/api/events.py
@@ -1,7 +1,7 @@
 from datetime import datetime
 
 import pytz
-from flask import request
+from flask import g, request
 from flask.blueprints import Blueprint
 from flask.json import jsonify
 from flask_jwt_extended import current_user, get_jwt_identity, verify_jwt_in_request
@@ -525,6 +525,8 @@ def before_update_object(self, event, data, view_kwargs):
         :param view_kwargs:
         :return:
         """
+        g.event_name = event.name
+
         is_date_updated = (
             data.get('starts_at') != event.starts_at
             or data.get('ends_at') != event.ends_at
@@ -552,6 +554,11 @@ def before_update_object(self, event, data, view_kwargs):
             start_image_resizing_tasks(event, data['original_image_url'])
 
     def after_update_object(self, event, data, view_kwargs):
+        if event.name != g.event_name:
+            from .helpers.tasks import rename_chat_room
+
+            rename_chat_room.delay(event.id)
+
         if event.state == Event.State.PUBLISHED and event.schedule_published_on:
             start_export_tasks(event)
         else:
diff --git a/app/api/helpers/tasks.py b/app/api/helpers/tasks.py
--- a/app/api/helpers/tasks.py
+++ b/app/api/helpers/tasks.py
@@ -20,6 +20,7 @@
     Mail,
 )
 
+from app.api.chat.rocket_chat import rename_rocketchat_room
 from app.api.exports import event_export_task_base
 from app.api.helpers.db import safe_query, save_to_db
 from app.api.helpers.files import (
@@ -146,9 +147,7 @@ def send_email_task_sendgrid(payload):
         elif e.code == 554:
             empty_attachments_send(sendgrid_client, message)
         else:
-            logging.exception(
-                "The following error has occurred with sendgrid-{}".format(str(e))
-            )
+            logging.exception(f"The following error has occurred with sendgrid-{str(e)}")
 
 
 @celery.task(name='send.email.post.smtp')
@@ -663,3 +662,10 @@ def delete_translations(self, zip_file_path):
         os.remove(zip_file_path)
     except:
         logger.exception('Error while deleting translations zip file')
+
+
+@celery.task(name='rename.chat.room')
+def rename_chat_room(event_id):
+    event = Event.query.get(event_id)
+    rename_rocketchat_room(event)
+    logging.info("Rocket chat room renamed successfully")


Test patch: