From d2de01100ac3432baa66597730c41027ae858de7 Mon Sep 17 00:00:00 2001 From: MrYummy Date: Fri, 2 Jun 2017 02:10:49 +0200 Subject: moved all search styling to CSS, removed friendly (but slow) URLs, moved WHERE strings into an array --- app/assets/javascripts/editor.js | 2 +- app/assets/stylesheets/style.css.scss | 11 ++- app/controllers/#forumthreads_controller.rb# | 106 +++++++++++++++++++++++++++ app/controllers/forumthreads_controller.rb | 6 +- app/controllers/users_controller.rb | 6 +- app/models/forumthread.rb | 21 ++++-- app/models/user.rb | 15 ++-- app/views/forums/show.html.erb | 3 +- app/views/forumthreads/index.html.erb | 44 ++++++----- app/views/forumthreads/search.html.erb | 4 +- 10 files changed, 163 insertions(+), 55 deletions(-) create mode 100644 app/controllers/#forumthreads_controller.rb# diff --git a/app/assets/javascripts/editor.js b/app/assets/javascripts/editor.js index c977571..4f4de5d 100644 --- a/app/assets/javascripts/editor.js +++ b/app/assets/javascripts/editor.js @@ -92,7 +92,7 @@ $(function() { // match up to 2 words (everything except some special characters) // each word can have up to 16 characters (up to 32 total) // words must be separated by a single space - match: /(^|\s)(([^!"§$%&\/()=?.,;+*@\s]{1,16} ?){0,1}[^!"§$%&\/()=?.,;+*@\s]{1,16})$/, + match: /(^|\s)([^!"§$%&\/()=?.,;+*@\s]{1,16})$/, search: function (text, callback, match) { console.log("Searching " + text); text = text.toLowerCase(); diff --git a/app/assets/stylesheets/style.css.scss b/app/assets/stylesheets/style.css.scss index 73ab11f..f8d1d8e 100644 --- a/app/assets/stylesheets/style.css.scss +++ b/app/assets/stylesheets/style.css.scss @@ -1049,18 +1049,17 @@ nav.pagination { } .searchfield { - margin:0px; height:40px; display: inline-block; - - .btn { + &.field { + width: 300px; + } + &.btn { margin: 4px 1px 0 0; - padding: 6px; cursor: default; color: #fff; - border: none; font-size: 12px; - line-height: normal; background: #4096ee; + width: 40px; } } diff --git a/app/controllers/#forumthreads_controller.rb# b/app/controllers/#forumthreads_controller.rb# new file mode 100644 index 0000000..1b43f15 --- /dev/null +++ b/app/controllers/#forumthreads_controller.rb# @@ -0,0 +1,106 @@ +class ForumthreadsController < ApplicationController + + before_filter :check_permission, only: [:show, :edit, :update, :destroy] + + def index + params[:id] = nil if params[:id] && !Forum.find_by(id: params[:id]) + + params.each {|k,v| params[k] = nil if v==""} + +c @threads = Forumthread.filter(current_user, params[:title], params[:content], params[:reply], params[:label], User.find_by(ign: params[:author].to_s.strip) || params[:author], params[:query], Forum.find_by(id: params[:id])) + .page(params[:page]).per(30) + end + def show + if params[:reverse] == "true" + @replies = @thread.replies.reverse_order.page(params[:page]) + else + @replies = @thread.replies.page(params[:page]) + end + end + + def edit + unless mod? || @thread.author.is?(current_user) + flash[:alert] = "You are not allowed to edit this thread!" + redirect_to @thread + end + end + + def new + @thread = Forumthread.new(forum: Forum.find(params[:forum])) + unless @thread.forum.can_write?(current_user) + flash[:alert] = "You are not allowed to write in this forum" + redirect_to forums_path + end + end + + def create + @thread = Forumthread.new(mod? ? thread_params([:sticky, :locked, :forum_id, :label_id]) : thread_params([:forum_id, :label_id])) + if @thread.forum.can_write?(current_user) + @thread.user_author = current_user + if @thread.save + @thread.send_new_mention_mail + flash[:notice] = "Thread created!" + redirect_to forumthread_path( @thread) + return + else + flash[:alert] = "Something went wrong while creating your thread." + render action: "new" + return + end + else + flash[:alert] = "You are not allowed to create a thread here!" + redirect_to @thread.forum + end + end + + def update + if mod? || @thread.author.is?(current_user) + @thread.user_editor = current_user + @thread.attributes = (mod? ? thread_params([:sticky, :locked, :forum_id, :label_id]) : thread_params) + old_content = @thread.content_was + if @thread.save + @thread.send_new_mention_mail(old_content) + redirect_to @thread, notice: 'Post has been updated.' + else + flash[:alert] = "There was a problem while updating the post" + render action: "edit" + end + else + flash[:alert] = "You are not allowed to edit this thread!" + redirect_to @thread + end + end + + def destroy + if mod? || @thread.author.is?(current_user) + if @thread.destroy + flash[:notice] = "Thread deleted!" + else + flash[:alert] = "There was a problem while deleting this thread" + end + else + flash[:alert] = "You are not allowed to delete this thread" + end + redirect_to @thread.forum + end + + def search + end + + private + + def check_permission + @thread = Forumthread.find(params[:id]) + unless @thread.can_read?(current_user) + flash[:alert] = "You are not allowed to view this thread" + redirect_to forums_path + end + end + + def thread_params(add = []) + a = [:title, :content] + a << :label_id if @thread && !@thread.locked? + a += add + params.require(:forumthread).permit(a) + end +end diff --git a/app/controllers/forumthreads_controller.rb b/app/controllers/forumthreads_controller.rb index 8827f89..81d420a 100644 --- a/app/controllers/forumthreads_controller.rb +++ b/app/controllers/forumthreads_controller.rb @@ -3,11 +3,11 @@ class ForumthreadsController < ApplicationController before_filter :check_permission, only: [:show, :edit, :update, :destroy] def index - params[:id] = nil if params[:id] && !Forum.find_by(id: params[:id]) + params[:forum] = nil if params[:forum] && !Forum.find_by(id: params[:forum]) - params.each {|k,v| params[k] = nil if v==""} + params.delete_if{|k,v| v.blank?} - @threads = Forumthread.filter(current_user, params[:title], params[:content], params[:reply], params[:label], User.find_by(ign: params[:author].to_s.strip) || params[:author], params[:query], Forum.find_by(id: params[:id])) + @threads = Forumthread.filter(current_user, params[:title].try(:slice, 0..255), params[:content].try(:slice, 0..255), params[:reply].try(:slice, 0..255), params[:label], User.find_by(ign: params[:author].to_s.strip) || params[:author], params[:query].try(:slice, 0..255), Forum.find_by(id: params[:forum])) .page(params[:page]).per(30) end def show diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 0a308c5..60011a2 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -7,10 +7,10 @@ class UsersController < ApplicationController before_filter :set_user, except: [:index, :new, :create, :lost_password, :reset_password, :suggestions] def index - params[:role] = nil if !Role.find_by(name: params[:role]) - params[:badge] = nil if !Badge.find_by(name: params[:badge]) + role = Role.find_by(name: params[:role]) unless role.try(:downcase) == "staff" + badge = Badge.find_by(name: params[:badge]) - @users = User.search(params[:search], params[:role], params[:badge]) + @users = User.search(params[:search], role, badge) @users = @users.order("roles.value desc", "confirmed desc", :name) unless params[:badge] @count = @users.size @users = @users.page(params[:page]).per(100) diff --git a/app/models/forumthread.rb b/app/models/forumthread.rb index 9650e28..f8efe97 100644 --- a/app/models/forumthread.rb +++ b/app/models/forumthread.rb @@ -73,11 +73,11 @@ class Forumthread < ActiveRecord::Base can_read = "COALESCE(forum_role_read.value, 0) <= ? AND COALESCE(forumgroup_role_read.value, 0) <= ?" # A user can view sticky threads in write-only forums without read permissions. sticky_can_write = "sticky = true AND (COALESCE(forum_role_write.value, 0) <= ? AND COALESCE(forumgroup_role_write.value, 0) <= ?)" - match = "MATCH (title, forumthreads.content) AGAINST (?) OR MATCH (threadreplies.content) AGAINST (?)" + match = ["MATCH (title, forumthreads.content) AGAINST (#{Forumthread.sanitize(order_phrase)})", "MATCH (threadreplies.content) AGAINST (#{Forumthread.sanitize(order_phrase)})", "MATCH (title, forumthreads.content) AGAINST (?) OR MATCH (threadreplies.content) AGAINST (?)", "MATCH (title) AGAINST (?)", "MATCH (forumthreads.content) AGAINST (?)", "MATCH (threadreplies.content) AGAINST (?)"] threads = forum.try(:forumthreads) || Forumthread - threads = threads.select("forumthreads.*", "(MATCH (title, forumthreads.content) AGAINST (#{Forumthread.sanitize(order_phrase)})) AS relevance", "(MATCH (threadreplies.content) AGAINST (#{Forumthread.sanitize(order_phrase)})) AS reply_rel") + threads = threads.select("forumthreads.*", "#{match[0]} AS relevance", "#{match[1]} AS reply_rel") threads = threads.joins(forum: :forumgroup) .joins("LEFT JOIN threadreplies ON forumthreads.id = threadreplies.forumthread_id") @@ -88,21 +88,26 @@ class Forumthread < ActiveRecord::Base threads = threads.where("forumthreads.user_author_id = ? OR (#{can_read}) OR (#{sticky_can_write})", user_id, role_value, role_value, role_value, role_value) if query - threads = threads.where("#{match}", query[0..99], query[0..99]) + threads = threads.where("#{match[2]}", query[0..99], query[0..99]) elsif [title, content, reply].any? - threads = threads.where("MATCH (title) AGAINST (?)", title[0..99]) if title - threads = threads.where("MATCH (forumthreads.content) AGAINST (?)", content[0..99]) if content - threads = threads.where("MATCH (threadreplies.content) AGAINST (?)", reply[0..99]) if reply + threads = threads.where("#{match[3]}", title[0..99]) if title + threads = threads.where("#{match[4]}", content[0..99]) if content + threads = threads.where("#{match[5]}", reply[0..99]) if reply end if label.try(:downcase) == "no label" threads = threads.where(label: nil) - elsif l = Label.find_by(name: label) && label + elsif label && l = Label.find_by(name: label) threads = threads.where(label: l) end threads = threads.where(user_author: author) if author threads = threads.group("forumthreads.id") - order_phrase.presence ? threads.order("GREATEST(relevance, reply_rel) DESC") : threads.order("sticky desc", "threadreplies.created_at DESC", "forumthreads.created_at DESC") + if order_phrase.present? + threads = threads.order("GREATEST(relevance, reply_rel) DESC") + else + threads = threads.order("sticky desc", "threadreplies.created_at DESC", "forumthreads.created_at DESC") + end + threads end end diff --git a/app/models/user.rb b/app/models/user.rb index 3098cfc..ff09c70 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -177,17 +177,18 @@ class User < ActiveRecord::Base def self.search (search, role, badge) if role - if role.downcase == "staff" + if role.try(:downcase) == "staff" users = User.joins(:role).where("roles.value >= ?", Role.get(:mod).to_i) - elsif r = Role.get(role) - users = User.joins(:role).where(role: r) else + users = User.joins(:role).where(role: role) end - elsif badge && b = Badge.get(badge) - users = User.joins(:badge).where(badge: b) + end + if badge + users = User.joins(:badge).where(badge: badge) else - users = User.joins(:role).where.not(id: User.first.id) #Remove first user + users = User.joins(:role).all.where.not(id: User.first.id) end - return users.where("users.name like ? OR ign like ?", "%#{User.send(:sanitize_sql_like, search.to_s)}%", "%#{User.send(:sanitize_sql_like, search.to_s)}%") + search_san = User.send(:sanitize_sql_like, search.to_s) + users.where("users.name like ? OR ign like ?", "%#{search_san}%", "%#{search_san}%") end end diff --git a/app/views/forums/show.html.erb b/app/views/forums/show.html.erb index 36741d9..b232292 100644 --- a/app/views/forums/show.html.erb +++ b/app/views/forums/show.html.erb @@ -2,8 +2,7 @@

