discourse/app/models/message_bus_observer.rb

50 lines
1.4 KiB
Ruby
Raw Normal View History

2013-02-06 03:16:51 +08:00
require_dependency 'discourse_observer'
2013-02-07 23:45:24 +08:00
# This class is responsible for notifying the message bus of various
2013-02-06 03:16:51 +08:00
# events.
class MessageBusObserver < DiscourseObserver
observe :notification, :user_action, :topic
2013-02-06 03:16:51 +08:00
def after_create_notification(notification)
refresh_notification_count(notification)
end
def after_destroy_notification(notification)
refresh_notification_count(notification)
2013-02-07 23:45:24 +08:00
end
2013-02-06 03:16:51 +08:00
def after_create_user_action(user_action)
MessageBus.publish("/users/#{user_action.user.username.downcase}", user_action.id)
end
def after_create_topic(topic)
# Don't publish invisible topics
return unless topic.visible?
return if topic.private_message?
2013-02-07 23:45:24 +08:00
2013-02-06 03:16:51 +08:00
topic.posters = topic.posters_summary
topic.posts_count = 1
topic_json = TopicListItemSerializer.new(topic).as_json
MessageBus.publish("/latest", topic_json)
2013-02-06 03:16:51 +08:00
# If it has a category, add it to the category views too
if topic.category.present?
2013-02-07 23:45:24 +08:00
MessageBus.publish("/category/#{topic.category.slug}", topic_json)
2013-02-06 03:16:51 +08:00
end
2013-02-07 23:45:24 +08:00
end
2013-02-06 03:16:51 +08:00
protected
def refresh_notification_count(notification)
user_id = notification.user.id
MessageBus.publish("/notification/#{user_id}",
2013-02-06 03:16:51 +08:00
{unread_notifications: notification.user.unread_notifications,
unread_private_messages: notification.user.unread_private_messages},
user_ids: [user_id] # only publish the notification to this user
2013-02-07 23:45:24 +08:00
)
2013-02-06 03:16:51 +08:00
end
end