Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mensagens de notificações #174

Merged
merged 10 commits into from
Feb 12, 2024
2 changes: 1 addition & 1 deletion app/controllers/notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ class NotificationsController < ApplicationController
before_action :authenticate_user!

def index
@notifications = current_user.profile.notifications
@notifications = current_user.profile.notifications.order(created_at: :desc)
end
end
12 changes: 12 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,16 @@ class Comment < ApplicationRecord
belongs_to :user
has_many :likes, as: :likeable, dependent: :destroy
has_many :reports, as: :reportable, dependent: :destroy
has_one :notification, as: :notifiable, dependent: :destroy

after_create :create_notification

private

def create_notification
comment_author = user.profile
return if comment_author == post.user.profile

Notification.create(profile: post.user.profile, notifiable: self)
end
end
2 changes: 2 additions & 0 deletions app/models/like.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Like < ApplicationRecord

def create_notification
post_author = likeable.user.profile
return if user == post_author.user

Notification.create(profile: post_author, notifiable: self)
end
end
1 change: 1 addition & 0 deletions app/views/notifications/_comment.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><%= link_to notification.notifiable.user.full_name, profile_path(notification.notifiable.user.profile) %> comentou em sua publicação
1 change: 1 addition & 0 deletions app/views/notifications/_connection.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><%= link_to notification.notifiable.follower.user.full_name, profile_path(notification.notifiable.follower) %> começou a te seguir
1 change: 1 addition & 0 deletions app/views/notifications/_invitation.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Você recebeu um convite para <%= link_to notification.notifiable.project_title, invitation_path(notification.notifiable) %>
5 changes: 5 additions & 0 deletions app/views/notifications/_like.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% if notification.notifiable.likeable.is_a?Post %>
<p><%= link_to notification.notifiable.user.full_name, profile_path(notification.notifiable.user.profile) %> curtiu sua publicação
<% else %>
<p><%= link_to notification.notifiable.user.full_name, profile_path(notification.notifiable.user.profile) %> curtiu seu comentário
<% end %>
1 change: 1 addition & 0 deletions app/views/notifications/_post.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><%= notification.notifiable.user.full_name %> fez uma <%= link_to 'publicação', post_path(notification.notifiable) %>
13 changes: 4 additions & 9 deletions app/views/notifications/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
<div class="container d-flex flex-column align-items-center">
<h2>Notificações</h2>
<% if @notifications&.any? %>
<div class="list-group">
<% @notifications.each do |notification| %>
<div class="list-group">
<% if notification.notifiable.is_a?Post %>
<p><%= notification.notifiable.user.full_name %> fez uma <%= link_to 'publicação', post_path(notification.notifiable) %>
<% elsif notification.notifiable.is_a?Invitation %>
<p>Você recebeu um convite para <%= link_to notification.notifiable.project_title, invitation_path(notification.notifiable) %>
<% elsif notification.notifiable.is_a?Connection %>
<p><%= link_to notification.notifiable.follower.user.full_name, profile_path(notification.notifiable.follower) %> começou a te seguir
<% end %>
<%= render(
partial: "#{notification.notifiable.class.name.downcase}" ,
locals: { notification: notification }
)%>
há <%= distance_of_time_in_words_to_now(notification.notifiable.created_at) %></p>
</div>
<% end %>
</div>
<% else %>
<p>Nenhuma notificação encontrada</p>
<% end %>
Expand Down
4 changes: 3 additions & 1 deletion app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
<%= link_to 'Convites', invitations_path, class: 'nav-link' %>
</li>
<li class="dropdown-item">
<%= link_to 'Notificações', notifications_path, class: 'nav-link' %>
<%= link_to notifications_path, class: 'nav-link' do %>
Notificações <span class="badge bg-primary"><%= current_user.profile.notifications.count %></span>
<% end %>
</li>
<% if current_user.admin? %>
<li class="dropdown-item" href="#">
Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb

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

20 changes: 20 additions & 0 deletions spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,24 @@
expect(comment.errors[:message]).to include('não pode ficar em branco')
end
end

describe '#create_notification' do
it 'cria notificação para o dono da publicação' do
user = create(:user)
other_user = create(:user)
post = create(:post, user:)
comment = create(:comment, user: other_user, post:)

