add Elements/StaticSelect, viewOpen now only takes a modal

This commit is contained in:
Deon George 2022-02-23 10:41:34 +11:00
parent 4ea6152194
commit a98debe69a
5 changed files with 117 additions and 16 deletions

View File

@ -316,7 +316,7 @@ final class API
{
Log::debug(sprintf('%s:Open a view',static::LOGKEY),['m'=>__METHOD__,'t'=>$trigger]);
return new Generic($this->execute('views.open',json_encode(['trigger_id'=>$trigger,'view'=>json_encode($view)])));
return new Generic($this->execute('views.open',json_encode(['trigger_id'=>$trigger,'view'=>$view])));
}
/**
@ -329,21 +329,21 @@ final class API
* @throws \Exception
* @todo Add some smarts to detect if the new view is the same as the current view, and thus no need to post.
*/
public function viewPublish(string $user,string $view,string $hash=''): Generic
public function viewPublish(string $user,Modal $view,string $hash=''): Generic
{
Log::debug(sprintf('%s:Publish a view',static::LOGKEY),['m'=>__METHOD__,'u'=>$user,'h'=>$hash]);
return new Generic($this->execute('views.publish',json_encode($hash ? ['user_id'=>$user,'view'=>$view,'hash'=>$hash] : ['user_id'=>$user,'view'=>$view])));
}
public function viewPush(string $trigger,string $view): Generic
public function viewPush(string $trigger,Modal $view): Generic
{
Log::debug(sprintf('%s:Push a view',static::LOGKEY),['m'=>__METHOD__,'t'=>$trigger]);
return new Generic($this->execute('views.push',json_encode(['trigger_id'=>$trigger,'view'=>$view])));
}
public function viewUpdate(string $view_id,string $view): Generic
public function viewUpdate(string $view_id,Modal $view): Generic
{
Log::debug(sprintf('%s:Update a view',static::LOGKEY),['m'=>__METHOD__,'id'=>$view_id]);

View File

@ -5,7 +5,7 @@ namespace Slack\Blockkit\Blocks;
use \Exception;
use Illuminate\Support\Collection;
use Slack\Blockkit\Blocks;
use Slack\Blockkit\Blocks\Elements\Text;
use Slack\Blockkit\Blocks\Elements\{Button,MultiStaticSelect,Text};
use Slack\Blockkit\Element;
final class Actions extends Blocks
@ -16,6 +16,12 @@ final class Actions extends Blocks
private const MAX_ELEMENTS = 5;
private const VALID_ELEMENTS = [
Button::class,
MultiStaticSelect::class,
Blocks\Accessories\Overflow::class,
];
/**
* @param Text $text
* @throws Exception
@ -33,6 +39,14 @@ final class Actions extends Blocks
return new self();
}
public function jsonSerialize()
{
if (! $this->elements)
throw new Exception('Must define at least 1 element');
return parent::jsonSerialize();
}
/* OPTIONAL ITEMS */
public function block_id(string $string): self
@ -47,6 +61,8 @@ final class Actions extends Blocks
if (count($collection) > self::MAX_ELEMENTS)
throw new Exception(sprintf('Can only have maximum %d elements',self::MAX_ELEMENTS));
// @todo Check that a valid element is added. https://api.slack.com/reference/block-kit/blocks#actions
$this->elements = $collection;
return $this;

View File

@ -15,6 +15,8 @@ final class MultiStaticSelect extends Element
private const MAX_OPTIONS = 100;
// @todo option_group? (double check it is applicable to this item)
/**
* @param Text $placeholder
* @param string $action_id
@ -43,7 +45,7 @@ final class MultiStaticSelect extends Element
throw new Exception(sprintf('Can only have maximum %d options',self::MAX_OPTIONS));
$this->options = $options->transform(function($item) {
return ['text'=>Text::item($item->name,'plain_text'),'value'=>(string)$item->id];
return ['text'=>Text::item($item->name,'plain_text'),'value'=>(string)$item->value];
});
}
@ -68,18 +70,18 @@ final class MultiStaticSelect extends Element
return $this;
}
public function initial_options(Collection $collection): self
public function initial_options(array $initial=NULL): self
{
// No initial options.
if (! count($collection))
return $this;
if (count($initial)) {
if (! $this->options)
throw new Exception('Cannot set an initial value without options defined first');
if (count($collection) > self::MAX_OPTIONS)
if (count($initial) > self::MAX_OPTIONS)
throw new Exception(sprintf('Can only have maximum %d options',self::MAX_OPTIONS));
$this->initial_options = $collection->transform(function($item) {
return ['text'=>Text::item($item->name,'plain_text'),'value'=>(string)$item->id];
});
$this->initial_options = $this->options->filter(function($item) use ($initial) { return in_array($item['value'],$initial); });
}
return $this;
}

View File

@ -0,0 +1,83 @@
<?php
namespace Slack\Blockkit\Blocks\Elements;
use \Exception;
use Illuminate\Support\Collection;
use Slack\Blockkit\Element;
final class StaticSelect extends Element
{
protected const LIMITS = [
'action_id' => 255,
'placeholder' => 150,
];
private const MAX_OPTIONS = 100;
// @todo option_group
/**
* @param Text $placeholder
* @param string $action_id
* @param Collection $options
* @throws Exception
* @todo We dont handle option_groups yet.
*/
public function __construct(Text $placeholder,string $action_id,Collection $options)
{
parent::__construct();
// Defaults
$this->type = 'static_select';
if ($placeholder->type != 'plain_text')
throw new Exception(sprintf('Text must be plain_text not %s',$placeholder->type));
if (strlen($placeholder->text) > self::LIMITS['placeholder'])
throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['placeholder']));
$this->placeholder = $placeholder;
$this->action_id = $this->validate('action_id',$action_id);
if (count($options) > self::MAX_OPTIONS)
throw new Exception(sprintf('Can only have maximum %d options',self::MAX_OPTIONS));
$this->options = $options->transform(function($item) {
return ['text'=>Text::item($item->name,'plain_text'),'value'=>(string)$item->value];
});
}
public static function item(Text $placeholder,string $action_id,Collection $options): self
{
return new self($placeholder,$action_id,$options);
}
/* OPTIONAL ITEMS */
public function confirm(Confirm $confirm): self
{
$this->confirm = $confirm;
return $this;
}
public function focus_on_load(bool $bool): self
{
$this->focus_on_load = $bool;
return $this;
}
public function initial_option(string $string=NULL): self
{
if (! $this->options)
throw new Exception('Cannot set an initial value without options defined first');
if ($string)
$this->initial_option = $this->options->first(function($item) use ($string) { return $item['value'] == $string; });
return $this;
}
}

View File

@ -56,9 +56,9 @@ class Team extends Model
*/
public function getAppTokenObfuscateAttribute(): string
{
$attrs = explode('-',$this->getAppTokenAttribute()->token);
$attrs = explode('-',$this->token->token);
$items = count($attrs)-1;
$attrs[$items] = '...'.substr($attrs[$items],-5);
$attrs[$items] = '...'.substr($attrs[$items],-3);
return implode('-',$attrs);
}