Skip to content

Commit 4b59fcd

Browse files
authored
Merge pull request #135 from merefield/fix_chat_editing_for_ai_gen_images
FIX: editing for AI generated images in chat
2 parents 4967e6b + 2379b3e commit 4b59fcd

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

lib/discourse_chatbot/message/message_reply_creator.rb

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,38 @@ def create
2020
end
2121
end
2222

23+
# if the message is picture, lets get the upload
24+
upload = find_upload_from_markdown(@message_body)
25+
26+
# if the message is a picture, message body is just a placeholder
2327
params = {
2428
chat_channel_id: @topic_or_channel_id,
2529
message: @message_body
2630
}
2731

2832
params.merge!(thread_id: @thread_id) if @thread_id.present?
2933

34+
message = nil
35+
3036
Chat::CreateMessage.call(
3137
params: params,
3238
guardian: @guardian
33-
)
39+
) do
40+
on_success { |message_instance:| message = message_instance }
41+
end
42+
43+
# if there's an upload
44+
# associate the upload with the message and
45+
# remove the redundant message body
46+
if upload && message
47+
message.message = ""
48+
message.cooked = ""
49+
message.excerpt = ""
50+
message.save!
51+
message.uploads = [upload]
52+
message.save!
53+
end
54+
3455
begin
3556
presence = PresenceChannel.new("/chat-reply/#{@topic_or_channel_id}")
3657
presence.leave(user_id: @author.id, client_id: "12345")
@@ -44,5 +65,21 @@ def create
4465
Rails.logger.error("Chatbot: There was a problem: #{e}")
4566
end
4667
end
68+
69+
private
70+
71+
def find_upload_from_markdown(string)
72+
regex = /\A!\[([^\]]+)\|690x460\]\((upload:\/\/[^\s)]+)\)\z/
73+
match = string.match(regex)
74+
return nil unless match
75+
76+
short_url = match[2]
77+
78+
# Find the upload using the short_url
79+
# This is a bit of a hack because short_url is not a field but a method
80+
Upload.order(id: :desc).limit(5).each do |upload|
81+
return upload if upload.short_url == short_url
82+
end
83+
end
4784
end
4885
end

plugin.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# frozen_string_literal: true
22
# name: discourse-chatbot
33
# about: a plugin that allows you to have a conversation with a configurable chatbot in Discourse Chat, Topics and Private Messages
4-
# version: 1.5.3
4+
# version: 1.5.4
55
# authors: merefield
66
# url: https://github.com/merefield/discourse-chatbot
77

8-
gem 'mime-types-data', '3.2025.0422', { require: false }
9-
gem 'mime-types', '3.6.2', { require: false }
108
gem 'multipart-post', '2.4.0', { require: false }
119
gem 'faraday-multipart', '1.0.4', { require: false }
1210
gem 'event_stream_parser', '1.0.0', { require: false }

0 commit comments

Comments
 (0)