Пример #1
0
 /**
  * Add a new message to this conversation.
  * Leave the user to null and the currently logged in user will send the message.
  *
  * @param string $content
  * @param Model $user
  * @return Message
  * @throws \Exception
  */
 public function addMessage($content, Model $user = null)
 {
     $user = $this->getUser($user);
     if (!$this->canWrite($user)) {
         return false;
     }
     $newMessage = new Message();
     $newMessage->user_id = $user->id;
     $newMessage->conversation_id = $this->id;
     $newMessage->content = $content;
     $newMessage->save();
     foreach ($this->users as $convUser) {
         $newMessageState = new MessageState();
         $newMessageState->user_id = $convUser->id;
         $newMessageState->message_id = $newMessage->id;
         $newMessageState->state = MessageState::indexOf('unread');
         if ($user->id == $convUser->id) {
             $newMessageState->state = MessageState::indexOf('own');
         }
         $newMessageState->save();
     }
     return $newMessage;
 }
Пример #2
0
 public function isDeleted(Model $byUser = null)
 {
     $messageState = $this->messageState($byUser);
     $deletedState = MessageState::indexOf('deleted');
     return $messageState->state == $deletedState;
 }