Skip to content

Commit e4488c0

Browse files
committed
Detect if slack redirected us when bridging a file, async version.
This likely indicates some failure, such as authentication. We do not retry, but that would be a nice addition as long as we don't block other handling.
1 parent 67e428e commit e4488c0

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

slack_reformat.py

+26-21
Original file line numberDiff line numberDiff line change
@@ -191,29 +191,34 @@ async def format_files_from_slack(files, needs_leading_newline,
191191
r = await aiohttp_session.get(file_private_url,
192192
headers={"Authorization": f"Bearer {slack_bearer_token}"})
193193
if r.status == 200:
194-
uploadable_file = BytesIO(await r.content.read())
195-
uploadable_file.name = file['name']
196-
197-
file_dict = {'file': uploadable_file}
198-
199-
# Because we want to use async io for this potentially long running request,
200-
# we can't use the zulip client library. Instead, REST/OpenAPI it is.
201-
upload_response = await aiohttp_session.post(
202-
f'{zulip_url}/api/v1/user_uploads',
203-
data=file_dict,
204-
auth=aiohttp_zulip_basic_auth)
205-
206-
response = {}
207-
if upload_response.status == 200:
208-
response = await upload_response.json()
209-
else:
194+
if str(r.url) != file_private_url:
195+
# we were redirected!
210196
_LOGGER.info(
211-
f"Upload to Zulip Failed for {file['name']}. Code {upload_response.status}.")
212-
213-
if upload_response.status == 200 and 'uri' in response and response['uri']:
214-
rendered_markdown_name = f"[{file['name']}]({response['uri']})"
197+
f'Apparent slack redirect from {file_private_url} to {str(r.url)} when bridging file. Skipping.')
215198
else:
216-
_LOGGER.info(f"Got bad response uploading to zulip for {file['name']}.. Body: {await upload_response.text()}")
199+
uploadable_file = BytesIO(await r.content.read())
200+
uploadable_file.name = file['name']
201+
202+
file_dict = {'file': uploadable_file}
203+
204+
# Because we want to use async io for this potentially long running request,
205+
# we can't use the zulip client library. Instead, REST/OpenAPI it is.
206+
upload_response = await aiohttp_session.post(
207+
f'{zulip_url}/api/v1/user_uploads',
208+
data=file_dict,
209+
auth=aiohttp_zulip_basic_auth)
210+
211+
response = {}
212+
if upload_response.status == 200:
213+
response = await upload_response.json()
214+
else:
215+
_LOGGER.info(
216+
f"Upload to Zulip Failed for {file['name']}. Code {upload_response.status}.")
217+
218+
if upload_response.status == 200 and 'uri' in response and response['uri']:
219+
rendered_markdown_name = f"[{file['name']}]({response['uri']})"
220+
else:
221+
_LOGGER.info(f"Got bad response uploading to zulip for {file['name']}.. Body: {await upload_response.text()}")
217222
else:
218223
_LOGGER.info(f"Got code {r.status_code} when fetching {file_private_url} from slack.")
219224

0 commit comments

Comments
 (0)