/** * @param int $id * @param ReplyRequest $request * @param Store $settings * * @return \Illuminate\Http\RedirectResponse */ public function postReply($id, ReplyRequest $request, Store $settings) { $this->failedValidationRedirect = route('conversations.read', ['id' => $id]); /** @var Conversation $conversation */ $conversation = $this->conversationRepository->find($id); if (!$conversation || !$conversation->participants->contains($this->guard->user())) { throw new ConversationNotFoundException(); } $message = $this->conversationMessageRepository->addMessageToConversation($conversation, ['author_id' => $this->guard->user()->id, 'message' => $request->input('message')]); if ($message) { $page = 1; if ($settings->get('conversations.message_order', 'desc') == 'asc') { $page = (int) ($conversation->messages->count() / $settings->get('user.posts_per_page', 10)) + 1; } return redirect()->route('conversations.read', ['id' => $conversation->id, 'page' => $page]); } return redirect()->route('conversations.read', ['id' => $conversation->id])->withInput()->withErrors(['content' => 'Error']); }
/** * @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(); } }