Skip to content

Commit ddfac21

Browse files
committed
Trigger event when message created
1 parent 6b1028f commit ddfac21

File tree

7 files changed

+44
-55
lines changed

7 files changed

+44
-55
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"html2text/html2text": "^4.3",
88
"illuminate/support": "^8.81",
99
"illuminate/database": "^8.81",
10-
"spatie/laravel-query-builder": "^4.0"
10+
"spatie/laravel-query-builder": "^4.0.2"
1111
},
1212

1313
"license": "MIT",

src/Contracts/Models/IMessage.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
* @property string|null $body_plain_text
1818
* @property string|null $from_type
1919
* @property string|null $from_id
20-
* @property string|null $created_at
21-
* @property string|null $updated_at
22-
* @property string|null $deleted_at
20+
* @property \Carbon\Carbon|null $created_at
21+
* @property \Carbon\Carbon|null $updated_at
22+
* @property \Carbon\Carbon|null $deleted_at
23+
*
24+
* @property \Illuminate\Database\Eloquent\Model $from
2325
*/
2426
interface IMessage
2527
{

src/Contracts/Models/IParticipant.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
* @author Kolado Sidibe <ksidibe@yahoo.com>
99
* @copyright 2022 Andale Technologies, SARL.
1010
* @license MIT
11+
*
12+
* @property string $nano_id
13+
* @property \Illuminate\Database\Eloquent\Model $thread
14+
* @property \Illuminate\Database\Eloquent\Model $message
15+
* @property \Illuminate\Database\Eloquent\Model $participant
1116
*/
1217
interface IParticipant
1318
{

src/Events/MessageCreated.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Andaletech\Inbox\Events;
4+
5+
use Illuminate\Queue\SerializesModels;
6+
use Illuminate\Foundation\Events\Dispatchable;
7+
use Andaletech\Inbox\Contracts\Models\IMessage;
8+
use Illuminate\Broadcasting\InteractsWithSockets;
9+
10+
class MessageCreated
11+
{
12+
use Dispatchable, InteractsWithSockets, SerializesModels;
13+
14+
public $message;
15+
16+
/**
17+
* Create a new event instance.
18+
*
19+
* @return void
20+
*/
21+
public function __construct(IMessage $message)
22+
{
23+
$this->message = $message;
24+
}
25+
}

src/Models/Participant.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public function __construct(array $attributes = [])
6262
protected static function boot()
6363
{
6464
parent::boot();
65+
static::creating(function ($participant) {
66+
if (empty($participant->nano_id)) {
67+
$participant->nano_id = Utils::getNanoId();
68+
}
69+
});
6570
}
6671

6772
#region relationships

src/Traits/HasInbox.php

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
use Html2Text\Html2Text;
66
use Illuminate\Database\Eloquent\Model;
7+
use Andaletech\Inbox\Events\MessageCreated;
78
use Illuminate\Database\Eloquent\Collection;
89
use Psr\Container\NotFoundExceptionInterface;
910
use Psr\Container\ContainerExceptionInterface;
10-
// use Andaletech\Inbox\Http\Contract\Models\IHasInbox;
1111
use Andaletech\Inbox\Contracts\Models\IHasInbox;
1212
use Illuminate\Database\Eloquent\Relations\Relation;
1313
use Illuminate\Contracts\Container\BindingResolutionException;
@@ -18,12 +18,6 @@
1818
*/
1919
trait HasInbox
2020
{
21-
// protected static $inboxThreadsTable;
22-
23-
// protected static $inboxMessagesTable;
24-
25-
// protected static $inboxParticipantsTable;
26-
2721
/**
2822
* @var bool
2923
*/
@@ -71,67 +65,23 @@ trait HasInbox
7165
*/
7266
public function threads()
7367
{
74-
// $threadClass = config('andale-inbox.eloquent.models.thread');
75-
76-
// return $threadClass::whereIn(
77-
// 'id',
78-
// );
7968
/**
8069
* @var \Illuminate\Database\Eloquent\Model $this
8170
*/
8271
$query = $this->belongsToMany(
8372
config('andale-inbox.eloquent.models.thread'),
8473
config('andale-inbox.tables.participants'),
85-
// config('andale-inbox.eloquent.models.participant'),
8674
'participant_id',
8775
'thread_id'
8876
)->where('participant_type', get_class($this))->distinct();
8977

90-
// return $this->addMultitenancyColumnIfNeeded($query);
9178
if ($this->isMultitenant()) {
9279
$query = $query->withPivot([config('andale-inbox.tenancy.tenant_id_column', 'tenant_id')]);
9380
}
9481

9582
return $query;
96-
// return $this->belongsToMany();
97-
return $this->morphedByMany(
98-
config('andale-inbox.eloquent.models.thread'),
99-
'participantxxx',
100-
config('andale-inbox.eloquent.models.participant'),
101-
'xxxxxx'
102-
);
103-
104-
return $this->hasMany(config('andale-inbox.eloquent.models.thread'));
105-
// $query = $this->morphMany(config('andale-inbox.eloquent.models.thread'), 'owner');
106-
// if ($this->isMultitenant()) {
107-
// $query = $query->with([config('andale-inbox.tenancy.tenant_id_column', 'tenant_id')]);
108-
// }
109-
110-
// return $query;
11183
}
11284

113-
// public function threadX()
114-
// {
115-
// return
116-
// }
117-
118-
// /**
119-
// *
120-
// * @return \Illuminate\Database\Eloquent\Relations\MorphMany
121-
// * @throws BindingResolutionException
122-
// * @throws NotFoundExceptionInterface
123-
// * @throws ContainerExceptionInterface
124-
// */
125-
// public function threadsXXXX()
126-
// {
127-
// $query = $this->morphMany(config('andale-inbox.eloquent.models.thread'), 'owner');
128-
// if ($this->isMultitenant()) {
129-
// $query = $query->with([config('andale-inbox.tenancy.tenant_id_column', 'tenant_id')]);
130-
// }
131-
132-
// return $query;
133-
// }
134-
13585
public function messages()
13686
{
13787
$query = $this->morphMany(config('andale-inbox.eloquent.models.message'), 'from');
@@ -254,6 +204,7 @@ protected function createInboxMessage($thread, $sendingUser = null)
254204
}
255205
$thread->messages()->save($message);
256206
$message->addParticipants([$this, ...$this->inboxNewMessageRecipients]);
207+
event(new MessageCreated($message));
257208

258209
return $message;
259210
}

src/database/migrations/2021_12_25_104649_create_inbox_participants_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function up()
1515
{
1616
Schema::create(config('andale-inbox.tables.participants', 'inbox_participants'), function (Blueprint $table) {
1717
$table->bigIncrements('id');
18+
$table->string('nano_id')->index();
1819
$table->unsignedBigInteger('thread_id');
1920
$table->unsignedBigInteger('message_id');
2021
$table->morphs('participant');

0 commit comments

Comments
 (0)