-
Notifications
You must be signed in to change notification settings - Fork 34
[Spike] Automatically handle and enforce BlockKit limits #198
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
Conversation
dc5019a to
cb88bc8
Compare
| stop ||= length_with_room_for_omission | ||
|
|
||
| "#{text[0, stop]}#{omission}" | ||
| end |
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.
Note: I used Rails' implementation of truncate for a couple reasons
- Certain things should maybe truncate differently (e.g.
block_idandaction_iddon't need to truncate with an ellipsis at the end, though that's supposedly still valid), so theomissionandseparatoroptions give us control - I could see us adding configuration options on top of
autofix_invalid_blocksthat allow users to specify how they'd like text to truncate (e.g.Slack::BlockKit.config.truncate_invalid_text_with = "…"or something)
|
I started working on a limiter for blocks that have a class Slack::BlockKit::Layout::Section
prepend Limiters::Text.new(length: 3000)
end
class Slack::BlockKit::Layout::Header
prepend Limiters::Text.new(length: 150)
endWhich works, but for the section block, it only really solves one aspect of their requirement, which is that the text can't be super long. It doesn't handle the fact that it has to be present (unless I've started to feel like ActiveModel validations would make these requirements much easier to codify, and that something like ActiveSupport's Definitely curious if you've given BlockKit limit/validation handling any more thought over the last year 😅 And I really share your wish that Slack would just republish their JSON Schema. |
|
Spent some time today starting to go through what the actual limits/validations are that Slack has documented so that I could get a better overall view of what any kind of client-side validation and related data-munging might entail. Most of the validations are still relatively simple, I wonder whether it is worth implementing some json-schema definition for this and seeing how that might hook into this library nicely. If we can avoid I guess the key requirement there is as you mentioned having some kind of Limits/validations defined by Slack related to BlockKitSurfacesMessage
Modal
Home
LayoutsActions
Context
Divider
Header
Image
Input
Markdown
RichText
Section
Video
CompositionConfirmation Dialog
Conversation Filter
Dispatch Action Configuration
Option
Option Group
Text
ElementsButton
Checkboxes
Date picker
Datetime picker
Email picker
File input
Image
Multi channels selectMulti conversations selectMulti external selectMulti static selectMulti user selectNumber inputOverflow menuPlain-text inputRadio button groupRich text inputChannel selectConversation selectUser selectExternal selectStatic selectTime pickerURL inputWorkflow button |
Yeah, that's totally reasonable! Once I got that idea in my head, I decided I really wanted to try pursuing that so ultimately I ended up building out my own library to do this 😅 I'll be focusing on that instead of this spike, but I'll leave this here for you to close out (or build off of if you're still interested in solving it and would like to use it)! |
|
Nice! https://github.com/davidcelis/block-kit looks great |
|
(Actually, I'll close it so it doesn't appear in my active list anymore 😅 you're still free to use it however you like, of course!) |
This pull request is my attempt to spike out a solution for #176, providing a way for handling BlockKit's various limits around text length, number of blocks/elements, etc. I know that one other pull request was opened with a potential solution for this (#180) but that PR has stagnated and I wanted to provide an alternative (and hopefully simpler) approach anyway.
Similarly to 180, my approach involves prepending blocks with relevant
Limiterclasses, starting with a limiter that truncates providedblock_ids to Slack's required length (255 characters). This behavior is off by default, though I think it would be a good idea to turn it on by default in a major release so that there's ample warning for existing users. I initially had this controlled by passing an option (e.g.autofix: true) to blocks that should support this, with the idea that you would likely do something likeSlack::BlockKit.blocks(autofix: true) { ... }and have it propagate down through the builder, but I worried that supporting this keyword argument and passing it down long chains of blocks would be cumbersome, so I thought a global configuration option would probably be a simpler start. I'm not sure how likely it is that users would want to selectively enable or disable autofixing anyway, but we could always introduce that in the future and use the config as the default.Either way, the result of this would be configuring the gem during some initialization process:
Once enabled, any block with a
block_id(currently just layout blocks) would automatically truncate thatblock_idif necessary. This truncation occurs at the time of rendering, so it doesn't modify the object in-place; only the resulting JSON (this felt the least surprising to me).Also, I'm open to any feedback or naming changes you have, of course! For instance, even though I started with
block_id, it andaction_idstrike me as limits that users might want to configure differently, since those identifiers matter when handling interactivity. Users might not want us to truncate those because they'd have to be careful to keep our truncated IDs as their reference and not the ID they passed in originally