MOON
Server: Apache
System: Linux e2e-78-16.ssdcloudindia.net 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64
User: imensosw (1005)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/imensosw/www/mpl.imenso.co/app/Models/MessageThread.php
<?php

namespace App\Models;

use App\Events\UserSendsMessage;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class MessageThread extends Model
{
    protected $table = 'message_threads';

    public function messages()
    {
        return $this->hasMany(\App\Models\Message::class, 'thread_id')->orderByDesc('created_at')->orderByDesc('id');
    }

    public function participants()
    {
        return $this->belongsToMany(\App\Models\User::class, 'message_thread_participants', 'thread_id', 'user_id');
    }

    public function teamerArtist()
    {
        return $this->belongsTo(\App\Models\User::class, 'teamer_artist_id');
    }

    public static function teamerThreadExists($teamer_artist_id)
    {
        if (DB::table('message_threads')->where(['teamer_artist_id' => $teamer_artist_id])->count() > 0) {
            return true;
        }

        return false;
    }

    public static function addNew($enquiry_id = 0, $teamer_artist_id = 0)
    {
        $thread = new self;
        $thread->enquiry_id = $enquiry_id;
        $thread->teamer_artist_id = $teamer_artist_id;
        $thread->save();

        return $thread;
    }

    public function getLastMessage()
    {
        if (! $this->last_message) {
            $this->last_message = $this->messages()->first();
        }

        return $this->last_message;
    }

    public function isTeamer()
    {
        if ($this->teamer_artist_id != 0) {
            return true;
        }

        return false;
    }

    public function isUnread(User $user)
    {
        $row = DB::table('message_thread_participants')
            ->where([
                'user_id'   => $user->id,
                'thread_id' => $this->id,
                'hidden' => 0,
            ])
            ->first();

        if (! $row) {
            return false;
        }

        if (! $this->getLastMessage()) {
            return false;
        }

        if ($row->last_message_seen >= $this->getLastMessage()->id) {
            return false;
        }

        return true;
    }

    public function isHidden(User $user)
    {
        if (DB::table('message_thread_participants')->where([
            'user_id' => $user->id,
            'thread_id' => $this->id,
            'hidden' => 1,
        ])->count() > 0) {
            return true;
        } else {
            return false;
        }
    }

    public function unhideThreadForAll()
    {
        foreach ($this->participants as $user) {
            $this->unhideThreadForUser($user);
        }
    }

    public function hideThreadForAll()
    {
        foreach ($this->participants as $user) {
            $this->hideThreadForUser($user);
        }
    }

    public function unhideThreadForUser(User $user)
    {
        $row = DB::table('message_thread_participants')->where([
            'user_id'   => $user->id,
            'thread_id' => $this->id,
        ])->update([
            'hidden' => 0,
        ]);
    }

    public function hideThreadForUser(User $user)
    {
        $row = DB::table('message_thread_participants')->where([
            'user_id'   => $user->id,
            'thread_id' => $this->id,
        ])->update([
            'hidden' => 1,
        ]);
    }

    public function lastMessageTime()
    {
        if ($this->last_message_time) {
            return $this->last_message_time;
        }

        if ($this->getLastMessage()) {
            $this->last_message_time = $this->getLastMessage()->created_at;

            return $this->getLastMessage()->created_at;
        } else {
            $this->last_message_time = $this->created_at;

            return $this->created_at;
        }
    }

    public function lastMessageExtract()
    {
        if ($this->extract) {
            return $this->extract;
        }

        if ($this->getLastMessage()) {
            $content = $this->getLastMessage()->getExtract();
        } else {
            $content = '';
        }

        $this->extract = $content;

        return $content;
    }

    public function getRecipient()
    {
        return User::findOrFail(DB::table('message_thread_participants')->where('thread_id', $this->id)->where('user_id', '!=', Auth::user()->id)->first()->user_id);
    }

    public function addParticipant(User $user)
    {
        DB::table('message_thread_participants')->insert([
            'thread_id' => $this->id,
            'user_id'   => $user->id,
        ]);
    }

    public function removeParticipant(User $user)
    {
        $connection = DB::table('message_thread_participants')->where([
            'thread_id' => $this->id,
            'user_id'   => $user->id,
        ])->delete();
    }

    public function remove()
    {
        foreach ($this->participants as $p) {
            $this->removeParticipant($p);
        }

        $this->delete();
    }

    public function addMessage($message_text, User $sender, $broadcast = true)
    {
        $now = \Carbon\Carbon::now();
        if (strlen($message_text) > 0) {
            foreach ($this->participants as $participant) {
                if ($participant->id == $sender->id) {
                    $last_message_time = $this->lastMessageTime();
                    $message = Message::addNew(
                        $this,
                        $sender,
                        nl2br($message_text)
                    );

                    $html = '';

                    if (! $last_message_time->isToday()) {
                        $html .= view('back-end/partials/messaging-day-divider')->with([
                            'day' => $now,
                        ])->render();
                    }

                    $html .= view('back-end/partials/messaging-message')->with([
                        'message' => $message,
                    ])->render();

                    if ($broadcast) {
                        event(new UserSendsMessage($this, $message, $html));
                    }
                }
            }
        }
    }
}