Compare commits
3 Commits
ba0f643dca
...
a46ce7ff9e
Author | SHA1 | Date | |
---|---|---|---|
a46ce7ff9e | |||
86995b03a8 | |||
61cfb773e2 |
@ -57,3 +57,7 @@ AWS_BUCKET=
|
|||||||
AWS_ENDPOINT=
|
AWS_ENDPOINT=
|
||||||
AWS_DEFAULT_REGION=home
|
AWS_DEFAULT_REGION=home
|
||||||
AWS_USE_PATH_STYLE_ENDPOINT=true
|
AWS_USE_PATH_STYLE_ENDPOINT=true
|
||||||
|
|
||||||
|
MATRIX_SERVER=
|
||||||
|
MATRIX_AS_TOKEN=
|
||||||
|
MATRIX_HS_TOKEN=
|
||||||
|
21
app/Events/Echomail.php
Normal file
21
app/Events/Echomail.php
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Events;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Events\Dispatchable;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
|
||||||
|
use App\Models\Echomail as EchomailModel;
|
||||||
|
|
||||||
|
class Echomail
|
||||||
|
{
|
||||||
|
use Dispatchable, SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new event instance.
|
||||||
|
*/
|
||||||
|
public function __construct(public EchomailModel $eo)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
64
app/Events/Matrix/Base.php
Normal file
64
app/Events/Matrix/Base.php
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Events\Matrix;
|
||||||
|
|
||||||
|
use Illuminate\Http\Client\ConnectionException;
|
||||||
|
use Illuminate\Support\Facades\Http;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
|
use App\Models\Echoarea;
|
||||||
|
|
||||||
|
abstract class Base
|
||||||
|
{
|
||||||
|
protected $_data = [];
|
||||||
|
|
||||||
|
public function __construct(array $request)
|
||||||
|
{
|
||||||
|
Log::info(sprintf('EMb:- Event Initialised [%s]',get_class($this)));
|
||||||
|
|
||||||
|
$this->_data = json_decode(json_encode($request));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enable getting values for keys in the response
|
||||||
|
*
|
||||||
|
* @note: This method is limited to certain values to ensure integrity reasons
|
||||||
|
* @note: Classes should return:
|
||||||
|
* + channel_id,
|
||||||
|
* + team_id,
|
||||||
|
* + ts,
|
||||||
|
* + user_id
|
||||||
|
* @param string $key
|
||||||
|
* @return mixed|object
|
||||||
|
* @throws ConnectionException
|
||||||
|
*/
|
||||||
|
public function __get(string $key)
|
||||||
|
{
|
||||||
|
switch ($key) {
|
||||||
|
case 'echoarea':
|
||||||
|
$rooms = collect(config('matrix.rooms'));
|
||||||
|
|
||||||
|
return Echoarea::where('name',$rooms->get($this->room_id))->single();
|
||||||
|
|
||||||
|
case 'room':
|
||||||
|
$room_alias = Http::withToken(config('matrix.as_token'))
|
||||||
|
->get(sprintf('https://%s/_matrix/client/v3/rooms/%s/state/m.room.canonical_alias',config('matrix.server'),$this->room_id));
|
||||||
|
|
||||||
|
return $room_alias->json('alias',$this->room_id);
|
||||||
|
|
||||||
|
case 'topic':
|
||||||
|
$subject = Http::withToken(config('matrix.as_token'))
|
||||||
|
->get(sprintf('https://%s/_matrix/client/v3/rooms/%s/state/m.room.topic',config('matrix.server'),$this->room_id));
|
||||||
|
|
||||||
|
return $subject->json('topic','Message from Matrix');
|
||||||
|
|
||||||
|
case 'ts':
|
||||||
|
return object_get($this->_data,'origin_server_ts');
|
||||||
|
|
||||||
|
case 'event_id':
|
||||||
|
case 'room_id':
|
||||||
|
default:
|
||||||
|
return object_get($this->_data,$key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
41
app/Events/Matrix/Factory.php
Normal file
41
app/Events/Matrix/Factory.php
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Events\Matrix;
|
||||||
|
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
use Illuminate\Support\Facades\App;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
|
class Factory {
|
||||||
|
private const LOGKEY = 'EMf';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array event type to event class mapping
|
||||||
|
*/
|
||||||
|
public const map = [
|
||||||
|
'm.room.message' => Message::class,
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns new event instance
|
||||||
|
*
|
||||||
|
* @param string $type
|
||||||
|
* @param array $request
|
||||||
|
* @return Base
|
||||||
|
*/
|
||||||
|
public static function create(string $type,array $request): Base
|
||||||
|
{
|
||||||
|
$class = Arr::get(self::map,$type,Unknown::class);
|
||||||
|
Log::debug(sprintf('%s:- Working out Event Class for [%s] as [%s]',static::LOGKEY,$type,$class));
|
||||||
|
|
||||||
|
if (App::environment() == 'local')
|
||||||
|
file_put_contents('/tmp/event.'.$type,print_r($request,TRUE));
|
||||||
|
|
||||||
|
return new $class($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function make(array $request): Base
|
||||||
|
{
|
||||||
|
return self::create(Arr::get($request,'type','unknown'),$request);
|
||||||
|
}
|
||||||
|
}
|
49
app/Events/Matrix/Message.php
Normal file
49
app/Events/Matrix/Message.php
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Events\Matrix;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A matrix message event
|
||||||
|
*
|
||||||
|
* Array
|
||||||
|
* (
|
||||||
|
* [age] => 37
|
||||||
|
* [content] => Array
|
||||||
|
* (
|
||||||
|
* [body] => This is my text
|
||||||
|
* [m.mentions] => Array
|
||||||
|
* (
|
||||||
|
* )
|
||||||
|
*
|
||||||
|
* [msgtype] => m.text
|
||||||
|
* )
|
||||||
|
*
|
||||||
|
* [event_id] => $fkpvy3qDkAGlB55nvqcH8mUfSxzELtaJ9TKJs6GP9us
|
||||||
|
* [origin_server_ts] => 1717917709298
|
||||||
|
* [room_id] => !bbXofZepRYOhKjihLH:matrix.dege.au
|
||||||
|
* [sender] => @deon:matrix.dege.au
|
||||||
|
* [type] => m.room.message
|
||||||
|
* [unsigned] => Array
|
||||||
|
* (
|
||||||
|
* [age] => 37
|
||||||
|
* )
|
||||||
|
*
|
||||||
|
* [user_id] => @deon:matrix.dege.au
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
class Message extends Base
|
||||||
|
{
|
||||||
|
public function __get($key)
|
||||||
|
{
|
||||||
|
switch ($key) {
|
||||||
|
case 'message':
|
||||||
|
return object_get($this->_data,'content.body');
|
||||||
|
|
||||||
|
case 'sender':
|
||||||
|
return object_get($this->_data,$key);
|
||||||
|
|
||||||
|
default:
|
||||||
|
return parent::__get($key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
20
app/Events/Matrix/Unknown.php
Normal file
20
app/Events/Matrix/Unknown.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Events\Matrix;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Catch all unknown events that we havent specifically programmed for.
|
||||||
|
*
|
||||||
|
* @package Slack\Event
|
||||||
|
*/
|
||||||
|
class Unknown extends Base
|
||||||
|
{
|
||||||
|
public function __construct(array $request)
|
||||||
|
{
|
||||||
|
Log::notice(sprintf('EMU:? UNKNOWN Event received [%s]',get_class($this)));
|
||||||
|
|
||||||
|
parent::__construct($request);
|
||||||
|
}
|
||||||
|
}
|
31
app/Http/Controllers/MatrixController.php
Normal file
31
app/Http/Controllers/MatrixController.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
|
use App\Events\Matrix\Factory as MatrixEventFactory;
|
||||||
|
use App\Events\Matrix\Message;
|
||||||
|
|
||||||
|
final class MatrixController extends Controller
|
||||||
|
{
|
||||||
|
private const LOGKEY = 'CMC';
|
||||||
|
|
||||||
|
public function webhook(Request $request): mixed
|
||||||
|
{
|
||||||
|
$event = MatrixEventFactory::make(Arr::get($request->events,0,[]));
|
||||||
|
|
||||||
|
// Catch our messages that we've posted
|
||||||
|
if (($event instanceof Message) && preg_match('#^.*\^[0-9]+_[0-9]+/[0-9]+(\.[0-9]+)?:#',$event->sender)) {
|
||||||
|
Log::info(sprintf('%s:- Ignoring Matrix Message event, probably from us [%s]',static::LOGKEY,$event->sender));
|
||||||
|
return response(['result'=>'OK']);
|
||||||
|
}
|
||||||
|
|
||||||
|
Log::info(sprintf('%s:- Dispatching Matrix Event [%s]',static::LOGKEY,get_class($event)));
|
||||||
|
event($event);
|
||||||
|
|
||||||
|
return response(['result'=>'OK']);
|
||||||
|
}
|
||||||
|
}
|
43
app/Listeners/EchomailListener.php
Normal file
43
app/Listeners/EchomailListener.php
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Listeners;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Notification;
|
||||||
|
|
||||||
|
use App\Notifications\Matrix\Echomail;
|
||||||
|
|
||||||
|
class EchomailListener implements ShouldQueue
|
||||||
|
{
|
||||||
|
protected const LOGKEY = 'LEL';
|
||||||
|
|
||||||
|
public string $queue = 'matrix';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the event listener.
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the event.
|
||||||
|
*/
|
||||||
|
public function handle(object $event): void
|
||||||
|
{
|
||||||
|
$ea = $event->eo->echoarea;
|
||||||
|
|
||||||
|
// Catch our messages that we've posted, so they dont go back
|
||||||
|
if (str_ends_with($event->eo->from,':'.config('matrix.server')))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if ($ea && collect(config('matrix.rooms'))->contains($ea->name)) {
|
||||||
|
Log::debug(sprintf('%s:- Sending echomail to matrix for [%s]',self::LOGKEY,$event->eo->msgid));
|
||||||
|
Notification::route('matrix',collect(config('matrix.rooms'))->search($ea->name))->notify(new Echomail($event->eo->withoutRelations()));
|
||||||
|
|
||||||
|
} else {
|
||||||
|
Log::debug(sprintf('%s:- Not sending echomail to matrix for [%s]',self::LOGKEY,$event->eo->msgid));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
31
app/Listeners/Matrix/MessageListener.php
Normal file
31
app/Listeners/Matrix/MessageListener.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Listeners\Matrix;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Notification;
|
||||||
|
|
||||||
|
use App\Events\Matrix\Message;
|
||||||
|
use App\Notifications\Echomails\MatrixMessage;
|
||||||
|
|
||||||
|
class MessageListener implements ShouldQueue
|
||||||
|
{
|
||||||
|
protected const LOGKEY = 'LMM';
|
||||||
|
|
||||||
|
public string $queue = 'matrix';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the event.
|
||||||
|
*
|
||||||
|
* @param Message $event
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function handle(Message $event): void
|
||||||
|
{
|
||||||
|
// Do some magic with event data
|
||||||
|
Log::info(sprintf('%s:- Message Event in [%s] from [%s]',self::LOGKEY,$event->room_id,$event->sender));
|
||||||
|
|
||||||
|
Notification::route('echomail',$event->echoarea)->notify(new MatrixMessage($event));
|
||||||
|
}
|
||||||
|
}
|
@ -11,6 +11,7 @@ use Illuminate\Support\Facades\Log;
|
|||||||
|
|
||||||
use App\Casts\{CollectionOrNull,CompressedStringOrNull,UTF8StringOrNull};
|
use App\Casts\{CollectionOrNull,CompressedStringOrNull,UTF8StringOrNull};
|
||||||
use App\Classes\FTN\Message;
|
use App\Classes\FTN\Message;
|
||||||
|
use App\Events\Echomail as EchomailEvent;
|
||||||
use App\Interfaces\Packet;
|
use App\Interfaces\Packet;
|
||||||
use App\Traits\{MessageAttributes,MsgID,ParseAddresses,QueryCacheableConfig};
|
use App\Traits\{MessageAttributes,MsgID,ParseAddresses,QueryCacheableConfig};
|
||||||
|
|
||||||
@ -270,6 +271,8 @@ final class Echomail extends Model implements Packet
|
|||||||
$model->seenby()->syncWithPivotValues($exportto,['export_at'=>Carbon::now()],FALSE);
|
$model->seenby()->syncWithPivotValues($exportto,['export_at'=>Carbon::now()],FALSE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
event(new EchomailEvent($model->withoutRelations()));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,11 +6,10 @@ use Illuminate\Notifications\Notification;
|
|||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
use App\Models\Echomail;
|
use App\Models\Echomail;
|
||||||
use App\Models\Setup;
|
|
||||||
|
|
||||||
class EchomailChannel
|
class EchomailChannel
|
||||||
{
|
{
|
||||||
private const LOGKEY = 'CN-';
|
private const LOGKEY = 'CNE';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Echomail
|
* @var Echomail
|
||||||
@ -18,7 +17,7 @@ class EchomailChannel
|
|||||||
protected Echomail $echomail;
|
protected Echomail $echomail;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new Netmail channel instance.
|
* Create a new Echomail channel instance.
|
||||||
*
|
*
|
||||||
* @param Echomail $o
|
* @param Echomail $o
|
||||||
*/
|
*/
|
||||||
|
47
app/Notifications/Channels/MatrixChannel.php
Normal file
47
app/Notifications/Channels/MatrixChannel.php
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Notifications\Channels;
|
||||||
|
|
||||||
|
use Illuminate\Notifications\Notification;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
|
class MatrixChannel
|
||||||
|
{
|
||||||
|
use interactsWithQueue;
|
||||||
|
|
||||||
|
private const LOGKEY = 'CNM';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new Matrix channel instance.
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send the given notification.
|
||||||
|
*
|
||||||
|
* @param mixed $notifiable
|
||||||
|
* @param \Illuminate\Notifications\Notification $notification
|
||||||
|
* @return \Psr\Http\Message\ResponseInterface|void
|
||||||
|
*/
|
||||||
|
public function send($notifiable,Notification $notification)
|
||||||
|
{
|
||||||
|
if (! $room = $notifiable->routeNotificationFor('matrix',$notification))
|
||||||
|
return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$o = $notification->toMatrix($notifiable);
|
||||||
|
|
||||||
|
// @todo Check this exception works as expected (putting the job back on the queue when the user doesnt exist), else it might need to go in Matrix/Echomail
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::info(sprintf('%s:= Exception [%s] posting to Matrix, putting job back on queue for [%s]',self::LOGKEY,$e->getMessage(),$room));
|
||||||
|
$this->release();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// @todo Be nice to get the message ID for debugging
|
||||||
|
Log::info(sprintf('%s:= Posted echomail to Matrix [%s]',self::LOGKEY,$room),['o'=>$o]);
|
||||||
|
}
|
||||||
|
}
|
@ -6,11 +6,10 @@ use Illuminate\Notifications\Notification;
|
|||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
use App\Models\Netmail;
|
use App\Models\Netmail;
|
||||||
use App\Models\Setup;
|
|
||||||
|
|
||||||
class NetmailChannel
|
class NetmailChannel
|
||||||
{
|
{
|
||||||
private const LOGKEY = 'CN-';
|
private const LOGKEY = 'CNN';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Netmail
|
* @var Netmail
|
||||||
|
@ -20,9 +20,7 @@ class AbsentNodes extends Echomails
|
|||||||
private Echomail $mo;
|
private Echomail $mo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reply to a netmail ping request.
|
* Report on nodes that are have been marked Idle.
|
||||||
*
|
|
||||||
* @param Echomail $mo
|
|
||||||
*/
|
*/
|
||||||
public function __construct(private Collection $aos)
|
public function __construct(private Collection $aos)
|
||||||
{
|
{
|
||||||
|
60
app/Notifications/Echomails/MatrixMessage.php
Normal file
60
app/Notifications/Echomails/MatrixMessage.php
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Notifications\Echomails;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
|
use App\Events\Matrix\Message;
|
||||||
|
use App\Models\Echomail;
|
||||||
|
use App\Notifications\Echomails;
|
||||||
|
use App\Traits\MessagePath;
|
||||||
|
|
||||||
|
class MatrixMessage extends Echomails
|
||||||
|
{
|
||||||
|
use MessagePath;
|
||||||
|
|
||||||
|
private const LOGKEY = 'NMM';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Post a message from Matrix.
|
||||||
|
*
|
||||||
|
* @param Message $mo
|
||||||
|
*/
|
||||||
|
public function __construct(private Message $mo)
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the mail representation of the notification.
|
||||||
|
*
|
||||||
|
* @param mixed $notifiable
|
||||||
|
* @return Echomail
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function toEchomail(object $notifiable): Echomail
|
||||||
|
{
|
||||||
|
$echoarea = $notifiable->routeNotificationFor(static::via);
|
||||||
|
$o = $this->setupEchomail($echoarea);
|
||||||
|
|
||||||
|
Log::info(sprintf('%s:+ Sending Matrix Message to [%s]',self::LOGKEY,$echoarea->name),['mo'=>$this->mo]);
|
||||||
|
|
||||||
|
$our = our_address($echoarea->domain)->last();
|
||||||
|
$o->to = 'All';
|
||||||
|
$o->from = $this->mo->sender;
|
||||||
|
$o->datetime = Carbon::createFromTimestampMs($this->mo->ts);
|
||||||
|
$o->subject = $this->mo->topic;
|
||||||
|
$o->fftn_id = $our->id;
|
||||||
|
$o->kludges->put('CHRS:','UTF8 2');
|
||||||
|
$o->kludges->put('EVENT:',$this->mo->event_id);
|
||||||
|
|
||||||
|
// Message
|
||||||
|
$o->msg = str_replace("\n","\r",$this->mo->message);
|
||||||
|
$o->set_origin = sprintf('Matrix %s (%s)',$this->mo->room,$our->ftn4d);
|
||||||
|
|
||||||
|
$o->save();
|
||||||
|
|
||||||
|
return $o;
|
||||||
|
}
|
||||||
|
}
|
@ -17,18 +17,14 @@ class Test extends Echomails
|
|||||||
|
|
||||||
private const LOGKEY = 'NNP';
|
private const LOGKEY = 'NNP';
|
||||||
|
|
||||||
private Echomail $mo;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reply to a netmail ping request.
|
* Respond to a test in echomail.
|
||||||
*
|
*
|
||||||
* @param Echomail $mo
|
* @param Echomail $mo
|
||||||
*/
|
*/
|
||||||
public function __construct(Echomail $mo)
|
public function __construct(private Echomail $mo)
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
$this->mo = $mo;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
48
app/Notifications/Matrix.php
Normal file
48
app/Notifications/Matrix.php
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Notifications;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Notifications\Notification;
|
||||||
|
|
||||||
|
use App\Classes\FTN\Message;
|
||||||
|
use App\Models\{Echoarea,Echomail,Setup};
|
||||||
|
|
||||||
|
abstract class Matrix extends Notification implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Queueable;
|
||||||
|
|
||||||
|
protected const via = 'matrix';
|
||||||
|
|
||||||
|
private const LOGKEY = 'NM-';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new notification instance.
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->queue = 'matrix';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the notification's delivery channels.
|
||||||
|
*
|
||||||
|
* @param mixed $notifiable
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function via($notifiable)
|
||||||
|
{
|
||||||
|
return [ self::via ];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the mail representation of the notification.
|
||||||
|
*
|
||||||
|
* @param mixed $notifiable
|
||||||
|
* @return Echomail
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
abstract public function toMatrix(object $notifiable): mixed;
|
||||||
|
}
|
128
app/Notifications/Matrix/Echomail.php
Normal file
128
app/Notifications/Matrix/Echomail.php
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Notifications\Matrix;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Http;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
|
use App\Models\Echomail as EchomailModel;
|
||||||
|
use App\Notifications\Matrix;
|
||||||
|
|
||||||
|
class Echomail extends Matrix
|
||||||
|
{
|
||||||
|
private const LOGKEY = 'NME';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Post a message from Matrix.
|
||||||
|
*
|
||||||
|
* @param EchomailModel $o
|
||||||
|
*/
|
||||||
|
public function __construct(private EchomailModel $o)
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the mail representation of the notification.
|
||||||
|
*
|
||||||
|
* @param mixed $notifiable
|
||||||
|
* @return mixed
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function toMatrix(object $notifiable): mixed
|
||||||
|
{
|
||||||
|
$room = $notifiable->routeNotificationFor(static::via);
|
||||||
|
|
||||||
|
Log::info(sprintf('%s:+ Sending Echo Message to Matrix [%s]',self::LOGKEY,$room));
|
||||||
|
|
||||||
|
$username = sprintf('%s^%d_%d/%d.%d',
|
||||||
|
$this->o->from,
|
||||||
|
$this->o->fftn->zone->zone_id,
|
||||||
|
$this->o->fftn->host_id,
|
||||||
|
$this->o->fftn->node_id,
|
||||||
|
$this->o->fftn->point_id,
|
||||||
|
);
|
||||||
|
|
||||||
|
$user = sprintf('@%s:%s',$username,config('matrix.server'));
|
||||||
|
|
||||||
|
// Set topic if it is different:
|
||||||
|
$subject = Http::withToken(config('matrix.as_token'))
|
||||||
|
->get(sprintf('https://%s/_matrix/client/v3/rooms/%s/state/m.room.topic',config('matrix.server'),$room));
|
||||||
|
|
||||||
|
if (($x=preg_replace('/^RE:\s*/i','',$this->o->subject)) !== $subject->json('topic','Message from Matrix')) {
|
||||||
|
$topic = Http::withToken(config('matrix.as_token'))
|
||||||
|
->put(sprintf('https://%s/_matrix/client/v3/rooms/%s/state/m.room.topic',config('matrix.server'),$room),[
|
||||||
|
'topic'=>$x,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($topic->status() !== 200)
|
||||||
|
Log::error(sprintf('%s:! Failed to set matrix room topic to [%s] in room [%s]',self::LOGKEY,$x,$room),['msg'=>$topic->body()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$msg = Http::withToken(config('matrix.as_token'))
|
||||||
|
->withQueryParameters(['user_id'=>$user])
|
||||||
|
->post(sprintf('https://%s/_matrix/client/v3/rooms/%s/send/m.room.message',config('matrix.server'),$room),[
|
||||||
|
'msgtype'=>'m.text',
|
||||||
|
'body'=>mb_convert_encoding(str_replace("\r","\n",$this->o->msg),'UTF-8','IBM850'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
switch ($msg->status()) {
|
||||||
|
case 200:
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 403:
|
||||||
|
Log::alert(sprintf('%s:! Got 403 with errcode [%s] reason [%s]',self::LOGKEY,$msg->json('errcode'),$msg->json('error')));
|
||||||
|
|
||||||
|
// @todo Test that the user doesnt exist
|
||||||
|
// If the user doesnt exist in matrix yet
|
||||||
|
if (str_starts_with($msg->json('error'),'Application service has not registered this user')) {
|
||||||
|
// Register user
|
||||||
|
$msg = Http::withToken(config('matrix.as_token'))
|
||||||
|
->post(sprintf('https://%s/_matrix/client/v3/register',config('matrix.server')),[
|
||||||
|
'type'=>'m.login.application_service',
|
||||||
|
'username'=>$username,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($msg->status() !== 200) {
|
||||||
|
Log::error(sprintf('%s:! Failed to register user [%s] to matrix',self::LOGKEY,$username),['msg'=>$msg->body()]);
|
||||||
|
throw new \Exception(sprintf('Failed to invite user [%s] to matrix',$username));
|
||||||
|
}
|
||||||
|
|
||||||
|
// @todo Test that the user has been invited
|
||||||
|
// Invite user
|
||||||
|
$msg = Http::withToken(config('matrix.as_token'))
|
||||||
|
//->withQueryParameters(['user_id'=>$user])
|
||||||
|
->post(sprintf('https://%s/_matrix/client/v3/rooms/%s/invite',config('matrix.server'),$room),[
|
||||||
|
'user_id'=>$user,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($msg->status() !== 200) {
|
||||||
|
Log::error(sprintf('%s:! Failed to invite user [%s] to matrix room [%s]',self::LOGKEY,$user,$room),['msg'=>$msg->body()]);
|
||||||
|
throw new \Exception(sprintf('Failed to invite user [%s] to matrix room [%s]',$user,$room));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Join as user
|
||||||
|
$msg = Http::withToken(config('matrix.as_token'))
|
||||||
|
->withQueryParameters(['user_id'=>$user])
|
||||||
|
->post(sprintf('https://%s/_matrix/client/v3/rooms/%s/join',config('matrix.server'),$room),[
|
||||||
|
'user_id'=>$user,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($msg->status() !== 200) {
|
||||||
|
Log::error(sprintf('%s:! Failed to join user [%s] to matrix room [%s]',self::LOGKEY,$user,$room),['msg'=>$msg->body()]);
|
||||||
|
throw new \Exception(sprintf('Failed to join user [%s] to matrix room [%s]',$user,$room));
|
||||||
|
}
|
||||||
|
|
||||||
|
// retry this message
|
||||||
|
throw new \Exception('Need to create user on matrix first');
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
Log::error(sprintf('%s:! Unknown status [%d] with errcode [%s] reason [%s] when posting message [%d] to matrix',self::LOGKEY,$msg->status(),$msg->json('errcode'),$msg->json('error'),$this->o->id),['msg'=>$msg->body()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $msg->body();
|
||||||
|
}
|
||||||
|
}
|
@ -2,11 +2,17 @@
|
|||||||
|
|
||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Notifications\ChannelManager;
|
use Illuminate\Notifications\ChannelManager;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Illuminate\Support\Facades\Event;
|
||||||
use Illuminate\Support\Facades\Notification;
|
use Illuminate\Support\Facades\Notification;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
use App\Notifications\Channels\{EchomailChannel,NetmailChannel};
|
use App\Events\Matrix\Message;
|
||||||
|
use App\Listeners\EchomailListener;
|
||||||
|
use App\Listeners\Matrix\MessageListener;
|
||||||
|
use App\Notifications\Channels\{EchomailChannel,MatrixChannel,NetmailChannel};
|
||||||
use App\Models\{Echomail,Netmail};
|
use App\Models\{Echomail,Netmail};
|
||||||
use App\Traits\SingleOrFail;
|
use App\Traits\SingleOrFail;
|
||||||
|
|
||||||
@ -29,6 +35,10 @@ class AppServiceProvider extends ServiceProvider
|
|||||||
$service->extend('netmail', function ($app) {
|
$service->extend('netmail', function ($app) {
|
||||||
return new NetmailChannel($app->make(Netmail::class));
|
return new NetmailChannel($app->make(Netmail::class));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$service->extend('matrix', function ($app) {
|
||||||
|
return new MatrixChannel($app->make(Echomail::class));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,5 +50,20 @@ class AppServiceProvider extends ServiceProvider
|
|||||||
public function boot()
|
public function boot()
|
||||||
{
|
{
|
||||||
static::bootSingleOrFail();
|
static::bootSingleOrFail();
|
||||||
|
|
||||||
|
Auth::viaRequest('matrix-token',function (Request $request) {
|
||||||
|
return (config('matrix.hs_token') && ($request->bearerToken() === config('matrix.hs_token'))) ? TRUE : NULL;
|
||||||
|
});
|
||||||
|
|
||||||
|
Event::listen(
|
||||||
|
Message::class,
|
||||||
|
MessageListener::class,
|
||||||
|
);
|
||||||
|
|
||||||
|
// @todo This should be detected automatically?
|
||||||
|
Event::listen(
|
||||||
|
\App\Events\Echomail::class,
|
||||||
|
EchomailListener::class,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,34 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Providers;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Event;
|
|
||||||
use Illuminate\Auth\Events\Registered;
|
|
||||||
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
|
|
||||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
|
||||||
|
|
||||||
class EventServiceProvider extends ServiceProvider
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* The event listener mappings for the application.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $listen = [
|
|
||||||
Registered::class => [
|
|
||||||
SendEmailVerificationNotification::class,
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Register any events for your application.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function boot()
|
|
||||||
{
|
|
||||||
parent::boot();
|
|
||||||
|
|
||||||
//
|
|
||||||
}
|
|
||||||
}
|
|
@ -179,7 +179,6 @@ return [
|
|||||||
App\Providers\AppServiceProvider::class,
|
App\Providers\AppServiceProvider::class,
|
||||||
App\Providers\AuthServiceProvider::class,
|
App\Providers\AuthServiceProvider::class,
|
||||||
// App\Providers\BroadcastServiceProvider::class,
|
// App\Providers\BroadcastServiceProvider::class,
|
||||||
App\Providers\EventServiceProvider::class,
|
|
||||||
App\Providers\RouteServiceProvider::class,
|
App\Providers\RouteServiceProvider::class,
|
||||||
App\Providers\CustomBladeServiceProvider::class,
|
App\Providers\CustomBladeServiceProvider::class,
|
||||||
|
|
||||||
|
@ -52,6 +52,10 @@ return [
|
|||||||
'provider' => 'users',
|
'provider' => 'users',
|
||||||
'hash' => false,
|
'hash' => false,
|
||||||
],
|
],
|
||||||
|
|
||||||
|
'matrix' => [
|
||||||
|
'driver' => 'matrix-token',
|
||||||
|
]
|
||||||
],
|
],
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
13
config/matrix.php
Normal file
13
config/matrix.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Matrix integration configuration
|
||||||
|
*/
|
||||||
|
return [
|
||||||
|
'server' => ENV('MATRIX_SERVER'),
|
||||||
|
'as_token' => ENV('MATRIX_AS_TOKEN'),
|
||||||
|
'hs_token' => ENV('MATRIX_HS_TOKEN'),
|
||||||
|
'rooms' => [
|
||||||
|
// '!bbXofZepRYOhKjihLH:matrix.dege.au' => 'PVT_TEST',
|
||||||
|
]
|
||||||
|
];
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
use App\Http\Controllers\{DomainController,SystemController,ZoneController};
|
use App\Http\Controllers\{DomainController,MatrixController};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
@ -16,3 +16,7 @@ use App\Http\Controllers\{DomainController,SystemController,ZoneController};
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Route::get('/domain/daily',[DomainController::class,'api_daily_stats']);
|
Route::get('/domain/daily',[DomainController::class,'api_daily_stats']);
|
||||||
|
|
||||||
|
Route::any('matrix/{item}',[MatrixController::class,'webhook'])
|
||||||
|
->where('item', '.*')
|
||||||
|
->middleware('auth:matrix');
|
Loading…
Reference in New Issue
Block a user