-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gcal Integration #378
base: master
Are you sure you want to change the base?
Gcal Integration #378
Changes from 1 commit
137f73e
0ab0f57
35f82bd
e1b9ada
0168e78
ccbdd7e
9bea3e1
581c692
89d636d
858324f
f242563
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ | |
from django.conf import settings | ||
from pytz import timezone, utc | ||
from sentry_sdk import capture_exception | ||
from slack.errors import SlackApiError | ||
|
||
from common.utils import get_slack_client | ||
from integrations.google import build_credentials, get_authorization_url, GoogleCalendar | ||
|
@@ -67,37 +66,57 @@ def post_organizer_edit_after_share_blocks(penny_chat_view_id): | |
) | ||
|
||
|
||
@background | ||
def add_google_meet(penny_chat_id): | ||
slack_client = get_slack_client() | ||
penny_chat_invitation = PennyChatSlackInvitation.objects.get(id=penny_chat_id) | ||
user = None | ||
def get_user_google_calendar_from_slack_id(slack_id): | ||
user = get_or_create_social_profile_from_slack_id(slack_id).user | ||
try: | ||
user = get_or_create_social_profile_from_slack_id(penny_chat_invitation.organizer_slack_id).user | ||
google_credentials = GoogleCredentials.objects.get(user=user) | ||
except GoogleCredentials.DoesNotExist: | ||
authorization_url = get_authorization_url(user) | ||
slack_client = get_slack_client() | ||
slack_client.chat_postMessage( | ||
channel=penny_chat_invitation.organizer_slack_id, | ||
channel=slack_id, | ||
blocks=add_google_integration_blocks(authorization_url, from_penny_chat=True), | ||
) | ||
return | ||
except SlackApiError: | ||
return | ||
|
||
credentials = build_credentials(google_credentials) | ||
calendar = GoogleCalendar(credentials=credentials) | ||
return GoogleCalendar(credentials=credentials) | ||
|
||
|
||
@background | ||
def add_google_meet(penny_chat_id): | ||
penny_chat_invitation = PennyChatSlackInvitation.objects.get(id=penny_chat_id) | ||
|
||
calendar = get_user_google_calendar_from_slack_id(penny_chat_invitation.organizer_slack_id) | ||
|
||
if calendar is None: | ||
return | ||
|
||
meet = calendar.create_event( | ||
summary=penny_chat_invitation.title, | ||
description=penny_chat_invitation.description, | ||
start=penny_chat_invitation.date | ||
) | ||
|
||
penny_chat_invitation.video_conference_link = meet['hangoutLink'] | ||
penny_chat_invitation.video_conference_link = meet.get('hangoutLink') | ||
penny_chat_invitation.google_event_id = meet.get('id') | ||
penny_chat_invitation.save() | ||
|
||
|
||
@background | ||
def update_google_meet(penny_chat_id): | ||
penny_chat_invitation = PennyChatSlackInvitation.objects.get(id=penny_chat_id) | ||
|
||
calendar = get_user_google_calendar_from_slack_id(penny_chat_invitation.organizer_slack_id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the user retracts google permissions then calendar will be None here, add an |
||
|
||
calendar.update_event( | ||
event_id=penny_chat_invitation.google_event_id, | ||
summary=penny_chat_invitation.title, | ||
description=penny_chat_invitation.description, | ||
start=penny_chat_invitation.date | ||
) | ||
|
||
|
||
@background | ||
def share_penny_chat_invitation(penny_chat_id): | ||
"""Shares penny chat invitations with people and channels in the invitee list.""" | ||
|
@@ -261,7 +280,7 @@ def _penny_chat_details_blocks(penny_chat_invitation, mode=None): | |
start_date = penny_chat_invitation.date.astimezone(utc).strftime('%Y%m%dT%H%M%SZ') | ||
end_date = (penny_chat_invitation.date.astimezone(utc) + timedelta(hours=1)).strftime('%Y%m%dT%H%M%SZ') | ||
|
||
description = f'{penny_chat_invitation.description} [Video Link]({penny_chat_invitation.video_conference_link})' | ||
description = f'{penny_chat_invitation.description}\nVideo Link: {penny_chat_invitation.video_conference_link}' | ||
google_cal_url = 'https://calendar.google.com/calendar/render?' \ | ||
'action=TEMPLATE&text=' \ | ||
f'{urllib.parse.quote(penny_chat_invitation.title)}&dates=' \ | ||
|
@@ -306,27 +325,25 @@ def _penny_chat_details_blocks(penny_chat_invitation, mode=None): | |
] | ||
|
||
if penny_chat_invitation.video_conference_link: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably a nit pick, but rather than have 3 sections here, I'd just have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So just remove the Video Link header from the invite and add parentheses? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup, just like you did - looks good |
||
body.append( | ||
{ | ||
'type': 'section', | ||
'text': { | ||
'type': 'mrkdwn', | ||
'text': '*Video Call Link*' | ||
} | ||
} | ||
) | ||
if mode in {PREVIEW, INVITE, UPDATE}: | ||
body.append( | ||
{ | ||
'type': 'section', | ||
'text': { | ||
'type': 'mrkdwn', | ||
'text': 'A video link will be provided shortly before the chat starts' | ||
'text': '_(A video link will be provided shortly before the chat starts)_' | ||
} | ||
} | ||
) | ||
elif mode in {REMIND}: | ||
body.append( | ||
body += [ | ||
{ | ||
'type': 'section', | ||
'text': { | ||
'type': 'mrkdwn', | ||
'text': '*Video Call Link*' | ||
} | ||
}, | ||
{ | ||
'type': 'actions', | ||
'elements': [ | ||
|
@@ -342,7 +359,7 @@ def _penny_chat_details_blocks(penny_chat_invitation, mode=None): | |
} | ||
] | ||
} | ||
) | ||
] | ||
|
||
if include_rsvp: | ||
body.append( | ||
|
@@ -533,7 +550,7 @@ def missing_google_auth_blocks(): | |
|
||
|
||
def add_google_integration_blocks(authorization_url, from_penny_chat=False): | ||
pre_add_button_blocks = missing_google_auth_blocks() if from_penny_chat else None | ||
pre_add_button_blocks = missing_google_auth_blocks() if from_penny_chat else [] | ||
blocks = pre_add_button_blocks + [ | ||
{ | ||
'type': 'section', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
default_app_config = 'integrations.apps.IntegrationsConfig' |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,6 @@ | |
|
||
class IntegrationsConfig(AppConfig): | ||
name = 'integrations' | ||
|
||
def ready(self): | ||
import integrations.signals # noqa |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django.db.models.signals import post_save | ||
from django.dispatch import receiver | ||
from django.utils import timezone | ||
|
||
from bot.tasks import add_google_meet | ||
from integrations.models import GoogleCredentials | ||
from pennychat.models import PennyChatSlackInvitation | ||
|
||
|
||
@receiver(post_save, sender=GoogleCredentials) | ||
def add_google_meet_to_upcoming_chats(sender, **kwargs): | ||
credentials = kwargs.get('instance') | ||
if credentials: | ||
user = credentials.user | ||
slack_ids = [profile.slack_id for profile in user.social_profiles.all()] | ||
invites = PennyChatSlackInvitation.objects.filter(organizer_slack_id__in=slack_ids, date__gt=timezone.now()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would there ever be any invites with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. disregard ... hey this box wine is great! |
||
for invite in invites: | ||
add_google_meet(invite.id) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 2.2.13 on 2020-11-18 01:20 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('pennychat', '0004_pennychat_video_conference_link'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='pennychat', | ||
name='google_event_id', | ||
field=models.TextField(null=True), | ||
), | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, new line