<%= title @forum %> - <% params[:id] = @forum.id %> - <%= link_to "Search Threads", forumthreads_path(params.to_hash), class: "btn blue right" %> + <%= link_to "Search Threads", forumthreads_path(forum: @forum.id), class: "btn blue right" %>

<% if @forum.can_write?(current_user) %>

diff --git a/app/views/forumthreads/index.html.erb b/app/views/forumthreads/index.html.erb index f41d43d..3a0493b 100644 --- a/app/views/forumthreads/index.html.erb +++ b/app/views/forumthreads/index.html.erb @@ -7,8 +7,8 @@ <% end %>

<% - if params[:id] - text = "forum '#{Forum.find(params[:id]).name}'" + if params[:forum] + text = "forum '#{Forum.find(params[:forum]).name}'" if params_list.any? text = "Search results in #{text} (#{@threads.length})" else @@ -21,29 +21,27 @@ end %> <%= title text %> -
- <%= link_to "Advanced Search", search_forumthreads_path(params_list), class: "btn right blue" %> - <% if params_list.any? %> - <% if params[:id] %> - <%= link_to "Show All Threads", forumthreads_path(params_list.except("id")), class: "btn right blue" %> - <% else %> - <%= link_to "Show All Threads", forumthreads_path, class: "btn right blue" %> - <% end %> - <% end %> - <% if params[:id] %> - <%= link_to "Go to Forum", forum_path(params[:id]), class: "btn right blue" %> - <% end %>

