Enable selfdestructing messages

This commit is contained in:
Deon George 2021-08-13 12:51:57 +10:00
parent 4533b1d0ed
commit d0b7038604
No known key found for this signature in database
GPG Key ID: 7670E8DC27415254

View File

@ -22,6 +22,7 @@ class Message implements \JsonSerializable
private Model $o; private Model $o;
private Blocks $blocks; private Blocks $blocks;
private ?Carbon $selfdestruct = NULL;
/** /**
* Message constructor. * Message constructor.
@ -127,6 +128,9 @@ class Message implements \JsonSerializable
*/ */
public function post(Carbon $delete=NULL): Generic public function post(Carbon $delete=NULL): Generic
{ {
if (! $delete && $this->selfdestruct)
$delete = $this->selfdestruct;
if ($this->_data->has('ephemeral')) if ($this->_data->has('ephemeral'))
abort('500','Cannot post ephemeral messages.'); abort('500','Cannot post ephemeral messages.');
@ -212,22 +216,22 @@ class Message implements \JsonSerializable
* Make the message self destruct * Make the message self destruct
* *
* @param Carbon $time * @param Carbon $time
* @return Generic * @return Message
* @throws \Exception
*/ */
public function selfdestruct(Carbon $time): Generic public function selfdestruct(Carbon $time): self
{ {
$this->blocks->addContextElements( $this->blocks->addContextElements(collect([
collect() Blocks\Text::item(sprintf('This message will self destruct in %s...',$time->diffForHumans(Carbon::now(),['syntax' => CarbonInterface::DIFF_RELATIVE_TO_NOW])))
->push(Blocks\Text::item(sprintf('This message will self destruct in %s...',$time->diffForHumans(Carbon::now(),['syntax' => CarbonInterface::DIFF_RELATIVE_TO_NOW]))))); ]));
return $this->post($time); $this->selfdestruct = $time;
return $this;
} }
/** /**
* Add an attachment to a message * Add an attachment to a message
* *
* @param Attachment $attachments * @param Attachments $attachments
* @return Message * @return Message
*/ */
public function setAttachments(Attachments $attachments): self public function setAttachments(Attachments $attachments): self