Skip to content

Commit 037a040

Browse files
committed
Rename column 'seen_at' to 'read_at'
* Remove the web and auth middlewares from routes.
1 parent 80b2a77 commit 037a040

File tree

8 files changed

+80
-17
lines changed

8 files changed

+80
-17
lines changed

src/Http/Controllers/InboxController.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,16 @@ public function getSluggedThreads($slug, $id)
5151
$query = $owner->threads()->summaryFor($owner);
5252
$total = $query->count();
5353
$threads = $this->applySkipAndTake($query)->get();
54-
$threads->each(function ($aThread) use ($owner) {
54+
foreach ($threads as $aThread) {
55+
$aThread->latestMessage()->forParticipant(get_class($owner), $owner->id)->first();
5556
$aThread->latestMessage->setPerspective($owner);
56-
});
57+
}
5758

5859
return $this->responder->toResponse([
5960
'threads' => $threads,
60-
'threads_sorted' => $threads->sortBy(function ($aThread) {
61+
'threads_sorted' => $threads/* ->sortBy(function ($aThread) {
6162
return $aThread->latestMessage ? $aThread->latestMessage->created_at->format('Y-m-d H:i:s') : null;
62-
}),
63+
}) */,
6364
'total' => $total,
6465
]);
6566
}
@@ -99,12 +100,13 @@ public function getSluggedThreadMessages(Request $request, $slug, $id, $threadId
99100
$query = $messageClassName::forThread($threadId)->for($owner)->withParticipants()->latest();
100101
$total = $query->count();
101102
$query = $this->applySkipAndTake($query);
103+
$messages = $query->get();
104+
$messages->each(function ($aMessage) use ($owner) {
105+
$aMessage->setPerspective($owner);
106+
});
102107
try {
103108
return $this->responder->toResponse([
104-
'messages' => $query->get(),
105-
// 'threads_sorted' => $threads->sortBy(function ($aThread) {
106-
// return $aThread->latestMessage ? $aThread->latestMessage->created_at->format('Y-m-d H:i:s') : null;
107-
// }),
109+
'messages' => $messages,
108110
'total' => $total,
109111
]);
110112
} catch (Exception $ex) {

src/InboxServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected function registerRoutes()
4242
'prefix' => config('andale-inbox.routing.prefix', 'andale-inbox'),
4343
'namespace' => 'Andaletech\Inbox\Http\Controllers',
4444
'middleware' => config('andale-inbox.routing.middleware', ['web', 'auth']),
45-
'name' => config('andale-inbox.routing.name', ['web', 'auth']),
45+
'name' => config('andale-inbox.routing.name', 'andaleInbox'),
4646
],
4747
function () {
4848
$this->loadRoutesFrom(__DIR__ . '/routes/web.php');

src/Models/Message.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ public function scopeWithParticipants(Builder $query)
126126
return $query->with(['participants']);
127127
}
128128

129+
public function scopeForParticipant(Builder $query, $type, $id)
130+
{
131+
return $query->whereHas('participants', function ($subQuery) use ($type, $id) {
132+
return $subQuery->forParticipant($type, $id);
133+
});
134+
}
135+
129136
#endregion query scopes
130137

131138
#region override parent methods
@@ -154,7 +161,7 @@ protected function addPerspective(array $arr)
154161
});
155162
if ($participant) {
156163
$arr['meta'] = [
157-
'seen_at' => $participant->seen_at,
164+
'read_at' => $participant->read_at,
158165
'trashed_at' => $participant->trashed_at,
159166
'extra' => $participant->extra,
160167
];

src/Models/Participant.php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Andaletech\Inbox\Models;
44

5+
use DateTime;
56
use Andaletech\Inbox\Libs\Utils;
67
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\Builder;
79
use Andaletech\Inbox\Contracts\Models\IParticipant;
810

911
/**
10-
* @property Carbon\Carbon $seen_at
12+
* @property Carbon\Carbon $read_at
1113
* @property Carbon\Carbon $trashed_at
1214
* @property array $tags
1315
* @property array $extra
@@ -88,6 +90,41 @@ public function participant()
8890

8991
#endregion relationships
9092

93+
#region query scope
94+
95+
public function scopeForParticipant(Builder $query, $type, $id)
96+
{
97+
return $query->where($query->qualifyColumn('participant_type'), $type)
98+
->where(
99+
$query->qualifyColumn('participant_id'),
100+
$id
101+
);
102+
}
103+
104+
public function scopeForMessageNanoId(Builder $query, $messageNanoId)
105+
{
106+
return $query->whereHas('message', function ($subQ) use ($messageNanoId) {
107+
return $subQ->where('nano_id', $messageNanoId);
108+
});
109+
}
110+
111+
public function scopeForThreadId(Builder $query, $threadId)
112+
{
113+
return $query->where('thread_id', $threadId);
114+
}
115+
116+
public function scopeWasRead(Builder $query)
117+
{
118+
return $query->whereNotNull('read_at');
119+
}
120+
121+
public function scopeNotRead(Builder $query)
122+
{
123+
return $query->whereNull('read_at');
124+
}
125+
126+
#endregion query scope
127+
91128
#region override parent methods.
92129

93130
public function toArray()
@@ -105,5 +142,18 @@ public function toArray()
105142

106143
#region class specific methods
107144

145+
public function markAsRead(?DateTime $dateTime = null, $save = true)
146+
{
147+
if ($this->read_at) {
148+
return $this->read_at;
149+
}
150+
$this->read_at = ($dateTime ?? (new DateTime()))->format('Y-m-d H:i:s');
151+
if ($save) {
152+
$this->save();
153+
}
154+
155+
return $this->read_at;;
156+
}
157+
108158
#endregion class specific methods
109159
}

src/Traits/HasInbox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function threads()
7373
config('andale-inbox.tables.participants'),
7474
'participant_id',
7575
'thread_id'
76-
)->where('participant_type', get_class($this))->distinct();
76+
)->where('participant_type', get_class($this))->latest('created_at')->distinct();
7777

7878
if ($this->isMultitenant()) {
7979
$query = $query->withPivot([config('andale-inbox.tenancy.tenant_id_column', 'tenant_id')]);

src/config/andale-inbox.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,15 @@
7878
'middleware' => ['web', 'auth'],
7979
'name' => 'andale-inbox.',
8080

81-
'id_patern' => '[0-9]+',
81+
'id_pattern' => '[0-9]+',
8282

83+
/**
84+
* For example in messaging route user_101 will be mapped to user model with id 101.
85+
* student_89 will be mapped to student model with id 89
86+
*/
8387
'slug_to_model_map' => [
8488
// 'users' => 'App\Models\User\User',
89+
// 'student' => 'App\Models\Student\Student,
8590
],
8691
'responder' => 'Andaletech\Inbox\Http\Response\ResponseBuilder',
8792
],

