Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented add/remove participant #29

Merged
merged 2 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/Http/Controllers/ConversationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\Request;
use Myckhel\ChatSystem\Http\Requests\PaginableRequest;
use DB;
use Myckhel\ChatSystem\Traits\Config;

class ConversationController extends Controller
{
Expand Down Expand Up @@ -99,7 +100,7 @@ public function store(Request $request)
*/
public function show(Request $request, $conversation)
{
$conversation = config('chat-system.models.conversation')::findOrFail($conversation);
$conversation = Config::config('models.conversation')::findOrFail($conversation);
$this->authorize('view', $conversation);
$user = $request->user();

Expand Down Expand Up @@ -129,7 +130,7 @@ public function show(Request $request, $conversation)
*/
public function update(Request $request, $conversation)
{
$conversation = config('chat-system.models.conversation')::findOrFail($conversation);
$conversation = Config::config('models.conversation')::findOrFail($conversation);
$this->authorize('update', $conversation);

@[
Expand Down Expand Up @@ -158,7 +159,7 @@ public function update(Request $request, $conversation)
*/
public function destroy(Request $request, $conversation)
{
$conversation = config('chat-system.models.conversation')::findOrFail($conversation);
$conversation = Config::config('models.conversation')::findOrFail($conversation);
$this->authorize('delete', $conversation);
$user = $request->user();
return ['status' => $conversation->makeDelete($user)];
Expand All @@ -178,4 +179,18 @@ function count(PaginableRequest $request) {
->when($type, fn ($q) => $q->whereHas('unread'))
->count();
}

function join(Request $request, $conversation) {
$conversation = Config::config('models.conversation')::findOrFail($conversation);
$this->authorize('join', $conversation);
$user = $request->user();
return $conversation->addParticipant($user);
}

function leave(Request $request, $conversation) {
$conversation = Config::config('models.conversation')::findOrFail($conversation);
$this->authorize('join', $conversation);
$user = $request->user();
return $conversation->removeParticipant($user);
}
}
27 changes: 27 additions & 0 deletions src/Models/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Myckhel\ChatSystem\Contracts\IConversation;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Myckhel\ChatSystem\Contracts\ChatEventMaker;

class Conversation extends Model implements IConversation
{
Expand All @@ -21,6 +22,32 @@ class Conversation extends Model implements IConversation
protected $casts = ['user_id' => 'int'];
protected $hidden = ['pivot'];

function addParticipant(ChatEventMaker $user, String $message = 'Someone joined the conversation') {
$participant = ['user_id' => $user->getKey()];
$participant = $this->participants()->firstOrCreate($participant, $participant);

$participant->wasRecentlyCreated && $this->createMessageActivity([
'user_id' => $user->getKey(),
'message' => $message,
]);

return $participant;
}

function removeParticipant(ChatEventMaker $user, String $message = 'Someone left the conversation') {
$participant = ['user_id' => $user->getKey()];
$participant = $this->participants()->whereUserId($user->getKey())->first();
$participant && $this->createMessageActivity([
'user_id' => $user->getKey(),
'message' => $message,
]);
return $participant?->delete();
}

protected function createMessageActivity(Array $create) {
return $this->messages()->create($create + ['type' => 'activity']);
}

protected static function newFactory(){
return ConversationFactory::new();
}
Expand Down
4 changes: 4 additions & 0 deletions src/Policies/ConversationPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,8 @@ public function forceDelete(ChatEventMaker $user, IConversation $conversation)
{
//
}

function join(ChatEventMaker $user, IConversation $conversation) {
return $conversation->type != 'private';
}
}
7 changes: 6 additions & 1 deletion src/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@
$prefix = Config::config('route.prefix');

Route::group(['prefix' => $prefix, 'middleware' => $middlewares], function(){
Route::get('conversations/count', [ConversationController::class, 'count']);
// conversations
Route::get('conversations/count', [ConversationController::class, 'count']);
Route::post('conversations/{conversation}/join', [ConversationController::class, 'join']);
Route::delete('conversations/{conversation}/join', [ConversationController::class, 'leave']);
// messages
Route::delete('messages', [MessageController::class, 'delete']);
// apiResources
Route::apiResources([
'conversations' => ConversationController::class,
'messages' => MessageController::class,
Expand Down