discourse/app/models/message_bus_observer.rb

59 lines
1.8 KiB
Ruby
Raw Normal View History

2013-02-06 03:16:51 +08:00
require_dependency 'message_bus'
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 :post, :notification, :user_action, :topic
def after_create_post(post)
MessageBus.publish("/topic/#{post.topic_id}",
2013-02-07 23:45:24 +08:00
id: post.id,
created_at: post.created_at,
2013-02-06 03:16:51 +08:00
user: BasicUserSerializer.new(post.user).as_json(root: false),
2013-02-07 23:45:24 +08:00
post_number: post.post_number)
2013-02-06 03:16:51 +08:00
end
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("/popular", topic_json)
# 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