diff --git a/app/controllers/dmails_controller.rb b/app/controllers/dmails_controller.rb index b68b20432..87421bd02 100644 --- a/app/controllers/dmails_controller.rb +++ b/app/controllers/dmails_controller.rb @@ -15,11 +15,7 @@ class DmailsController < ApplicationController end def index - if params[:folder] && params[:set_default_folder] - cookies.permanent[:dmail_folder] = params[:folder] - end - - @dmails = Dmail.visible.paginated_search(params, defaults: { is_spam: false, is_deleted: false }, count_pages: true) + @dmails = Dmail.visible.paginated_search(params, defaults: { folder: "received" }, count_pages: true) respond_with(@dmails) end diff --git a/app/helpers/dmails_helper.rb b/app/helpers/dmails_helper.rb deleted file mode 100644 index a61ac948a..000000000 --- a/app/helpers/dmails_helper.rb +++ /dev/null @@ -1,28 +0,0 @@ -module DmailsHelper - def dmails_current_folder_path - case cookies[:dmail_folder] - when "sent" - sent_dmails_path - when "received" - received_dmails_path - else - all_dmails_path - end - end - - def all_dmails_path(params = {}) - dmails_path(folder: "all", **params) - end - - def sent_dmails_path(params = {}) - dmails_path(search: {from_id: CurrentUser.id}, folder: "sent", **params) - end - - def spam_dmails_path - dmails_path(search: {to_id: CurrentUser.id, is_spam: true}, folder: "spam") - end - - def received_dmails_path(params = {}) - dmails_path(search: {to_id: CurrentUser.id}, folder: "received", **params) - end -end diff --git a/app/models/dmail.rb b/app/models/dmail.rb index b635af47b..792ea43f2 100644 --- a/app/models/dmail.rb +++ b/app/models/dmail.rb @@ -19,6 +19,15 @@ class Dmail < ApplicationRecord api_attributes including: [:key] + scope :active, -> { where(is_deleted: false) } + scope :deleted, -> { where(is_deleted: true) } + scope :read, -> { where(is_read: true) } + scope :unread, -> { where(is_read: false) } + scope :spam, -> { where(is_spam: true) } + scope :visible, -> { where(owner: CurrentUser.user) } + scope :sent, -> { where("dmails.owner_id = dmails.from_id") } + scope :received, -> { where("dmails.owner_id = dmails.to_id") } + concerning :SpamMethods do class_methods do def is_spammer?(user) @@ -109,24 +118,21 @@ class Dmail < ApplicationRecord where("dmails.from_id = ? AND dmails.owner_id != ?", user.id, user.id) end - def active - where("is_deleted = ?", false) - end - - def deleted - where("is_deleted = ?", true) - end - - def read - where(is_read: true) - end - - def unread - where("is_read = false and is_deleted = false") - end - - def visible - where("owner_id = ?", CurrentUser.id) + def folder_matches(folder) + case folder + when "received" + active.received + when "unread" + active.received.unread + when "sent" + active.sent + when "spam" + active.spam + when "deleted" + deleted + else + all + end end def search(params) @@ -136,6 +142,8 @@ class Dmail < ApplicationRecord q = q.text_attribute_matches(:title, params[:title_matches]) q = q.text_attribute_matches(:body, params[:message_matches], index_column: :message_index) + q = q.folder_matches(params[:folder]) + q = q.read if params[:read].to_s.truthy? q = q.unread if params[:read].to_s.falsy? diff --git a/app/views/dmails/_search.html.erb b/app/views/dmails/_search.html.erb index 32a20ab81..09bdc23d6 100644 --- a/app/views/dmails/_search.html.erb +++ b/app/views/dmails/_search.html.erb @@ -1,5 +1,5 @@ <%= search_form_for(dmails_path) do |f| %> - <%= f.hidden_field :folder, value: params[:folder] %> + <%= f.input :folder, as: :hidden, input_html: { value: params[:search][:folder] } %> <%= f.input :title_matches, label: "Title", hint: "Use * for wildcard", input_html: { value: params[:search][:title_matches] } %> <%= f.input :message_matches, label: "Message", hint: "Use * for wildcard", input_html: { value: params[:search][:messages_matches] } %> <%= f.input :to_name, label: "To", input_html: { value: params[:search][:to_name], data: { autocomplete: "user" } } %> diff --git a/app/views/dmails/_secondary_links.html.erb b/app/views/dmails/_secondary_links.html.erb index 9a3649914..0ccd16b88 100644 --- a/app/views/dmails/_secondary_links.html.erb +++ b/app/views/dmails/_secondary_links.html.erb @@ -1,10 +1,11 @@ <% content_for(:secondary_links) do %> <%= quick_search_form_for(:message_matches, dmails_path, "dmails") %> - <%= subnav_link_to "All", all_dmails_path(set_default_folder: true) %> - <%= subnav_link_to "Received", received_dmails_path(set_default_folder: true) %> - <%= subnav_link_to "Sent", sent_dmails_path(set_default_folder: true) %> - <%= subnav_link_to "Spam", spam_dmails_path %> - <%= subnav_link_to "Deleted", dmails_path(search: { is_deleted: true }) %> + <%= subnav_link_to "All", dmails_path(search: { folder: "all" }) %> + <%= subnav_link_to "Received", dmails_path(search: { folder: "received" }) %> + <%= subnav_link_to "Unread", dmails_path(search: { folder: "unread" }) %> + <%= subnav_link_to "Sent", dmails_path(search: { folder: "sent" }) %> + <%= subnav_link_to "Spam", dmails_path(search: { folder: "spam" }) %> + <%= subnav_link_to "Deleted", dmails_path(search: { folder: "deleted" }) %>