Compare commits

...

3 Commits

Author SHA1 Message Date
philipp lang 7bffa6c5ae Add: Exclude members
continuous-integration/drone/push Build is passing Details
2023-07-10 11:36:58 +02:00
philipp lang 139c7623ab Simplify mail sync 2023-07-10 11:28:47 +02:00
philipp lang 1038f26171 Lint 2023-07-10 11:13:18 +02:00
5 changed files with 43 additions and 17 deletions

View File

@ -10,4 +10,9 @@ class MailEntry extends Data
{
$this->email = strtolower($email);
}
public function is(self $mailEntry): bool
{
return $this->email === $mailEntry->email;
}
}

View File

@ -3,6 +3,7 @@
namespace App\Mailgateway\Types;
use App\Maildispatcher\Data\MailEntry;
use App\Mailman\Data\MailingList;
use App\Mailman\Exceptions\MailmanServiceException;
use App\Mailman\Support\MailmanService;
use Illuminate\Support\Collection;
@ -67,8 +68,7 @@ class MailmanType extends Type
public function search(string $name, string $domain, string $email): ?MailEntry
{
$list = $this->service()->getLists()->first(fn ($list) => $list->fqdnListname === "{$name}@{$domain}");
throw_if(!$list, MailmanServiceException::class, "List for {$name}@{$domain} not found");
$list = $this->getList($name, $domain);
$member = $this->service()->members($list)->first(fn ($member) => $member->email === $email);
return $member ? MailEntry::from(['email' => $member->email]) : null;
@ -76,8 +76,7 @@ class MailmanType extends Type
public function add(string $name, string $domain, string $email): void
{
$list = $this->service()->getLists()->first(fn ($list) => $list->fqdnListname === "{$name}@{$domain}");
throw_if(!$list, MailmanServiceException::class, "List for {$name}@{$domain} not found");
$list = $this->getList($name, $domain);
$this->service()->addMember($list, $email);
}
@ -86,21 +85,27 @@ class MailmanType extends Type
*/
public function list(string $name, string $domain): Collection
{
$list = $this->service()->getLists()->first(fn ($list) => $list->fqdnListname === "{$name}@{$domain}");
throw_if(!$list, MailmanServiceException::class, "List for {$name}@{$domain} not found");
$list = $this->getList($name, $domain);
return collect($this->service()->members($list)->map(fn ($member) => MailEntry::from(['email' => $member->email]))->all());
}
public function remove(string $name, string $domain, string $email): void
{
$list = $this->service()->getLists()->first(fn ($list) => $list->fqdnListname === "{$name}@{$domain}");
throw_if(!$list, MailmanServiceException::class, "List for {$name}@{$domain} not found");
$list = $this->getList($name, $domain);
$member = $this->service()->members($list)->first(fn ($member) => $member->email === $email);
throw_if(!$member, MailmanServiceException::class, 'Member for removing not found');
$this->service()->removeMember($member);
}
private function getList(string $name, string $domain): MailingList
{
$list = $this->service()->getLists()->first(fn ($list) => $list->fqdnListname === "{$name}@{$domain}");
throw_if(!$list, MailmanServiceException::class, "List for {$name}@{$domain} not found");
return $list;
}
private function service(): MailmanService
{
return app(MailmanService::class)->setCredentials($this->url, $this->user, $this->password);

View File

@ -79,8 +79,9 @@ abstract class Type
*/
public function sync(string $name, string $domain, Collection $results): void
{
$members = $this->list($name, $domain);
foreach ($results as $result) {
if ($this->search($name, $domain, $result->email)) {
if ($members->first(fn ($member) => $member->is($result))) {
continue;
}
@ -88,7 +89,7 @@ abstract class Type
}
$this->list($name, $domain)
->filter(fn ($listEntry) => null === $results->first(fn ($r) => $r->email === $listEntry->email))
->filter(fn ($listEntry) => $results->doesntContain(fn ($r) => $r->is($listEntry)))
->each(fn ($listEntry) => $this->remove($name, $domain, $listEntry->email));
}

View File

@ -20,7 +20,8 @@ class FilterScope extends Filter
* @param array<int, int> $activityIds
* @param array<int, int> $subactivityIds
* @param array<int, int> $groupIds
* @param array<int, int> $additional
* @param array<int, int> $include
* @param array<int, int> $exclude
*/
public function __construct(
public bool $ausstand = false,
@ -29,7 +30,8 @@ class FilterScope extends Filter
public array $subactivityIds = [],
public ?string $search = '',
public array $groupIds = [],
public array $additional = [],
public array $include = [],
public array $exclude = [],
) {
}
@ -73,9 +75,13 @@ class FilterScope extends Filter
}
});
}
if (count($this->exclude)) {
$query->whereNotIn('id', $this->exclude);
}
})->orWhere(function ($query) {
if (count($this->additional)) {
$query->whereIn('id', $this->additional);
if (count($this->include)) {
$query->whereIn('id', $this->include);
}
});
});

View File

@ -34,14 +34,23 @@
size="sm"
></f-multipleselect>
<f-multipleselect
id="additional"
name="additional"
id="include"
name="include"
:options="members.meta.members"
v-model="model.filter.additional"
v-model="model.filter.include"
@update:modelValue="reload(1)"
label="Zusätzliche Mitglieder"
size="sm"
></f-multipleselect>
<f-multipleselect
id="exclude"
name="exclude"
:options="members.meta.members"
v-model="model.filter.exclude"
@update:modelValue="reload(1)"
label="Mitglieder ausschließen"
size="sm"
></f-multipleselect>
<f-multipleselect
id="groupIds"
name="groupIds"