Skip to content
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

Fix Slack file upload and unfurling issues (resolves #121672) #132451

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from

Conversation

jsuar
Copy link

@jsuar jsuar commented Dec 6, 2024

Proposed change

This PR addresses the file upload and unfurling issues in the Home Assistant Slack integration, as described in #121672. Specifically, it:

  • Switches from using WebClient methods directly to utilizing AsyncWebClient where asynchronous await operations are required. This prevents TypeError: Incompatible types in "await" errors and ensures proper asynchronous handling of Slack API requests.
  • Improves the mocking and testing of Slack API calls (e.g., auth.test, conversations_list) in the integration’s tests. This resolves the AssertionError: No mock registered for POST https://slack.com/api/auth.test errors and ensures the test suite runs cleanly.
  • Updates the file uploading process, including resizing images to meet Slack’s minimum dimension requirements (800x400), ensuring that remote file shares and image unfurls succeed as intended. Details in the files.remote.add documentation.
  • Improves error handling and logging, making it easier to diagnose issues with Slack file uploads and unfurling in the future.

In April 2024, Slack announced adjustments to their file upload methods, making it clear that a newer, more stable approach to uploading and managing files is here to stay. This clarification deprecates older workflows and reinforces that developers should rely on updated, officially supported methods (like files.remote.* and files.upload.v2) for future compatibility and consistent behavior. By aligning the Home Assistant Slack integration with these updated methods, we ensure the integration remains robust and compatible with Slack’s evolving platform standards.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

Copy link

@home-assistant home-assistant bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jsuar

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant
Copy link

home-assistant bot commented Dec 6, 2024

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft December 6, 2024 03:59
@home-assistant
Copy link

home-assistant bot commented Dec 6, 2024

Hey there @tkdrob, @FletcherAU, mind taking a look at this pull request as it has been labeled with an integration (slack) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of slack can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign slack Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@jsuar jsuar marked this pull request as ready for review December 6, 2024 18:09
@FletcherAU
Copy link
Contributor

It might be worth adding a note to the docs regarding the preview image resizing? It's not necessarily intuitive unless you're familiar with Slack's preview requirements.

@FletcherAU
Copy link
Contributor

(That's definitely a "has this been considered" more than a "you should do this" by the way)

Copy link
Contributor

@FletcherAU FletcherAU left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not every file upload has a generatable preview image. (The preview image is also optional within the Slack API)

Comment on lines 254 to 280
# Step 3: Resize the image to meet the minimum dimension requirements
# https://api.slack.com/methods/files.remote.add#markdown
try:
data = await resp.read() # Read the bytes from the StreamReader
# Open the image and convert to RGB if necessary
image = Image.open(BytesIO(data))
if image.mode in ("RGBA", "P"): # Convert if image has alpha channel
image = image.convert("RGB")

# Get current dimensions
width, height = image.size

# Determine the scaling factor to meet minimum dimensions
scale_w = 800 / width if width < 800 else 1
scale_h = 400 / height if height < 400 else 1
scale = max(scale_w, scale_h) # Scale up to meet both requirements

# Resize if necessary
if scale > 1:
new_width = int(width * scale)
new_height = int(height * scale)
image = image.resize((new_width, new_height), Image.LANCZOS)
_LOGGER.info("Image resized to %sx%s", new_width, new_height)
else:
_LOGGER.info(
"Image meets the minimum dimension requirements; no resizing needed"
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it'll fail when uploading files that aren't images.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. Thanks for the catch. I updated the code, but I need to add some test coverage for that function and the change. Hoping to get the changes pushed up in the next day or so.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed up some changes to account for non-image files. Also added some more test coverage.

@jsuar
Copy link
Author

jsuar commented Dec 30, 2024

It might be worth adding a note to the docs regarding the preview image resizing? It's not necessarily intuitive unless you're familiar with Slack's preview requirements.
(That's definitely a "has this been considered" more than a "you should do this" by the way)

This is a good call out. I will review the docs and suggest possible updates

@jsuar
Copy link
Author

jsuar commented Jan 3, 2025

@FletcherAU I opened a minor documentation PR (home-assistant/home-assistant.io#36694) as well so I think this PR is ready for another review. Thank you! 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Method deprecated for file-based message: SlackApi
2 participants