Skip to content

Commit

Permalink
typehinted Message model args
Browse files Browse the repository at this point in the history
  • Loading branch information
myckhel committed Jul 14, 2021
1 parent 7b72362 commit 47c2c17
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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) =>
Expand All @@ -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))
);
Expand All @@ -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) =>
Expand All @@ -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')
Expand All @@ -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,
Expand All @@ -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)))
Expand Down

0 comments on commit 47c2c17

Please sign in to comment.