expect(Notification.count).to eq 1
expect(Notification.last.notifiable).to eq comment
end

it 'não cria notificação para o dono do comentário' do
user = create(:user)
post = create(:post, user:)
create(:comment, user:, post:)

expect(Notification.count).to eq 0
end
end
end
20 changes: 20 additions & 0 deletions spec/models/like_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,24 @@
expect(like).to be_valid
end
end

describe '#create_notification' do
it 'cria notificação para o dono da publicação' do
user = create(:user)
other_user = create(:user)
post = create(:post, user:)
like = create(:like, likeable: post, user: other_user)

expect(Notification.count).to eq 1
expect(Notification.last.notifiable).to eq like
end

it 'não cria notificação para o dono do like' do
user = create(:user)
post = create(:post, user:)
create(:like, likeable: post, user:)

expect(Notification.count).to eq 0
end
end
end
66 changes: 66 additions & 0 deletions spec/system/notifications/user_sees_comments_notification_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require 'rails_helper'

describe 'Usuário vê notificação de comentário' do
it 'ao comentarem na sua publicação' do
user = create(:user)
post = create(:post, user:)
other_user = create(:user)
comment = create(:comment, post:, user: other_user)

login_as user
visit notifications_path

expect(Notification.count).to eq 1
expect(page).to have_current_path notifications_path
expect(page).to have_content 'comentou em sua publicação'
expect(page).to have_link comment.user.profile.full_name, href: profile_path(comment.user.profile)
end

it 'e não vê notificação de seu comentário' do
user = create(:user)
post = create(:post, user:)
comment = create(:comment, post:, user:)

login_as user
visit notifications_path

expect(Notification.count).to eq 0
expect(page).not_to have_content 'comentou em sua publicação'
expect(page).not_to have_link comment.user.profile.full_name, href: profile_path(comment.user.profile)
end

it 'ao curtirem seu comentário' do
user = create(:user)
post = create(:post)
comment = create(:comment, post:, user:)
like = create(:like, likeable: comment)

login_as user
visit root_path
click_button class: 'dropdown-toggle'
within 'nav' do
click_on 'Notificações'
end

expect(page).to have_current_path notifications_path
expect(page).to have_content 'curtiu seu comentário'
expect(page).to have_link like.user.profile.full_name, href: profile_path(like.user.profile)
end

it 'e não recebe ao curtir seu próprio comentário' do
user = create(:user)
post = create(:post)
comment = create(:comment, post:, user:)
like = create(:like, likeable: comment, user:)

login_as user
visit root_path
click_button class: 'dropdown-toggle'
within 'nav' do
click_on 'Notificações'
end

expect(page).not_to have_content 'curtiu seu comentário'
expect(page).not_to have_link like.user.profile.full_name, href: profile_path(like.user.profile)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,13 @@
invitation = create(:invitation, profile: user.profile, created_at: 1.day.ago)

login_as user
visit root_path
click_button class: 'dropdown-toggle'
within 'nav' do
click_on 'Notificações'
end
visit notifications_path

expect(page).to have_current_path notifications_path
expect(page).to have_content "Você recebeu um convite para #{invitation.project_title} há 1 dia"
expect(page).to have_link invitation.project_title, href: invitation_path(invitation)
end

it 'e deve estar logado' do
visit notifications_path

expect(page).to have_current_path new_user_session_path
end

it 'e não tem convites' do
user = create(:user)

login_as user

visit notifications_path

expect(page).to have_content 'Nenhuma notificação'
end

