2013-06-11 03:33:37 +08:00
|
|
|
require_dependency 'email/renderer'
|
2013-06-04 04:12:24 +08:00
|
|
|
|
|
|
|
class Admin::EmailController < Admin::AdminController
|
|
|
|
|
|
|
|
def index
|
2014-02-15 07:50:08 +08:00
|
|
|
data = { delivery_method: delivery_method, settings: delivery_settings }
|
|
|
|
render_json_dump(data)
|
2013-06-04 04:12:24 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test
|
|
|
|
params.require(:email_address)
|
2014-11-19 23:49:12 +08:00
|
|
|
begin
|
|
|
|
Jobs::TestEmail.new.execute(to_address: params[:email_address])
|
|
|
|
render nothing: true
|
|
|
|
rescue => e
|
|
|
|
render json: {errors: [e.message]}, status: 422
|
|
|
|
end
|
2013-06-04 04:12:24 +08:00
|
|
|
end
|
|
|
|
|
2014-02-15 08:17:13 +08:00
|
|
|
def all
|
|
|
|
email_logs = filter_email_logs(EmailLog.all, params)
|
|
|
|
render_serialized(email_logs, EmailLogSerializer)
|
|
|
|
end
|
|
|
|
|
2014-02-15 07:50:08 +08:00
|
|
|
def sent
|
|
|
|
email_logs = filter_email_logs(EmailLog.sent, params)
|
|
|
|
render_serialized(email_logs, EmailLogSerializer)
|
2014-02-15 02:06:21 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def skipped
|
2014-02-15 07:50:08 +08:00
|
|
|
email_logs = filter_email_logs(EmailLog.skipped, params)
|
|
|
|
render_serialized(email_logs, EmailLogSerializer)
|
2013-06-04 04:12:24 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def preview_digest
|
|
|
|
params.require(:last_seen_at)
|
2015-10-31 02:05:54 +08:00
|
|
|
params.require(:username)
|
|
|
|
user = User.find_by_username(params[:username])
|
|
|
|
renderer = Email::Renderer.new(UserNotifications.digest(user, since: params[:last_seen_at]))
|
2013-06-04 04:12:24 +08:00
|
|
|
render json: MultiJson.dump(html_content: renderer.html, text_content: renderer.text)
|
|
|
|
end
|
|
|
|
|
2015-10-19 05:33:24 +08:00
|
|
|
def handle_mail
|
|
|
|
params.require(:email)
|
|
|
|
Email::Receiver.new(params[:email]).process
|
|
|
|
render text: "email was processed"
|
|
|
|
end
|
|
|
|
|
2013-06-12 07:00:13 +08:00
|
|
|
private
|
|
|
|
|
2014-02-15 07:50:08 +08: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-15 08:17:13 +08:00
|
|
|
email_logs = email_logs.where("email_logs.skipped_reason LIKE ?", "%#{params[:skipped_reason]}%") if params[:skipped_reason].present?
|
2014-02-15 07:50:08 +08:00
|
|
|
email_logs.to_a
|
|
|
|
end
|
|
|
|
|
2013-06-12 07:00:13 +08:00
|
|
|
def delivery_settings
|
|
|
|
action_mailer_settings
|
2014-08-15 05:54:55 +08:00
|
|
|
.reject { |k, _| k == :password }
|
2013-06-12 07:00:13 +08:00
|
|
|
.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
|
2013-06-04 04:12:24 +08:00
|
|
|
end
|