discourse/app/controllers/admin/email_controller.rb

63 lines
2.1 KiB
Ruby
Raw Normal View History

2013-06-10 15:33:37 -04:00
require_dependency 'email/renderer'
class Admin::EmailController < Admin::AdminController
def index
2014-02-14 18:50:08 -05:00
data = { delivery_method: delivery_method, settings: delivery_settings }
render_json_dump(data)
end
def test
params.require(:email_address)
Jobs::TestEmail.new.execute(to_address: params[:email_address])
render nothing: true
end
2014-02-14 19:17:13 -05:00
def all
email_logs = filter_email_logs(EmailLog.all, params)
render_serialized(email_logs, EmailLogSerializer)
end
2014-02-14 18:50:08 -05:00
def sent
email_logs = filter_email_logs(EmailLog.sent, params)
render_serialized(email_logs, EmailLogSerializer)
end
def skipped
2014-02-14 18:50:08 -05:00
email_logs = filter_email_logs(EmailLog.skipped, params)
render_serialized(email_logs, EmailLogSerializer)
end
def preview_digest
params.require(:last_seen_at)
renderer = Email::Renderer.new(UserNotifications.digest(current_user, since: params[:last_seen_at]))
render json: MultiJson.dump(html_content: renderer.html, text_content: renderer.text)
end
private
2014-02-14 18:50:08 -05:00
def filter_email_logs(email_logs, params)
email_logs = email_logs.limit(50).includes(:user).order("email_logs.created_at desc").references(:user)
email_logs = email_logs.where("users.username LIKE ?", "%#{params[:user]}%") if params[:user].present?
email_logs = email_logs.where("email_logs.to_address LIKE ?", "%#{params[:address]}%") if params[:address].present?
email_logs = email_logs.where("email_logs.email_type LIKE ?", "%#{params[:type]}%") if params[:type].present?
email_logs = email_logs.where("email_logs.reply_key LIKE ?", "%#{params[:reply_key]}%") if params[:reply_key].present?
2014-02-14 19:17:13 -05:00
email_logs = email_logs.where("email_logs.skipped_reason LIKE ?", "%#{params[:skipped_reason]}%") if params[:skipped_reason].present?
2014-02-14 18:50:08 -05:00
email_logs.to_a
end
def delivery_settings
action_mailer_settings
.reject { |k, _| k == :password }
.map { |k, v| { name: k, value: v }}
end
def delivery_method
ActionMailer::Base.delivery_method
end
def action_mailer_settings
ActionMailer::Base.public_send "#{delivery_method}_settings"
end
end