/** * {@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 Post[] $posts * * @return Post */ public function mergePosts(array $posts) { if (!is_array_of($posts, 'MyBB\\Core\\Database\\Models\\Post')) { throw new \InvalidArgumentException('$posts must be an array of Post objects'); } $collection = new Collection($posts); $collection = $collection->sortBy('created_at'); $firstPost = $collection->shift(); $firstPostContent = $firstPost->content; foreach ($collection as $post) { if ($post->author->id !== $firstPost->author->id) { throw new \InvalidArgumentException("All posts being merged must have the same author"); } $firstPostContent .= "\n[hr]\n" . $post->content; $this->deletePost($post); } $firstPost->content = $firstPostContent; $firstPost->content_parsed = $this->formatter->parse($firstPost->content); $firstPost->save(); return $firstPost; }
/** * @param int $forumId * @param Request $request * @param MessageFormatter $formatter * * @return \Illuminate\View\View */ public function create($forumId, Request $request, MessageFormatter $formatter) { // Forum permissions are checked in "find" $forum = $this->forumRepository->find($forumId); if (!$forum) { throw new ForumNotFoundException(); } $this->breadcrumbs->setCurrentRoute('topics.create', $forum); $username = trans('general.guest'); $preview = null; if ($request->has('content')) { if (!$this->guard->check()) { $userId = null; $username = $request->get('username'); } else { $userId = $this->guard->user()->id; $username = $this->guard->user()->name; } $preview = new Post(['user_id' => $userId, 'username' => $username, 'content' => $request->get('content'), 'content_parsed' => $formatter->parse($request->get('content'), [MessageFormatter::ME_USERNAME => $this->guard->user()->name]), 'created_at' => new \DateTime()]); } return view('topic.create', compact('forum', 'preview', 'username')); }
/** * @param int $id * @param Request $request * @param MessageFormatter $formatter * * @return \Illuminate\View\View */ public function getRead($id, Request $request, MessageFormatter $formatter) { $conversation = $this->conversationRepository->find($id); if (!$conversation || !$conversation->participants->contains($this->guard->user())) { throw new ConversationNotFoundException(); } Breadcrumbs::setCurrentRoute('conversations.read', $conversation); $this->conversationRepository->updateLastRead($conversation, $this->guard->user()); // Load the participants here as we're changing them above and we want to avoid caching issues $conversation->load('participants'); $messages = $this->conversationMessageRepository->getAllForConversation($conversation); $preview = null; if ($request->has('message')) { $preview = new ConversationMessage(['author_id' => $this->guard->user()->id, 'message' => $request->get('message'), 'message_parsed' => $formatter->parse($request->get('message'), [MessageFormatter::ME_USERNAME => $this->guard->user()->name]), 'created_at' => new \DateTime()]); } return view('conversation.show', compact('conversation', 'messages', 'preview')); }