Skip to content

Commit

Permalink
Add notifications for likes on comments
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperTux88 committed Nov 13, 2023
1 parent 3e1407d commit b0c196a
Show file tree
Hide file tree
Showing 22 changed files with 218 additions and 73 deletions.
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ Layout/HashAlignment:
EnforcedHashRocketStyle: table
EnforcedColonStyle: table

# This rule makes haml files less readable, as there is no 'end' there.
Layout/CaseIndentation:
Exclude:
- "app/views/**/*"

# Mixing the styles looks just silly.
Style/HashSyntax:
EnforcedStyle: ruby19_no_mixed_keys
Expand Down
58 changes: 22 additions & 36 deletions app/controllers/notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,9 @@
class NotificationsController < ApplicationController
before_action :authenticate_user!

def update
note = Notification.where(:recipient_id => current_user.id, :id => params[:id]).first
if note
note.set_read_state(params[:set_unread] != "true" )

respond_to do |format|
format.json { render :json => { :guid => note.id, :unread => note.unread } }
end

else
respond_to do |format|
format.json { render :json => {}.to_json }
end
end
end

def index
conditions = {:recipient_id => current_user.id}
conditions = {recipient_id: current_user.id}
types = NotificationService::NOTIFICATIONS_JSON_TYPES
if params[:type] && types.has_key?(params[:type])
conditions[:type] = types[params[:type]]
end
Expand All @@ -33,7 +18,7 @@ def index
per_page = params[:per_page] || 25
@notifications = WillPaginate::Collection.create(page, per_page, Notification.where(conditions).count ) do |pager|
result = Notification.where(conditions)
.includes(:target, :actors => :profile)
.includes(:target, actors: :profile)
.order("updated_at desc")
.limit(pager.per_page)
.offset(pager.offset)
Expand All @@ -52,13 +37,28 @@ def index

respond_to do |format|
format.html
format.xml { render :xml => @notifications.to_xml }
format.xml { render xml: @notifications.to_xml }
format.json {
render json: render_as_json(@unread_notification_count, @grouped_unread_notification_counts, @notifications)
}
end
end

def update
note = Notification.where(recipient_id: current_user.id, id: params[:id]).first
if note
note.set_read_state(params[:set_unread] != "true")

respond_to do |format|
format.json { render json: {guid: note.id, unread: note.unread} }
end
else
respond_to do |format|
format.json { render json: {}.to_json }
end
end
end

def default_serializer_options
{
context: self,
Expand All @@ -67,7 +67,7 @@ def default_serializer_options
end

def read_all
current_type = types[params[:type]]
current_type = NotificationService::NOTIFICATIONS_JSON_TYPES[params[:type]]
notifications = Notification.where(recipient_id: current_user.id, unread: true)
notifications = notifications.where(type: current_type) if params[:type]
notifications.update_all(unread: false)
Expand All @@ -79,8 +79,8 @@ def read_all
format.html { redirect_to stream_path }
format.mobile { redirect_to stream_path }
end
format.xml { render :xml => {}.to_xml }
format.json { render :json => {}.to_json }
format.xml { render xml: {}.to_xml }
format.json { render json: {}.to_json }
end
end

Expand All @@ -95,18 +95,4 @@ def render_as_json(unread_count, unread_count_by_type, notification_list)
}
}.as_json
end

