/** * @param Model $content * * @return mixed */ public function removeLikesForContent(Model $content) { $baseQuery = $this->likesModel->where('content_type', '=', get_class($content))->where('content_id', '=', $content->id); $likes = $baseQuery->get(); foreach ($likes as $like) { User::find($like->user_id)->decrement('num_likes_made'); } return $baseQuery->delete(); }
/** * Create a new topic * * @param array $details Details about the topic. * * @return mixed */ public function create(array $details = []) { $details = array_merge(['title' => '', 'forum_id' => 0, 'user_id' => $this->guard->user()->id, 'username' => null, 'first_post_id' => 0, 'last_post_id' => 0, 'views' => 0, 'num_posts' => 0, 'content' => ''], $details); $details['slug'] = $this->createSlugForTitle($details['title']); if ($details['user_id'] > 0) { $details['username'] = User::find($details['user_id'])->name; // TODO: Use User Repository! } else { $details['user_id'] = null; if ($details['username'] == trans('general.guest')) { $details['username'] = null; } } $topic = null; $this->dbManager->transaction(function () use($details, &$topic) { $topic = $this->topicModel->create(['title' => $details['title'], 'slug' => $details['slug'], 'forum_id' => $details['forum_id'], 'user_id' => $details['user_id'], 'username' => $details['username']]); $firstPost = $this->postRepository->addPostToTopic($topic, ['content' => $details['content'], 'username' => $details['username']]); $topic->update(['first_post_id' => $firstPost->id, 'last_post_id' => $firstPost->id, 'num_posts' => 1]); }); $topic->forum->increment('num_topics'); if ($topic->user_id > 0) { $topic->author->increment('num_topics'); } return $topic; }
/** * Find a single user by ID. * * @param int $id The ID of the user to find. * * @return mixed */ public function find($id = 0) { return $this->userModel->find($id); }
/** * Add a post to a topic. * * @param Topic $topic The topic to add a post to. * @param array $postDetails The details of the post to add. * * @return mixed */ public function addPostToTopic(Topic $topic, array $postDetails) { $postDetails = array_merge(['user_id' => $this->guard->user()->id, 'username' => null, 'content' => '', 'content_parsed' => ''], $postDetails); $postDetails['content_parsed'] = $this->formatter->parse($postDetails['content'], [MessageFormatter::ME_USERNAME => $this->guard->user()->name]); // TODO: Parser options... if ($postDetails['user_id'] > 0) { $postDetails['username'] = User::find($postDetails['user_id'])->name; } else { $postDetails['user_id'] = null; if ($postDetails['username'] == trans('general.guest')) { $postDetails['username'] = null; } } $post = $topic->posts()->create($postDetails); if ($post !== false) { $topic->increment('num_posts'); $topic->update(['last_post_id' => $post['id']]); $topic->forum->increment('num_posts'); $topic->forum->update(['last_post_id' => $post->id, 'last_post_user_id' => $postDetails['user_id']]); } if ($post->user_id > 0) { $post->author->increment('num_posts'); } return $post; }