Replace usages of smiles with reaction appendables

This commit is contained in:
Karina Kwiatek 2022-02-27 15:43:19 +01:00 committed by Karina Kwiatek
parent ac6798877d
commit 0cfe5ee964
10 changed files with 44 additions and 60 deletions

View File

@ -47,7 +47,7 @@ class Ajax::SmileController < AjaxController
comment = Comment.find(params[:id])
begin
current_user.smile_comment comment
current_user.smile comment
rescue Errors::Base => e
@response[:status] = e.code
@response[:message] = I18n.t(e.locale_tag)
@ -70,7 +70,7 @@ class Ajax::SmileController < AjaxController
comment = Comment.find(params[:id])
begin
current_user.unsmile_comment comment
current_user.unsmile comment
rescue => e
Sentry.capture_exception(e)
@response[:status] = :fail

View File

@ -1,4 +1,19 @@
# frozen_string_literal: true
class Appendable::Reaction < Appendable
after_create do
Notification.notify parent.user, self unless parent.user == user
user.increment! :smiled_count
parent.increment! :smile_count
end
before_destroy do
Notification.denotify parent.user, self unless parent.user == user
user.decrement! :smiled_count
answer.decrement! :smile_count
end
def notification_type(*_args)
Notifications::Smiled
end
end

View File

@ -3,7 +3,7 @@ class Comment < ApplicationRecord
belongs_to :answer
validates :user_id, presence: true
validates :answer_id, presence: true
has_many :smiles, class_name: "CommentSmile", foreign_key: :comment_id, dependent: :destroy
has_many :smiles, class_name: "Appendable::Reaction", foreign_key: :parent_id, dependent: :destroy
validates :content, length: { maximum: 160 }

View File

@ -39,6 +39,7 @@ class Notification < ApplicationRecord
def make_notification(recipient, target, notification_type)
n = notification_type.new(target: target,
target_type: target.type, # To ensure we don't get a type of 'Appenddable'
recipient: recipient,
new: true)
n.save!

View File

@ -127,27 +127,6 @@ class User < ApplicationRecord
question.answers.pluck(:user_id).include? self.id
end
# smiles a comment
# @param comment [Comment] the comment to smile
def smile_comment(comment)
# rubocop:disable Style/RedundantSelf
raise Errors::ReactingSelfBlockedOther if self.blocking?(comment.user)
raise Errors::ReactingOtherBlockedSelf if comment.user.blocking?(self)
# rubocop:enable Style/RedundantSelf
CommentSmile.create!(user: self, comment: comment)
end
# unsmile an comment
# @param comment [Comment] the comment to unsmile
def unsmile_comment(comment)
CommentSmile.find_by(user: self, comment: comment).destroy
end
def smiled_comment?(comment)
comment.smiles.pluck(:user_id).include? self.id
end
def comment(answer, content)
# rubocop:disable Style/RedundantSelf
raise Errors::CommentingSelfBlockedOther if self.blocking?(answer.user)

View File

@ -24,7 +24,7 @@
%i.fa.fa-fw.fa-smile-o
%span{ id: "ab-comment-smile-count-#{comment.id}" }= comment.smile_count
- if user_signed_in?
- if current_user.smiled_comment? comment
- if current_user.smiled? comment
%button.btn.btn-link.answerbox__action{ type: :button, name: "ab-smile-comment", data: { c_id: comment.id, action: :unsmile } }
%i.fa.fa-fw.fa-frown-o
%span{ id: "ab-comment-smile-count-#{comment.id}" }= comment.smile_count

View File

@ -19,7 +19,7 @@
- else
- notifications.each do |notification|
.dropdown-item
= render "notifications/type/#{notification.target_type.downcase}", notification: notification
= render "notifications/type/#{notification.target_type.downcase.split('::').last}", notification: notification
%a.dropdown-item.text-center{ href: notifications_path }
%i.fa.fa-fw.fa-chevron-right

View File

@ -1,17 +0,0 @@
.media.notification
.notification__icon
%i.fa.fa-2x.fa-fw.fa-smile-o
.media-body
.notification__heading
%img.avatar-xs{ src: notification.target.user.profile_picture.url(:small) }
= user_screen_name notification.target.user
smiled
= link_to 'your comment', show_user_answer_path(username: notification.target.user.screen_name, id: notification.target.comment.answer.id)
= time_tooltip(notification.target)
ago
.list-group
.list-group-item
.media.question-media
.media-body
%h6.notification__list-heading comment
= markdown notification.target.comment.content[0..60] + (notification.target.comment.content.length > 60 ? '[...]' : '')

View File

@ -0,0 +1,23 @@
.media.notification
.notification__icon
%i.fa.fa-2x.fa-fw.fa-smile-o
.media-body
.notification__heading
%img.avatar-xs{ src: notification.target.user.profile_picture.url(:small) }
= user_screen_name notification.target.user
smiled
- if notification.target.parent_type == 'Answer'
= link_to 'your answer', show_user_answer_path(username: notification.target.user.screen_name, id: notification.target.parent.id)
- elsif notification.target.parent_type == 'Comment'
= link_to 'your comment', show_user_answer_path(username: notification.target.user.screen_name, id: notification.target.parent.answer.id)
= time_tooltip(notification.target)
ago
.list-group
.list-group-item
.media.question-media
.media-body
- if notification.target.parent_type == 'Answer'
%h6.notification__list-heading answer
- elsif notification.target.parent_type == 'Comment'
%h6.notification__list-heading comment
= markdown notification.target.parent.content[0..60] + (notification.target.parent.content.length > 60 ? '[...]' : '')

View File

@ -1,17 +0,0 @@
.media.notification
.notification__icon
%i.fa.fa-2x.fa-fw.fa-smile-o
.media-body
.notification__heading
%img.avatar-xs{ src: notification.target.user.profile_picture.url(:small) }
= user_screen_name notification.target.user
smiled
= link_to 'your answer', show_user_answer_path(username: notification.target.user.screen_name, id: notification.target.answer.id)
= time_tooltip(notification.target)
ago
.list-group
.list-group-item
.media.question-media
.media-body
%h6.notification__list-heading answer
= markdown notification.target.answer.content[0..60] + (notification.target.answer.content.length > 60 ? '[...]' : '')