Commit 15b18651 authored by realtin's avatar realtin Committed by Jonne Haß

add moderators to report email recievers

and refactor coding style according to pull request comments

(#5324)
parent 098c30c2
......@@ -5,7 +5,7 @@
class ApplicationController < ActionController::Base
before_action :force_tablet_html
has_mobile_fu
protect_from_forgery :except => :receive
protect_from_forgery except: :receive
before_action :ensure_http_referer_is_set
before_action :set_locale
......@@ -15,7 +15,7 @@ class ApplicationController < ActionController::Base
before_action :gon_set_current_user
before_action :gon_set_preloads
inflection_method :grammatical_gender => :gender
inflection_method grammatical_gender: :gender
helper_method :all_aspects,
:all_contacts_count,
......@@ -34,7 +34,7 @@ class ApplicationController < ActionController::Base
end
def ensure_http_referer_is_set
request.env['HTTP_REFERER'] ||= '/'
request.env["HTTP_REFERER"] ||= "/"
end
# Overwriting the sign_out redirect path method
......@@ -67,11 +67,11 @@ class ApplicationController < ActionController::Base
end
def set_diaspora_header
headers['X-Diaspora-Version'] = AppConfig.version_string
headers["X-Diaspora-Version"] = AppConfig.version_string
if AppConfig.git_available?
headers['X-Git-Update'] = AppConfig.git_update if AppConfig.git_update.present?
headers['X-Git-Revision'] = AppConfig.git_revision if AppConfig.git_revision.present?
headers["X-Git-Update"] = AppConfig.git_update if AppConfig.git_update.present?
headers["X-Git-Revision"] = AppConfig.git_revision if AppConfig.git_revision.present?
end
end
......@@ -86,17 +86,13 @@ class ApplicationController < ActionController::Base
end
def redirect_unless_admin
unless current_user.admin?
redirect_to stream_url, :notice => 'you need to be an admin to do that'
return
end
return if current_user.admin?
redirect_to stream_url, notice: "you need to be an admin to do that"
end
def redirect_unless_admin_or_moderator
unless current_user.moderator? || current_user.admin?
redirect_to stream_url, :notice => 'you need to be an admin or moderator to do that'
return
end
def redirect_unless_moderator
return if current_user.moderator? || current_user.admin?
redirect_to stream_url, notice: "you need to be an admin or moderator to do that"
end
def set_grammatical_gender
......@@ -104,7 +100,7 @@ class ApplicationController < ActionController::Base
gender = current_user.gender.to_s.tr('!()[]"\'`*=|/\#.,-:', '').downcase
unless gender.empty?
i_langs = I18n.inflector.inflected_locales(:gender)
i_langs.delete I18n.locale
i_langs.delete I18n.locale
i_langs.unshift I18n.locale
i_langs.each do |lang|
token = I18n.inflector.true_token(gender, :gender, lang)
......@@ -149,7 +145,7 @@ class ApplicationController < ActionController::Base
return unless user_signed_in?
a_ids = session[:a_ids] || []
user = UserPresenter.new(current_user, a_ids)
gon.push({:user => user})
gon.push(user: user)
end
def gon_set_preloads
......
......@@ -3,8 +3,8 @@
# the COPYRIGHT file.
class ReportController < ApplicationController
before_filter :authenticate_user!
before_filter :redirect_unless_admin_or_moderator, :except => [:create]
before_action :authenticate_user!
before_action :redirect_unless_moderator, except: [:create]
def index
@reports = Report.where(reviewed: false)
......@@ -19,19 +19,19 @@ class ReportController < ApplicationController
def destroy
if (report = Report.where(id: params[:id]).first) && report.destroy_reported_item
flash[:notice] = I18n.t 'report.status.destroyed'
flash[:notice] = I18n.t "report.status.destroyed"
else
flash[:error] = I18n.t 'report.status.failed'
flash[:error] = I18n.t "report.status.failed"
end
redirect_to :action => :index
redirect_to action: :index
end
def create
report = current_user.reports.new(report_params)
if report.save
render :json => true, :status => 200
render json: true, status: 200
else
render :nothing => true, :status => 409
render nothing: true, status: 409
end
end
......
class ReportMailer < ActionMailer::Base
default :from => AppConfig.mail.sender_address
default from: AppConfig.mail.sender_address
def new_report(type, id)
def self.new_report(type, id)
Role.moderators.map {|role| super(type, id, role) }
end
def new_report(type, id, role)
resource = {
:url => report_index_url,
:type => I18n.t('notifier.report_email.type.' + type),
:id => id
url: report_index_url,
type: I18n.t("notifier.report_email.type." + type),
id: id
}
Role.admins.each do |role|
person = Person.find(role.person_id)
if person.local?
user = User.find_by_id(person.owner_id)
unless user.user_preferences.exists?(:email_type => :someone_reported)
resource[:email] = user.email
format(resource)
end
end
person = Person.find(role.person_id)
return unless person.local?
user = User.find_by_id(person.owner_id)
return if user.user_preferences.exists?(email_type: :someone_reported)
I18n.with_locale(user.language) do
resource[:email] = user.email
format(resource)
end
end
private
def format(resource)
mail(to: resource[:email], subject: I18n.t('notifier.report_email.subject', :type => resource[:type])) do |format|
format.html { render 'report/report_email', :locals => { :resource => resource } }
format.text { render 'report/report_email', :locals => { :resource => resource } }
end
def format(resource)
mail(to: resource[:email], subject: I18n.t("notifier.report_email.subject", type: resource[:type])) do |format|
format.html { render "report/report_email", locals: {resource: resource} }
format.text { render "report/report_email", locals: {resource: resource} }
end
end
end
......@@ -8,6 +8,7 @@ class Role < ActiveRecord::Base
validates :name, inclusion: {in: %w(admin moderator spotlight)}
scope :admins, -> { where(name: "admin") }
scope :moderators, -> { where(name: %w(moderator admin)) }
def self.is_admin?(person)
exists?(person_id: person.id, name: "admin")
......
......@@ -482,7 +482,7 @@ class User < ActiveRecord::Base
end
def moderator?
Role.moderator?(self.person)
Role.moderator?(person)
end
def podmin_account?
......
......@@ -6,17 +6,16 @@ class UserPresenter
self.aspects_ids = aspects_ids
end
def to_json(options = {})
self.user.person.as_api_response(:backbone).update(
{ :notifications_count => notifications_count,
:unread_messages_count => unread_messages_count,
:admin => admin,
:moderator => moderator,
:aspects => aspects,
:services => services,
:following_count => self.user.contacts.receiving.count,
:configured_services => self.configured_services,
}
def to_json(options={})
user.person.as_api_response(:backbone).update(
notifications_count: notifications_count,
unread_messages_count: unread_messages_count,
admin: admin,
moderator: moderator,
aspects: aspects,
services: services,
following_count: user.contacts.receiving.count,
configured_services: configured_services
).to_json(options)
end
......@@ -25,14 +24,14 @@ class UserPresenter
end
def configured_services
user.services.map{|service| service.provider }
user.services.map(&:provider)
end
def aspects
@aspects ||= begin
aspects = AspectPresenter.as_collection(user.aspects)
no_aspects = self.aspects_ids.empty?
aspects.each{ |a| a[:selected] = no_aspects || self.aspects_ids.include?(a[:id].to_s) }
no_aspects = aspects_ids.empty?
aspects.each {|a| a[:selected] = no_aspects || aspects_ids.include?(a[:id].to_s) }
end
end
......
- content_for :head do
= stylesheet_link_tag :admin
.container
%div
- if current_user.admin?
......
......@@ -4,9 +4,8 @@ module Workers
sidekiq_options queue: :mail
def perform(type, id)
ReportMailer.new_report(type, id).deliver_now
ReportMailer.new_report(type, id).each(&:deliver_now)
end
end
end
end
......@@ -1433,4 +1433,4 @@ de:
welcome: "Willkommen!"
will_paginate:
next_label: "nächstes &raquo;"
previous_label: "&laquo; voriges"
\ No newline at end of file
previous_label: "&laquo; voriges"
......@@ -219,6 +219,7 @@ en:
settings: "Settings"
help: "Help"
admin: "Admin"
moderator: "Moderator"
log_out: "Log out"
notifications: "Notifications"
......
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
......@@ -103,12 +104,14 @@ describe ReportController, type: :controller do
before do
Role.add_moderator(alice.person)
end
it "succeeds" do
put :update, id: @message.id, type: "post"
expect(response.status).to eq(302)
expect(Report.where(reviewed: true, item_id: @message.id, item_type: "post")).to be_truthy
end
end
context "mark comment report as moderator" do
before do
Role.add_moderator(alice.person)
......
......@@ -2,31 +2,40 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require 'spec_helper'
require "spec_helper"
describe Report, :type => :mailer do
describe '#make_notification' do
describe Report, type: :mailer do
describe "#make_notification" do
before do
@remote = FactoryGirl.create(:person, :diaspora_handle => "remote@remote.net")
@user = FactoryGirl.create(:user_with_aspect, :username => "local")
@remote = FactoryGirl.create(:person, diaspora_handle: "remote@remote.net")
@user = FactoryGirl.create(:user_with_aspect, username: "local", language: "de")
@user2 = FactoryGirl.create(:user_with_aspect, username: "locally")
Role.add_admin(@user.person)
Role.add_moderator(@user2.person)
end
it "should deliver successfully" do
expect {
ReportMailer.new_report('post', 666).deliver_now
ReportMailer.new_report("post", 666).each(&:deliver_now)
}.to_not raise_error
end
it "should be added to the delivery queue" do
expect {
ReportMailer.new_report('post', 666).deliver_now
}.to change(ActionMailer::Base.deliveries, :size).by(1)
ReportMailer.new_report("post", 666).each(&:deliver_now)
}.to change(ActionMailer::Base.deliveries, :size).by(2)
end
it "should include correct recipient" do
ReportMailer.new_report('post', 666).deliver_now
ReportMailer.new_report("post", 666).each(&:deliver_now)
expect(ActionMailer::Base.deliveries[0].to[0]).to include(@user.email)
expect(ActionMailer::Base.deliveries[1].to[0]).to include(@user2.email)
end
it "should send mail in recipent's prefered language" do
ReportMailer.new_report("post", 666).each(&:deliver_now)
expect(ActionMailer::Base.deliveries[0].subject).to match("Ein neuer post wurde als anstößig markiert")
expect(ActionMailer::Base.deliveries[1].subject).to match("A new post was marked as offensive")
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment