discourse/app/models/message_bus_observer.rb

50 lines
1.4 KiB
Ruby
Raw Normal View History

2013-02-05 14:16:51 -05:00
require_dependency 'discourse_observer'
2013-02-07 10:45:24 -05:00
# This class is responsible for notifying the message bus of various
2013-02-05 14:16:51 -05:00
# events.
class MessageBusObserver < DiscourseObserver
observe :notification, :user_action, :topic
2013-02-05 14:16:51 -05:00
def after_create_notification(notification)
refresh_notification_count(notification)
end
def after_destroy_notification(notification)
refresh_notification_count(notification)
2013-02-07 10:45:24 -05:00
end
2013-02-05 14:16:51 -05: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 10:45:24 -05:00
2013-02-05 14:16:51 -05:00
topic.posters = topic.posters_summary
topic.posts_count = 1
topic_json = TopicListItemSerializer.new(topic).as_json
MessageBus.publish("/popular", topic_json)
# If it has a category, add it to the category views too
if topic.category.present?
2013-02-07 10:45:24 -05:00
MessageBus.publish("/category/#{topic.category.slug}", topic_json)
2013-02-05 14:16:51 -05:00
end
2013-02-07 10:45:24 -05:00
end
2013-02-05 14:16:51 -05:00
protected
def refresh_notification_count(notification)
user_id = notification.user.id
MessageBus.publish("/notification/#{user_id}",
2013-02-05 14:16:51 -05: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 10:45:24 -05:00
)
2013-02-05 14:16:51 -05:00
end
end