This commit is contained in:
Renzo 2016-07-30 02:57:59 -05:00
parent bf96cc0e28
commit eb78266bee

View File

@ -15,7 +15,7 @@ class PostController extends ApplicationController
public function upload() public function upload()
{ {
$this->set_title('Upload'); $this->set_title('Upload');
$this->deleted_posts = FlaggedPostDetail::new_deleted_posts(current_user()); $this->deleted_posts = FlaggedPostDetail::new_deleted_posts(current_user());
# if $this->params()->url # if $this->params()->url
# $this->post = Post.find(:first, 'conditions' => ["source = ?", $this->params()->url]) # $this->post = Post.find(:first, 'conditions' => ["source = ?", $this->params()->url])
@ -48,9 +48,9 @@ class PostController extends ApplicationController
} else { } else {
$user_id = current_user()->id; $user_id = current_user()->id;
} }
// $is_upload = array_key_exists('post', $_FILES); // $is_upload = array_key_exists('post', $_FILES);
# iTODO # iTODO
$post_params = array_merge($this->params()->post ?: array(), array( $post_params = array_merge($this->params()->post ?: array(), array(
'updater_user_id' => current_user()->id, 'updater_user_id' => current_user()->id,
@ -65,9 +65,9 @@ class PostController extends ApplicationController
// 'tempfile_name' => $is_upload ? $_FILES['post']['name']['file'] : null, // 'tempfile_name' => $is_upload ? $_FILES['post']['name']['file'] : null,
// 'is_upload' => $is_upload, // 'is_upload' => $is_upload,
)); ));
$this->post = Post::create($post_params); $this->post = Post::create($post_params);
if ($this->post->errors()->blank()) { if ($this->post->errors()->blank()) {
if ($this->params()->md5 && $this->post->md5 != strtolower($this->params()->md5)) { if ($this->params()->md5 && $this->post->md5 != strtolower($this->params()->md5)) {
$this->post->destroy(); $this->post->destroy();
@ -95,7 +95,7 @@ class PostController extends ApplicationController
} }
} elseif ($this->post->errors()->on('md5')) { } elseif ($this->post->errors()->on('md5')) {
$p = Post::where(['md5' => $this->post->md5])->first(); $p = Post::where(['md5' => $this->post->md5])->first();
$update = array('tags' => $p->cached_tags . " " . (isset($this->params()->post['tags']) ? $this->params()->post['tags'] : ''), 'updater_user_id' => $this->session()->user_id, 'updater_ip_addr' => $this->request()->remoteIp()); $update = array('tags' => $p->cached_tags . " " . (isset($this->params()->post['tags']) ? $this->params()->post['tags'] : ''), 'updater_user_id' => $this->session()->user_id, 'updater_ip_addr' => $this->request()->remoteIp());
if (!$p->source && $this->post->source) if (!$p->source && $this->post->source)
$update['source'] = $this->post->source; $update['source'] = $this->post->source;
@ -114,36 +114,36 @@ class PostController extends ApplicationController
public function moderate() public function moderate()
{ {
$this->set_title('Moderation Queue'); $this->set_title('Moderation Queue');
if ($this->request()->isPost()) { if ($this->request()->isPost()) {
$posts = new Rails\ActiveRecord\Collection(); $posts = new Rails\ActiveRecord\Collection();
if ($this->params()->ids) { if ($this->params()->ids) {
foreach (array_keys($this->params()->ids) as $post_id) { foreach (array_keys($this->params()->ids) as $post_id) {
$post = Post::find($post_id); $post = Post::find($post_id);
if ($this->params()->commit == "Approve") if ($this->params()->commit == "Approve")
$post->approve(current_user()->id); $post->approve(current_user()->id);
elseif ($this->params()->commit == "Delete") { elseif ($this->params()->commit == "Delete") {
$post->destroy_with_reason(($this->params()->reason ? $this->params()->reason : $this->params()->reason2), current_user()); $post->destroy_with_reason(($this->params()->reason ? $this->params()->reason : $this->params()->reason2), current_user());
# Include post data for the parent: deleted posts aren't counted as children, so # Include post data for the parent: deleted posts aren't counted as children, so
# their has_children attribute may change. # their has_children attribute may change.
if ($post->parent_id) if ($post->parent_id)
$posts[] = $post->get_parent(); $posts[] = $post->get_parent();
} }
# Post may have been permanently deleted. # Post may have been permanently deleted.
if (!CONFIG()->delete_posts_permanently) { if (!CONFIG()->delete_posts_permanently) {
$post->reload(); $post->reload();
} }
$posts[] = $post; $posts[] = $post;
} }
} }
$posts->unique(); $posts->unique();
if ($this->request()->format() == "json" || $this->request()->format() == "xml") if ($this->request()->format() == "json" || $this->request()->format() == "xml")
$api_data = Post::batch_api_data($posts->members()); $api_data = Post::batch_api_data($posts->members());
else else
@ -153,7 +153,7 @@ class PostController extends ApplicationController
$this->respond_to_success("Post approved", "#moderate", array('api' => $api_data)); $this->respond_to_success("Post approved", "#moderate", array('api' => $api_data));
elseif ($this->params()->commit == "Delete") elseif ($this->params()->commit == "Delete")
$this->respond_to_success("Post deleted", "#moderate", array('api' => $api_data)); $this->respond_to_success("Post deleted", "#moderate", array('api' => $api_data));
} else { } else {
if ($this->params()->query) { if ($this->params()->query) {
list($sql, $params) = Post::generate_sql($this->params()->query, array('pending' => true, 'order' => "id desc")); list($sql, $params) = Post::generate_sql($this->params()->query, array('pending' => true, 'order' => "id desc"));
@ -175,13 +175,13 @@ class PostController extends ApplicationController
return; return;
} }
$user_id = current_user()->id; $user_id = current_user()->id;
$post = $this->params()->post; $post = $this->params()->post;
Post::filter_api_changes($post); Post::filter_api_changes($post);
$post['updater_user_id'] = current_user()->id; $post['updater_user_id'] = current_user()->id;
$post['updater_ip_addr'] = $this->request()->remoteIp(); $post['updater_ip_addr'] = $this->request()->remoteIp();
if ($this->post->updateAttributes($post)) { if ($this->post->updateAttributes($post)) {
# Reload the post to send the new status back; not all changes will be reflected in # Reload the post to send the new status back; not all changes will be reflected in
# $this->post due to after_save changes. # $this->post due to after_save changes.
@ -200,7 +200,7 @@ class PostController extends ApplicationController
public function updateBatch() public function updateBatch()
{ {
$user_id = current_user()->id; $user_id = current_user()->id;
$ids = array(); $ids = array();
if (!is_array($this->params()->post)) if (!is_array($this->params()->post))
$this->params()->post = []; $this->params()->post = [];
@ -217,7 +217,7 @@ class PostController extends ApplicationController
$p = Post::find($post_id); $p = Post::find($post_id);
$ids[] = $p->id; $ids[] = $p->id;
# If an entry has only an ID, it was just included in the list to receive changes to # If an entry has only an ID, it was just included in the list to receive changes to
# a post without changing it (for example, to receive the parent's data after reparenting # a post without changing it (for example, to receive the parent's data after reparenting
# a post under it). # a post under it).
@ -226,7 +226,7 @@ class PostController extends ApplicationController
$old_parent_id = $p->parent_id; $old_parent_id = $p->parent_id;
Post::filter_api_changes($post); Post::filter_api_changes($post);
if ($p->updateAttributes(array_merge($post, array('updater_user_id' => $user_id, 'updater_ip_addr' => $this->request()->remoteIp())))) { if ($p->updateAttributes(array_merge($post, array('updater_user_id' => $user_id, 'updater_ip_addr' => $this->request()->remoteIp())))) {
// post.merge(:updater_user_id => user_id, :updater_ip_addr => request.remoteIp)) // post.merge(:updater_user_id => user_id, :updater_ip_addr => request.remoteIp))
# Reload the post to send the new status back; not all changes will be reflected in # Reload the post to send the new status back; not all changes will be reflected in
@ -261,7 +261,7 @@ class PostController extends ApplicationController
else else
$this->post_parent = null; $this->post_parent = null;
} }
public function destroy() public function destroy()
{ {
if ($this->params()->commit == "Cancel") { if ($this->params()->commit == "Cancel") {
@ -290,7 +290,7 @@ class PostController extends ApplicationController
$options = ['api' => Post::batch_api_data([$this->post])]; $options = ['api' => Post::batch_api_data([$this->post])];
else else
$options = []; $options = [];
$this->respond_to_success($notice, array('#show', 'id' => $this->params()->id), $options); $this->respond_to_success($notice, array('#show', 'id' => $this->params()->id), $options);
} }
} else { } else {
@ -305,13 +305,13 @@ class PostController extends ApplicationController
} }
$page = $this->page_number(); $page = $this->page_number();
$query = Post::order("flagged_post_details.created_at DESC") $query = Post::order("flagged_post_details.created_at DESC")
->select('posts.*') ->select('posts.*')
->group('posts.id') ->group('posts.id')
->joins("JOIN flagged_post_details ON flagged_post_details.post_id = posts.id") ->joins("JOIN flagged_post_details ON flagged_post_details.post_id = posts.id")
->page($page)->perPage(25); ->page($page)->perPage(25);
if ($this->params()->user_id) { if ($this->params()->user_id) {
$user_id = (int)$this->params()->user_id; $user_id = (int)$this->params()->user_id;
$this->posts = $query->where("posts.status = 'deleted' AND posts.user_id = ? ", $user_id)->paginate(); $this->posts = $query->where("posts.status = 'deleted' AND posts.user_id = ? ", $user_id)->paginate();
@ -319,14 +319,14 @@ class PostController extends ApplicationController
$this->posts = $query->where("posts.status = 'deleted'")->paginate(); $this->posts = $query->where("posts.status = 'deleted'")->paginate();
} }
} }
public function acknowledgeNewDeletedPosts() public function acknowledgeNewDeletedPosts()
{ {
if (!current_user()->is_anonymous()) if (!current_user()->is_anonymous())
current_user()->updateAttribute('last_deleted_post_seen_at', date('Y-m-d H:i:s')); current_user()->updateAttribute('last_deleted_post_seen_at', date('Y-m-d H:i:s'));
$this->respond_to_success("Success", array()); $this->respond_to_success("Success", array());
} }
public function index() public function index()
{ {
$tags = $this->params()->tags; $tags = $this->params()->tags;
@ -334,7 +334,7 @@ class PostController extends ApplicationController
$page = $this->page_number(); $page = $this->page_number();
$this->tag_suggestions = $this->searching_pool = array(); $this->tag_suggestions = $this->searching_pool = array();
/* if $this->current_user.is_member_or_lower? && count(split_tags) > 2 /* if $this->current_user.is_member_or_lower? && count(split_tags) > 2
# $this->respond_to_error("You can only search up to two tags at once with a basic account", 'action' => "error") # $this->respond_to_error("You can only search up to two tags at once with a basic account", 'action' => "error")
# return; # return;
# elseif count(split_tags) > 6 # elseif count(split_tags) > 6
@ -352,9 +352,9 @@ class PostController extends ApplicationController
$limit > 1000 && $limit = 1000; $limit > 1000 && $limit = 1000;
$count = 0; $count = 0;
$this->set_title("/" . str_replace("_", " ", $tags)); $this->set_title("/" . str_replace("_", " ", $tags));
// try { // try {
$count = Post::fast_count($tags); $count = Post::fast_count($tags);
// vde($count); // vde($count);
@ -363,7 +363,7 @@ class PostController extends ApplicationController
// return; // return;
// } // }
$this->ambiguous_tags = Tag::select_ambiguous($split_tags); $this->ambiguous_tags = Tag::select_ambiguous($split_tags);
if (isset($q['pool']) and is_int($q['pool'])) if (isset($q['pool']) and is_int($q['pool']))
$this->searching_pool = Pool::where(['id' => $q['pool']])->first(); $this->searching_pool = Pool::where(['id' => $q['pool']])->first();
@ -372,13 +372,13 @@ class PostController extends ApplicationController
// $this->posts = Post::find_all(array('page' => $page, 'per_page' => $limit, $count)); // $this->posts = Post::find_all(array('page' => $page, 'per_page' => $limit, $count));
// $this->posts = WillPaginate::Collection.new(page, limit, count); // $this->posts = WillPaginate::Collection.new(page, limit, count);
// $offset = $this->posts->offset(); // $offset = $this->posts->offset();
// $posts_to_load = $this->posts->per_page(); // $posts_to_load = $this->posts->per_page();
$per_page = $limit; $per_page = $limit;
$offset = ($page - 1) * $per_page; $offset = ($page - 1) * $per_page;
$posts_to_load = $per_page; $posts_to_load = $per_page;
if (!$from_api) { if (!$from_api) {
# For forward preloading: # For forward preloading:
// $posts_to_load += $this->posts->per_page(); // $posts_to_load += $this->posts->per_page();
@ -398,16 +398,16 @@ class PostController extends ApplicationController
$this->showing_holds_only = isset($q['show_holds']) && $q['show_holds'] == 'only'; $this->showing_holds_only = isset($q['show_holds']) && $q['show_holds'] == 'only';
list ($sql, $params) = Post::generate_sql($q, array('original_query' => $tags, 'from_api' => $from_api, 'order' => "p.id DESC", 'offset' => $offset, 'limit' => $posts_to_load)); list ($sql, $params) = Post::generate_sql($q, array('original_query' => $tags, 'from_api' => $from_api, 'order' => "p.id DESC", 'offset' => $offset, 'limit' => $posts_to_load));
$results = Post::findBySql($sql, $params); $results = Post::findBySql($sql, $params);
$this->preload = new Rails\ActiveRecord\Collection(); $this->preload = new Rails\ActiveRecord\Collection();
if (!$from_api) { if (!$from_api) {
if ($page && $page > 1) { if ($page && $page > 1) {
$this->preload = $results->slice(0, $limit); $this->preload = $results->slice(0, $limit);
$results = $results->slice($limit); $results = $results->slice($limit);
} }
$this->preload->merge($results->slice($limit)); $this->preload->merge($results->slice($limit));
$results = $results->slice(0, $limit); $results = $results->slice(0, $limit);
@ -425,7 +425,7 @@ class PostController extends ApplicationController
return; return;
} }
$this->posts = new Rails\ActiveRecord\Collection($results->members(), ['page' => $page, 'perPage' => $per_page, 'totalRows' => $count]); $this->posts = new Rails\ActiveRecord\Collection($results->members(), ['page' => $page, 'perPage' => $per_page, 'totalRows' => $count]);
if ($count < CONFIG()->post_index_default_limit && count($split_tags) == 1) { if ($count < CONFIG()->post_index_default_limit && count($split_tags) == 1) {
$this->tag_suggestions = Tag::find_suggestions($tags); $this->tag_suggestions = Tag::find_suggestions($tags);
} }
@ -448,21 +448,21 @@ class PostController extends ApplicationController
$this->render(array('json' => array_map(function($p){return $p->api_attributes();}, $this->posts->members()))); $this->render(array('json' => array_map(function($p){return $p->api_attributes();}, $this->posts->members())));
return; return;
} }
$api_data = Post::batch_api_data($this->posts->members(), array( $api_data = Post::batch_api_data($this->posts->members(), array(
'exclude_tags' => $this->params()->include_tags != "1", 'exclude_tags' => $this->params()->include_tags != "1",
'exclude_votes' => $this->params()->include_votes != "1", 'exclude_votes' => $this->params()->include_votes != "1",
'exclude_pools' => $this->params()->include_pools != "1", 'exclude_pools' => $this->params()->include_pools != "1",
'fake_sample_url' => CONFIG()->fake_sample_url 'fake_sample_url' => CONFIG()->fake_sample_url
)); ));
$this->render(array('json' => json_encode($api_data))); $this->render(array('json' => json_encode($api_data)));
} }
// , // ,
// 'atom' // 'atom'
)); ));
} }
// private function is_mobile_browser() // private function is_mobile_browser()
// { // {
// if ($agent = $this->request()->get("HTTP_USER_AGENT")) { // if ($agent = $this->request()->get("HTTP_USER_AGENT")) {
@ -499,28 +499,28 @@ class PostController extends ApplicationController
public function show() public function show()
{ {
$this->helper('Avatar'); $this->helper('Avatar');
try { try {
if ($this->params()->cache) if ($this->params()->cache)
$this->response()->headers()->add("Cache-Control", "max-age=300"); $this->response()->headers()->add("Cache-Control", "max-age=300");
$this->cache = $this->params()->cache; # temporary $this->cache = $this->params()->cache; # temporary
$this->body_only = (int)$this->params()->body == 1; $this->body_only = (int)$this->params()->body == 1;
if ($this->params()->md5) { if ($this->params()->md5) {
if (!$this->post = Post::where(['md5' => strtolower($this->params())])->first()) if (!$this->post = Post::where(['md5' => strtolower($this->params())])->first())
throw Rails\ActiveRecord\Exception\RecordNotFoundException(); throw Rails\ActiveRecord\Exception\RecordNotFoundException();
} else { } else {
$this->post = Post::find($this->params()->id); $this->post = Post::find($this->params()->id);
} }
$this->pools = Pool::where("pools_posts.post_id = {$this->post->id} AND pools_posts.active")->joins("JOIN pools_posts ON pools_posts.pool_id = pools.id")->order("pools.name")->select("pools.name, pools.id")->take(); $this->pools = Pool::where("pools_posts.post_id = {$this->post->id} AND pools_posts.active")->joins("JOIN pools_posts ON pools_posts.pool_id = pools.id")->order("pools.name")->select("pools.name, pools.id")->take();
if ($this->params()->pool_id) { if ($this->params()->pool_id) {
$this->following_pool_post = PoolPost::where("pool_id = ? AND post_id = ?", $this->params()->pool_id, $this->post->id)->first(); $this->following_pool_post = PoolPost::where("pool_id = ? AND post_id = ?", $this->params()->pool_id, $this->post->id)->first();
} else { } else {
$this->following_pool_post = PoolPost::where("post_id = ?", $this->post->id)->first(); $this->following_pool_post = PoolPost::where("post_id = ?", $this->post->id)->first();
} }
$this->tags = array('include' => $this->post->tags()); $this->tags = array('include' => $this->post->tags());
$this->include_tag_reverse_aliases = true; $this->include_tag_reverse_aliases = true;
$this->set_title(str_replace('_', ' ', $this->post->title_tags())); $this->set_title(str_replace('_', ' ', $this->post->title_tags()));
@ -570,12 +570,11 @@ class PostController extends ApplicationController
} }
$this->post_params = $this->params()->all(); $this->post_params = $this->params()->all();
$end = time();
$this->start = strtotime('-'.$period); $this->start = strtotime('-'.$period);
$this->set_title('Exploring ' . $this->period_name); $this->set_title('Exploring ' . $this->period_name);
$this->posts = Post::where("status <> 'deleted' AND posts.index_timestamp >= ? AND posts.index_timestamp <= ? ", date('Y-m-d', $this->start), date('Y-m-d', $end))->order("score DESC")->limit(20)->take(); $this->posts = Post::where("status <> 'deleted' AND posts.index_timestamp >= ? AND posts.index_timestamp <= ? ", date('Y-m-d', $this->start), date('Y-m-d H:i:s'))->order("score DESC")->limit(20)->take();
$this->respond_to_list("posts"); $this->respond_to_list("posts");
} }
@ -588,9 +587,9 @@ class PostController extends ApplicationController
} }
$this->set_title('Exploring '.date('Y', $this->day).'/'.date('m', $this->day).'/'.date('d', $this->day)); $this->set_title('Exploring '.date('Y', $this->day).'/'.date('m', $this->day).'/'.date('d', $this->day));
$this->posts = Post::available()->where('created_at BETWEEN ? AND ?', date('Y-m-d', $this->day), date('Y-m-d', strtotime('+1 day', $this->day)))->order("score DESC")->limit(20)->take(); $this->posts = Post::available()->where('created_at BETWEEN ? AND ?', date('Y-m-d', $this->day), date('Y-m-d', strtotime('+1 day', $this->day)))->order("score DESC")->limit(20)->take();
$this->respond_to_list("posts"); $this->respond_to_list("posts");
} }
@ -602,7 +601,7 @@ class PostController extends ApplicationController
} }
$this->end = strtotime('next week', $this->start); $this->end = strtotime('next week', $this->start);
$this->set_title('Exploring '.date('Y', $this->start).'/'.date('m', $this->start).'/'.date('d', $this->start) . ' - '.date('Y', $this->end).'/'.date('m', $this->end).'/'.date('d', $this->end)); $this->set_title('Exploring '.date('Y', $this->start).'/'.date('m', $this->start).'/'.date('d', $this->start) . ' - '.date('Y', $this->end).'/'.date('m', $this->end).'/'.date('d', $this->end));
$this->posts = Post::available()->where('created_at BETWEEN ? AND ?', date('Y-m-d', $this->start), date('Y-m-d', $this->end))->order('score DESC')->limit(20)->take(); $this->posts = Post::available()->where('created_at BETWEEN ? AND ?', date('Y-m-d', $this->start), date('Y-m-d', $this->end))->order('score DESC')->limit(20)->take();
@ -617,7 +616,7 @@ class PostController extends ApplicationController
} }
$this->end = strtotime('+1 month', $this->start); $this->end = strtotime('+1 month', $this->start);
$this->set_title('Exploring '.date('Y', $this->start).'/'.date('m', $this->start)); $this->set_title('Exploring '.date('Y', $this->start).'/'.date('m', $this->start));
$this->posts = Post::available()->where('created_at BETWEEN ? AND ?', date('Y-m-d', $this->start), date('Y-m-d', $this->end))->order('score DESC')->limit(20)->take(); $this->posts = Post::available()->where('created_at BETWEEN ? AND ?', date('Y-m-d', $this->start), date('Y-m-d', $this->end))->order('score DESC')->limit(20)->take();
@ -642,7 +641,7 @@ class PostController extends ApplicationController
$this->respond_to_success("", array(), array('vote' => $score)); $this->respond_to_success("", array(), array('vote' => $score));
return; return;
} }
$p = Post::find($this->params()->id); $p = Post::find($this->params()->id);
$score = (int)$this->params()->score; $score = (int)$this->params()->score;
@ -669,7 +668,7 @@ class PostController extends ApplicationController
if ($this->params()->unflag == '1') { if ($this->params()->unflag == '1') {
# Allow the user who flagged a post to unflag it. # Allow the user who flagged a post to unflag it.
# #
# posts # posts
# "approve" is used both to mean "unflag post" and "approve pending post". # "approve" is used both to mean "unflag post" and "approve pending post".
if ($post->status != "flagged") { if ($post->status != "flagged") {
$this->respond_to_error("Can only unflag flagged posts", array("#show", 'id' => $this->params()->id)); $this->respond_to_error("Can only unflag flagged posts", array("#show", 'id' => $this->params()->id));
@ -709,13 +708,13 @@ class PostController extends ApplicationController
foreach(range(1, 10) as $i) { foreach(range(1, 10) as $i) {
$post = Post::where("id = ? AND status <> 'deleted'", rand(1, $max_id) + 1)->first(); $post = Post::where("id = ? AND status <> 'deleted'", rand(1, $max_id) + 1)->first();
if ($post && $post->can_be_seen_by(current_user())) { if ($post && $post->can_be_seen_by(current_user())) {
$this->redirectTo(array('#show', 'id' => $post->id, 'tag_title' => $post->tag_title())); $this->redirectTo(array('#show', 'id' => $post->id, 'tag_title' => $post->tag_title()));
return; return;
} }
} }
$this->notice("Couldn't find a post in 10 tries. Try again."); $this->notice("Couldn't find a post in 10 tries. Try again.");
$this->redirectTo("#index"); $this->redirectTo("#index");
} }
@ -734,10 +733,10 @@ class PostController extends ApplicationController
'width' => null, 'width' => null,
'height' => null 'height' => null
], $this->params()->toArray()); ], $this->params()->toArray());
if (!empty($params['data_search']) && !current_user()->is_mod_or_higher()) if (!empty($params['data_search']) && !current_user()->is_mod_or_higher())
unset($params['data_search']); unset($params['data_search']);
if (!SimilarImages::valid_saved_search($params['search_id'])) $params['search_id'] = null; if (!SimilarImages::valid_saved_search($params['search_id'])) $params['search_id'] = null;
if (!empty($params['width'])) $params['width'] = (int)$params['width']; if (!empty($params['width'])) $params['width'] = (int)$params['width'];
if (!empty($params['height'])) $params['height'] = (int)$params['height']; if (!empty($params['height'])) $params['height'] = (int)$params['height'];
@ -748,18 +747,18 @@ class PostController extends ApplicationController
} }
$this->services = SimilarImages::get_services($params['services']); $this->services = SimilarImages::get_services($params['services']);
if ($this->params()->id) { if ($this->params()->id) {
$this->compared_post = Post::find($this->params()->id); $this->compared_post = Post::find($this->params()->id);
} else { } else {
$this->compared_post = new Post(); $this->compared_post = new Post();
} }
$this->errors = null; $this->errors = null;
$this->posts = Post::emptyCollection(); $this->posts = Post::emptyCollection();
$this->similar = []; $this->similar = [];
$similarity = []; $similarity = [];
if ($this->compared_post && $this->compared_post->is_deleted()) { if ($this->compared_post && $this->compared_post->is_deleted()) {
$this->respond_to_error("Post deleted", ['post#show', 'id' => $this->params()->id, 'tag_title' => $this->compared_post->tag_title()]); $this->respond_to_error("Post deleted", ['post#show', 'id' => $this->params()->id, 'tag_title' => $this->compared_post->tag_title()]);
return; return;
@ -846,11 +845,11 @@ class PostController extends ApplicationController
return SimilarImages::similar_images($options); return SimilarImages::similar_images($options);
}; };
$this->searched = false; $this->searched = false;
if ($this->params()->url || $this->params()->id || (!empty($_FILES['file']) && empty($_FILES['file']['error'])) || !empty($this->params()->search_id)) { if ($this->params()->url || $this->params()->id || (!empty($_FILES['file']) && empty($_FILES['file']['error'])) || !empty($this->params()->search_id)) {
$res = $search($params); $res = $search($params);
# Error when no service was selected and/or local search isn't supported # Error when no service was selected and/or local search isn't supported
if (is_string($res['errors'])) { if (is_string($res['errors'])) {
$this->notice($res['errors']); $this->notice($res['errors']);
@ -866,13 +865,13 @@ class PostController extends ApplicationController
$res = []; $res = [];
$this->errors = []; $this->errors = [];
} }
if ($res && $this->searched) { if ($res && $this->searched) {
!empty($res['posts']) && $this->posts = $res['posts']; !empty($res['posts']) && $this->posts = $res['posts'];
$this->similar = $res; $this->similar = $res;
!empty($res['similarity']) && $similarity = $res['similarity']; !empty($res['similarity']) && $similarity = $res['similarity'];
} }
if ($this->request()->format() == "json" || $this->request()->format() == "xml") { if ($this->request()->format() == "json" || $this->request()->format() == "xml") {
if (!empty($this->errors['error'])) { if (!empty($this->errors['error'])) {
$this->respond_to_error($this->errors['error'], ['#index'], ['status' => 503]); $this->respond_to_error($this->errors['error'], ['#index'], ['status' => 503]);
@ -931,7 +930,7 @@ class PostController extends ApplicationController
$api_data['source'] = $res['external_source']; $api_data['source'] = $res['external_source'];
else else
$api_data['source'] = ''; $api_data['source'] = '';
if (!empty($res['errors'])) { if (!empty($res['errors'])) {
$api_data['error'] = []; $api_data['error'] = [];
foreach ($res['errors'] as $server => $error) { foreach ($res['errors'] as $server => $error) {
@ -939,10 +938,10 @@ class PostController extends ApplicationController
$api_data['error'][] = [ 'server' => $server, 'message' => $error['message'], 'services' => $services ]; $api_data['error'][] = [ 'server' => $server, 'message' => $error['message'], 'services' => $services ];
} }
} }
$this->respond_to_success('', [], ['api' => $api_data]); $this->respond_to_success('', [], ['api' => $api_data]);
} }
// fmt.xml do // fmt.xml do
// x = Builder::XmlMarkup.new('indent' => 2) // x = Builder::XmlMarkup.new('indent' => 2)
// x.instruct! // x.instruct!
@ -975,19 +974,19 @@ class PostController extends ApplicationController
// } // }
// } // }
]); ]);
$this->params = $params; $this->params = $params;
} }
public function undelete() public function undelete()
{ {
$post = Post::where(['id' => $this->params()->id])->first(); $post = Post::where(['id' => $this->params()->id])->first();
if (!$post) { if (!$post) {
$this->respond_to_error("Post not found", ['#show', 'id' => $this->params()->id]); $this->respond_to_error("Post not found", ['#show', 'id' => $this->params()->id]);
return; return;
} }
$post->undelete(); $post->undelete();
$affected_posts = [$post]; $affected_posts = [$post];
@ -1008,7 +1007,7 @@ class PostController extends ApplicationController
{ {
throw new Exception(); throw new Exception();
} }
public function import() public function import()
{ {
$import_dir = Rails::publicPath() . '/data/import/'; $import_dir = Rails::publicPath() . '/data/import/';
@ -1024,18 +1023,18 @@ class PostController extends ApplicationController
$error = true; $error = true;
} }
} }
$resp = !empty($error) ? array('reason' => 'Some files could not be deleted') : array('success' => true); $resp = !empty($error) ? array('reason' => 'Some files could not be deleted') : array('success' => true);
$this->render(array('json' => $resp)); $this->render(array('json' => $resp));
return; return;
} }
$this->setLayout(false); $this->setLayout(false);
$this->errors = $this->dupe = false; $this->errors = $this->dupe = false;
$post = $this->params()->post; $post = $this->params()->post;
$post['filename'] = stripslashes(utf8_decode($post['filename'])); $post['filename'] = stripslashes(utf8_decode($post['filename']));
$filepath = $import_dir . $post['filename']; $filepath = $import_dir . $post['filename'];
# Take folders as tags # Take folders as tags
if (is_int(strpos($post['filename'], '/'))) { if (is_int(strpos($post['filename'], '/'))) {
$folders = str_replace('#', ':', $post['filename']); $folders = str_replace('#', ':', $post['filename']);
@ -1043,7 +1042,7 @@ class PostController extends ApplicationController
array_pop($tags); array_pop($tags);
$post['tags'] = trim($post['tags'].' '.implode(' ', $tags)); $post['tags'] = trim($post['tags'].' '.implode(' ', $tags));
} }
$post = array_merge($post, array( $post = array_merge($post, array(
'ip_addr' => $this->request()->remoteIp(), 'ip_addr' => $this->request()->remoteIp(),
'user_id' => current_user()->id, 'user_id' => current_user()->id,
@ -1053,9 +1052,9 @@ class PostController extends ApplicationController
'is_import' => true, 'is_import' => true,
)); ));
unset($post['filename'], $post['i']); unset($post['filename'], $post['i']);
$this->post = Post::create($post); $this->post = Post::create($post);
if ($this->post->errors()->blank()) { if ($this->post->errors()->blank()) {
$this->import_status = 'Posted'; $this->import_status = 'Posted';
} elseif ($this->post->errors()->invalid('md5')) { } elseif ($this->post->errors()->invalid('md5')) {
@ -1070,11 +1069,11 @@ class PostController extends ApplicationController
} else { } else {
$this->set_title('Import'); $this->set_title('Import');
$this->invalid_files = $this->files = []; $this->invalid_files = $this->files = [];
list($this->files, $this->invalid_files, $this->invalid_folders) = Post::get_import_files($import_dir); list($this->files, $this->invalid_files, $this->invalid_folders) = Post::get_import_files($import_dir);
$pools = Pool::where('is_active')->take(); $pools = Pool::where('is_active')->take();
if ($pools) { if ($pools) {
$this->pool_list = '<datalist id="pool_list">'; $this->pool_list = '<datalist id="pool_list">';
foreach ($pools as $pool) foreach ($pools as $pool)
@ -1084,12 +1083,12 @@ class PostController extends ApplicationController
$this->pool_list = null; $this->pool_list = null;
} }
} }
public function searchExternalData() public function searchExternalData()
{ {
if (!CONFIG()->enable_find_external_data) if (!CONFIG()->enable_find_external_data)
throw new Rails\ActiveRecord\Exception\RecordNotFoundException(); throw new Rails\ActiveRecord\Exception\RecordNotFoundException();
if ($this->params()->ids) { if ($this->params()->ids) {
$ids = $this->params()->ids; $ids = $this->params()->ids;
!is_array($ids) && $ids = [$ids]; !is_array($ids) && $ids = [$ids];
@ -1097,7 +1096,7 @@ class PostController extends ApplicationController
} else { } else {
$this->posts = new Rails\ActiveRecord\Collection(); $this->posts = new Rails\ActiveRecord\Collection();
} }
$this->services = SimilarImages::get_services('all'); $this->services = SimilarImages::get_services('all');
} }
@ -1124,7 +1123,7 @@ class PostController extends ApplicationController
{ {
$this->helper('Avatar', 'Wiki', 'Tag', 'Comment', 'Pool', 'Favorite', 'Advertisements'); $this->helper('Avatar', 'Wiki', 'Tag', 'Comment', 'Pool', 'Favorite', 'Advertisements');
} }
protected function filters() protected function filters()
{ {
return [ return [
@ -1145,4 +1144,4 @@ class PostController extends ApplicationController
] ]
]; ];
} }
} }