Compare commits
No commits in common. "a46ce7ff9ea84df9a3d8f6cd9e1f409fd9eafc73" and "ba0f643dca085ad997b2075ed90bca5545180748" have entirely different histories.
a46ce7ff9e
...
ba0f643dca
@ -57,7 +57,3 @@ 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=
|
|
||||||
|
@ -1,21 +0,0 @@
|
|||||||
<?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)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,64 +0,0 @@
|
|||||||
<?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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
<?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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
<?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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
<?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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
<?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']);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
<?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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
<?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,7 +11,6 @@ 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};
|
||||||
|
|
||||||
@ -271,8 +270,6 @@ 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,10 +6,11 @@ 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 = 'CNE';
|
private const LOGKEY = 'CN-';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Echomail
|
* @var Echomail
|
||||||
@ -17,7 +18,7 @@ class EchomailChannel
|
|||||||
protected Echomail $echomail;
|
protected Echomail $echomail;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new Echomail channel instance.
|
* Create a new Netmail channel instance.
|
||||||
*
|
*
|
||||||
* @param Echomail $o
|
* @param Echomail $o
|
||||||
*/
|
*/
|
||||||
|
@ -1,47 +0,0 @@
|
|||||||
<?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,10 +6,11 @@ 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 = 'CNN';
|
private const LOGKEY = 'CN-';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Netmail
|
* @var Netmail
|
||||||
|
@ -20,7 +20,9 @@ class AbsentNodes extends Echomails
|
|||||||
private Echomail $mo;
|
private Echomail $mo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Report on nodes that are have been marked Idle.
|
* Reply to a netmail ping request.
|
||||||
|
*
|
||||||
|
* @param Echomail $mo
|
||||||
*/
|
*/
|
||||||
public function __construct(private Collection $aos)
|
public function __construct(private Collection $aos)
|
||||||
{
|
{
|
||||||
|
@ -1,60 +0,0 @@
|
|||||||
<?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,14 +17,18 @@ class Test extends Echomails
|
|||||||
|
|
||||||
private const LOGKEY = 'NNP';
|
private const LOGKEY = 'NNP';
|
||||||
|
|
||||||
|
private Echomail $mo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Respond to a test in echomail.
|
* Reply to a netmail ping request.
|
||||||
*
|
*
|
||||||
* @param Echomail $mo
|
* @param Echomail $mo
|
||||||
*/
|
*/
|
||||||
public function __construct(private Echomail $mo)
|
public function __construct(Echomail $mo)
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
|
$this->mo = $mo;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,48 +0,0 @@
|
|||||||
<?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;
|
|
||||||
}
|
|
@ -1,128 +0,0 @@
|
|||||||
<?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,17 +2,11 @@
|
|||||||
|
|
||||||
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\Events\Matrix\Message;
|
use App\Notifications\Channels\{EchomailChannel,NetmailChannel};
|
||||||
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;
|
||||||
|
|
||||||
@ -35,10 +29,6 @@ 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));
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,20 +40,5 @@ 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,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
34
app/Providers/EventServiceProvider.php
Normal file
34
app/Providers/EventServiceProvider.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?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,6 +179,7 @@ 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,
|
||||||
|
|
||||||
|
240
config/auth.php
240
config/auth.php
@ -2,140 +2,136 @@
|
|||||||
|
|
||||||
return [
|
return [
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Authentication Defaults
|
| Authentication Defaults
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
|
||||||
| This option controls the default authentication "guard" and password
|
| This option controls the default authentication "guard" and password
|
||||||
| reset options for your application. You may change these defaults
|
| reset options for your application. You may change these defaults
|
||||||
| as required, but they're a perfect start for most applications.
|
| as required, but they're a perfect start for most applications.
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'defaults' => [
|
'defaults' => [
|
||||||
'guard' => 'web',
|
'guard' => 'web',
|
||||||
'passwords' => 'users',
|
'passwords' => 'users',
|
||||||
],
|
],
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Authentication Guards
|
| Authentication Guards
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
|
||||||
| Next, you may define every authentication guard for your application.
|
| Next, you may define every authentication guard for your application.
|
||||||
| Of course, a great default configuration has been defined for you
|
| Of course, a great default configuration has been defined for you
|
||||||
| here which uses session storage and the Eloquent user provider.
|
| here which uses session storage and the Eloquent user provider.
|
||||||
|
|
|
|
||||||
| All authentication drivers have a user provider. This defines how the
|
| All authentication drivers have a user provider. This defines how the
|
||||||
| users are actually retrieved out of your database or other storage
|
| users are actually retrieved out of your database or other storage
|
||||||
| mechanisms used by this application to persist your user's data.
|
| mechanisms used by this application to persist your user's data.
|
||||||
|
|
|
|
||||||
| Supported: "session", "token"
|
| Supported: "session", "token"
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'guards' => [
|
'guards' => [
|
||||||
'web' => [
|
'web' => [
|
||||||
'driver' => 'session',
|
'driver' => 'session',
|
||||||
'provider' => 'users',
|
'provider' => 'users',
|
||||||
],
|
],
|
||||||
|
|
||||||
'api' => [
|
'api' => [
|
||||||
'driver' => 'sanctum',
|
'driver' => 'sanctum',
|
||||||
'provider' => 'users',
|
'provider' => 'users',
|
||||||
'hash' => false,
|
'hash' => false,
|
||||||
],
|
],
|
||||||
|
|
||||||
'token' => [
|
'token' => [
|
||||||
'driver' => 'token',
|
'driver' => 'token',
|
||||||
'provider' => 'users',
|
'provider' => 'users',
|
||||||
'hash' => false,
|
'hash' => false,
|
||||||
],
|
],
|
||||||
|
],
|
||||||
|
|
||||||
'matrix' => [
|
/*
|
||||||
'driver' => 'matrix-token',
|
|--------------------------------------------------------------------------
|
||||||
]
|
| User Providers
|
||||||
],
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| All authentication drivers have a user provider. This defines how the
|
||||||
|
| users are actually retrieved out of your database or other storage
|
||||||
|
| mechanisms used by this application to persist your user's data.
|
||||||
|
|
|
||||||
|
| If you have multiple user tables or models you may configure multiple
|
||||||
|
| sources which represent each model / table. These sources may then
|
||||||
|
| be assigned to any extra authentication guards you have defined.
|
||||||
|
|
|
||||||
|
| Supported: "database", "eloquent"
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
/*
|
'providers' => [
|
||||||
|--------------------------------------------------------------------------
|
'users' => [
|
||||||
| User Providers
|
'driver' => 'eloquent',
|
||||||
|--------------------------------------------------------------------------
|
'model' => App\Models\User::class,
|
||||||
|
|
],
|
||||||
| All authentication drivers have a user provider. This defines how the
|
|
||||||
| users are actually retrieved out of your database or other storage
|
|
||||||
| mechanisms used by this application to persist your user's data.
|
|
||||||
|
|
|
||||||
| If you have multiple user tables or models you may configure multiple
|
|
||||||
| sources which represent each model / table. These sources may then
|
|
||||||
| be assigned to any extra authentication guards you have defined.
|
|
||||||
|
|
|
||||||
| Supported: "database", "eloquent"
|
|
||||||
|
|
|
||||||
*/
|
|
||||||
|
|
||||||
'providers' => [
|
// 'users' => [
|
||||||
'users' => [
|
// 'driver' => 'database',
|
||||||
'driver' => 'eloquent',
|
// 'table' => 'users',
|
||||||
'model' => App\Models\User::class,
|
// ],
|
||||||
],
|
],
|
||||||
|
|
||||||
// 'users' => [
|
/*
|
||||||
// 'driver' => 'database',
|
|--------------------------------------------------------------------------
|
||||||
// 'table' => 'users',
|
| Resetting Passwords
|
||||||
// ],
|
|--------------------------------------------------------------------------
|
||||||
],
|
|
|
||||||
|
| You may specify multiple password reset configurations if you have more
|
||||||
|
| than one user table or model in the application and you want to have
|
||||||
|
| separate password reset settings based on the specific user types.
|
||||||
|
|
|
||||||
|
| The expire time is the number of minutes that the reset token should be
|
||||||
|
| considered valid. This security feature keeps tokens short-lived so
|
||||||
|
| they have less time to be guessed. You may change this as needed.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
/*
|
'passwords' => [
|
||||||
|--------------------------------------------------------------------------
|
'users' => [
|
||||||
| Resetting Passwords
|
'provider' => 'users',
|
||||||
|--------------------------------------------------------------------------
|
'table' => 'password_resets',
|
||||||
|
|
'expire' => 60,
|
||||||
| You may specify multiple password reset configurations if you have more
|
'throttle' => 60,
|
||||||
| than one user table or model in the application and you want to have
|
],
|
||||||
| separate password reset settings based on the specific user types.
|
],
|
||||||
|
|
|
||||||
| The expire time is the number of minutes that the reset token should be
|
|
||||||
| considered valid. This security feature keeps tokens short-lived so
|
|
||||||
| they have less time to be guessed. You may change this as needed.
|
|
||||||
|
|
|
||||||
*/
|
|
||||||
|
|
||||||
'passwords' => [
|
/*
|
||||||
'users' => [
|
|--------------------------------------------------------------------------
|
||||||
'provider' => 'users',
|
| Password Confirmation Timeout
|
||||||
'table' => 'password_resets',
|
|--------------------------------------------------------------------------
|
||||||
'expire' => 60,
|
|
|
||||||
'throttle' => 60,
|
| Here you may define the amount of seconds before a password confirmation
|
||||||
],
|
| times out and the user is prompted to re-enter their password via the
|
||||||
],
|
| confirmation screen. By default, the timeout lasts for three hours.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
/*
|
'password_timeout' => 10800,
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
| Password Confirmation Timeout
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
|
|
|
||||||
| Here you may define the amount of seconds before a password confirmation
|
|
||||||
| times out and the user is prompted to re-enter their password via the
|
|
||||||
| confirmation screen. By default, the timeout lasts for three hours.
|
|
||||||
|
|
|
||||||
*/
|
|
||||||
|
|
||||||
'password_timeout' => 10800,
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Social Network Configuration
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
/*
|
'social' => [
|
||||||
|--------------------------------------------------------------------------
|
'w3id' => [
|
||||||
| Social Network Configuration
|
'name' => 'W3id',
|
||||||
|--------------------------------------------------------------------------
|
'id' => 'w3id',
|
||||||
*/
|
'class' => 'btn-primary',
|
||||||
|
'icon' => 'fas fa-address-card',
|
||||||
'social' => [
|
],
|
||||||
'w3id' => [
|
],
|
||||||
'name' => 'W3id',
|
|
||||||
'id' => 'w3id',
|
|
||||||
'class' => 'btn-primary',
|
|
||||||
'icon' => 'fas fa-address-card',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
];
|
];
|
@ -1,13 +0,0 @@
|
|||||||
<?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,MatrixController};
|
use App\Http\Controllers\{DomainController,SystemController,ZoneController};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
@ -16,7 +16,3 @@ use App\Http\Controllers\{DomainController,MatrixController};
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
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