src/database/migrations/2021_12_25_104649_create_inbox_participants_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function up()
2121
$table->morphs('participant');
2222
$table->json('tags')->nullable();
2323
$table->json('extra')->nullable();
24-
$table->timestamp('seen_at')->nullable();
24+
$table->timestamp('read_at')->nullable();
2525
$table->timestamp('trashed_at')->nullable();
2626
$table->timestamps();
2727
$table->softDeletes();

src/routes/sluggedRoutes.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
Route::group(
77
[
8-
'prefix' => '/{slug}', /* 'as' => 'profile', */ /* 'middleware' => ['can:view,user'], */
8+
'prefix' => '/{slug}',
99
'where' => [
1010
'slug' => join(
1111
'',
@@ -21,10 +21,9 @@ function () {
2121
Route::group(
2222
[
2323
'prefix' => '/{id}',
24-
'where' => ['id' => config('andale-inbox.routing.id_patern', '[0-9]+')],
24+
'where' => ['id' => config('andale-inbox.routing.id_pattern', '[0-9]+')],
2525
],
2626
function () {
27-
// Route::get('/', 'InboxController@getSluggedThreads');
2827
Route::name('threads.')->prefix('threads')->group(__DIR__ . '/./parts/threads.php');
2928
}
3029
);

0 commit comments

Comments
 (0)