Skip to content

Commit

Permalink
Merge pull request #7 from coralsio/messaging_discussions
Browse files Browse the repository at this point in the history
delete Conversation and changes
  • Loading branch information
saeed-corals authored Jun 10, 2024
2 parents cf49060 + cfeecbd commit 8e75da8
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 26 deletions.
31 changes: 26 additions & 5 deletions src/Http/Controllers/API/DiscussionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
namespace Corals\Modules\Messaging\Http\Controllers\API;

use Corals\Foundation\Http\Controllers\APIBaseController;
use Corals\Foundation\Models\BaseModel;
use Corals\Foundation\Search\Search;
use Corals\Modules\Messaging\DataTables\DiscussionsDataTable;
use Corals\Modules\Messaging\Http\Requests\DiscussionRequest;
use Corals\Modules\Messaging\Models\Discussion;
use Corals\Modules\Messaging\Models\Message;
use Corals\Modules\Messaging\Services\DiscussionService;
use Corals\Modules\Messaging\Transformers\API\DiscussionPresenter;
use Corals\User\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class DiscussionsController extends APIBaseController
Expand Down Expand Up @@ -67,4 +62,30 @@ public function markAsRead(Request $request, Discussion $discussion)
return apiExceptionResponse($e);
}
}

/**
* @param Request $request
* @param Discussion $discussion
* @return \Illuminate\Http\JsonResponse
*/
public function deleteConversation(Request $request, Discussion $discussion)
{
try {

$this->discussionService->deleteConversation($discussion);

return apiResponse([], trans('Corals::messages.success.deleted', ['item' => 'Discussion']));
} catch (\Exception $exception) {
return apiExceptionResponse($exception);
}
}

public function discussionsCountForUnReadMessages(Request $request)
{
try {
return apiResponse($this->discussionService->discussionsCountForUnReadMessages());
} catch (\Exception $e) {
return apiExceptionResponse($e);
}
}
}
18 changes: 14 additions & 4 deletions src/Models/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Corals\Modules\Messaging\Models;

use Carbon\Carbon;

use Corals\Foundation\Models\BaseModel;
use Corals\Foundation\Search\Indexable;
use Corals\Foundation\Transformers\PresentableTrait;
Expand Down Expand Up @@ -75,7 +74,12 @@ public function getUserParticipation($user = null)
->first();
}