def types
{
"also_commented" => "Notifications::AlsoCommented",
"comment_on_post" => "Notifications::CommentOnPost",
"liked" => "Notifications::Liked",
"mentioned" => "Notifications::MentionedInPost",
"mentioned_in_comment" => "Notifications::MentionedInComment",
"reshared" => "Notifications::Reshared",
"started_sharing" => "Notifications::StartedSharing",
"contacts_birthday" => "Notifications::ContactsBirthday"
}
end
helper_method :types
end
13 changes: 12 additions & 1 deletion app/helpers/notifications_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def object_link(note, actors_html)
elsif %w(Notifications::CommentOnPost Notifications::AlsoCommented Notifications::Reshared Notifications::Liked)
.include?(note.type)
opts.merge!(opts_for_post(note.linked_object))
elsif note.is_a?(Notifications::LikedComment)
opts.merge!(opts_for_comment(note.linked_object))
elsif note.is_a?(Notifications::ContactsBirthday)
opts.merge!(opts_for_birthday(note))
end
Expand All @@ -33,7 +35,16 @@ def opts_for_post(post)
post_link: link_to(post_page_title(post),
post_path(post),
data: {ref: post.id},
class: "hard_object_link").html_safe
class: "hard_object_link")
}
end

def opts_for_comment(comment)
{
comment_link: link_to(comment.message.title,
post_path(comment.post, anchor: comment.guid),
data: {ref: comment.id},
class: "hard_object_link")
}
end

Expand Down
16 changes: 16 additions & 0 deletions app/mailers/notification_mailers/liked_comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module NotificationMailers
class LikedComment < NotificationMailers::Base
attr_accessor :like

delegate :target, to: :like, prefix: true

def set_headers(like_id) # rubocop:disable Naming/AccessorMethodName
@like = Like.find(like_id)

@headers[:subject] = I18n.t("notifier.liked_comment.liked", name: @sender.name)
@headers[:in_reply_to] = @headers[:references] = "<#{@like.parent.commentable.guid}@#{AppConfig.pod_uri.host}>"
end
end
end
26 changes: 26 additions & 0 deletions app/models/notifications/liked_comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Notifications
class LikedComment < Notification
def mail_job
Workers::Mail::LikedComment
end

def popup_translation_key
"notifications.liked_comment"
end

def deleted_translation_key
"notifications.liked_comment_deleted"
end

def self.notify(like, _recipient_user_ids)
actor = like.author
target_author = like.target.author

return unless like.target_type == "Comment" && target_author.local? && actor != target_author

concatenate_or_create(target_author.owner, like.target, actor).email_the_user(like, actor)
end
end
end
23 changes: 13 additions & 10 deletions app/models/user_preference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ class UserPreference < ApplicationRecord
validate :must_be_valid_email_type

VALID_EMAIL_TYPES =
["someone_reported",
"mentioned",
"mentioned_in_comment",
"comment_on_post",
"private_message",
"started_sharing",
"also_commented",
"liked",
"reshared",
"contacts_birthday"]
%w[
someone_reported
mentioned
mentioned_in_comment
comment_on_post
private_message
started_sharing
also_commented
liked
liked_comment
reshared
contacts_birthday
].freeze

