php-plug-web3/src/Lemmy/Delete.php

76 lines
1.7 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Core\Plug\Web3\Lemmy;
use Core\Plug\Concern\BuildsResponse;
use Core\Plug\Concern\ManagesTokens;
use Core\Plug\Concern\UsesHttp;
use Core\Plug\Contract\Deletable;
use Core\Plug\Response;
/**
* Lemmy post deletion.
*/
class Delete implements Deletable
{
use BuildsResponse;
use ManagesTokens;
use UsesHttp;
private string $instanceUrl = '';
/**
* Set the instance URL.
*/
public function forInstance(string $instanceUrl): self
{
$this->instanceUrl = rtrim($instanceUrl, '/');
return $this;
}
/**
* Delete a post.
*/
public function delete(string $id): Response
{
if (! $this->instanceUrl) {
return $this->error('Instance URL is required');
}
$response = $this->http()
->withHeaders(['Authorization' => 'Bearer '.$this->accessToken()])
->post($this->instanceUrl.'/api/v3/post/delete', [
'post_id' => (int) $id,
'deleted' => true,
]);
return $this->fromHttp($response, fn () => [
'deleted' => true,
]);
}
/**
* Delete a comment.
*/
public function deleteComment(int $commentId): Response
{
if (! $this->instanceUrl) {
return $this->error('Instance URL is required');
}
$response = $this->http()
->withHeaders(['Authorization' => 'Bearer '.$this->accessToken()])
->post($this->instanceUrl.'/api/v3/comment/delete', [
'comment_id' => $commentId,
'deleted' => true,
]);
return $this->fromHttp($response, fn () => [
'deleted' => true,
]);
}
}