2017-09-07 03:40:18 -04:00
|
|
|
# name: discourse-presence
|
|
|
|
# about: Show which users are writing a reply to a topic
|
|
|
|
# version: 1.0
|
|
|
|
# authors: André Pereira, David Taylor
|
|
|
|
# url: https://github.com/discourse/discourse-presence.git
|
|
|
|
|
|
|
|
enabled_site_setting :presence_enabled
|
|
|
|
|
|
|
|
register_asset 'stylesheets/presence.scss'
|
|
|
|
|
2018-01-29 04:48:42 -05:00
|
|
|
PLUGIN_NAME ||= -"discourse-presence"
|
2017-09-07 03:40:18 -04:00
|
|
|
|
|
|
|
after_initialize do
|
|
|
|
|
|
|
|
module ::Presence
|
|
|
|
class Engine < ::Rails::Engine
|
|
|
|
engine_name PLUGIN_NAME
|
|
|
|
isolate_namespace Presence
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module ::Presence::PresenceManager
|
2017-12-18 16:00:55 -05:00
|
|
|
MAX_BACKLOG_AGE ||= 60
|
|
|
|
|
2017-09-07 03:40:18 -04:00
|
|
|
def self.get_redis_key(type, id)
|
|
|
|
"presence:#{type}:#{id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_messagebus_channel(type, id)
|
|
|
|
"/presence/#{type}/#{id}"
|
|
|
|
end
|
|
|
|
|
2017-12-18 16:00:55 -05:00
|
|
|
# return true if a key was added
|
2017-09-07 03:40:18 -04:00
|
|
|
def self.add(type, id, user_id)
|
2017-12-17 23:41:20 -05:00
|
|
|
key = get_redis_key(type, id)
|
|
|
|
result = $redis.hset(key, user_id, Time.zone.now)
|
2017-12-18 16:00:55 -05:00
|
|
|
$redis.expire(key, MAX_BACKLOG_AGE)
|
2017-12-17 23:41:20 -05:00
|
|
|
result
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
|
2017-12-18 16:00:55 -05:00
|
|
|
# return true if a key was deleted
|
2017-09-07 03:40:18 -04:00
|
|
|
def self.remove(type, id, user_id)
|
2017-12-17 23:41:20 -05:00
|
|
|
key = get_redis_key(type, id)
|
2017-12-18 16:00:55 -05:00
|
|
|
$redis.expire(key, MAX_BACKLOG_AGE)
|
2017-12-17 23:41:20 -05:00
|
|
|
$redis.hdel(key, user_id) > 0
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_users(type, id)
|
2017-12-06 15:58:59 -05:00
|
|
|
user_ids = $redis.hkeys(get_redis_key(type, id)).map(&:to_i)
|
2017-09-07 03:40:18 -04:00
|
|
|
User.where(id: user_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.publish(type, id)
|
|
|
|
users = get_users(type, id)
|
|
|
|
serialized_users = users.map { |u| BasicUserSerializer.new(u, root: false) }
|
2017-12-17 23:41:20 -05:00
|
|
|
message = { users: serialized_users, time: Time.now.to_i }
|
2017-09-08 16:06:27 -04:00
|
|
|
messagebus_channel = get_messagebus_channel(type, id)
|
2017-12-06 15:58:59 -05:00
|
|
|
|
|
|
|
topic = type == 'post' ? Post.find_by(id: id).topic : Topic.find_by(id: id)
|
|
|
|
|
2018-03-05 02:38:05 -05:00
|
|
|
if topic.private_message?
|
2017-12-06 15:58:59 -05:00
|
|
|
user_ids = User.where('admin OR moderator').pluck(:id) + topic.allowed_users.pluck(:id)
|
2017-12-18 20:17:08 -05:00
|
|
|
group_ids = topic.allowed_groups.pluck(:id)
|
|
|
|
|
2017-12-18 16:00:55 -05:00
|
|
|
MessageBus.publish(
|
|
|
|
messagebus_channel,
|
|
|
|
message.as_json,
|
|
|
|
user_ids: user_ids,
|
2017-12-18 20:17:08 -05:00
|
|
|
group_ids: group_ids,
|
2017-12-18 16:00:55 -05:00
|
|
|
max_backlog_age: MAX_BACKLOG_AGE
|
|
|
|
)
|
2017-09-08 16:06:27 -04:00
|
|
|
else
|
2017-12-18 16:00:55 -05:00
|
|
|
MessageBus.publish(
|
|
|
|
messagebus_channel,
|
|
|
|
message.as_json,
|
|
|
|
group_ids: topic.secure_group_ids,
|
|
|
|
max_backlog_age: MAX_BACKLOG_AGE
|
|
|
|
)
|
2017-09-08 16:06:27 -04:00
|
|
|
end
|
2017-09-07 03:40:18 -04:00
|
|
|
|
|
|
|
users
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.cleanup(type, id)
|
2017-12-06 15:58:59 -05:00
|
|
|
has_changed = false
|
2017-09-07 03:40:18 -04:00
|
|
|
|
|
|
|
# Delete entries older than 20 seconds
|
2017-12-06 15:58:59 -05:00
|
|
|
hash = $redis.hgetall(get_redis_key(type, id))
|
2017-09-07 03:40:18 -04:00
|
|
|
hash.each do |user_id, time|
|
|
|
|
if Time.zone.now - Time.parse(time) >= 20
|
2017-12-06 15:58:59 -05:00
|
|
|
has_changed |= remove(type, id, user_id)
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-06 15:58:59 -05:00
|
|
|
has_changed
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
require_dependency "application_controller"
|
|
|
|
|
|
|
|
class Presence::PresencesController < ::ApplicationController
|
2017-09-08 10:45:14 -04:00
|
|
|
requires_plugin PLUGIN_NAME
|
2017-08-31 00:06:56 -04:00
|
|
|
before_action :ensure_logged_in
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2018-01-29 04:48:42 -05:00
|
|
|
ACTIONS ||= [-"edit", -"reply"].freeze
|
2017-12-06 15:58:59 -05:00
|
|
|
|
2017-09-07 03:40:18 -04:00
|
|
|
def publish
|
2017-10-02 00:00:43 -04:00
|
|
|
data = params.permit(
|
|
|
|
:response_needed,
|
|
|
|
current: [:action, :topic_id, :post_id],
|
|
|
|
previous: [:action, :topic_id, :post_id]
|
|
|
|
)
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2017-10-02 00:00:43 -04:00
|
|
|
payload = {}
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2017-12-06 15:58:59 -05:00
|
|
|
if data[:previous] && data[:previous][:action].in?(ACTIONS)
|
2017-09-07 03:40:18 -04:00
|
|
|
type = data[:previous][:post_id] ? 'post' : 'topic'
|
|
|
|
id = data[:previous][:post_id] ? data[:previous][:post_id] : data[:previous][:topic_id]
|
|
|
|
|
2017-12-06 15:58:59 -05:00
|
|
|
topic = type == 'post' ? Post.find_by(id: id)&.topic : Topic.find_by(id: id)
|
2017-09-08 16:06:27 -04:00
|
|
|
|
2017-10-02 00:00:43 -04:00
|
|
|
if topic
|
|
|
|
guardian.ensure_can_see!(topic)
|
2017-09-08 16:06:27 -04:00
|
|
|
|
2017-12-18 16:00:55 -05:00
|
|
|
Presence::PresenceManager.remove(type, id, current_user.id)
|
|
|
|
Presence::PresenceManager.cleanup(type, id)
|
|
|
|
Presence::PresenceManager.publish(type, id)
|
2017-10-02 00:00:43 -04:00
|
|
|
end
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
|
2017-12-06 15:58:59 -05:00
|
|
|
if data[:current] && data[:current][:action].in?(ACTIONS)
|
2017-09-07 03:40:18 -04:00
|
|
|
type = data[:current][:post_id] ? 'post' : 'topic'
|
|
|
|
id = data[:current][:post_id] ? data[:current][:post_id] : data[:current][:topic_id]
|
|
|
|
|
2017-12-06 15:58:59 -05:00
|
|
|
topic = type == 'post' ? Post.find_by(id: id)&.topic : Topic.find_by(id: id)
|
2017-09-08 16:06:27 -04:00
|
|
|
|
2017-10-02 00:00:43 -04:00
|
|
|
if topic
|
|
|
|
guardian.ensure_can_see!(topic)
|
2017-09-08 16:06:27 -04:00
|
|
|
|
2017-12-18 16:00:55 -05:00
|
|
|
Presence::PresenceManager.add(type, id, current_user.id)
|
|
|
|
Presence::PresenceManager.cleanup(type, id)
|
|
|
|
users = Presence::PresenceManager.publish(type, id)
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2017-10-02 00:00:43 -04:00
|
|
|
if data[:response_needed]
|
|
|
|
messagebus_channel = Presence::PresenceManager.get_messagebus_channel(type, id)
|
2017-12-06 15:58:59 -05:00
|
|
|
users ||= Presence::PresenceManager.get_users(type, id)
|
|
|
|
payload = json_payload(messagebus_channel, users)
|
2017-10-02 00:00:43 -04:00
|
|
|
end
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-02 00:00:43 -04:00
|
|
|
render json: payload
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
|
2017-12-06 15:58:59 -05:00
|
|
|
def json_payload(channel, users)
|
|
|
|
{
|
|
|
|
messagebus_channel: channel,
|
|
|
|
messagebus_id: MessageBus.last_id(channel),
|
2018-01-29 04:48:42 -05:00
|
|
|
users: users.limit(SiteSetting.presence_max_users_shown).map { |u| BasicUserSerializer.new(u, root: false) }
|
2017-12-06 15:58:59 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Presence::Engine.routes.draw do
|
|
|
|
post '/publish' => 'presences#publish'
|
|
|
|
end
|
|
|
|
|
|
|
|
Discourse::Application.routes.append do
|
|
|
|
mount ::Presence::Engine, at: '/presence'
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|