@@ -20,17 +20,38 @@ def create
20
20
end
21
21
end
22
22
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
23
27
params = {
24
28
chat_channel_id : @topic_or_channel_id ,
25
29
message : @message_body
26
30
}
27
31
28
32
params . merge! ( thread_id : @thread_id ) if @thread_id . present?
29
33
34
+ message = nil
35
+
30
36
Chat ::CreateMessage . call (
31
37
params : params ,
32
38
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
+
34
55
begin
35
56
presence = PresenceChannel . new ( "/chat-reply/#{ @topic_or_channel_id } " )
36
57
presence . leave ( user_id : @author . id , client_id : "12345" )
@@ -44,5 +65,21 @@ def create
44
65
Rails . logger . error ( "Chatbot: There was a problem: #{ e } " )
45
66
end
46
67
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
47
84
end
48
85
end
0 commit comments