ahmadbadpey's avatar

Hide deleted messages for sender and reciver in a conversation system in laravel

I'm working on a Chat (one to one) system like Telegram. suppose there is a Message model like this :

message_id
creator // user_id of that starts conversation
from_user_id
to_user_id
chat_id // id of chat that Message is belongs to that
text
receiver_deleted_at
sender_deleted_at

In this Chat system , each sender or receiver users can delete messages For themselves So when they are in Chat environment, should not can see deleted messages.

Now I want to hide deleted messages for sender and receiver user when they open a Chat.(exactly like Telegram).

For that I tried to use Global Scopes like this But I think that is wrong use:

public function apply (Builder $builder, Model $model)
        {
            $loggedUser = Auth::user();

            if ($model->chat->is_private) {
                if ($model->from_user_id == $loggedUser->user_id) {
                    $builder->whereNull('sender_deleted_at');
                } 
                elseif (!is_null($model->to_user_id) and $model->to_user_id == $loggedUser->user_id) {
                    $builder->whereNull('receiver_deleted_at');
                }
            } else {
                $builder->whereNull('sender_deleted_at');
            }

        }

Can anyone help to me to solve this problem ? Does anyone have experience in this case?

0 likes
0 replies

Please or to participate in this conversation.