aufgeräumt
This commit is contained in:
parent
4869f45ca6
commit
9682c795ef
52
extend.php
52
extend.php
|
|
@ -1,37 +1,29 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
use Flarum\Extend;
|
namespace SbpJm\FlarumMSTeamsWebhook\Job;
|
||||||
use SbpJm\FlarumMSTeamsWebhook\Console\TestTeamsUserCommand;
|
|
||||||
use SbpJm\FlarumMSTeamsWebhook\Listener\MirrorAlertsToTeams;
|
|
||||||
use SbpJm\FlarumMSTeamsWebhook\Notification\TeamsNotificationDriver;
|
|
||||||
|
|
||||||
return [
|
use Flarum\Notification\Blueprint\BlueprintInterface;
|
||||||
(new Extend\Locales(__DIR__ . '/locale')),
|
use Flarum\User\User;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use SbpJm\FlarumMSTeamsWebhook\Service\TeamsActivityNotifier;
|
||||||
|
|
||||||
// Admin UI (Settings etc.)
|
class SendTeamsActivityNotificationJob implements ShouldQueue
|
||||||
(new Extend\Frontend('admin'))
|
{
|
||||||
->js(__DIR__ . '/js/dist/admin.js'),
|
use InteractsWithQueue;
|
||||||
|
use SerializesModels;
|
||||||
// Forum UI (User notification settings – Teams checkbox)
|
|
||||||
(new Extend\Frontend('forum'))
|
|
||||||
->js(__DIR__ . '/js/dist/forum.js'),
|
|
||||||
|
|
||||||
// CLI test command
|
|
||||||
(new Extend\Console())
|
|
||||||
->command(TestTeamsUserCommand::class),
|
|
||||||
|
|
||||||
// Notification hooks
|
|
||||||
(new Extend\Notification())
|
|
||||||
->beforeSending(MirrorAlertsToTeams::class)
|
|
||||||
->driver('teams', TeamsNotificationDriver::class, []),
|
|
||||||
|
|
||||||
// Deine bisherigen Preferences (SettingsPage-Toggles)
|
|
||||||
(new Extend\User())
|
|
||||||
->registerPreference('sbp-jm-msteams.enabled', 'boolval', false)
|
|
||||||
->registerPreference('sbp-jm-msteams.types.newPost', 'boolval', false)
|
|
||||||
->registerPreference('sbp-jm-msteams.types.postMentioned', 'boolval', false)
|
|
||||||
->registerPreference('sbp-jm-msteams.types.userMentioned', 'boolval', false),
|
|
||||||
];
|
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
protected User $user,
|
||||||
|
protected BlueprintInterface $blueprint
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handle(TeamsActivityNotifier $notifier): void
|
||||||
|
{
|
||||||
|
$notifier->notify($this->user, $this->blueprint);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -7,9 +7,7 @@ export default function extendNotificationGridWithTeams() {
|
||||||
items.add('teams', {
|
items.add('teams', {
|
||||||
name: 'teams',
|
name: 'teams',
|
||||||
icon: 'fab fa-microsoft',
|
icon: 'fab fa-microsoft',
|
||||||
label: app.translator.trans(
|
label: app.translator.trans('sbp-jm-flarum-msteams-webhook.forum.notification_method.teams'),
|
||||||
'sbp-jm-flarum-msteams-webhook.forum.notification_method.teams'
|
|
||||||
),
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
sbp-jm-flarum-msteams-webhook:
|
||||||
|
forum:
|
||||||
|
notification_method:
|
||||||
|
teams: Teams
|
||||||
|
|
@ -1,23 +1,4 @@
|
||||||
sbp-jm-msteams-webhook:
|
|
||||||
admin:
|
|
||||||
title: MS Teams Activity Feed
|
|
||||||
description: Mirror Flarum web notifications to Microsoft Teams.
|
|
||||||
notification:
|
|
||||||
drivers:
|
|
||||||
teams: Microsoft Teams
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
sbp-jm-msteams:
|
|
||||||
forum:
|
|
||||||
teams:
|
|
||||||
header: Microsoft Teams
|
|
||||||
newPost: Someone replies to a discussion I am following
|
|
||||||
postMentioned: Someone mentions me in a post
|
|
||||||
userMentioned: Someone mentions me
|
|
||||||
|
|
||||||
|
|
||||||
sbp-jm-flarum-msteams-webhook:
|
sbp-jm-flarum-msteams-webhook:
|
||||||
forum:
|
forum:
|
||||||
notification_method:
|
notification_method:
|
||||||
teams: Teams
|
teams: Teams
|
||||||
|
|
@ -1,70 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace SbpJm\FlarumMSTeamsWebhook\Listener;
|
|
||||||
|
|
||||||
use Flarum\Notification\Blueprint\BlueprintInterface;
|
|
||||||
use Flarum\Settings\SettingsRepositoryInterface;
|
|
||||||
use Flarum\User\User;
|
|
||||||
use Illuminate\Contracts\Queue\Queue;
|
|
||||||
use SbpJm\FlarumMSTeamsWebhook\Job\SendTeamsActivityNotificationJob;
|
|
||||||
use SbpJm\FlarumMSTeamsWebhook\Support\TargetResolver;
|
|
||||||
|
|
||||||
class MirrorAlertsToTeams
|
|
||||||
{
|
|
||||||
public function __construct(
|
|
||||||
protected SettingsRepositoryInterface $settings,
|
|
||||||
protected Queue $queue,
|
|
||||||
protected TargetResolver $targetResolver
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param User[] $newRecipients
|
|
||||||
* @return User[]
|
|
||||||
*/
|
|
||||||
public function __invoke(BlueprintInterface $blueprint, array $newRecipients): array
|
|
||||||
{
|
|
||||||
if (!$this->isEnabled()) {
|
|
||||||
return $newRecipients;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($newRecipients as $user) {
|
|
||||||
if (!$user instanceof User) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$this->shouldMirrorForUser($user, $blueprint)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->targetResolver->resolve($user) === null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->queue->push(new SendTeamsActivityNotificationJob($blueprint, $user));
|
|
||||||
}
|
|
||||||
|
|
||||||
return $newRecipients;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function isEnabled(): bool
|
|
||||||
{
|
|
||||||
return filter_var($this->settings->get('sbp-jm-msteams-webhook.enabled', '0'), FILTER_VALIDATE_BOOLEAN);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function shouldMirrorForUser(User $user, BlueprintInterface $blueprint): bool
|
|
||||||
{
|
|
||||||
$webPreferenceKey = User::getNotificationPreferenceKey($blueprint::getType(), 'alert');
|
|
||||||
$webEnabled = (bool) $user->getPreference($webPreferenceKey, false);
|
|
||||||
|
|
||||||
if (!$webEnabled) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$globalOptOutKey = 'sbp-jm-msteams-webhook.user_disabled';
|
|
||||||
|
|
||||||
return !(bool) $user->getPreference($globalOptOutKey, false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -5,26 +5,47 @@ namespace SbpJm\FlarumMSTeamsWebhook\Notification;
|
||||||
use Flarum\Notification\Blueprint\BlueprintInterface;
|
use Flarum\Notification\Blueprint\BlueprintInterface;
|
||||||
use Flarum\Notification\Driver\NotificationDriverInterface;
|
use Flarum\Notification\Driver\NotificationDriverInterface;
|
||||||
use Flarum\User\User;
|
use Flarum\User\User;
|
||||||
|
use Illuminate\Contracts\Queue\Queue;
|
||||||
|
use SbpJm\FlarumMSTeamsWebhook\Jobs\SendTeamsNotificationJob;
|
||||||
|
|
||||||
class TeamsNotificationDriver implements NotificationDriverInterface
|
class TeamsNotificationDriver implements NotificationDriverInterface
|
||||||
{
|
{
|
||||||
|
public function __construct(
|
||||||
|
protected Queue $queue
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wird vom Core aufgerufen, wenn eine Notification dispatched wird
|
||||||
|
*/
|
||||||
public function send(BlueprintInterface $blueprint, array $users): void
|
public function send(BlueprintInterface $blueprint, array $users): void
|
||||||
{
|
{
|
||||||
foreach ($users as $user) {
|
foreach ($users as $user) {
|
||||||
$prefKey = User::getNotificationPreferenceKey($blueprint::getType(), 'teams');
|
$prefKey = User::getNotificationPreferenceKey(
|
||||||
|
$blueprint::getType(),
|
||||||
|
'teams'
|
||||||
|
);
|
||||||
|
|
||||||
if (! $user->getPreference($prefKey)) {
|
if (! $user->getPreference($prefKey)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Hier dein bestehendes "echtes Teams senden" aufrufen.
|
// ✅ Versand immer über Queue
|
||||||
|
$this->queue->push(
|
||||||
|
new SendTeamsNotificationJob($blueprint, $user)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registriert die notify_*_teams Preferences für ALLE Notification-Typen
|
||||||
|
*/
|
||||||
public function registerType(string $blueprintClass, array $driversEnabledByDefault): void
|
public function registerType(string $blueprintClass, array $driversEnabledByDefault): void
|
||||||
{
|
{
|
||||||
User::registerPreference(
|
User::registerPreference(
|
||||||
User::getNotificationPreferenceKey($blueprintClass::getType(), 'teams'),
|
User::getNotificationPreferenceKey(
|
||||||
|
$blueprintClass::getType(),
|
||||||
|
'teams'
|
||||||
|
),
|
||||||
'boolval',
|
'boolval',
|
||||||
in_array('teams', $driversEnabledByDefault)
|
in_array('teams', $driversEnabledByDefault)
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue