From 145e322317a31d5ce163554d396af22b1a8077e0 Mon Sep 17 00:00:00 2001 From: Deon George Date: Tue, 6 Sep 2022 17:23:54 +1000 Subject: [PATCH] Fixes for interactive messages, we use block_id to determine what needs to be done --- src/Base.php | 9 ++++-- src/BlockKit.php | 4 +-- src/Blockkit/Blocks/Elements/Confirm.php | 41 +++++++++++++++++++++--- src/Interactive/BlockActions.php | 15 ++++++--- src/Message.php | 4 +-- src/Message/AttachmentAction.php | 2 +- src/Models/User.php | 2 -- 7 files changed, 58 insertions(+), 19 deletions(-) diff --git a/src/Base.php b/src/Base.php index 567237b..218daa5 100644 --- a/src/Base.php +++ b/src/Base.php @@ -65,15 +65,18 @@ abstract class Base } /** - * Separate out an callback command to the id that the command relates to + * Separate out a callback command to the id that the command relates to * * @param string $key - * @param string $item + * @param string|null $item * @return string|null * @throws \Exception */ - final protected function keyitem(string $key,string $item): ?string + final protected function keyitem(string $key,string $item=NULL): ?string { + if (! $item) + return $item; + $regex = '/^([a-z_]+)\|([0-9]+)$/'; $id = NULL; $value = NULL; diff --git a/src/BlockKit.php b/src/BlockKit.php index 1bb32cc..b91230f 100644 --- a/src/BlockKit.php +++ b/src/BlockKit.php @@ -11,9 +11,9 @@ use Slack\Exceptions\SlackSyntaxException; * Class BlockKit - Slack Blockit Objects * * @notes - * + callback_id is used to identify the source of any action (modal). (Message blocks do not have a callback_id, accept in attachments). + * + callback_id is used to identify the source of any action (modal). (Message blocks do not have a callback_id, accept in legacy attachments). * eg: hometab, add_product, ask_modal - * + block_id is used to identify the sub action(s) of any action (modal) + * + block_id is used to identify the sub action(s) of any action (modal). (Messages with blocks can have a block_id, we need to use this to determine what to do.) * eg: multiple blocks (list of something) * + action_id is used to identify the action that was initiated * eg: view, edit, review diff --git a/src/Blockkit/Blocks/Elements/Confirm.php b/src/Blockkit/Blocks/Elements/Confirm.php index 391e6dd..2972a1a 100644 --- a/src/Blockkit/Blocks/Elements/Confirm.php +++ b/src/Blockkit/Blocks/Elements/Confirm.php @@ -3,18 +3,51 @@ namespace Slack\Blockkit\Blocks\Elements; use Slack\Blockkit\Element; +use Slack\Exceptions\SlackSyntaxException; final class Confirm extends Element { - public function __construct() + protected const LIMITS = [ + 'title' => 100, + 'text' => 300, + 'confirm' => 30, + 'deny' => 30, + ]; + + public function __construct(Text $title,Text $text,Text $confirm,Text $deny) { parent::__construct(); - abort(500,'Not Implememted'); + if ($title->type != 'plain_text') + throw new SlackSyntaxException(sprintf('Title must be plain_text not %s',$title->type)); + + if ($confirm->type != 'plain_text') + throw new SlackSyntaxException(sprintf('Title must be plain_text not %s',$confirm->type)); + + if ($deny->type != 'plain_text') + throw new SlackSyntaxException(sprintf('Title must be plain_text not %s',$deny->type)); + + $this->title = $this->validate('title',$title->text) ? $title : NULL; + + $this->text = $this->validate('text',$text->text) ? $text : NULL; + + $this->confirm = $this->validate('confirm',$confirm->text) ? $confirm : NULL; + + $this->deny = $this->validate('deny',$deny->text) ? $deny : NULL; } - public static function item(): self + public static function item(Text $title,Text $text,Text $confirm,Text $deny): self { - return new self(); + return new self($title,$text,$confirm,$deny); + } + + public function style(string $string): self + { + if (! in_array($string,['default','primary','danger'])) + throw new SlackSyntaxException(sprintf('Unknown style %s',$string)); + + $this->style = $string; + + return $this; } } \ No newline at end of file diff --git a/src/Interactive/BlockActions.php b/src/Interactive/BlockActions.php index 01b1207..855aa36 100644 --- a/src/Interactive/BlockActions.php +++ b/src/Interactive/BlockActions.php @@ -64,20 +64,25 @@ final class BlockActions extends Base case 'action': return Arr::get(object_get($this->_data,'actions'),$this->index); - case 'action_id': - return object_get(Arr::get($this->actions,$this->index),$key); - // An event can have more than 1 action, each action can have 1 value. case 'action_key': - return $this->keyitem('id',object_get($this->action,'action_id')); + return $this->keyitem('id',$this->action_id); case 'action_value': - return $this->keyitem('value',object_get($this->action,'action_id')); + return $this->keyitem('value',$this->action_id); + + // Interactive Messages have a block_id. + case 'block_key': + return $this->keyitem('id',$this->block_id); + + case 'block_value': + return $this->keyitem('value',$this->block_id); case 'actions': case 'response_url': return object_get($this->_data,$key); + case 'action_id': case 'block_id': return object_get($this->action,$key); diff --git a/src/Message.php b/src/Message.php index 270f079..ad1a819 100644 --- a/src/Message.php +++ b/src/Message.php @@ -165,10 +165,10 @@ final class Message extends BlockKit if ($this->_data->get('blocks') && $this->_data->get('attachments')) throw new SlackSyntaxException('Message cannot have blocks and attachments.'); - if ((! isset($this->o)) || (! $this->o->team)) + if ((! isset($this->o)) || ((! $this->o->team) && (! $this->o->user_team))) throw new SlackSyntaxException('Message needs to have a user or a channel to work out the team.'); - $api = $this->o->team->slackAPI(); + $api = $this->o->team ? $this->o->team->slackAPI() : $this->o->user_team->slackAPI(); if ($this->ephemeral) { $response = $api->postEphemeral($this); diff --git a/src/Message/AttachmentAction.php b/src/Message/AttachmentAction.php index f2bab2b..be84387 100644 --- a/src/Message/AttachmentAction.php +++ b/src/Message/AttachmentAction.php @@ -8,7 +8,7 @@ use Slack\Exceptions\SlackSyntaxException; /** * Class MessageAttachmentAction - Slack Message Attachments Actions - * Represents an Single Action for a Slack Message Attachment + * Represents a Single Action for a Slack Message Attachment * * @package App\Slack\Message * @note These are now legacy - use blocks instead diff --git a/src/Models/User.php b/src/Models/User.php index 6210501..200267c 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -61,8 +61,6 @@ class User extends Model */ public function getUserTeamAttribute(): ?Team { - Log::debug(sprintf('%s:User [%s], Team [%s], Enterprise [%s]',self::LOGKEY,$this->id,$this->team_id,$this->enterprise_id),['m'=>__METHOD__]); - return $this->team_id ? $this->team : (($x=$this->enterprise->teams) ? $x->first() : NULL); }