diff --git a/app/controllers/ajax/smile_controller.rb b/app/controllers/ajax/smile_controller.rb index 078c6b31..4ff67eaf 100644 --- a/app/controllers/ajax/smile_controller.rb +++ b/app/controllers/ajax/smile_controller.rb @@ -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 diff --git a/app/models/appendable/reaction.rb b/app/models/appendable/reaction.rb index 3d1a6e28..77d2726b 100644 --- a/app/models/appendable/reaction.rb +++ b/app/models/appendable/reaction.rb @@ -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 diff --git a/app/models/comment.rb b/app/models/comment.rb index 1392067c..00703f6e 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 } diff --git a/app/models/notification.rb b/app/models/notification.rb index 97670ae2..a0e91b1c 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -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! diff --git a/app/models/user.rb b/app/models/user.rb index 8f40e3f2..14862120 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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) diff --git a/app/views/answerbox/_comments.haml b/app/views/answerbox/_comments.haml index 8d4ee3b9..43b196f0 100644 --- a/app/views/answerbox/_comments.haml +++ b/app/views/answerbox/_comments.haml @@ -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 diff --git a/app/views/navigation/main/_notifications.haml b/app/views/navigation/main/_notifications.haml index 80f43d9c..ee192700 100644 --- a/app/views/navigation/main/_notifications.haml +++ b/app/views/navigation/main/_notifications.haml @@ -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 diff --git a/app/views/notifications/type/_commentsmile.haml b/app/views/notifications/type/_commentsmile.haml deleted file mode 100644 index b5567eb8..00000000 --- a/app/views/notifications/type/_commentsmile.haml +++ /dev/null @@ -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 ? '[...]' : '') diff --git a/app/views/notifications/type/_reaction.haml b/app/views/notifications/type/_reaction.haml new file mode 100644 index 00000000..850fa4c5 --- /dev/null +++ b/app/views/notifications/type/_reaction.haml @@ -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 ? '[...]' : '') diff --git a/app/views/notifications/type/_smile.haml b/app/views/notifications/type/_smile.haml deleted file mode 100644 index 9a94d92b..00000000 --- a/app/views/notifications/type/_smile.haml +++ /dev/null @@ -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 ? '[...]' : '')