-
- <%= form_tag({controller: "forumthreads", action: "index"}, method: :get, enforce_utf8: nil) do %> - <%= text_field_tag "query", params[:query], placeholder: "Search...", style: "width:300px" %> - <% params_list.compact.except("query").each do |key, value| %> - <%= hidden_field_tag key, params[key] %> - <% end %> - <%= submit_tag "Go", class: "searchfield btn", style: "width:40px", name: nil %> +
+<%= form_tag(forumthreads_path, method: :get) do %> + <%= text_field_tag "query", params[:query], placeholder: "Search...", class: "searchfield field" %> + <%= submit_tag "Go", class: "searchfield btn" %> + <% params.slice(:title, :content, :reply, :label, :author).each do |key, value| %> + <%= hidden_field_tag key, params[key] %> <% end %> -
- +<% end %> +<%= link_to "Advanced Search", search_forumthreads_path(params_list), class: "btn right blue" %> +<% if params_list.any? %> + <% if params[:forum] %> + <%= link_to "Show All Threads", forumthreads_path(params_list.except("forum")), class: "btn right blue" %> + <% elsif params_list.except(:controller, :action).any? %> + <%= link_to "Show All Threads", forumthreads_path, class: "btn right blue" %> + <% end %> +<% end %> +<% if params[:forum] %> + <%= link_to "Go to Forum", forum_path(params[:forum]), class: "btn right blue" %> +<% end %> +
<% @threads.each do |thread| %>
diff --git a/app/views/forumthreads/search.html.erb b/app/views/forumthreads/search.html.erb index 6db71a4..125868a 100644 --- a/app/views/forumthreads/search.html.erb +++ b/app/views/forumthreads/search.html.erb @@ -4,7 +4,7 @@ <% label = Label.where(name: params[:label]).first %> -<%= form_tag({controller: "forumthreads", action: "index"}, method: :get, enforce_utf8: false) do %> +<%= form_tag(forumthreads_path, method: :get) do %> <% forums = [] Forum.select{|f| f.can_read?(current_user)}.sort_by{ |f| f.forumgroup && f.forumgroup.position || 0 }.each do |f| @@ -14,7 +14,7 @@ <% label_list = Label.pluck(:name).prepend("No Label") %> - + -- cgit v1.2.3
Forum<%= select_tag "id", options_for_select(forums, params[:id]), include_blank: "Search All Threads" %><%= select_tag "forum", options_for_select(forums, params[:forum]), include_blank: "Search All Threads" %>
Label