moved all search styling to CSS, removed friendly (but slow) URLs, moved WHERE strings into an array

This commit is contained in:
MrYummy
2017-06-02 02:10:49 +02:00
parent a658852e86
commit a1ade53445
10 changed files with 164 additions and 56 deletions

View File

@@ -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();

View File

@@ -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;
}
}

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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)
else
users = User.joins(:role).where.not(id: User.first.id) #Remove first user
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)}%")
if badge
users = User.joins(:badge).where(badge: badge)
else
users = User.joins(:role).all.where.not(id: User.first.id)
end
search_san = User.send(:sanitize_sql_like, search.to_s)
users.where("users.name like ? OR ign like ?", "%#{search_san}%", "%#{search_san}%")
end
end

View File

@@ -2,8 +2,7 @@
<h1>
<%= 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" %>
</h1>
<% if @forum.can_write?(current_user) %>
<p>

View File

@@ -7,8 +7,8 @@
<% end %>
<h1>
<%
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 %>
<br>
<%= 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 %>
</h1>
<div class="searchfield">
<%= 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 %>
<br>
<%= 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 %>
</div>
</h1>
<% 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 %>
<div id="forum_groups">
<% @threads.each do |thread| %>
<div class="item-group with-avatar" id="thread-<%= thread.id %>">

View File

@@ -4,7 +4,7 @@
<% label = Label.where(name: params[:label]).first %>
<table>
<tbody>
<%= 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") %>
<tr>
<td>Forum</td>
<td><%= select_tag "id", options_for_select(forums, params[:id]), include_blank: "Search All Threads" %></td>
<td><%= select_tag "forum", options_for_select(forums, params[:forum]), include_blank: "Search All Threads" %></td>
</tr>
<tr>
<td>Label</td>