def must_be_valid_email_type
unless VALID_EMAIL_TYPES.include?(self.email_type)
Expand Down
3 changes: 2 additions & 1 deletion app/services/notification_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class NotificationService
NOTIFICATION_TYPES = {
Comment => [Notifications::MentionedInComment, Notifications::CommentOnPost, Notifications::AlsoCommented],
Like => [Notifications::Liked],
Like => [Notifications::Liked, Notifications::LikedComment],
StatusMessage => [Notifications::MentionedInPost],
Conversation => [Notifications::PrivateMessage],
Message => [Notifications::PrivateMessage],
Expand All @@ -15,6 +15,7 @@ class NotificationService
"also_commented" => "Notifications::AlsoCommented",
"comment_on_post" => "Notifications::CommentOnPost",
"liked" => "Notifications::Liked",
"liked_comment" => "Notifications::LikedComment",
"mentioned" => "Notifications::MentionedInPost",
"mentioned_in_comment" => "Notifications::MentionedInComment",
"reshared" => "Notifications::Reshared",
Expand Down
7 changes: 5 additions & 2 deletions app/views/notifications/_notification.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.media.stream-element{data: {guid: note.id, type: (types.key(note.type) || "")},
class: (note.unread ? "unread" : "read")}
.media.stream-element{data: {
guid: note.id,
type: (NotificationService::NOTIFICATIONS_JSON_TYPES.key(note.type) || "")
},
class: (note.unread ? "unread" : "read")}
.unread-toggle.pull-right
%i.entypo-eye{title: (note.unread ? t("notifications.index.mark_read") : t("notifications.index.mark_unread"))}
- if note.type == "Notifications::StartedSharing" && (!defined?(no_aspect_dropdown) || !no_aspect_dropdown)
Expand Down
7 changes: 5 additions & 2 deletions app/views/notifications/_notification.mobile.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.notification_element{data: {guid: note.id, type: (types.key(note.type) || "")},
class: (note.unread ? "unread" : "read")}
.notification_element{data: {
guid: note.id,
type: (NotificationService::NOTIFICATIONS_JSON_TYPES.key(note.type) || "")
},
class: (note.unread ? "unread" : "read")}
.pull-right.unread-toggle
%i.entypo-eye{title: (note.unread ? t("notifications.index.mark_read") : t("notifications.index.mark_unread"))}
= person_image_tag note.actors.first, :thumb_small
Expand Down
2 changes: 1 addition & 1 deletion app/views/notifications/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
- case key
- when "also_commented", "comment_on_post"
%i.entypo-comment
- when "liked"
- when "liked", "liked_comment"
%i.entypo-heart
- when "mentioned", "mentioned_in_comment"
%span.mentionIcon
Expand Down
4 changes: 2 additions & 2 deletions app/views/notifier/liked.html.haml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
- if @notification.like_target.public?
%p
#{t('.liked', name: @notification.sender_name)}:
#{t(".liked", name: @notification.sender_name)}:
= post_message(@notification.like_target, html: true)
- else
%p
#{t('notifier.liked.limited_post', name: @notification.sender_name)}.
#{t(".limited_post", name: @notification.sender_name)}.

%p
= link_to t(".view_post"), post_url(@notification.like_target)
2 changes: 1 addition & 1 deletion app/views/notifier/liked.text.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<%= post_message(@notification.like_target) %>
<% else %>
<%= "#{t("notifier.liked.limited_post", name: @notification.sender_name)}." %>
<%= "#{t(".limited_post", name: @notification.sender_name)}." %>
<% end %>
<%= t(".view_post") %>
Expand Down
10 changes: 10 additions & 0 deletions app/views/notifier/liked_comment.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- if @notification.like_target.public?
%p
#{t(".liked", name: @notification.sender_name)}:
= post_message(@notification.like_target, html: true)
- else
%p
#{t(".limited_post", name: @notification.sender_name)}.

%p
= link_to t(".view_comment"), post_url(@notification.like_target.root, anchor: @notification.like_target.guid)
10 changes: 10 additions & 0 deletions app/views/notifier/liked_comment.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<% if @notification.like_target.public? %>
<%= "#{t(".liked", name: @notification.sender_name)}:" %>
<%= post_message(@notification.like_target) %>
<% else %>
<%= "#{t(".limited_post", name: @notification.sender_name)}." %>
<% end %>
<%= t(".view_comment") %>
<%= post_url(@notification.like_target.root, anchor: @notification.like_target.guid) %>
5 changes: 5 additions & 0 deletions app/views/users/_edit.haml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@
= t(".liked")
.small-horizontal-spacer

= type.label :liked_comment, class: "checkbox-inline" do
= type.check_box :liked_comment, {checked: email_prefs["liked_comment"]}, false, true
= t(".liked_comment")
.small-horizontal-spacer

= type.label :reshared, class: "checkbox-inline" do
= type.check_box :reshared, {checked: email_prefs["reshared"]}, false, true
= t(".reshared")
Expand Down
1 change: 0 additions & 1 deletion app/workers/mail/liked.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ def perform(*args)
end
end
end

8 changes: 8 additions & 0 deletions app/workers/mail/liked_comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module Workers
module Mail
class LikedComment < Liked
end
end
end
Loading

0 comments on commit b0c196a

Please sign in to comment.