Skip to content

Commit

Permalink
Use favorite type for notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
teceler committed Nov 9, 2024
1 parent fd6f43b commit 584e9c6
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 16 deletions.
21 changes: 15 additions & 6 deletions app/helpers/notification_helper.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
# frozen_string_literal: true
module NotificationHelper
NOTIFICATION_MESSAGES = {
'import_success' => 'Post import succeeded',
'import_fail' => 'Post import failed',
'new_favorite_post' => 'An author you favorited has written a new post',
'joined_favorite_post' => 'An author you favorited has joined a post',
nil => {
'import_success' => 'Post import succeeded',
'import_fail' => 'Post import failed',
'new_favorite_post' => 'A once-favorited subject has a new post',
'joined_favorite_post' => 'A once-favorited author has joined a post',
},
'user' => {
'new_favorite_post' => 'An author you favorited has written a new post',
'joined_favorite_post' => 'An author you favorited has joined a post',
},
'board' => {
'new_favorite_post' => 'An continuity you favorited has a new post',
},
}

def subject_for_type(notification_type)
NOTIFICATION_MESSAGES[notification_type]
def subject_for_type(notification_type, favorite_type)
NOTIFICATION_MESSAGES[favorite_type][notification_type]
end
end
8 changes: 5 additions & 3 deletions app/jobs/notify_followers_of_new_post_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ def notify_of_post_creation(post, post_user)
favorites = Favorite.where(favorite: post_user).or(Favorite.where(favorite: post.board))
user_ids = favorites.select(:user_id).distinct.pluck(:user_id)
users = filter_users(post, user_ids)
favorites = favorites.order(favorite_type: :desc)

return if users.empty?

users.each { |user| Notification.notify_user(user, :new_favorite_post, post: post) }
users.each { |user| Notification.notify_user(user, :new_favorite_post, post: post, favorite: favorites.find_by(user: user.id)) }
end

def notify_of_post_joining(post, new_user)
users = filter_users(post, Favorite.where(favorite: new_user).pluck(:user_id))
favorites = Favorite.where(favorite: new_user)
users = filter_users(post, favorites.pluck(:user_id))
return if users.empty?

users.each do |user|
next if already_notified_about?(post, user)
Notification.notify_user(user, :joined_favorite_post, post: post)
Notification.notify_user(user, :joined_favorite_post, post: post, favorite: favorites.find_by(user: user.id))
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def new_message(message_id)

def new_notification(notification_id)
@notification = Notification.find(notification_id)
@subject = subject_for_type(@notification.notification_type)
@subject = subject_for_type(@notification.notification_type, @notification.favorite.favorite_type)
@subject += ": #{@notification.post.subject}" if @notification.post.present?
@subject += ": #{@notification.error_msg}" if @notification.error_msg.present?
mail(to: @notification.user.email, subject: @subject)
Expand Down
2 changes: 2 additions & 0 deletions app/models/favorite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class Favorite < ApplicationRecord
belongs_to :user, inverse_of: :favorites, optional: false
belongs_to :favorite, polymorphic: true, optional: false

has_many :notifications, inverse_of: :favorite, dependent: :nullify

validates :user_id, uniqueness: { scope: [:favorite_id, :favorite_type] }
validate :not_yourself

Expand Down
5 changes: 3 additions & 2 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
class Notification < ApplicationRecord
belongs_to :user, inverse_of: :notifications, optional: false
belongs_to :post, inverse_of: :notifications, optional: true
belongs_to :favorite, inverse_of: :notifications, optional: true

before_create :check_read
after_create_commit :notify_recipient
Expand Down Expand Up @@ -31,8 +32,8 @@ class Notification < ApplicationRecord

attr_accessor :skip_email

def self.notify_user(user, type, post: nil, error: nil)
Notification.create!(user: user, notification_type: type, post: post, error_msg: error)
def self.notify_user(user, type, post: nil, error: nil, favorite: nil)
Notification.create!(user: user, notification_type: type, post: post, error_msg: error, favorite: favorite)
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/views/characters/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
- templates = @user.templates.ordered
- templates = templates.paginate(per_page: 25, page: params[:page]) if templates.count > 50
= render partial: partial_type, collection: templates, as: :name
- if (templateless = @user.characters.non_npcs.where(template_id: nil)).exists? && (templates.methods.exclude?(:total_pages) || templates.total_pages == params[:page])
- if (templateless = @user.characters.non_npcs.where(template_id: nil)).exists? && (templates.methods.exclude?(:total_pages) || templates.total_pages == params[:page].to_i)
= render partial_type, name: "No Template", characters: templateless.ordered, show_new_character_button: @user.id == current_user&.id
- else
%tr
Expand Down
2 changes: 1 addition & 1 deletion app/views/notifications/_notification.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- row_klass = klass
- row_klass += ' bold' if notification.unread?
%td.padding-left-5{class: row_klass}= # just padding
%td{class: row_klass, colspan: 4}= subject_for_type(notification.notification_type)
%td{class: row_klass, colspan: 4}= subject_for_type(notification.notification_type, notification.favorite.favorite_type)
%td{class: row_klass}= pretty_time(notification.created_at)
%td.post-check-box{class: klass}= check_box_tag :'marked_ids[]', notification.id, false, class: 'checkbox', id: nil
- if @posts.key?(notification.post_id)
Expand Down
2 changes: 1 addition & 1 deletion app/views/user_mailer/new_notification.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%div{style: 'width: 500px; margin: 0px auto; background-color: #8B8687; color:#EEEEEE; padding: 0px; font-size: 18px;'}
%div{style: 'padding: 15px; overflow: hidden;'}
%span{style: 'line-height: 28px;'} subject_for_type(@notification).titlecase
%span{style: 'line-height: 28px;'} subject_for_type(@notification.notification_type, @notification.favorite.favorite_type).titlecase

- if @notification.post || @notification.error_msg
%div{style: 'background-color: #D8D8D8; color:#101010; overflow: auto; width: 500px; margin: 0px auto;'}
Expand Down
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2023_09_30_053037) do
ActiveRecord::Schema[7.1].define(version: 2024_09_30_223829) do
# These are extensions that must be enabled in order to support this database
enable_extension "citext"
enable_extension "plpgsql"
Expand Down Expand Up @@ -274,6 +274,7 @@
t.datetime "read_at", precision: nil
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.integer "favorite_id"
t.index ["user_id"], name: "index_notifications_on_user_id"
end

Expand Down
1 change: 1 addition & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ def ordered_numbers
user
post
notification_type { :new_favorite_post }
favorite { association :favorite, user: user, favorite_type: 'Board', favorite: post.board }

factory :error_notification do
sequence :error_msg, ordered_numbers do |n|
Expand Down

0 comments on commit 584e9c6

Please sign in to comment.