-
Notifications
You must be signed in to change notification settings - Fork 835
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 #1450 slack_file in image block/element #1452
Changes from all commits
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 |
---|---|---|
|
@@ -559,3 +559,32 @@ def to_dict(self) -> Dict[str, Any]: # skipcq: PYL-W0221 | |
else: | ||
json["trigger"] = self._trigger | ||
return json | ||
|
||
|
||
class SlackFile(JsonObject): | ||
attributes = {"id", "url"} | ||
|
||
def __init__( | ||
self, | ||
*, | ||
id: Optional[str] = None, | ||
url: Optional[str] = None, | ||
filmaj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
): | ||
"""An object containing Slack file information to be used in an image block or image element. | ||
https://api.slack.com/reference/block-kit/composition-objects#slack_file | ||
|
||
Args: | ||
id: Slack ID of the file. | ||
url: This URL can be the url_private or the permalink of the Slack file. | ||
""" | ||
self._id = id | ||
seratch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self._url = url | ||
|
||
def to_dict(self) -> Dict[str, Any]: # skipcq: PYL-W0221 | ||
self.validate_json() | ||
json = {} | ||
if self._id is not None: | ||
json["id"] = self._id | ||
if self._url is not None: | ||
json["url"] = self._url | ||
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 a user sets both, then both properties would be set. According to the docs on this object, this would result in the payload being rejected:
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. that's true but runtime error with server side error should be okay. i would like to avoid having duplicated logic on the client side for this (we already have a lot in this sdk but maintaining them is a concern I've been thinking about) |
||
return json |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
JsonValidator, | ||
EnumValidator, | ||
) | ||
from .basic_components import ButtonStyles, Workflow | ||
from .basic_components import ButtonStyles, Workflow, SlackFile | ||
from .basic_components import ConfirmObject | ||
from .basic_components import DispatchActionConfig | ||
from .basic_components import MarkdownTextObject | ||
|
@@ -551,13 +551,14 @@ class ImageElement(BlockElement): | |
|
||
@property | ||
def attributes(self) -> Set[str]: | ||
return super().attributes.union({"alt_text", "image_url"}) | ||
return super().attributes.union({"alt_text", "image_url", "slack_file"}) | ||
|
||
def __init__( | ||
self, | ||
*, | ||
image_url: Optional[str] = None, | ||
alt_text: Optional[str] = None, | ||
image_url: Optional[str] = None, | ||
slack_file: Optional[Union[Dict[str, Any], SlackFile]] = None, | ||
**others: dict, | ||
): | ||
"""An element to insert an image - this element can be used in section and | ||
|
@@ -566,18 +567,20 @@ def __init__( | |
https://api.slack.com/reference/block-kit/block-elements#image | ||
|
||
Args: | ||
image_url (required): The URL of the image to be displayed. | ||
alt_text (required): A plain-text summary of the image. This should not contain any markup. | ||
image_url: The URL of the image to be displayed. | ||
slack_file: A Slack image file object that defines the source of the image. | ||
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. Should we state that either 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. yeah, but in this sdk we don't mention such in comments |
||
""" | ||
super().__init__(type=self.type) | ||
show_unknown_key_warning(self, others) | ||
|
||
self.image_url = image_url | ||
self.alt_text = alt_text | ||
self.slack_file = slack_file if slack_file is None or isinstance(slack_file, SlackFile) else SlackFile(**slack_file) | ||
|
||
@JsonValidator(f"image_url attribute cannot exceed {image_url_max_length} characters") | ||
def _validate_image_url_length(self) -> bool: | ||
return len(self.image_url) <= self.image_url_max_length | ||
return self.image_url is None or len(self.image_url) <= self.image_url_max_length | ||
|
||
@JsonValidator(f"alt_text attribute cannot exceed {alt_text_max_length} characters") | ||
def _validate_alt_text_length(self) -> bool: | ||
|
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.
Maybe add a docstring linking to https://api.slack.com/reference/block-kit/composition-objects#slack_file here?
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.
I've added the comment for this class!