/** * Determine if the user is authorized to update a given post * * a post can be edited at any time by * - topic moderator * - bbs administrator * - the creator of the post within X seconds if that post is the most recent in the topic * * @return bool */ public function authorize() { $return = false; // specify which form we are dealing with to separate // the errors in the form // @todo: this seems like a dumb method $this->session()->flash('postForm', $this::input('id')); if (!\Auth::check()) { $return = false; } $post = \Nexus\Post::findOrFail($this::input('id')); // is this the most recent post in this topic, is it by the logged in user and is it recent $latestPost = $post->topic->posts->last(); if ($post['id'] == $latestPost['id'] && $post->author->id == \Auth::user()->id && $post->time->diffInSeconds() <= config('nexus.recent_edit')) { $return = true; } // is the auth user a moderator of the current section if ($post->topic->section->moderator->id == \Auth::id()) { $return = true; } // is the auth user an administrator of the bbs if (\Auth::user()->administrator) { $return = true; } return $return; }
/** * Display the specified resource. * * @param int $id * @return Response */ public function show($topic_id) { $posts = \Nexus\Post::with('author')->where('topic_id', $topic_id)->orderBy('id', 'dsc'); $topic = \Nexus\Topic::findOrFail($topic_id); // is this topic readonly to the authenticated user? $readonly = true; if ($topic->readonly == false) { $readonly = false; } if ($topic->section->moderator->id === \Auth::user()->id) { $readonly = false; } if (\Auth::user()->administrator) { $readonly = false; } // is this topic secret to the authenticated user? $userCanSeeSecrets = false; if ($topic->section->moderator->id === \Auth::user()->id) { $userCanSeeSecrets = true; } if (\Auth::user()->administrator) { $userCanSeeSecrets = true; } // get the previously read progress so we can indicate this in the view $readProgress = \Nexus\Helpers\ViewHelper::getReadProgress(\Auth::user(), $topic); // get the subscription status $topicStatus = \Nexus\Helpers\ViewHelper::getTopicStatus(\Auth::user(), $topic); $unsubscribed = $topicStatus['unsubscribed']; \Nexus\Helpers\ViewHelper::updateReadProgress(\Auth::user(), $topic); \Nexus\Helpers\ActivityHelper::updateActivity(\Auth::user()->id, "Reading <em>{$topic->title}</em>", action('Nexus\\TopicController@show', ['id' => $topic->id])); $breadcrumbs = \Nexus\Helpers\BreadcrumbHelper::breadcrumbForTopic($topic); return view('topics.index', compact('topic', 'posts', 'readonly', 'userCanSeeSecrets', 'readProgress', 'breadcrumbs', 'unsubscribed')); }
public function getMostRecentPostTimeAttribute() { $result = false; $latestPost = Post::select('time')->where('topic_id', $this->id)->orderBy('time', 'dec')->first(); if ($latestPost) { $result = $latestPost->time; } return $result; }
public function getMostRecentPostAttribute() { $post = null; $topicIDs = Topic::select('id')->where('section_id', $this->id)->get()->toArray(); $postID = Post::select('id')->whereIn('topic_id', $topicIDs)->orderBy('id', 'desc')->get()->first(); if ($postID) { $post = Post::find($postID->id); } return $post; }
/** * perform a search against all the posts and * return some results * @todo - ignore word order * @todo - remove stop words * @todo - deal with exact phrases */ public function find($text) { $phraseSearch = false; $displaySearchResults = true; // if text is ^"(.*)"$ or ^'(.*)'$ then we are searching for a phrase $pattern = <<<'pattern' /^['|"](.*)['|"]$/ pattern; $matches = false; preg_match($pattern, $text, $matches); if (!$matches) { // set initial results as nothing $results = false; // look for all the words $rawSearchTerms = explode(' ', $text); $searchTerms = array(); // remove stop words here foreach ($rawSearchTerms as $word) { if (!in_array(strtolower($word), self::$stopWords)) { $searchTerms[] = $word; } } // dd($searchTerms); foreach ($searchTerms as $word) { // remove unwanted characters from the start and end // @todo this does not remove multiple unwanted commas etc $word = trim($word); if (strlen($word) !== 0) { if ($results) { $results = $results->where('text', 'like', "%{$word}%"); } else { // first where $results = \Nexus\Post::where('text', 'like', "%{$word}%"); } } } } else { $phrase = trim($matches[1]); $results = \Nexus\Post::where('text', 'like', "%{$phrase}%"); } if ($results) { $results->orderBy('time', 'desc'); } \Nexus\Helpers\ActivityHelper::updateActivity(\Auth::user()->id, "Searching", action('Nexus\\SearchController@index')); $breadcrumbs = \Nexus\Helpers\BreadcrumbHelper::breadcumbForUtility('Search'); return view('search.results', compact('results', 'breadcrumbs', 'text', 'displaySearchResults')); }
public function test_deleting_topic_soft_deletes_its_posts() { // GIVEN we have a topic with post $topic = factory(Topic::class, 1)->create(); factory(Post::class, 20)->create(['topic_id' => $topic->id]); // we have 1 topic with 20 posts $this->assertEquals(Topic::all()->count(), 1); $this->assertEquals(Post::where('topic_id', $topic->id)->count(), 20); // WHEN we archive the topic $topic->delete(); // THEN we have no topics and no posts $this->assertEquals(Topic::all()->count(), 0); $this->assertEquals(Post::all()->count(), 0); // BUT we have 1 trashed topic and 20 trashed posts $this->assertEquals(Topic::withTrashed()->count(), 1); $this->assertEquals(Post::withTrashed()->where('topic_id', $topic->id)->count(), 20); }
/** * Determine if the user is authorized to make this request. * * true if * user is the moderator of the topic * user is an administrator * * @todo * user is the author * post time is within XX sections * * @return bool */ public function authorize() { $return = false; $post = \Nexus\Post::findOrFail($this->post); try { if ($post->topic->section->moderator->id == \Auth::id()) { $return = true; } } catch (\Exception $e) { $return = false; \Log::error('Post Delete - attempt to delete post by non-moderator ' . $e); } // is the auth user an administrator of the bbs if (\Auth::user()->administrator) { $return = true; } return $return; }
private function migratePosts() { $this->info('Importing Posts'); if (!\Nexus\Post::first()) { $errorCount = 0; $count = \DB::select('select count(message_id) as count from messagetable')[0]->count; $this->line("Found {$count} posts"); $bar = $this->output->createProgressBar($count); \DB::table('messagetable')->chunk(1000, function ($posts) use(&$errorCount, &$count, &$bar) { foreach ($posts as $classicPost) { $newPost = new \Nexus\Post(); $newPost->id = $classicPost->message_id; $newPost->text = $classicPost->message_text; $newPost->topic_id = $classicPost->topic_id; $newPost->user_id = $classicPost->user_id; $newPost->title = $classicPost->message_title; $newPost->time = $classicPost->message_time; $newPost->popname = $classicPost->message_popname; $newPost->update_user_id = $classicPost->update_user_id; if ($classicPost->message_html === 'n') { $newPost->html = false; } else { $newPost->html = true; } try { $newPost->save(); $bar->advance(); } catch (\Exception $e) { $errorCount++; \Log::info('Nexus:upgrade - Failed to import post: ' . $e); } } }); $bar->finish(); if ($errorCount) { $this->error("\nFailed to import {$errorCount} posts. See log for details"); } $this->info("\nPosts Complete\n"); } else { $this->error('Upgrade: found existing posts - skipping Posts'); } }
public function recentTopics($maxresults = 10) { $latestPosts = \Nexus\Post::orderBy('id', 'desc')->take($maxresults)->get(['topic_id'])->groupBy('topic_id'); $topics = array(); foreach ($latestPosts as $topic) { $topics[] = $topic[0]->topic; } return $topics; }
/** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy(Requests\Post\DeleteRequest $request, $id) { // using forceDelete here because in this case we do not want a soft delete $post = \Nexus\Post::findOrFail($id); $topicID = $post->topic_id; $post->forceDelete(); return redirect()->route('topic.show', ['id' => $post->topic_id]); }