Skip to content
Extraits de code Groupes Projets
Valider 2e36f8d3 rédigé par Lukas Matt's avatar Lukas Matt
Parcourir les fichiers

Diaspora review part 1

* join the conditions of the inner ifs
* add a uniqueness constraint to the model
* differentiate between author is a local or a remote user
* simplify controller/mailer functions
parent ed96ddac
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -90,7 +90,13 @@ app.views.Base = Backbone.View.extend({ ...@@ -90,7 +90,13 @@ app.views.Base = Backbone.View.extend({
var type = $(evt.currentTarget).data("type"); var type = $(evt.currentTarget).data("type");
report.fetch({ report.fetch({
data: { id: id, type: type, text: msg }, data: {
report: {
post_id: id,
post_type: type,
text: msg
}
},
type: 'POST', type: 'POST',
statusCode: { statusCode: {
200: function(xhr) { 200: function(xhr) {
...@@ -142,4 +148,4 @@ app.views.StaticContentView = app.views.Base.extend({ ...@@ -142,4 +148,4 @@ app.views.StaticContentView = app.views.Base.extend({
presenter : function(){ presenter : function(){
return this.data; return this.data;
}, },
}); });
\ No newline at end of file
#report { #reports {
padding-top: 2em; padding-top: 2em;
span { span {
display: block; display: block;
......
...@@ -3,65 +3,39 @@ class ReportController < ApplicationController ...@@ -3,65 +3,39 @@ class ReportController < ApplicationController
before_filter :redirect_unless_admin, :except => [:create] before_filter :redirect_unless_admin, :except => [:create]
def index def index
@report = Report.where(reviewed: false).all @reports = Report.where(reviewed: false).all
end end
def update def update
if Report.where(post_type: params[:type]).exists?(post_id: params[:id]) if report = Report.where(id: params[:id]).first
mark_as_reviewed report.mark_as_reviewed
end end
redirect_to :action => :index and return redirect_to :action => :index
end end
def destroy def destroy
if (params[:type].eql? "post") if report = Report.where(id: params[:id]).first
if Post.exists?(params[:id]) if report.destroy_reported_item
delete_post flash[:notice] = I18n.t 'report.status.destroyed'
end else
elsif (params[:type].eql? "comment") flash[:error] = I18n.t 'report.status.failed'
if Comment.exists?(params[:id])
delete_comment
end end
else
flash[:error] = I18n.t 'report.status.failed'
end end
redirect_to :action => :index and return redirect_to :action => :index
end end
def create def create
code = 400 if current_user.reports.create! report_params
username = current_user.username flash.now[:notice] = I18n.t 'report.status.created'
post = Report.new( else
:post_id => params[:id], flash.now[:error] = I18n.t 'report.status.failed'
:post_type => params[:type],
:user_id => username,
:text => params[:text])
unless Report.where("post_id = ? AND post_type = ?", params[:id], params[:type]).exists?(user_id: username)
result = post.save
code = 200 if result
end end
render :nothing => true, :status => code
end end
private private
def delete_post def report_params
post = Post.find(params[:id]) params.require(:report).permit(:post_id, :post_type, :text)
current_user.retract(post)
mark_as_reviewed
flash[:notice] = I18n.t 'report.status.destroyed'
end
def delete_comment
comment = Comment.find(params[:id])
#current_user.retract(comment)
comment.destroy
mark_as_reviewed
flash[:notice] = I18n.t 'report.status.destroyed'
end
def mark_as_reviewed
posts = Report.where("post_id = ? AND post_type = ?", params[:id], params[:type])
posts.each do |post|
post.update_attributes(reviewed: true)
end
flash[:notice] = I18n.t 'report.status.marked'
end end
end end
...@@ -2,14 +2,12 @@ class ReportMailer < ActionMailer::Base ...@@ -2,14 +2,12 @@ class ReportMailer < ActionMailer::Base
default :from => AppConfig.mail.sender_address default :from => AppConfig.mail.sender_address
def new_report(type, id) def new_report(type, id)
url = AppConfig.pod_uri.to_s resource = {
url << report_index_path
resource = Hash[
:subject => I18n.t('notifier.report_email.subject', :type => type), :subject => I18n.t('notifier.report_email.subject', :type => type),
:url => url, :url => report_index_url,
:type => type, :type => type,
:id => id :id => id
] }
Role.admins.each do |role| Role.admins.each do |role|
resource[:email] = User.find_by_id(role.person_id).email resource[:email] = User.find_by_id(role.person_id).email
format(resource).deliver format(resource).deliver
......
...@@ -2,14 +2,59 @@ class Report < ActiveRecord::Base ...@@ -2,14 +2,59 @@ class Report < ActiveRecord::Base
validates :user_id, presence: true validates :user_id, presence: true
validates :post_id, presence: true validates :post_id, presence: true
validates :post_type, presence: true validates :post_type, presence: true
validates :text, presence: true
validate :entry_exists, :on => :create
belongs_to :user belongs_to :user
belongs_to :post belongs_to :post
belongs_to :comment
has_many :reports
after_create :send_report_notification after_create :send_report_notification
def entry_exists
if Report.where(post_id: post_id, post_type: post_type).exists?(user_id: user_id)
errors[:base] << 'You cannot report the same post twice.'
end
end
def destroy_reported_item
if post_type == 'post'
delete_post
elsif post_type == 'comment'
delete_comment
end
mark_as_reviewed
end
def delete_post
if post = Post.where(id: post_id).first
if post.author.local?
post.author.owner.retract(post)
else
post.destroy
end
end
end
def delete_comment
if comment = Comment.where(id: post_id).first
if comment.author.local?
comment.author.owner.retract(comment)
elsif comment.parent.author.local?
comment.parent.author.owner.retract(comment)
else
comment.destroy
end
end
end
def mark_as_reviewed
if reports = Report.where(post_id: post_id, post_type: post_type)
reports.update_all(reviewed: true)
end
end
def send_report_notification def send_report_notification
Workers::Mail::ReportWorker.perform_async(self.post_type, self.post_id) Workers::Mail::ReportWorker.perform_async(self.post_type, self.post_id)
end end
......
...@@ -69,6 +69,8 @@ class User < ActiveRecord::Base ...@@ -69,6 +69,8 @@ class User < ActiveRecord::Base
has_many :notifications, :foreign_key => :recipient_id has_many :notifications, :foreign_key => :recipient_id
has_many :reports
before_save :guard_unconfirmed_email, before_save :guard_unconfirmed_email,
:save_person! :save_person!
......
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
.span-24.last .span-24.last
%h1 %h1
= t('report.title') = t('report.title')
%div#report %div#reports
- @report.each do |r| - @reports.each do |r|
%div.content %div.content
%span %span
= report_content(r.post_id, r.post_type) = report_content(r.post_id, r.post_type)
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
= t('report.reason_label', text: r.text) = t('report.reason_label', text: r.text)
%div.options %div.options
%span %span
= link_to t('report.review_link'), report_path(r.post_id, :type => r.post_type), method: :put = link_to t('report.review_link'), report_path(r.id, :type => r.post_type), method: :put
%span %span
= link_to t('report.delete_link'), report_path(r.post_id, :type => r.post_type), method: :delete = link_to t('report.delete_link'), report_path(r.id, :type => r.post_type), method: :delete
%div.clear %div.clear
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter