Posting messages to matrix
This commit is contained in:
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]);
|
||||
}
|
||||
}
|
@@ -38,7 +38,7 @@ class MatrixMessage extends Echomails
|
||||
$echoarea = $notifiable->routeNotificationFor(static::via);
|
||||
$o = $this->setupEchomail($echoarea);
|
||||
|
||||
Log::info(sprintf('%s:+ Sending Matrix Message to [%s]',self::LOGKEY,$echoarea->name));
|
||||
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';
|
||||
@@ -47,9 +47,10 @@ class MatrixMessage extends Echomails
|
||||
$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 = $this->mo->message;
|
||||
$o->msg = str_replace("\n","\r",$this->mo->message);
|
||||
$o->set_origin = sprintf('Matrix %s (%s)',$this->mo->room,$our->ftn4d);
|
||||
|
||||
$o->save();
|
||||
|
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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user