/**
  * {@inheritdoc}
  */
 public function addMessageToConversation(Conversation $conversation, array $details, $checkParticipants = true)
 {
     $details['message_parsed'] = $this->messageFormatter->parse($details['message'], [MessageFormatter::ME_USERNAME => $this->userRepository->find($details['author_id'])->name]);
     // TODO: Parser options...
     $message = $conversation->messages()->create($details);
     if ($message) {
         $conversation->update(['last_message_id' => $message->id]);
         if ($checkParticipants) {
             $users = $conversation->participants()->wherePivot('has_left', true)->get(['user_id'])->lists('user_id');
             $conversation->participants()->newPivotStatement()->where('conversation_id', $conversation->id)->whereIn('user_id', $users)->update(['has_left' => false]);
             // This would be the better query but only MySQL wants to run it, PgSQL and SQLite don't like it
             // $conversation->participants()->wherePivot('has_left', true)->update(['has_left' => false]);
         }
     }
     return $message;
 }
 /**
  * @param Conversation $conversation
  * @param User         $exceptUser
  */
 private function checkForDeletion(Conversation $conversation, User $exceptUser = null)
 {
     $participants = $conversation->participants;
     /** @var Collection $activeParticipants */
     $activeParticipants = $participants->whereLoose('pivot.has_left', false)->whereLoose('pivot.ignores', false);
     if ($exceptUser != null) {
         $activeParticipants = $activeParticipants->filter(function ($item) use($exceptUser) {
             return $item->id != $exceptUser->id;
         });
     }
     if ($activeParticipants->count() == 0) {
         // All participants either ignore or left this conversation so delete everything related to it
         $conversation->update(['last_message_id' => null]);
         // Calling sync with an empty array will delete all records
         $conversation->participants()->sync([]);
         $this->conversationMessageRepository->deleteMessagesFromConversation($conversation);
         $conversation->delete();
     }
 }