Skip to content

Commit

Permalink
Merge pull request #187 from TreinaDev/feat/admin-rules-report
Browse files Browse the repository at this point in the history
Adiciona lógica de decisão de denúncias de conteúdo.
  • Loading branch information
Luckvc authored Feb 13, 2024
2 parents 2da01f6 + a417f6c commit 19a931c
Show file tree
Hide file tree
Showing 25 changed files with 197 additions and 70 deletions.
7 changes: 7 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class PostsController < ApplicationController
before_action :set_post, only: %w[show edit update pin]
before_action :authorize!, only: %w[edit update pin]
before_action :blocks_update, only: %w[update]
before_action :redirect_if_removed_content, only: %w[show edit update pin]

require 'image_processing/mini_magick'

Expand Down Expand Up @@ -71,4 +72,10 @@ def authorize!
def blocks_update
redirect_to root_path, alert: t('.error') if @post.published? && @post.published_at && post_params['published_at']
end

def redirect_if_removed_content
return if current_user&.admin?

redirect_to root_path, alert: t('.redirect_alert.invalid_user') if @post.removed?
end
end
20 changes: 17 additions & 3 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class ReportsController < ApplicationController
before_action :redirect_unless_published_post
before_action :authorize!, only: %i[index show]
before_action :redirect_if_self_report, only: :create
before_action :set_report, only: %i[reject show remove_content]

def new
set_offences
Expand All @@ -18,13 +19,22 @@ def create

def index
return @reports = Report.granted.all if params[:filter] == 'granted'
return @reports = Report.not_granted.all if params[:filter] == 'not_granted'
return @reports = Report.rejected.all if params[:filter] == 'rejected'

@reports = Report.pending.all
end

def show
@report = Report.find(params[:id])
def show; end

def reject
@report.rejected!
redirect_to @report, notice: t('.success')
end

def remove_content
@report.reportable.removed!
@report.granted!
redirect_to @report, notice: t('.success')
end

private
Expand Down Expand Up @@ -57,6 +67,10 @@ def set_offences
]
end

def set_report
@report = Report.find(params[:id])
end

def post_and_published?
return true unless @reportable.is_a? Post

Expand Down
2 changes: 2 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Comment < ApplicationRecord
has_many :reports, as: :reportable, dependent: :destroy
has_many :notifications, as: :notifiable, dependent: :destroy

enum status: { published: 0, removed: 20 }

after_create :notify_interested_users
after_create :create_notification

Expand Down
5 changes: 3 additions & 2 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Post < ApplicationRecord
validate :file_size
validate :validate_published_at

enum status: { published: 0, archived: 5, draft: 10, scheduled: 15 }
enum status: { published: 0, archived: 5, draft: 10, scheduled: 15, removed: 20 }
acts_as_ordered_taggable_on :tags

