|
7 | 7 | from django.core.mail import send_mail |
8 | 8 | from smtplib import SMTPException |
9 | 9 |
|
| 10 | +from accounts.models import CustomUser as User |
10 | 11 | from accounts.models import EmailTemplate, SlackSiteSettings |
11 | 12 |
|
12 | 13 | from celery import shared_task |
@@ -60,56 +61,76 @@ def create_new_hackathon_slack_channel(hackathon_id, channel_name): |
60 | 61 | f"{hackathon.display_name} in Slack Workspace " |
61 | 62 | f"{settings.SLACK_WORKSPACE}({settings.SLACK_TEAM_ID})")) |
62 | 63 |
|
| 64 | + # Use a workspace admin's User Token to create the channel |
| 65 | + # This is required because a bot is seen as a Slack member and |
| 66 | + # if a workspace is set to only allow admins to create channels |
| 67 | + # creating the channel with a Bot Token will give an error |
63 | 68 | admin_client = CustomSlackClient(settings.SLACK_ADMIN_TOKEN) |
64 | 69 | channel_response = admin_client.create_slack_channel( |
65 | 70 | team_id=settings.SLACK_TEAM_ID, |
66 | 71 | channel_name=channel_name, |
67 | 72 | is_private=True, |
68 | 73 | ) |
69 | 74 |
|
70 | | - if not channel_response['ok']: |
71 | | - logger.error(channel_response['error']) |
72 | | - |
73 | 75 | channel = channel_response.get('channel', {}).get('id') |
74 | 76 | channel_url = f'https://{settings.SLACK_WORKSPACE}.slack.com/archives/{channel}' |
75 | 77 | hackathon.channel_url = channel_url |
76 | 78 | hackathon.save() |
77 | 79 | logger.info(f"Channel with id {channel} created.") |
78 | | - |
| 80 | + |
79 | 81 | # Add admins to channel for administration purposes |
80 | | - users = [admin.username for admin in hackathon.channel_admins.all()] |
81 | | - # First need to add Slack Bot to then add users to channel |
82 | | - response = admin_client.invite_users_to_slack_channel( |
83 | | - users=settings.SLACK_BOT_ID, |
84 | | - channel=channel, |
85 | | - ) |
86 | | - if not response['ok']: |
87 | | - logger.error(response['error']) |
88 | | - return |
89 | | - |
90 | | - bot_client = CustomSlackClient(settings.SLACK_BOT_TOKEN) |
| 82 | + admin_usernames = [admin.username for admin in hackathon.channel_admins.all()] |
91 | 83 | pattern = re.compile(r'^U[a-zA-Z0-9]*[_]T[a-zA-Z0-9]*$') |
92 | | - users_to_invite = ','.join([user.split('_')[0] |
93 | | - for user in users if pattern.match(user)]) |
94 | | - bot_client.invite_users_to_slack_channel( |
95 | | - users=users_to_invite, |
| 84 | + admin_user_ids = ','.join([username.split('_')[0] |
| 85 | + for username in admin_usernames |
| 86 | + if pattern.match(username)]) |
| 87 | + admin_client.invite_users_to_slack_channel( |
| 88 | + users=admin_user_ids, |
96 | 89 | channel=channel, |
97 | 90 | ) |
98 | 91 |
|
99 | | - if not response['ok']: |
100 | | - logger.error(response['error']) |
101 | | - return |
102 | | - |
103 | | - if slack_site_settings.remove_admin_from_channel: |
104 | | -# remove_admin_from_channel(users_to_invite, channel) |
105 | | - pass |
106 | | - |
107 | 92 |
|
108 | 93 | @shared_task |
109 | 94 | def invite_user_to_hackathon_slack_channel(hackathon_id, user_id): |
110 | | - bot_client = CustomSlackClient(settings.SLACK_BOT_TOKEN) |
| 95 | + slack_site_settings = SlackSiteSettings.objects.first() |
| 96 | + if (not (settings.SLACK_ENABLED or settings.SLACK_BOT_TOKEN or settings.SLACK_ADMIN_TOKEN |
| 97 | + or settings.SLACK_WORKSPACE or not slack_site_settings)): |
| 98 | + logger.info("This feature is not enabeled.") |
| 99 | + return |
| 100 | + |
| 101 | + hackathon = Hackathon.objects.get(id=hackathon_id) |
| 102 | + user = User.objects.get(id=user_id) |
| 103 | + logger.info(f"Inviting user {user_id} to hackathon {hackathon_id}'s slack channel") |
| 104 | + |
| 105 | + admin_client = CustomSlackClient(settings.SLACK_ADMIN_TOKEN) |
| 106 | + channel = hackathon.channel_url.split('/')[-1] |
| 107 | + pattern = re.compile(r'^U[a-zA-Z0-9]*[_]T[a-zA-Z0-9]*$') |
| 108 | + slack_user_id = admin_client._extract_userid_from_username(user.username) |
| 109 | + admin_client.invite_users_to_slack_channel( |
| 110 | + users=[slack_user_id], |
| 111 | + channel=channel |
| 112 | + ) |
| 113 | + logger.info(f"Successfully invited user {user_id} to hackathon {hackathon_id}'s slack channel") |
111 | 114 |
|
112 | 115 |
|
113 | 116 | @shared_task |
114 | | -def kick_user_to_hackathon_slack_channel(user, channel): |
115 | | - pass |
| 117 | +def kick_user_from_hackathon_slack_channel(hackathon_id, user_id): |
| 118 | + slack_site_settings = SlackSiteSettings.objects.first() |
| 119 | + if (not (settings.SLACK_ENABLED or settings.SLACK_BOT_TOKEN or settings.SLACK_ADMIN_TOKEN |
| 120 | + or settings.SLACK_WORKSPACE or not slack_site_settings)): |
| 121 | + logger.info("This feature is not enabeled.") |
| 122 | + return |
| 123 | + |
| 124 | + hackathon = Hackathon.objects.get(id=hackathon_id) |
| 125 | + user = User.objects.get(id=user_id) |
| 126 | + logger.info(f"Kicking user {user_id} to hackathon {hackathon_id}'s slack channel") |
| 127 | + admin_client = CustomSlackClient(settings.SLACK_ADMIN_TOKEN) |
| 128 | + channel = hackathon.channel_url.split('/')[-1] |
| 129 | + pattern = re.compile(r'^U[a-zA-Z0-9]*[_]T[a-zA-Z0-9]*$') |
| 130 | + slack_user_id = admin_client._extract_userid_from_username(user.username) |
| 131 | + kicked = admin_client.kick_user_from_slack_channel( |
| 132 | + user=slack_user_id, |
| 133 | + channel=channel |
| 134 | + ) |
| 135 | + logger.info(f"Successfully kicked user {user_id} to hackathon {hackathon_id}'s slack channel") |
| 136 | + |
0 commit comments