Added Searching Features

* Added Thread Search Feature

* Added User Search Feature

* Re-organized searching, added @mention support to author search
This commit is contained in:
MrYummy
2017-05-28 18:08:57 -04:00
parent e7463524af
commit 1316d7ca03
20 changed files with 293 additions and 27 deletions

View File

@@ -32,4 +32,4 @@ class Forum < ActiveRecord::Base
def to_param
[id, to_s.parameterize].join("-")
end
end
end

View File

@@ -65,4 +65,47 @@ class Forumthread < ActiveRecord::Base
def to_param
[id, to_s.parameterize].join("-")
end
def self.filter (user, title, content, reply, label, author, query, forum)
userid = user.try(:id).to_i
role = user.try(:role).to_i
can_read = "COALESCE(forum_role_read.value, 0) <= ? AND COALESCE(forumgroup_role_read.value, 0) <= ?"
sticky_can_write = "sticky = true AND (COALESCE(forum_role_write.value, 0) <= ? OR COALESCE(forumgroup_role_write.value, 0) <= ?)"
threads = forum.try(:forumthreads) || Forumthread
threads = threads.where("forumthreads.user_author_id = ? OR (#{can_read}) OR (#{sticky_can_write})", userid, role, role, role, role)
.joins("LEFT JOIN threadreplies ON forumthreads.id = threadreplies.forumthread_id")
.joins(forum: :forumgroup)
.joins("LEFT JOIN roles as forum_role_read ON forums.role_read_id = forum_role_read.id")
.joins("LEFT JOIN roles as forum_role_write ON forums.role_write_id = forum_role_write.id")
.joins("LEFT JOIN roles as forumgroup_role_read ON forumgroups.role_read_id = forumgroup_role_read.id")
.joins("LEFT JOIN roles as forumgroup_role_write ON forumgroups.role_write_id = forumgroup_role_write.id")
if [content, title, reply, label, author, query].any?
label_o = Label.find_by(name: label)
if label_o
threads = threads.where(label: label_o)
elsif label.try(:downcase) == "no label"
threads = threads.where(label: nil)
end
threads = threads.where(user_author: author) if author
if query
threads = threads.where("MATCH (title, forumthreads.content) AGAINST (?) OR MATCH (threadreplies.content) AGAINST (?)", query, query)
elsif [title, content, reply].any?
query = [title, content, reply].select(&:present?).join(" ")
threads = threads.where("MATCH (title) AGAINST (?)", title) if title
threads = threads.where("MATCH (forumthreads.content) AGAINST (?)", content) if content
threads = threads.where("MATCH (threadreplies.content) AGAINST (?)", reply) if reply
threads = threads.group("threadreplies.id", "forumthreads.id")
threads = threads.order("(MATCH (title, forumthreads.content) AGAINST ('#{query}')) DESC")
end
end
threads = threads.order("sticky desc", "threadreplies.created_at desc", "forumthreads.created_at desc") if threads.order_values.empty?
threads
end
end

View File

@@ -53,4 +53,4 @@ class Role < ActiveRecord::Base
Role.order(:value).select {|r| r >= from}.select {|r| r <= to}
end
end
end

View File

@@ -174,4 +174,8 @@ class User < ActiveRecord::Base
def set_email_token
self.email_token ||= SecureRandom.hex(16)
end
def self.search (users, search)
return users.where("users.name like ? OR ign like ?", "%#{User.send(:sanitize_sql_like, search)}%", "%#{User.send(:sanitize_sql_like, search)}%")
end
end