it 'e não vê convites de outros usuários' do
user = create(:user)
invitation = create(:invitation, profile: user.profile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@
Connection.create!(followed_profile: followed.profile, follower: follower.profile)

login_as followed
visit root_path
click_button class: 'dropdown-toggle'
within 'nav' do
click_on 'Notificações'
end
visit notifications_path

expect(page).to have_current_path notifications_path
expect(page).to have_content 'começou a te seguir'
expect(page).to have_content 'Paulo começou a te seguir'
expect(page).to have_link follower.full_name, href: profile_path(follower)
end
end
58 changes: 58 additions & 0 deletions spec/system/notifications/user_sees_notifications_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require 'rails_helper'

describe 'Usuário vê notificações' do
it 'com sucesso' do
user = create(:user)
other_user = create(:user)
Connection.create!(follower: user.profile, followed_profile: other_user.profile)
Connection.create!(follower: other_user.profile, followed_profile: user.profile)
post = create(:post, user:)

new_post_notification_job_spy = spy(NewPostNotificationJob)
stub_const('NewPostNotificationJob', new_post_notification_job_spy)

create(:post, user: other_user)
create(:like, likeable: post, user: other_user)
create(:comment, post:, user: other_user)
comment_user = create(:comment, post:, user:)
create(:like, likeable: comment_user, user: other_user)
create(:invitation, profile: user.profile)

login_as user
visit root_path
click_button class: 'dropdown-toggle'
within 'nav' do
click_on 'Notificações'
end

expect(page).to have_current_path notifications_path
expect(new_post_notification_job_spy).to have_received(:perform_later)
expect(user.profile.notifications.count).to eq 5
expect(page).to have_content 'começou a te seguir'
expect(page).to have_content 'curtiu sua publicação'
expect(page).to have_content 'comentou em sua publicação'
expect(page).to have_content 'curtiu seu comentário'
expect(page).to have_content 'Você recebeu um convite'
expect(page.body.index('começou a te seguir')).to be > page.body.index('curtiu sua publicação')
expect(page.body.index('curtiu sua publicação')).to be > page.body.index('comentou em sua publicação')
expect(page.body.index('comentou em sua publicação')).to be > page.body.index('curtiu seu comentário')
expect(page.body.index('curtiu seu comentário')).to be > page.body.index('Você recebeu um convite')
hreis1 marked this conversation as resolved.
Show resolved Hide resolved
end

it 'deve estar logado' do
visit notifications_path

expect(page).to have_current_path new_user_session_path
end

it 'e não tem notificações' do
user = create(:user)

login_as user

visit notifications_path

expect(page).to have_content 'Nenhuma notificação'
expect(Notification.count).to eq 0
end
end
54 changes: 47 additions & 7 deletions spec/system/notifications/user_sees_post_notification_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,58 @@
require 'rails_helper'

describe 'Nova publicação envia notificação para seguidores' do
it 'com sucesso' do
describe 'Usuário vê notificações de publicações' do
it 'e visualiza a notificação' do
follower = create(:user, full_name: 'Paulo')
followed = create(:user, full_name: 'Ana')
Connection.create!(followed_profile: followed.profile, follower: follower.profile)
new_post_notification_job_spy = spy(NewPostNotificationJob)
stub_const('NewPostNotificationJob', new_post_notification_job_spy)
create(:post, user: followed)
post = create(:post, user: followed)

NewPostNotificationJob.perform_now(post)

login_as follower
visit notifications_path

expect(page).to have_current_path notifications_path
expect(new_post_notification_job_spy).to have_received(:perform_later)
expect(page).to have_content 'Ana fez uma publicação'
expect(page).to have_link 'publicação', href: post_path(post)
end

context 'nova publicação notifica seguidores' do
it 'com sucesso' do
follower = create(:user, full_name: 'Paulo')
followed = create(:user, full_name: 'Ana')
Connection.create!(followed_profile: followed.profile, follower: follower.profile)
new_post_notification_job_spy = spy(NewPostNotificationJob)
stub_const('NewPostNotificationJob', new_post_notification_job_spy)
create(:post, user: followed)

expect(new_post_notification_job_spy).to have_received(:perform_later)
end
end

context 'e vê curtidas' do
it 'em seu post' do
user = create(:user)
post = create(:post, user:)
like = create(:like, likeable: post)

login_as user
visit notifications_path

expect(page).to have_current_path notifications_path
expect(page).to have_content 'curtiu sua publicação'
expect(page).to have_link like.user.full_name, href: profile_path(like.user.profile)
end

it 'e não recebe quando curte sua própria publicação' do
user = create(:user)
post = create(:post, user:)
like = create(:like, likeable: post, user:)

login_as user
visit notifications_path

expect(page).not_to have_content 'curtiu sua publicação'
expect(page).not_to have_link like.user.profile.full_name, href: profile_path(like.user.profile)
end
end
end
Loading