Receiving messages from matrix

This commit is contained in:
Deon George 2024-06-10 09:18:59 +10:00
parent 61cfb773e2
commit 86995b03a8
13 changed files with 455 additions and 123 deletions

View File

@ -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=

View File

@ -0,0 +1,60 @@
<?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('%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('%s/_matrix/client/v3/rooms/%s/state/m.room.topic',config('matrix.server'),$this->room_id));
return $subject->json('topic','Message from Matrix');
case 'room_id':
default:
return object_get($this->_data,$key);
}
}
}

View File

@ -0,0 +1,50 @@
<?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
{
// During the life of the event, this method is called twice - once during Middleware processing, and finally by the Controller.
static $o = NULL;
static $or = NULL;
if (! $o OR ($or != $request)) {
$or = $request;
$o = self::create(Arr::get($request,'type','unknown'),$request);
}
return $o;
}
}

View File

@ -0,0 +1,52 @@
<?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);
case 'ts':
return object_get($this->_data,'origin_server_ts');
default:
return parent::__get($key);
}
}
}

View 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);
}
}

View File

@ -0,0 +1,23 @@
<?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;
final class MatrixController extends Controller
{
private const LOGKEY = 'CMC';
public function webhook(Request $request)
{
$event = MatrixEventFactory::make(Arr::get($request->events,0,[]));
Log::info(sprintf('%s:- Dispatching Matrix Event [%s]',static::LOGKEY,get_class($event)));
event($event);
return response(['result'=>'OK']);
}
}

View 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));
}
}

View File

@ -0,0 +1,59 @@
<?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));
$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');
// Message
$o->msg = $this->mo->message;
$o->set_origin = sprintf('Matrix %s (%s)',$this->mo->room,$our->ftn4d);
$o->save();
return $o;
}
}

View File

@ -2,7 +2,9 @@
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\Notification; use Illuminate\Support\Facades\Notification;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
@ -40,5 +42,9 @@ 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;
});
} }
} }

View File

@ -2,10 +2,13 @@
namespace App\Providers; namespace App\Providers;
use Illuminate\Support\Facades\Event;
use Illuminate\Auth\Events\Registered; use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
use App\Events\Matrix\Message;
use App\Listeners\Matrix\MessageListener;
class EventServiceProvider extends ServiceProvider class EventServiceProvider extends ServiceProvider
{ {
@ -29,6 +32,9 @@ class EventServiceProvider extends ServiceProvider
{ {
parent::boot(); parent::boot();
// Event::listen(
Message::class,
MessageListener::class,
);
} }
} }

View File

@ -2,136 +2,140 @@
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' => [ |--------------------------------------------------------------------------
'driver' => 'eloquent', | User Providers
'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"
|
*/
// 'users' => [ 'providers' => [
// 'driver' => 'database', 'users' => [
// 'table' => 'users', 'driver' => 'eloquent',
// ], 'model' => App\Models\User::class,
], ],
/* // 'users' => [
|-------------------------------------------------------------------------- // 'driver' => 'database',
| Resetting Passwords // 'table' => 'users',
|-------------------------------------------------------------------------- // ],
| ],
| 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' => [ |--------------------------------------------------------------------------
'provider' => 'users', | Resetting Passwords
'table' => 'password_resets', |--------------------------------------------------------------------------
'expire' => 60, |
'throttle' => 60, | 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' => [
| Password Confirmation Timeout 'provider' => 'users',
|-------------------------------------------------------------------------- 'table' => 'password_resets',
| 'expire' => 60,
| Here you may define the amount of seconds before a password confirmation 'throttle' => 60,
| 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' => [ |--------------------------------------------------------------------------
'name' => 'W3id', | Social Network Configuration
'id' => 'w3id', |--------------------------------------------------------------------------
'class' => 'btn-primary', */
'icon' => 'fas fa-address-card',
], 'social' => [
], 'w3id' => [
]; 'name' => 'W3id',
'id' => 'w3id',
'class' => 'btn-primary',
'icon' => 'fas fa-address-card',
],
],
];

13
config/matrix.php Normal file
View 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',
]
];

View File

@ -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};
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@ -15,4 +15,8 @@ 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');