From 47c2c17802590679277e4aa3eec73bc9f33681ff Mon Sep 17 00:00:00 2001 From: Michael Ishola Date: Wed, 14 Jul 2021 14:40:52 +0100 Subject: [PATCH] typehinted Message model args --- src/Models/Message.php | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/Models/Message.php b/src/Models/Message.php index a41f814..26ff391 100644 --- a/src/Models/Message.php +++ b/src/Models/Message.php @@ -26,7 +26,7 @@ class Message extends Model implements IMessage protected $appends = ['isSender']; protected $hidden = ['media']; - function scopeWhereNotSender($q, $user = null) { + function scopeWhereNotSender($q, ChatEventMaker|int $user = null) { $user_id = $user->id ?? $user ?? auth()->user()->id; $q->where('user_id', '!=', $user_id); } @@ -44,7 +44,7 @@ protected static function newFactory(){ return MessageFactory::new(); } - function scopeNotMsgEvents($q, $type = null, $user = null, $conversationScope = null) { + function scopeNotMsgEvents($q, $type = null, ChatEventMaker|int $user = null, $conversationScope = null) { $user_id = $user->id ?? $user ?? auth()->user()->id; $q->whereHas('conversation', fn ($q) => @@ -56,7 +56,7 @@ function scopeNotMsgEvents($q, $type = null, $user = null, $conversationScope = ); } - function scopeWhereRelatedToUser($q, $user = null) { + function scopeWhereRelatedToUser($q, ChatEventMaker|int $user) { $q->whereHas('conversation', fn ($q) => $q->whereHas('participants', fn ($q) => $q->whereUserId($user->id ?? $user)) ); @@ -74,10 +74,6 @@ function scopeHasNoEvent($q, callable $eventScope = null) { ); } - function getSystemAttribute() { - return $this->id < 100; - } - function scopeWhereConversationWasntDeleted($q, ChatEventMaker $by = null) { $q->whereDoesntHave('conversation', fn ($q) => $q->whereHas('chatEvents', fn ($q) => @@ -93,11 +89,7 @@ function getIsSenderAttribute() { return $user_id === $this->user_id; } - function makeDelete($user, $all = false) { - return $this->makeChatEvent($user, 'delete', $all); - } - - function participantsHasDeleted($maker_id = null){ + function participantsHasDeleted(int $maker_id = null){ [$participantsCount, $deleteEventsCount] = [ $this->conversation->participants()->count(), $this->chatEvents(false)->whereType('delete') @@ -107,14 +99,18 @@ function participantsHasDeleted($maker_id = null){ return $deleteEventsCount == $participantsCount-1; } - function makeRead($user) { + function makeDelete(ChatEventMaker $user, $all = false) { + return $this->makeChatEvent($user, 'delete', $all); + } + + function makeRead(ChatEventMaker $user) { return $this->makeChatEvent($user, 'read'); } - function makeDelivered($user) { + function makeDelivered(ChatEventMaker $user) { return $this->makeChatEvent($user, 'deliver'); } - private function makeChatEvent($user, $type = 'read', $all = false) { + private function makeChatEvent(ChatEventMaker $user, $type = 'read', $all = false) { $create = [ 'made_id' => $this->id, 'made_type' => $this::class, @@ -132,7 +128,7 @@ private function makeChatEvent($user, $type = 'read', $all = false) { // ); // } - public function participants($user = null){ + public function participants(ChatEventMaker|int $user = null){ $user_id = $user->id ?? $user ?? null; return self::config('models.conversation_user')::whereHas('conversation', fn ($q) => $q->whereId($this->conversation_id)->whereHas('participants', fn ($q) => $q->when($user_id, fn ($q) => $q->whereUserId($user_id)))