public function getReceiverParticipations($user = null)
/**
* @param null $user
* @param bool $withTrashed
* @return mixed
*/
public function getReceiverParticipations($user = null,bool $withTrashed = false)
{
if (is_null($user)) {
$user = user();
Expand All @@ -84,6 +88,7 @@ public function getReceiverParticipations($user = null)
return $this->hasMany(Participation::class)
->where("participable_type", '=', $user->getMorphClass())
->where("participable_id", '<>', $user->getKey())
->when($withTrashed, fn(Builder $builder) => $builder->withTrashed())
->get();
}

Expand All @@ -94,7 +99,12 @@ public function getReceiverParticipations($user = null)
*/
public function messages()
{
return $this->hasMany(Message::class)->orderBy('messaging_messages.created_at', 'desc');
$latest_deleted_message_id = optional($this->getUserParticipation())->latest_deleted_message_id;

return $this->hasMany(Message::class)
->when($latest_deleted_message_id, function ($q) use ($latest_deleted_message_id) {
$q->where('id', '>', $latest_deleted_message_id);
})->orderBy('messaging_messages.created_at', 'desc');
}

/**
Expand Down Expand Up @@ -411,7 +421,7 @@ public function getParticipationByParticipable(EloquentModel $participable)
*/
public function getTrashedParticipations()
{
return $this->participations()->get();
return $this->participations()->withTrashed()->get();
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ public function participations()
return $this->hasMany(Participation::class, 'discussion_id', 'discussion_id');
}

/**
* @param null $user
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\HasMany|object|null
*/
public function userParticipation($user = null)
{
if (is_null($user)) {
$user = user();
}

return $this->participations()
->where("participable_type", '=', $user->getMorphClass())
->where("participable_id", '=', $user->getKey())
->first();
}

public function canDeleteMessage($discussion_id = 0)
{
$user = user();
Expand Down
15 changes: 4 additions & 11 deletions src/Models/Participation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
use Corals\Foundation\Models\BaseModel;
use Corals\Foundation\Transformers\PresentableTrait;
use Corals\Modules\Messaging\Contracts\Participation as ParticipationContract;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Traits\LogsActivity;

class Participation extends BaseModel implements ParticipationContract
{
use PresentableTrait;
use LogsActivity;
use SoftDeletes;

/**
* Model configuration.
* @var string
*/
public $config = 'messaging.models.participation';

protected $fillable = ['discussion_id', 'participable_type', 'participable_id', 'last_read', 'status', 'unread_counts'];
protected $fillable = ['discussion_id', 'participable_type', 'participable_id', 'last_read', 'status', 'unread_counts', 'latest_deleted_message_id', 'deleted_at'];

/**
* The attributes that should be cast to native types.
Expand All @@ -30,7 +32,7 @@ class Participation extends BaseModel implements ParticipationContract
'discussion_id' => 'integer',
'participable_id' => 'integer',
'unread_counts' => 'integer',
'last_read' => 'datetime'
'last_read' => 'datetime',
];

protected $table = 'messaging_participations';
Expand Down Expand Up @@ -80,15 +82,6 @@ public function stringInfo()
return $this->participable->getAttribute('name');
}

/**
* Restore a soft-deleted model instance.
*
* @return bool|null
*/
public function restore()
{
// TODO: Implement restore() method.
}

public function canBeRead()
{
Expand Down
27 changes: 25 additions & 2 deletions src/Services/DiscussionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use Corals\Foundation\Services\BaseServiceClass;
use Corals\Modules\Messaging\Models\Discussion;
use Corals\Modules\Messaging\Models\Participation;

class DiscussionService extends BaseServiceClass
{
Expand All @@ -24,4 +23,28 @@ public function markAsRead(Discussion $discussion)

return $this->getModelDetails($discussion->fresh());
}
}

/**
* @param Discussion $discussion
*/
public function deleteConversation(Discussion $discussion)
{
$lastMessage = $discussion->messages()->first();

$discussion->getUserParticipation()->update([
'deleted_at' => now(),
'latest_deleted_message_id' => $lastMessage->id
]);
}

/**
* @return int
*/
public function discussionsCountForUnReadMessages()
{
return Discussion::query()
->forUser(user())
->where('messaging_participations.unread_counts', '>', 0)
->count();
}
}
21 changes: 18 additions & 3 deletions src/Services/MessageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use Corals\Modules\Messaging\Models\Discussion;
use Corals\Modules\Messaging\Models\Message;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

class MessageService extends BaseServiceClass
{
Expand All @@ -18,20 +20,28 @@ class MessageService extends BaseServiceClass
*/
public function preStore(Request $request, &$additionalData)
{
$secondParticipationId = $request->get('second_participation_id');

if ($request->filled('discussion_id')) {
$discussion = tap(
Discussion::find($request->get('discussion_id'))
)->restoreAllParticipations();

return;
}

$secondParticipationId = $request->get('second_participation_id');

//check if they have already chatted.
$discussion = user()->discussions()
->whereHas(
'participations', fn($q) => $q->where('messaging_participations.participable_id', $secondParticipationId)
'participations', fn($q) => $q->withTrashed()
->where('messaging_participations.participable_id', $secondParticipationId)
)->select('messaging_discussions.id')
->first();

if ($discussion) {
// check if the discussion is already deleted, we need to restore it.
$discussion->restoreAllParticipations();
$additionalData['discussion_id'] = $discussion->id;
return;
}
Expand Down Expand Up @@ -69,7 +79,12 @@ public function fetchMoreMessages(Message $message)
->orderBy('created_at', 'desc')
->with(['participations', 'media'])
->where('discussion_id', $message->discussion_id)
->where('id', '<', $message->id)
->where(function (Builder $q) use ($message) {
$q->where('id', '<', $message->id)
->when($message->userParticipation()->latest_deleted_message_id, function (Builder $q, $lastDeletedMsgId) {
$q->where('id', '>', $lastDeletedMsgId);
});
})
->paginate();

return $this->getPresenter()->present($messages);
Expand Down
2 changes: 1 addition & 1 deletion src/Transformers/API/DiscussionTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function transform(Discussion $discussion)
{
$participables = new Collection;

foreach ($discussion->getReceiverParticipations(user()) as $participations) {
foreach ($discussion->getReceiverParticipations(user(), true) as $participations) {
$participables->push($participations->participable);
}

Expand Down
8 changes: 8 additions & 0 deletions src/database/migrations/MessagingTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function up()
$table->morphs('participable');
$table->integer('unread_counts')->default(0);
$table->timestamp('last_read')->nullable();
$table->unsignedInteger('latest_deleted_message_id')->nullable();
$table->enum('status', ['read', 'unread', 'deleted', 'important', 'star'])->nullable()->default('unread');
$table->text('properties')->nullable();

Expand All @@ -61,6 +62,13 @@ public function up()
$table->softDeletes();

$table->timestamps();


$table->foreign('latest_deleted_message_id')
->references('id')
->on('messaging_messages')
->restrictOnDelete()
->cascadeOnUpdate();
});
}

Expand Down
3 changes: 3 additions & 0 deletions src/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
});

Route::post('discussions/{discussion}/mark-as-read', 'DiscussionsController@markAsRead');
Route::get('discussions/un-read-messages', 'DiscussionsController@discussionsCountForUnReadMessages');

Route::post('discussions/{discussion}/delete-conversation', 'DiscussionsController@deleteConversation');

Route::apiResource('messages', 'MessageController')->only('store');
Route::apiResource('discussions', 'DiscussionsController')->only('index', 'show');

0 comments on commit 8e75da8

Please sign in to comment.