2022-10-18 15:04:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Mailman\Support;
|
|
|
|
|
2022-10-20 11:11:52 +02:00
|
|
|
use App\Mailman\Data\MailingList;
|
2023-06-12 17:07:56 +02:00
|
|
|
use App\Mailman\Data\Member;
|
2022-10-18 16:44:36 +02:00
|
|
|
use App\Mailman\Exceptions\MailmanServiceException;
|
2022-10-20 02:15:28 +02:00
|
|
|
use Illuminate\Http\Client\ConnectionException;
|
2022-10-18 15:21:14 +02:00
|
|
|
use Illuminate\Http\Client\PendingRequest;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
2022-10-18 16:44:36 +02:00
|
|
|
use Illuminate\Support\LazyCollection;
|
2022-10-18 17:55:53 +02:00
|
|
|
use Zoomyboy\LaravelNami\Support\Paginator;
|
2022-10-18 15:21:14 +02:00
|
|
|
|
2022-10-18 15:04:47 +02:00
|
|
|
class MailmanService
|
|
|
|
{
|
2022-10-18 15:21:14 +02:00
|
|
|
private string $baseUrl;
|
|
|
|
private string $username;
|
|
|
|
private string $password;
|
2023-07-17 16:50:05 +02:00
|
|
|
private string $owner;
|
2022-10-18 15:21:14 +02:00
|
|
|
|
2022-10-18 15:04:47 +02:00
|
|
|
public function setCredentials(string $baseUrl, string $username, string $password): self
|
|
|
|
{
|
2022-10-18 15:21:14 +02:00
|
|
|
$this->baseUrl = $baseUrl;
|
|
|
|
$this->username = $username;
|
|
|
|
$this->password = $password;
|
|
|
|
|
|
|
|
return $this;
|
2022-10-18 15:04:47 +02:00
|
|
|
}
|
|
|
|
|
2023-07-17 16:50:05 +02:00
|
|
|
public function setOwner(string $owner): self
|
2022-10-20 11:34:22 +02:00
|
|
|
{
|
2023-07-17 16:50:05 +02:00
|
|
|
$this->owner = $owner;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createList(string $mailAddress): MailingList
|
|
|
|
{
|
|
|
|
$response = $this->http()->post('/lists', [
|
|
|
|
'fqdn_listname' => $mailAddress,
|
|
|
|
]);
|
|
|
|
throw_unless(201 === $response->status(), MailmanServiceException::class, 'Creating list failed');
|
|
|
|
$list = $this->getLists()->first(fn ($list) => $list->fqdnListname === $mailAddress);
|
|
|
|
$response = $this->http()->patch("/lists/{$list->listId}/config", [
|
|
|
|
'advertised' => 'False',
|
|
|
|
'respond_to_post_requests' => 'False',
|
|
|
|
'send_goodbye_message' => 'False',
|
|
|
|
'send_welcome_message' => 'False',
|
|
|
|
'admin_immed_notify' => 'True',
|
|
|
|
]);
|
|
|
|
throw_unless(204 === $response->status(), MailmanServiceException::class, 'Updating list config failed');
|
|
|
|
$response = $this->http()->post('/members', [
|
|
|
|
'list_id' => $list->listId,
|
|
|
|
'subscriber' => $this->owner,
|
|
|
|
'role' => 'owner',
|
|
|
|
]);
|
|
|
|
throw_unless(201 === $response->status(), MailmanServiceException::class, 'Creating list owner failed');
|
|
|
|
|
|
|
|
return $list;
|
2022-10-20 11:34:22 +02:00
|
|
|
}
|
|
|
|
|
2023-07-17 17:08:07 +02:00
|
|
|
public function deleteList(string $mailAddress): void
|
|
|
|
{
|
|
|
|
$list = $this->getLists()->first(fn ($list) => $list->fqdnListname === $mailAddress);
|
|
|
|
$response = $this->http()->delete("/lists/{$list->listId}");
|
|
|
|
throw_unless(204 === $response->status(), MailmanServiceException::class, 'Deleting list failed');
|
|
|
|
}
|
|
|
|
|
2022-10-18 15:04:47 +02:00
|
|
|
public function check(): bool
|
|
|
|
{
|
2022-10-20 02:15:28 +02:00
|
|
|
try {
|
|
|
|
$response = $this->http()->get('/system/versions');
|
2022-10-18 15:21:14 +02:00
|
|
|
|
2022-10-20 02:15:28 +02:00
|
|
|
return 200 === $response->status();
|
|
|
|
} catch (ConnectionException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-18 15:21:14 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 16:44:36 +02:00
|
|
|
/**
|
2023-06-12 17:07:56 +02:00
|
|
|
* @return LazyCollection<int, Member>
|
2022-10-18 16:44:36 +02:00
|
|
|
*/
|
2023-06-12 17:07:56 +02:00
|
|
|
public function members(MailingList $list): LazyCollection
|
2022-10-18 16:44:36 +02:00
|
|
|
{
|
2022-10-18 17:55:53 +02:00
|
|
|
return app(Paginator::class)->result(
|
2023-06-12 17:07:56 +02:00
|
|
|
fn ($page) => $this->http()->get("/lists/{$list->listId}/roster/member?page={$page}&count=10"),
|
|
|
|
function ($response) use ($list) {
|
|
|
|
throw_unless($response->ok(), MailmanServiceException::class, 'Fetching members for listId '.$list->listId.' failed.');
|
|
|
|
/** @var array<int, array{email: string, self_link: string}>|null */
|
|
|
|
$entries = data_get($response->json(), 'entries', []);
|
2022-10-18 16:44:36 +02:00
|
|
|
throw_if(is_null($entries), MailmanServiceException::class, 'Failed getting member list from response');
|
|
|
|
|
|
|
|
foreach ($entries as $entry) {
|
2023-06-12 17:07:56 +02:00
|
|
|
yield Member::from([
|
|
|
|
...$entry,
|
|
|
|
'member_id' => strrev(preg_split('/\//', strrev($entry['self_link']))[0]),
|
|
|
|
]);
|
2022-10-18 16:44:36 +02:00
|
|
|
}
|
2022-10-18 17:55:53 +02:00
|
|
|
},
|
|
|
|
fn ($response) => data_get($response->json(), 'total_size')
|
|
|
|
);
|
2022-10-18 16:44:36 +02:00
|
|
|
}
|
|
|
|
|
2023-06-12 17:07:56 +02:00
|
|
|
public function addMember(MailingList $list, string $email): void
|
|
|
|
{
|
|
|
|
$response = $this->http()->post('members', [
|
|
|
|
'list_id' => $list->listId,
|
|
|
|
'subscriber' => $email,
|
|
|
|
'pre_verified' => 'true',
|
|
|
|
'pre_approved' => 'true',
|
|
|
|
'send_welcome_message' => 'false',
|
|
|
|
'pre_confirmed' => 'true',
|
|
|
|
]);
|
|
|
|
|
|
|
|
throw_unless(201 === $response->status(), MailmanServiceException::class, 'Adding member '.$email.' to '.$list->listId.' failed');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function removeMember(Member $member): void
|
|
|
|
{
|
|
|
|
$response = $this->http()->delete("members/{$member->memberId}");
|
|
|
|
|
2023-06-15 00:33:13 +02:00
|
|
|
throw_unless(204 === $response->status(), MailmanServiceException::class, 'Removing member failed');
|
2023-06-12 17:07:56 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 15:21:14 +02:00
|
|
|
private function http(): PendingRequest
|
|
|
|
{
|
|
|
|
return Http::withBasicAuth($this->username, $this->password)->withOptions(['base_uri' => $this->baseUrl]);
|
2022-10-18 15:04:47 +02:00
|
|
|
}
|
2022-10-20 11:11:52 +02:00
|
|
|
|
2023-02-17 18:57:11 +01:00
|
|
|
/**
|
|
|
|
* @return LazyCollection<int, MailingList>
|
|
|
|
*/
|
2022-10-20 11:11:52 +02:00
|
|
|
public function getLists(): LazyCollection
|
|
|
|
{
|
|
|
|
return app(Paginator::class)->result(
|
|
|
|
fn ($page) => $this->http()->get("/lists?page={$page}&count=10"),
|
|
|
|
function ($response) {
|
|
|
|
throw_unless($response->ok(), MailmanServiceException::class, 'Fetching lists failed.');
|
|
|
|
/** @var array<int, array{email: string}>|null */
|
|
|
|
$entries = data_get($response->json(), 'entries');
|
|
|
|
throw_if(is_null($entries), MailmanServiceException::class, 'Failed getting lists from response');
|
|
|
|
|
|
|
|
foreach ($entries as $entry) {
|
|
|
|
yield MailingList::from($entry);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
fn ($response) => data_get($response->json(), 'total_size')
|
|
|
|
);
|
|
|
|
}
|
2022-10-18 15:04:47 +02:00
|
|
|
}
|