Skip to content

Commit

Permalink
added conversation->createMessageWithToken
Browse files Browse the repository at this point in the history
- cleanup unused functions
- added more typehints to conversation methods
- reordered `conversation->notMsgEvents` arguments
- added more typehints to chatEvent methods
  • Loading branch information
myckhel committed Jul 26, 2021
1 parent 500a060 commit c722710
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 45 deletions.
2 changes: 1 addition & 1 deletion docs
Submodule docs updated from bbb743 to 819c64
2 changes: 1 addition & 1 deletion src/Http/Controllers/ConversationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function index(PaginableRequest $request)
->withCount([
'messages as latest_message_at' => fn ($q) => $q->select(DB::raw('max(created_at)')),
'participant as isParticipant' => fn ($q) => $q->whereUserId($user->id),
'unread',
'unread' => fn ($q) => $q->whereNotSender($user->id),
])
->orderByDesc('latest_message_at')
->with([
Expand Down
26 changes: 4 additions & 22 deletions src/Models/ChatEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,20 @@ function scopeWithTrashed($q, ChatEventMaker $user) {
$q->select('id', 'maker_id', 'maker_type', 'made_id', 'made_type', 'all')->whereMakerId($user->id)->orWhere('all', true);
}

function scopeNotMessanger($q, $userId) {
$q->whereDoesntHave('message', fn($q) => $q->whereUserId($userId));
function scopeNotMessanger($q, ChatEventMaker|int $user) {
$q->whereDoesntHave('message', fn($q) => $q->whereUserId($user->id ?? $user));
}

function message(): BelongsTo {
$message = self::config('models.message');
return $this->belongsTo($message, 'made_id')->whereMadeType($message);
}

function conversation(): BelongsTo {
$conversation = self::config('models.conversation');
return $this->belongsTo($conversation, 'made_id')->whereMadeType($conversation);
}

function scopeWithMakerEvents($q, $maker = null) {
$maker_id = $maker->id ?? $maker ?? null;
$q->select(['type', 'made_id', 'maker_id', 'created_at'])
->where(fn ($q) =>
$q->where(
fn ($q) => $q->makerTypeIs('delete', $maker_id)
)
->orWhere(fn ($q) => $q->makerTypeIsNot('deliver', $maker_id))
->orWhere(fn ($q) => $q->makerTypeIsNot('read', $maker_id))
);
}


function scopeMakerTypeIs($q, $type, $maker_id) {
$q->whereType($type)->whereMakerId($maker_id);
}
function scopeMakerTypeIsNot($q, $type, $maker_id) {
$q->whereType($type)->where('maker_id', '!=', $maker_id);
}

function maker(): MorphTo{
return $this->morphTo();
}
Expand Down
94 changes: 73 additions & 21 deletions src/Models/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@ class Conversation extends Model implements IConversation
protected $casts = ['user_id' => 'int'];
protected $hidden = ['pivot'];

/**
* Creates a message with token.
*
* @param string|int $token
* @param array $message
* @return Myckhel\ChatSystem\Models\Message
*/
function createMessageWithToken($token, array $message) {
return $this->messages()
->when(
$token,
fn ($q) => $q->where('metas->token', $token),
fn ($q) => $q->whereNull('id')
)
->firstOrCreate([], $message + ['metas' => $token ? ['token' => $token] : null]);
}

/**
* Adds a user as participant of the conversaton.
*
* @param Myckhel\ChatSystem\Contracts\ChatEventMaker $user
* @param string $message
* @return Myckhel\ChatSystem\Models\ConversationUser
*/
function addParticipant(ChatEventMaker $user, String $message = 'Someone joined the conversation') {
$participant = ['user_id' => $user->getKey()];
$participant = $this->participants()->firstOrCreate($participant, $participant);
Expand All @@ -34,6 +58,13 @@ function addParticipant(ChatEventMaker $user, String $message = 'Someone joined
return $participant;
}

/**
* Removes a user as participant of the conversaton.
*
* @param Myckhel\ChatSystem\Contracts\ChatEventMaker $user
* @param string $message
* @return bool|null
*/
function removeParticipant(ChatEventMaker $user, String $message = 'Someone left the conversation') {
$participant = ['user_id' => $user->getKey()];
$participant = $this->participants()->whereUserId($user->getKey())->first();
Expand All @@ -44,14 +75,20 @@ function removeParticipant(ChatEventMaker $user, String $message = 'Someone left
return $participant?->delete();
}

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

protected static function newFactory(){
return ConversationFactory::new();
}

/**
* Adds query where conversation has latest message where message is not a system message.
*
* @param Myckhel\ChatSystem\Contarcts\ChatEventMaker $user
* @return QueryBuilder
*/
function scopeWhereHasLastMessage($q, $user = null) {
$q->whereHas('last_message', fn ($q) =>
$q->where('type', '!=', 'system')
Expand Down Expand Up @@ -89,23 +126,36 @@ public function last_message(){
return $this->hasOne(self::config('models.message'))->latest();
}

/**
* Adds query where conversation doesn't have the given user as a participant.
*
* @param Myckhel\ChatSystem\Contarcts\ChatEventMaker $user
* @return QueryBuilder
*/
function scopeWhereNotParticipant($q, $user) {
$q->whereHas('participants', fn ($q) => $q->where('user_id', '!=', $user->id ?? $user));
}

public function participants(): HasMany {
return $this->hasMany(self::config('models.conversation_user'));
}

public function participant($user = null): HasOne {
return $this->hasOne(self::config('models.conversation_user'))->latest()
->when($user, fn ($q) => $q->whereUserId($user->id ?? $user));
->when($user, fn ($q) => $q->whereUserId($user->id ?? $user));
}

public function otherParticipant($user = null): HasOne {
$user_id = $user->id ?? $user ?? auth()->user()->id ?? null;
return $this->hasOne(self::config('models.conversation_user'))->latest()
->where('user_id', '!=', $user_id);
return $this->participant()
->where('user_id', '!=', $user->id ?? $user);
}

public function otherParticipants($user): HasOne {
return $this->participants()
->where('user_id', '!=', $user->id ?? $user);
}

// TODO investigate use
public function participant_id(){
return $this->participants();
}
Expand All @@ -114,14 +164,22 @@ public function messages(){
return $this->hasMany(self::config('models.message'));
}

public function unread($user = null){
$user_id = $user->id ?? $user ?? auth()->user()->id;
public function unread(int|ChatEventMaker $user = null){
$user_id = $user->id ?? $user ?? auth()->user()?->id;

return $this->notMsgEvents('read', $user_id)->latest()->whereNotSender($user_id);
return $this->notMsgEvents($user_id, 'read')->latest()
->when($user_id, fn ($q) => $q->whereNotSender($user_id));
}

function notMsgEvents($type = null, $user = null) {
$user_id = $user->id ?? $user ?? auth()->user()->id;
function undelivered(int|ChatEventMaker $user = null){
$user_id = $user->id ?? $user ?? auth()->user()?->id;

return $this->notMsgEvents($user_id, 'deliver')
->where('user_id', '!=', $user_id);
}

function notMsgEvents(int|ChatEventMaker $user, $type = null) {
$user_id = $user->id ?? $user;

return $this->messages()
->whereHas('conversation', fn ($q) =>
Expand All @@ -133,13 +191,6 @@ function notMsgEvents($type = null, $user = null) {
);
}

function undelivered($user = null){
$user_id = $user->id ?? $user ?? auth()->user()->id;

return $this->notMsgEvents('deliver', $user_id)
->where('user_id', '!=', $user_id);
}

public function author(){
return $this->belongsTo(self::config('models.user'), 'user_id');
}
Expand All @@ -150,16 +201,17 @@ public function newCollection(array $models = Array()){
}

class ConversationCollection extends Collection {
function makeDelivered($user = null){
$user = $user ?? auth()->user() ?? null;
function makeDelivered(ChatEventMaker $user){
$user = $user ?? null;
MakeEvent::dispatch($user, 'deliver', $this)->afterResponse();
return $this;
}

// TODO investigate use
function undelivered($user = null){
$user_id = $user->id ?? $user ?? auth()->user()->id;

return $this->notMsgEvents('deliver', $user_id)
return $this->notMsgEvents($user_id, 'deliver')
->where('user_id', '!=', $user_id);
}
}

0 comments on commit c722710

Please sign in to comment.