From 667af3ab395b6600f77a3ec890c4788781df33f8 Mon Sep 17 00:00:00 2001 From: Andrew Redden Date: Wed, 11 Mar 2020 12:59:31 -0300 Subject: [PATCH 1/2] move from querying handles in profiles to handle__iexact to ensure case doesn't miss users - fix for the hackathon team updates, adding the creating profile into the chat now --- app/app/local.env | 3 ++- app/assets/v2/js/base.js | 12 +++++++++--- app/chat/tasks.py | 14 +++++++++----- app/dashboard/tasks.py | 2 +- app/dashboard/views.py | 17 ++++++++++++----- 5 files changed, 33 insertions(+), 15 deletions(-) diff --git a/app/app/local.env b/app/app/local.env index 484d2477a26..e24f9f59bd7 100644 --- a/app/app/local.env +++ b/app/app/local.env @@ -28,7 +28,8 @@ GITHUB_API_TOKEN= GITHUB_CLIENT_ID= GITHUB_CLIENT_SECRET= GITHUB_APP_NAME= - +CHAT_SERVER_URL=chat +CHAT_URL=localhost # Etherscan API to get ETH and token trasaction history from an account address ETHERSCAN_API_KEY= diff --git a/app/assets/v2/js/base.js b/app/assets/v2/js/base.js index 464b61adf22..b27bbb4259a 100644 --- a/app/assets/v2/js/base.js +++ b/app/assets/v2/js/base.js @@ -444,7 +444,8 @@ if (document.contxt.chat_access_token && document.contxt.chat_id) { // setup polling check for any updated data // scope our polling function so any potential js crashes won't affect it. (($) => { - setInterval(() => { + + const checkChatNotifications = () => { $.ajax({ beforeSend: function(request) { request.setRequestHeader('Authorization', `Bearer ${document.contxt.chat_access_token}`); @@ -455,7 +456,7 @@ if (document.contxt.chat_access_token && document.contxt.chat_id) { let notified = false; JSONUnread.forEach((team) => { - if ((team.msg_count || team.mention_count) && !notified) { + if ((team.msg_count > 0 || team.mention_count > 0) && !notified) { $('#chat-notification-dot').addClass('notification__dot_active'); notified = true; } @@ -466,7 +467,12 @@ if (document.contxt.chat_access_token && document.contxt.chat_id) { console.log(error); }) }); - }, 30000); + }; + + setInterval(() => { + checkChatNotifications(); + }, 15000); + checkChatNotifications(); })(jQuery); } // carousel/collabs/... inside menu diff --git a/app/chat/tasks.py b/app/chat/tasks.py index bb3bd28feeb..710cacdd7b0 100644 --- a/app/chat/tasks.py +++ b/app/chat/tasks.py @@ -96,8 +96,12 @@ def associate_chat_to_profile(profile): break except Exception as e: logger.error(str(e)) - profile_access_token = chat_driver.users.create_user_access_token(user_id=profile.chat_id, options={ - 'description': "Grants Gitcoin access to modify your account"}) + try: + profile_access_token = chat_driver.users.create_user_access_token(user_id=profile.chat_id, options={ + 'description': "Grants Gitcoin access to modify your account"}) + except Exception as e: + logger.info('Failed to create access token') + logger.error(str(e)) profile.gitcoin_chat_access_token = profile_access_token['token'] @@ -238,7 +242,7 @@ def hackathon_chat_sync(self, hackathon_id: str, profile_handle: str = None, ret else: profiles_to_connect.append(reg.registrant.chat_id) else: - profile = Profile.objects.get(handle=profile_handle) + profile = Profile.objects.get(handle__iexact=profile_handle) if profile.chat_id is '' or profile.chat_id is None: created, updated_profile = associate_chat_to_profile(profile) profiles_to_connect.append(updated_profile.chat_id) @@ -314,7 +318,7 @@ def create_user(self, options, params, profile_handle='', retry: bool = True): params=params ) if profile_handle: - profile = Profile.objects.get(handle=profile_handle) + profile = Profile.objects.get(handle__iexact=profile_handle) profile.chat_id = create_user_response['id'] profile_access_token = chat_driver.users.create_user_access_token(user_id=profile.chat_id, options={ @@ -354,7 +358,7 @@ def patch_chat_user(self, query_opts, update_opts, retry: bool = True) -> None: try: chat_user = chat_driver.users.get_user_by_username(query_opts['handle']) chat_id = chat_user['id'] - user_profile = Profile.objects.filter(handle=query_opts['handle']) + user_profile = Profile.objects.filter(handle__iexact=query_opts['handle']) user_profile.chat_id = chat_id user_profile.save() except Exception as e: diff --git a/app/dashboard/tasks.py b/app/dashboard/tasks.py index 1494083aec8..ace280db083 100644 --- a/app/dashboard/tasks.py +++ b/app/dashboard/tasks.py @@ -57,7 +57,7 @@ def bounty_emails(self, emails, msg, profile_handle, invite_url=None, kudos_invi """ with redis.lock("tasks:bounty_email:%s" % invite_url, timeout=LOCK_TIMEOUT): # need to look at how to send bulk emails with SG - profile = Profile.objects.get(handle=profile_handle) + profile = Profile.objects.get(handle__iexact=profile_handle) try: for email in emails: to_email = email diff --git a/app/dashboard/views.py b/app/dashboard/views.py index 93603a7e462..aebe11725a6 100644 --- a/app/dashboard/views.py +++ b/app/dashboard/views.py @@ -3944,6 +3944,14 @@ def hackathon_save_project(request): 'work_url': clean(request.POST.get('work_url'), strip=True) } + try: + + if profile.chat_id is '' or profile.chat_id is None: + created, profile = associate_chat_to_profile(profile) + except Exception as e: + logger.info("Bounty Profile owner not apart of gitcoin") + profiles_to_connect = [profile.chat_id] + if project_id: try: @@ -3954,9 +3962,9 @@ def hackathon_save_project(request): }) project.update(**kwargs) - profiles_to_connect = [] + try: - bounty_profile = Profile.objects.get(handle=project.bounty.bounty_owner_github_username) + bounty_profile = Profile.objects.get(handle__iexact=project.bounty.bounty_owner_github_username) if bounty_profile.chat_id is '' or bounty_profile.chat_id is None: created, bounty_profile = associate_chat_to_profile(bounty_profile) @@ -3970,7 +3978,7 @@ def hackathon_save_project(request): created, curr_profile = associate_chat_to_profile(curr_profile) profiles_to_connect.append(curr_profile.chat_id) - add_to_channel.delay(project.chat_channel_id, profiles_to_connect) + add_to_channel.delay(project.first().chat_channel_id, profiles_to_connect) profiles.append(str(profile.id)) project.first().profiles.set(profiles) @@ -3993,9 +4001,8 @@ def hackathon_save_project(request): 'channel_name': project_channel_name[:60], 'channel_type': 'P' }) - profiles_to_connect = [] try: - bounty_profile = Profile.objects.get(handle=bounty_obj.bounty_owner_github_username) + bounty_profile = Profile.objects.get(handle__iexact=bounty_obj.bounty_owner_github_username) if bounty_profile.chat_id is '' or bounty_profile.chat_id is None: created, bounty_profile = associate_chat_to_profile(bounty_profile) From 05a376ca7b2df475c23efb3c1620bb59874dda5e Mon Sep 17 00:00:00 2001 From: Andrew Redden Date: Wed, 11 Mar 2020 13:01:36 -0300 Subject: [PATCH 2/2] move the profile update for chat components into the sync_profile so that its more in line of when it has to happen --- app/app/utils.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/app/app/utils.py b/app/app/utils.py index 1ae7d2d2d00..3719fe31334 100644 --- a/app/app/utils.py +++ b/app/app/utils.py @@ -200,6 +200,17 @@ def sync_profile(handle, user=None, hide_profile=True): profile.handle = data['login'] profile.email = user.email profile.save() + + if profile is not None and (profile.chat_id is '' or profile.gitcoin_chat_access_token is ''): + + try: + from chat.tasks import associate_chat_to_profile + created, profile = associate_chat_to_profile(profile) + + except Exception as e: + logger.error(str(e)) + raise ValueError(e) + except UserSocialAuth.DoesNotExist: pass else: @@ -458,15 +469,6 @@ def get_profile(request): if is_authed and not profile: profile = sync_profile(request.user.username, request.user, hide_profile=False) - if profile is not None and (profile.chat_id is '' or profile.gitcoin_chat_access_token is ''): - - try: - from chat.tasks import associate_chat_to_profile - created, profile = associate_chat_to_profile(profile) - - except Exception as e: - logger.error(str(e)) - raise ValueError(e) return profile