2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-07 03:40:18 -04:00
|
|
|
# name: discourse-presence
|
|
|
|
# about: Show which users are writing a reply to a topic
|
2020-04-29 00:48:55 -04:00
|
|
|
# version: 2.0
|
|
|
|
# authors: André Pereira, David Taylor, tgxworld
|
2018-04-06 00:46:42 -04:00
|
|
|
# url: https://github.com/discourse/discourse/tree/master/plugins/discourse-presence
|
2017-09-07 03:40:18 -04:00
|
|
|
|
|
|
|
enabled_site_setting :presence_enabled
|
2018-05-15 18:43:09 -04:00
|
|
|
hide_plugin if self.respond_to?(:hide_plugin)
|
2017-09-07 03:40:18 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
MessageBus.register_client_message_filter('/presence/') do |message|
|
|
|
|
published_at = message.data["published_at"]
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
if published_at
|
|
|
|
(Time.zone.now.to_i - published_at) <= ::Presence::MAX_BACKLOG_AGE_SECONDS
|
|
|
|
else
|
|
|
|
false
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
2020-04-29 00:48:55 -04:00
|
|
|
end
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
module ::Presence
|
|
|
|
MAX_BACKLOG_AGE_SECONDS = 10
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
class Engine < ::Rails::Engine
|
|
|
|
engine_name PLUGIN_NAME
|
|
|
|
isolate_namespace Presence
|
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
|
2020-04-29 00:48:55 -04:00
|
|
|
before_action :ensure_presence_enabled
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
EDITING_STATE = 'editing'
|
|
|
|
REPLYING_STATE = 'replying'
|
|
|
|
CLOSED_STATE = 'closed'
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
def handle_message
|
|
|
|
[:state, :topic_id].each do |key|
|
|
|
|
raise ActionController::ParameterMissing.new(key) unless params.key?(key)
|
|
|
|
end
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
topic_id = permitted_params[:topic_id]
|
|
|
|
topic = Topic.find_by(id: topic_id)
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
raise Discourse::InvalidParameters.new(:topic_id) unless topic
|
|
|
|
guardian.ensure_can_see!(topic)
|
2017-09-08 16:06:27 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
post = nil
|
2017-09-08 16:06:27 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
if (permitted_params[:post_id])
|
|
|
|
if (permitted_params[:state] != EDITING_STATE)
|
|
|
|
raise Discourse::InvalidParameters.new(:state)
|
2017-10-02 00:00:43 -04:00
|
|
|
end
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
post = Post.find_by(id: permitted_params[:post_id])
|
|
|
|
raise Discourse::InvalidParameters.new(:topic_id) unless post
|
2017-09-08 16:06:27 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
guardian.ensure_can_edit!(post)
|
|
|
|
end
|
2017-09-08 16:06:27 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
opts = {
|
|
|
|
max_backlog_age: Presence::MAX_BACKLOG_AGE_SECONDS
|
|
|
|
}
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
case permitted_params[:state]
|
|
|
|
when EDITING_STATE
|
|
|
|
opts[:group_ids] = [Group::AUTO_GROUPS[:staff]]
|
|
|
|
|
|
|
|
if !post.locked? && !permitted_params[:is_whisper]
|
|
|
|
opts[:user_ids] = [post.user_id]
|
|
|
|
|
|
|
|
if topic.private_message?
|
|
|
|
if post.wiki
|
|
|
|
opts[:user_ids] = opts[:user_ids].concat(
|
|
|
|
topic.allowed_users.where(
|
|
|
|
"trust_level >= ? AND NOT admin OR moderator",
|
|
|
|
SiteSetting.min_trust_to_edit_wiki_post
|
|
|
|
).pluck(:id)
|
|
|
|
)
|
|
|
|
|
|
|
|
opts[:user_ids].uniq!
|
|
|
|
|
|
|
|
# Ignore trust level and just publish to all allowed groups since
|
|
|
|
# trying to figure out which users in the allowed groups have
|
|
|
|
# the necessary trust levels can lead to a large array of user ids
|
|
|
|
# if the groups are big.
|
|
|
|
opts[:group_ids] = opts[:group_ids].concat(
|
|
|
|
topic.allowed_groups.pluck(:id)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if post.wiki
|
|
|
|
opts[:group_ids] << Group::AUTO_GROUPS[:"trust_level_#{SiteSetting.min_trust_to_edit_wiki_post}"]
|
|
|
|
elsif SiteSetting.trusted_users_can_edit_others?
|
|
|
|
opts[:group_ids] << Group::AUTO_GROUPS[:trust_level_4]
|
|
|
|
end
|
2017-10-02 00:00:43 -04:00
|
|
|
end
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
2020-04-29 00:48:55 -04:00
|
|
|
when REPLYING_STATE
|
|
|
|
if permitted_params[:is_whisper]
|
|
|
|
opts[:group_ids] = [Group::AUTO_GROUPS[:staff]]
|
|
|
|
elsif topic.private_message?
|
|
|
|
opts[:user_ids] = topic.allowed_users.pluck(:id)
|
|
|
|
|
|
|
|
opts[:group_ids] = [Group::AUTO_GROUPS[:staff]].concat(
|
|
|
|
topic.allowed_groups.pluck(:id)
|
|
|
|
)
|
|
|
|
else
|
|
|
|
opts[:group_ids] = topic.secure_group_ids
|
|
|
|
end
|
|
|
|
when CLOSED_STATE
|
|
|
|
if topic.private_message?
|
|
|
|
opts[:user_ids] = topic.allowed_users.pluck(:id)
|
|
|
|
|
|
|
|
opts[:group_ids] = [Group::AUTO_GROUPS[:staff]].concat(
|
|
|
|
topic.allowed_groups.pluck(:id)
|
|
|
|
)
|
|
|
|
else
|
|
|
|
opts[:group_ids] = topic.secure_group_ids
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
payload = {
|
|
|
|
user: BasicUserSerializer.new(current_user, root: false).as_json,
|
|
|
|
state: permitted_params[:state],
|
|
|
|
is_whisper: permitted_params[:is_whisper].present?,
|
|
|
|
published_at: Time.zone.now.to_i
|
|
|
|
}
|
|
|
|
|
|
|
|
if (post_id = permitted_params[:post_id]).present?
|
|
|
|
payload[:post_id] = post_id
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
MessageBus.publish("/presence/#{topic_id}", payload, opts)
|
|
|
|
|
|
|
|
render json: success_json
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def ensure_presence_enabled
|
|
|
|
if !SiteSetting.presence_enabled ||
|
|
|
|
current_user.user_option.hide_profile_and_presence?
|
|
|
|
|
|
|
|
raise Discourse::NotFound
|
|
|
|
end
|
2017-12-06 15:58:59 -05:00
|
|
|
end
|
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
def permitted_params
|
|
|
|
params.permit(:state, :topic_id, :post_id, :is_whisper)
|
|
|
|
end
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Presence::Engine.routes.draw do
|
2020-04-29 00:48:55 -04:00
|
|
|
post '/publish' => 'presences#handle_message'
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Discourse::Application.routes.append do
|
|
|
|
mount ::Presence::Engine, at: '/presence'
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|