enum pin: { unpinned: 0, pinned: 10 }
Expand Down Expand Up @@ -89,7 +89,8 @@ def validate_attachment_size(attachment, content_type, size_limit, error_message

def validate_published_at
return if published_at.nil?
return unless scheduled?

errors.add(:published_at, 'não pode estar no passado') if published_at < (Time.zone.now - 1.second)
errors.add(:published_at, 'não pode estar no passado') if published_at < Time.zone.now
end
end
2 changes: 1 addition & 1 deletion app/models/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Report < ApplicationRecord
belongs_to :profile
belongs_to :reportable, polymorphic: true

enum status: { pending: 0, granted: 5, not_granted: 9 }
enum status: { pending: 0, granted: 5, rejected: 9 }

def truncated_message
message.truncate(50)
Expand Down
64 changes: 34 additions & 30 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -69,41 +69,45 @@
<%= @post.comments.count %> <%= Comment.model_name.human(count: @post.comments.count) %>
</div>
<% @post.comments.each do |comment| %>
<div class="card-body" id="<%= dom_id(comment) %>">
<blockquote class="blockquote mb-0">
<p><%= comment.message %></p>
<footer class="blockquote-footer">
<%= link_to comment.user.full_name, comment.user.profile %> <%= '(autor)' if comment.user == @post.user %>
</footer>
</blockquote>
<div class="card-body comment" id="<%= dom_id(comment) %>">
<% if comment.removed?%>
<p><%= t('comments.removed_content') %> </p>
<% else %>
<blockquote class="blockquote mb-0">
<p><%= comment.message %></p>
<footer class="blockquote-footer">
<%= link_to comment.user.full_name, comment.user.profile %> <%= '(autor)' if comment.user == @post.user %>
</footer>
</blockquote>

<% if comment.user.deleted_at.nil? %>
<div class="btn-group flex-column">
<div class="d-flex">
<div class="mt-2 me-2">
<%= comment.likes.count %> <%= Like.model_name.human(count: comment.likes.count) %>
</div>
<% if comment.user.deleted_at.nil? %>
<div class="btn-group flex-column">
<div class="d-flex">
<div class="mt-2 me-2">
<%= comment.likes.count %> <%= Like.model_name.human(count: comment.likes.count) %>
</div>

<div class="me-2">
<% if user_signed_in? && comment.likes.where(user_id: current_user.id).any? %>
<% like = comment.likes.find_by(user_id: current_user.id) %>
<%= button_to comment_like_path(comment, like), method: :delete, class: 'btn btn-sm', id: 'unlike' do %>
<%= image_tag 'thumbs-up-solid', width: '20rem', class: 'mb-4' %>
<div class="me-2">
<% if user_signed_in? && comment.likes.where(user_id: current_user.id).any? %>
<% like = comment.likes.find_by(user_id: current_user.id) %>
<%= button_to comment_like_path(comment, like), method: :delete, class: 'btn btn-sm', id: 'unlike' do %>
<%= image_tag 'thumbs-up-solid', width: '20rem', class: 'mb-4' %>
<% end %>
<% else %>
<%= button_to comment_likes_path(comment), method: :post, class: 'btn btn-sm', id: 'like' do %>
<%= image_tag 'thumbs-up-regular', width: '20rem', class: 'mb-2' %>
<% end %>
<% end %>
<% else %>
<%= button_to comment_likes_path(comment), method: :post, class: 'btn btn-sm', id: 'like' do %>
<%= image_tag 'thumbs-up-regular', width: '20rem', class: 'mb-2' %>
<% end %>
<% end %>
</div>
</div>
</div>

<% if current_user != comment.user %>
<div class="report-link-wrapper">
<%= link_to t('reports.report_btn'), new_report_path(reportable: comment, reportable_type: comment.class), class: 'btn btn-secondary btn-sm' %>
</div>
<% end %>
</div>
<% if current_user != comment.user %>
<div class="report-link-wrapper">
<%= link_to t('reports.report_btn'), new_report_path(reportable: comment, reportable_type: comment.class), class: 'btn btn-secondary btn-sm' %>
</div>
<% end %>
</div>
<% end %>
<% end %>
</div>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/profiles/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<% end %>
</div>
<div class="mt-2">
<%= link_to t('reports.report_btn'), new_report_path(reportable: @profile.id, reportable_type: @profile.class), class: 'btn btn-dark btn-sm' %>
<%= link_to t('reports.report_btn'), new_report_path(reportable: @profile.id, reportable_type: @profile.class), class: 'btn btn-secondary btn-sm' %>
</div>
<% end %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/reports/_post.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="card text-dark d-flex flex-column px-5 shadow mb-4 p-3 rounded justify-content-center">
<div class="card-body">
<h2 class="card-title"><%= post.title %></h2>
<h2 class="card-title"><%= link_to post.title, post_path(post) %></h2>
<h6 class="card-subtitle mb-2">
<%= t('posts.views.show.authored_by', author_name: post.user.full_name) %>
</h6>
Expand Down
14 changes: 7 additions & 7 deletions app/views/reports/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<nav class="d-flex nav nav-underline">
<%= link_to t('reports.pending'), reports_path, class: "nav-item nav-link link-body-emphasis #{'active' if params[:filter].blank? }" %>
<%= link_to t('reports.granted'), reports_path(params: { filter: 'granted' }), class: "nav-item nav-link link-body-emphasis #{'active' if params[:filter] == 'granted'}" %>
<%= link_to t('reports.not_granted'), reports_path(params: { filter: 'not_granted' }), class: "nav-item nav-link link-body-emphasis #{'active' if params[:filter] == 'not_granted'}" %>
<%= link_to t('reports.rejected_tab'), reports_path(params: { filter: 'rejected' }), class: "nav-item nav-link link-body-emphasis #{'active' if params[:filter] == 'rejected'}" %>
</nav>
</div>

Expand All @@ -15,13 +15,13 @@
<% if @reports.empty? %>
<p><%= t('reports.empty_state') %></p>
<% else %>
<table class="table table-hover">
<thead>
<table class="table table-hover text-left">
<thead class="thead-light">
<tr>
<th><%= Report.human_attribute_name :offence_type %></th>
<th><%= Report.human_attribute_name :message %></th>
<th><%= Report.human_attribute_name :reportable_type %></th>
<th><%= t('reports.action') %></th>
<th scope="col col-lg-2"><%= Report.human_attribute_name :offence_type %></th>
<th scope="col col-lg-2"><%= Report.human_attribute_name :message %></th>
<th scope="col col-lg-2"><%= Report.human_attribute_name :reportable_type %></th>
<th scope="col col-lg-2"><%= t('reports.action') %></th>
</tr>
</thead>

Expand Down
17 changes: 17 additions & 0 deletions app/views/reports/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@
<p class="card-subtitle mb-2 text-muted">
<%= I18n.t('reports.reporting_profile') %>: <%= link_to @report.profile.full_name, profile_path(@report.profile) %>
</p>
<% if @report.pending? %>
<div class="d-flex flex-row justify-content-center mt-5 gap-5">
<% unless @report.reportable.is_a? Profile %>
<%= button_to t('reports.remove_content_btn'), remove_content_report_path(@report), class:'card-btn flex-column btn btn-danger btn-lg' %>
<% end %>
<%= button_to t('reports.reject_btn'), reject_report_path(@report), class:'card-btn flex-column btn btn-secondary btn-lg ml-2' %>
</div>
<% else %>
<div class="d-flex flex-row justify-content-center mt-5">
<h4 class= "card-subtitle mb-2 text-dark">
<%= I18n.t('reports.rejected') if @report.rejected? %>
</h4>
<h4 class= "card-subtitle mb-2 text-danger">
<%= I18n.t('reports.granted') if @report.granted? %>
</h4>
</div>
<% end %>
</div>
</div>
</aside>
Expand Down
3 changes: 2 additions & 1 deletion config/locales/comments.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ pt-BR:
comments:
create:
success: Comentário enviado com sucesso
error: Não foi possível fazer o comentário
error: Não foi possível fazer o comentário
removed_content: Comentário removido pela administração
2 changes: 1 addition & 1 deletion config/locales/posts.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pt-BR:
status_published: Publicada
posts:
redirect_alert:
invalid_user: Você não pode realizar essa ação
invalid_user: Você não pode acessar este conteúdo ou realizar esta ação
create:
success: "Publicação %{status} com sucesso!"
error: Não foi possível criar sua publicação
Expand Down
15 changes: 13 additions & 2 deletions config/locales/reports.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,24 @@ pt-BR:
not_published_post: Essa publicação não está disponível.
new:
not_published_post: Essa publicação não está disponível.
reject:
success: Denúncia rejeitada com sucesso
remove_content:
success: Conteúdo removido com sucesso
reject_btn: Rejeitar denúncia
report_btn: Denunciar
pending: Pendente
granted: Deferido
not_granted: Indeferido
granted: Conteúdo removido
rejected: Denúncia rejeitada
rejected_tab: Denúncias rejeitadas
empty_state: Nenhuma denúncia encontrada
action: Ver mais
reporting_profile: Denunciado por
reported_when: Denunciado em
see_post: Ver publicação
self_report: Você não pode denunciar sí mesmo ou o próprio conteúdo.
remove_content_btn: Remover conteúdo

pending: Pendente
granted: Conteúdo removido
rejected: Denúncia rejeitada
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
post 'pin', on: :member
end

resources :reports, only: %i[index new create show]
resources :reports, only: %i[index new create show] do
post 'reject', 'remove_content', on: :member
end

resources :posts, only: %i[] do
resources :likes, only: %i[create destroy], module: :posts
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240209191251_add_status_to_comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStatusToComment < ActiveRecord::Migration[7.1]
def change
add_column :comments, :status, :integer, default: 0
end
end
1 change: 1 addition & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@

image_post_one = ActiveStorage::Blob.create_and_upload!(io: File.open(Rails.root.join('app', 'assets', 'images', 'seeds', 'turma_11.jpeg')), filename: 'turma_11.jpeg')
html_post_one = %(<action-text-attachment sgid="#{image_post_one.attachable_sgid}"></action-text-attachment>)
post_joao_1 = joao.posts.create(title: 'Turma 11', content: "A melhor turma de todas<br> #{html_post_one}", tag_list: ['treinadev', 'tdd'])
post_joao_1 = joao.posts.create(published_at: Time.zone.now, title: 'Turma 11', content: "A melhor turma de todas<br> #{html_post_one}", tag_list: ['treinadev', 'tdd'])

post_joao_2 = joao.posts.create(title: 'Warehouses', content: "Vamos aprender a fazer um app de gestão de galpões<br>", tag_list: ['tdd'])
post_joao_2 = joao.posts.create(published_at: Time.zone.now, title: 'Warehouses', content: "Vamos aprender a fazer um app de gestão de galpões<br>", tag_list: ['tdd'])

post_joao_3 = joao.posts.create(title: 'Rubocop: devo usar?', content: "No começo, tem que aprender na marra.<br>", tag_list: ['rubocop'])
post_joao_3 = joao.posts.create(published_at: Time.zone.now, title: 'Rubocop: devo usar?', content: "No começo, tem que aprender na marra.<br>", tag_list: ['rubocop'])

image_post_two = ActiveStorage::Blob.create_and_upload!(io: File.open(Rails.root.join('app', 'assets', 'images', 'seeds', 'git_github.jpg')), filename: 'git_github.jpg')
html_post_two = %(<action-text-attachment sgid="#{image_post_two.attachable_sgid}"></action-text-attachment>)
post_andre_1 = andre.posts.create(title: 'Pull Request', content: "Façam o Pull Request na main antes de usar o código nas branches dos outros<br> #{html_post_two}", tag_list: ['git'])
post_andre_1 = andre.posts.create(published_at: Time.zone.now, title: 'Pull Request', content: "Façam o Pull Request na main antes de usar o código nas branches dos outros<br> #{html_post_two}", tag_list: ['git'])

post_andre_2 = andre.posts.create(title: 'Desafios Exclusivos', content: "Eu fiz o batalha naval mesmo para desafiar a galera<br>", tag_list: ['desafios'])
post_andre_2 = andre.posts.create(published_at: Time.zone.now, title: 'Desafios Exclusivos', content: "Eu fiz o batalha naval mesmo para desafiar a galera<br>", tag_list: ['desafios'])

post_andre_3 = andre.posts.create(title: 'SOLID', content: "Hoje, vamos falar sobre boas prática de desenvolvimento de código<br>", tag_list: ['solid', 'boaspraticas'])
post_andre_3 = andre.posts.create(published_at: Time.zone.now, title: 'SOLID', content: "Hoje, vamos falar sobre boas prática de desenvolvimento de código<br>", tag_list: ['solid', 'boaspraticas'])

image_post_three = ActiveStorage::Blob.create_and_upload!(io: File.open(Rails.root.join('app', 'assets', 'images', 'seeds', 'vue_js.jpg')), filename: 'vue_js.jpg')
html_post_three = %(<action-text-attachment sgid="#{image_post_three.attachable_sgid}"></action-text-attachment>)
post_gabriel_1 = gabriel.posts.create(title: 'Como fazer uma app Vue', content: "Não esqueça de usar o app.mount<br> #{html_post_three}", tag_list: ['vue'])
post_gabriel_1 = gabriel.posts.create(published_at: Time.zone.now, title: 'Como fazer uma app Vue', content: "Não esqueça de usar o app.mount<br> #{html_post_three}", tag_list: ['vue'])

post_gabriel_2 = gabriel.posts.create(title: 'Boas práticas em Zoom', content: "Hoje vamos falar sobre breakout rooms!<br>", tag_list: ['zoom'])
post_gabriel_2 = gabriel.posts.create(published_at: Time.zone.now, title: 'Boas práticas em Zoom', content: "Hoje vamos falar sobre breakout rooms!<br>", tag_list: ['zoom'])

post_gabriel_3 = gabriel.posts.create(title: 'Robô Saltitante: como resolver?', content: "Vamos falar sobre a tarefa mais complexa do Code Saga!<br>", tag_list: ['codesaga'])
post_gabriel_3 = gabriel.posts.create(published_at: Time.zone.now, title: 'Robô Saltitante: como resolver?', content: "Vamos falar sobre a tarefa mais complexa do Code Saga!<br>", tag_list: ['codesaga'])

joao.profile.update(cover_letter: 'Sou profissional organizado, esforçado e apaixonado pelo que faço', work_status: 'unavailable')
andre.profile.update(cover_letter: 'Sou profissional organizado, esforçado e apaixonado pelo que faço', work_status: 'open_to_work')
Expand Down
2 changes: 1 addition & 1 deletion spec/models/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
context 'data de publicação' do
it 'não deve ser no passado' do
user = create(:user)
post = build(:post, user:, published_at: Time.zone.yesterday)
post = build(:post, user:, published_at: Time.zone.yesterday, status: :scheduled)

expect(post).not_to be_valid
expect(post.errors[:published_at]).to include('não pode estar no passado')
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/posts/user_edit_post_status_pin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
post pin_post_path(post)

expect(response).to redirect_to(root_path)
expect(flash[:alert]).to eq('Você não pode realizar essa ação')
expect(flash[:alert]).to eq('Você não pode acessar este conteúdo ou realizar esta ação')
expect(post.reload.pinned?).to eq(false)
end
end
2 changes: 1 addition & 1 deletion spec/requests/reports/user_reports_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

it 'mas post está agendado' do
user = create(:user)
post = create(:post, status: :scheduled, published_at: Time.current)
post = create(:post, status: :scheduled, published_at: Time.current + 5.seconds)

login_as user
post reports_path, params: {
Expand Down
2 changes: 1 addition & 1 deletion spec/system/posts/user_edits_post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
visit edit_post_path(post)

expect(current_path).to eq root_path
expect(page).to have_content 'Você não pode realizar essa ação'
expect(page).to have_content 'Você não pode acessar este conteúdo ou realizar esta ação'
end

it 'mas não vê o link de editar caso não seja seu post' do
Expand Down
Loading

0 comments on commit 19a931c

Please sign in to comment.