diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 951638b30..7d1b99d33 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/report_controller.rb b/app/controllers/report_controller.rb index f0603eceb..8e0826c1f 100644 --- a/app/controllers/report_controller.rb +++ b/app/controllers/report_controller.rb @@ -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 diff --git a/app/mailers/report_mailer.rb b/app/mailers/report_mailer.rb index b3479cc74..35aafcf3e 100644 --- a/app/mailers/report_mailer.rb +++ b/app/mailers/report_mailer.rb @@ -1,30 +1,32 @@ 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 diff --git a/app/models/role.rb b/app/models/role.rb index b5452e8c5..d9f60e867 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -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") diff --git a/app/models/user.rb b/app/models/user.rb index 137f7d722..f3ef55715 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -482,7 +482,7 @@ class User < ActiveRecord::Base end def moderator? - Role.moderator?(self.person) + Role.moderator?(person) end def podmin_account? diff --git a/app/presenters/user_presenter.rb b/app/presenters/user_presenter.rb index f70bae290..076734d96 100644 --- a/app/presenters/user_presenter.rb +++ b/app/presenters/user_presenter.rb @@ -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 diff --git a/app/views/report/index.html.haml b/app/views/report/index.html.haml index 5caf2f719..69645271f 100644 --- a/app/views/report/index.html.haml +++ b/app/views/report/index.html.haml @@ -1,3 +1,6 @@ +- content_for :head do + = stylesheet_link_tag :admin + .container %div - if current_user.admin? diff --git a/app/workers/mail/report_worker.rb b/app/workers/mail/report_worker.rb index 6d272f486..ede0f0867 100644 --- a/app/workers/mail/report_worker.rb +++ b/app/workers/mail/report_worker.rb @@ -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 - diff --git a/config/locales/diaspora/de.yml b/config/locales/diaspora/de.yml index f28c8eecf..3203c2b73 100644 --- a/config/locales/diaspora/de.yml +++ b/config/locales/diaspora/de.yml @@ -1433,4 +1433,4 @@ de: welcome: "Willkommen!" will_paginate: next_label: "nächstes »" - previous_label: "« voriges" \ No newline at end of file + previous_label: "« voriges" diff --git a/config/locales/javascript/javascript.en.yml b/config/locales/javascript/javascript.en.yml index c7f01c680..9104fff90 100644 --- a/config/locales/javascript/javascript.en.yml +++ b/config/locales/javascript/javascript.en.yml @@ -219,6 +219,7 @@ en: settings: "Settings" help: "Help" admin: "Admin" + moderator: "Moderator" log_out: "Log out" notifications: "Notifications" diff --git a/spec/controllers/report_controller_spec.rb b/spec/controllers/report_controller_spec.rb index c214e34d1..e5d5a49eb 100644 --- a/spec/controllers/report_controller_spec.rb +++ b/spec/controllers/report_controller_spec.rb @@ -1,3 +1,4 @@ + # 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) diff --git a/spec/mailers/report_spec.rb b/spec/mailers/report_spec.rb index b5ec54642..32c2afdb1 100644 --- a/spec/mailers/report_spec.rb +++ b/spec/mailers/report_spec.rb @@ -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