discourse/plugins/chat/lib/chat/post_notification_handler.rb
Joffrey JAFFEUX 12a18d4d55
DEV: properly namespace chat (#20690)
This commit main goal was to comply with Zeitwerk and properly rely on autoloading. To achieve this, most resources have been namespaced under the `Chat` module.

- Given all models are now namespaced with `Chat::` and would change the stored types in DB when using polymorphism or STI (single table inheritance), this commit uses various Rails methods to ensure proper class is loaded and the stored name in DB is unchanged, eg: `Chat::Message` model will be stored as `"ChatMessage"`, and `"ChatMessage"` will correctly load `Chat::Message` model.
- Jobs are now using constants only, eg: `Jobs::Chat::Foo` and should only be enqueued this way

Notes:
- This commit also used this opportunity to limit the number of registered css files in plugin.rb
- `discourse_dev` support has been removed within this commit and will be reintroduced later

<!-- NOTE: All pull requests should have tests (rspec in Ruby, qunit in JavaScript). If your code does not include test coverage, please include an explanation of why it was omitted. -->
2023-03-17 14:24:38 +01:00

43 lines
1.3 KiB
Ruby

# frozen_string_literal: true
##
# Handles :post_alerter_after_save_post events from
# core. Used for notifying users that their chat message
# has been quoted in a post.
module Chat
class PostNotificationHandler
attr_reader :post
def initialize(post, notified_users)
@post = post
@notified_users = notified_users
end
def handle
return false if post.post_type == Post.types[:whisper]
return false if post.topic.blank?
return false if post.topic.private_message?
quoted_users = extract_quoted_users(post)
if @notified_users.present?
quoted_users = quoted_users.where("users.id NOT IN (?)", @notified_users)
end
opts = { user_id: post.user.id, display_username: post.user.username }
quoted_users.each do |user|
# PostAlerter.create_notification handles many edge cases, such as
# muting, ignoring, double notifications etc.
PostAlerter.new.create_notification(user, Notification.types[:chat_quoted], post, opts)
end
end
private
def extract_quoted_users(post)
usernames =
post.raw.scan(/\[chat quote=\"([^;]+);.+\"\]/).uniq.map { |q| q.first.strip.downcase }
User.where.not(id: post.user_id).where(username_lower: usernames)
end
end
end