Lhc_fl
(Linca)
November 12, 2023, 12:16pm
1
Now that PostCreator
can specify user
as post’s creator and guardian
to verify posting permissions, I would like Discourse Chat to have similar behavior, which would allow plugins to help send messages from a user (whether it is in the chat channel or not) with the guardian of the system user.
return true if contract.thread_id.blank?
thread&.channel == channel
end
def ensure_thread_matches_parent(thread:, reply:)
return true unless thread && reply
reply.thread == thread
end
def fetch_uploads(contract:, guardian:)
return [] if !SiteSetting.chat_allow_uploads
guardian.user.uploads.where(id: contract.upload_ids)
end
def clean_message(contract:)
contract.message =
TextCleaner.clean(
contract.message,
strip_whitespaces: contract.strip_whitespaces,
strip_zero_width_spaces: true,
)
Usage: In Discourse Automation and Chat bridge, you need to send chat messages from plugin. But currently this script will most likely fail to be sent because the user you specify (e.g. DiscoBot) may not be in the channel.
Some time ago, we encountered a problem with Discourse-Automation where we could not send chat messages regularly. We use it to send weekly news in the chat channel. I noticed in the /logs that chat messages failed to send due to permission issues.
I think the sender configured in discourse automation should simply ignore the channel’s permission settings, since this is set by the administrator. In fact, I am using DiscoBot as the messaging user. Since DiscoBot was not active in the category bo…
script do |context, fields, automation|
sender = User.find_by(username: fields.dig("sender", "value")) || Discourse.system_user
channel = Chat::Channel.find_by(id: fields.dig("chat_channel_id", "value"))
placeholders = { channel_name: channel.title(sender) }.merge(context["placeholders"] || {})
creator =
::Chat::CreateMessage.call(
chat_channel_id: channel.id,
guardian: sender.guardian,
message: utils.apply_placeholders(fields.dig("message", "value"), placeholders),
)
if creator.failure?
Rails.logger.warn "[discourse-automation] Chat message failed to send:\n#{creator.inspect_steps.inspect}\n#{creator.inspect_steps.error}"
end
end
end
end
I know the team is very busy, so I can do this by myself. I hope the Discourse team thinks this can be added to the core, then I will take the time to modify the code here and submit a pull request.
1 Like
merefield
(Robert)
November 13, 2023, 2:52pm
2
Discourse Chatbot solved a similar challenge with current code without needing any specific changes in core, have you taken a look at my solution?
module ::DiscourseChatbot
class MessageReplyCreator < ReplyCreator
def initialize(options = {})
super(options)
end
def create
::DiscourseChatbot.progress_debug_message("5. Creating a new Chat Nessage...")
begin
Chat::CreateMessage.call(
chat_channel_id: @topic_or_channel_id,
guardian: @guardian,
message: @message_body,
)
presence = PresenceChannel.new("/chat-reply/#{@topic_or_channel_id}")
presence.leave(user_id: @author.id, client_id: "12345")
::DiscourseChatbot.progress_debug_message("6. The Message has been created successfully")
rescue => e
1 Like
Lhc_fl
(Linca)
November 14, 2023, 2:36am
3
thank you so much for your suggestion! let me see it
Yes as @merefield said, a chat service accepts a guardian, and you can set the guardian as you want.
1 Like
This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.