Enhancements to Message::class making it more similar to Modal::class in terms of methods

This commit is contained in:
Deon George 2022-09-09 14:55:19 +10:00
parent 6c6d32b4eb
commit 67f78b2f5d
2 changed files with 50 additions and 8 deletions

View File

@ -11,7 +11,7 @@ use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Slack\Blockkit\Blocks;
use Slack\Blockkit\Blocks\Context;
use Slack\Blockkit\Blocks\{Context,Divider,Section};
use Slack\Blockkit\Blocks\Elements\Text;
use Slack\Exceptions\{SlackException,SlackSyntaxException};
use Slack\Jobs\{DeleteChat,DeleteResponse};
@ -124,6 +124,14 @@ final class Message extends BlockKit
return $this;
}
// This is a helper method
public function divider(): self
{
$this->blocks->push(Divider::item());
return $this;
}
public function forgetTS(): self
{
$this->_data->forget('ts');
@ -162,9 +170,6 @@ final class Message extends BlockKit
if (! $delete && $this->selfdestruct)
$delete = $this->selfdestruct;
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) && (! $this->o->user_team)))
throw new SlackSyntaxException('Message needs to have a user or a channel to work out the team.');
@ -178,11 +183,19 @@ final class Message extends BlockKit
}
if ($delete) {
Log::debug(sprintf('%s:Scheduling Delete of [%s:%s] on [%s]',static::LOGKEY,object_get($this->o,'channel_id',$this->o->id),$response->ts,$delete->format('Y-m-d')),['m'=>__METHOD__]);
Log::debug(sprintf('%s:Scheduling Delete of [%s:%s] on [%s]',static::LOGKEY,object_get($this->o,'channel_id',$this->o->id),$response->ts,$delete->format('Y-m-d H:i')),['m'=>__METHOD__,'r'=>$response,'message_ts'=>$response->ts]);
if ($this->o instanceof User) {
Log::error(sprintf('%s:Cannot schedule delete of [%s:%s] on user channels [%s]',static::LOGKEY,object_get($this->o,'channel_id',$this->o->id),$response->ts,$this->o->user_id),['m'=>__METHOD__]);
} elseif ($this->ephemeral) {
Log::error(sprintf('%s:Ignoring delete of [%s:%s] on ephemeral messages',static::LOGKEY,object_get($this->o,'channel_id',$this->o->id),$response->ts),['m'=>__METHOD__]);
} else {
// Queue the delete of the message if requested
dispatch((new DeleteChat($this->o,$response->ts))->onQueue('slack')->delay($delete));
}
}
return $response;
}
@ -250,6 +263,7 @@ final class Message extends BlockKit
*
* @param Carbon $time
* @return Message
* @throws SlackSyntaxException
*/
public function selfdestruct(Carbon $time): self
{
@ -292,6 +306,14 @@ final class Message extends BlockKit
return $this;
}
// This is a helper method
public function spacer(): self
{
$this->blocks->push(Section::item(Text::item(' ')));
return $this;
}
/* CONFIGURATION METHODS */
/**
@ -396,6 +418,25 @@ final class Message extends BlockKit
return $this;
}
/**
* Post the message to user
*
* @param string $user
* @return $this
*/
public function user(string $user): self
{
$this->user = $user;
return $this;
}
/**
* Set bot username (used with as_user)
*
* @param string $user
* @return $this
*/
public function username(string $user): self
{
$this->username = $user;

View File

@ -55,8 +55,9 @@ abstract class Base extends SlackBase implements \JsonSerializable
case 'scheduled_messages': // Used by scheduledMessagesList()
return collect(object_get($this->_data,$key));
case 'team_id':
case 'ts':
return object_get($this->_data,$key) ?: object_get($this->_data,'message_ts');
case 'team_id':
case 'user_id':
case 'type': // Needed by URL verification
return object_get($this->_data,$key);