| 
									
										
										
										
											2019-05-13 10:37:49 +08:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-13 14:36:16 -05:00
										 |  |  | module DiscourseChatIntegration | 
					
						
							| 
									
										
										
										
											2017-07-27 17:09:44 +01:00
										 |  |  |   module Provider | 
					
						
							|  |  |  |     module MatrixProvider | 
					
						
							|  |  |  |       PROVIDER_NAME = "matrix".freeze | 
					
						
							|  |  |  |       PROVIDER_ENABLED_SETTING = :chat_integration_matrix_enabled | 
					
						
							|  |  |  |       CHANNEL_PARAMETERS = [ | 
					
						
							| 
									
										
										
										
											2017-08-01 20:53:39 +01:00
										 |  |  |                         { key: "name", regex: '^\S+' }, | 
					
						
							|  |  |  |                         { key: "room_id", regex: '^\!\S+:\S+$', unique: true, hidden: true } | 
					
						
							| 
									
										
										
										
											2017-07-27 17:09:44 +01:00
										 |  |  |                        ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def self.send_message(room_id, message) | 
					
						
							|  |  |  |         homeserver = SiteSetting.chat_integration_matrix_homeserver | 
					
						
							|  |  |  |         event_type = 'm.room.message' | 
					
						
							|  |  |  |         uid = Time.now.to_i | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-01 20:53:39 +01:00
										 |  |  |         url_params = URI.encode_www_form(access_token: SiteSetting.chat_integration_matrix_access_token) | 
					
						
							| 
									
										
										
										
											2017-07-27 17:09:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         url = "#{homeserver}/_matrix/client/r0/rooms/#{CGI::escape(room_id)}/send/#{event_type}/#{uid}" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-01 20:53:39 +01:00
										 |  |  |         uri = URI([url, url_params].join('?')) | 
					
						
							| 
									
										
										
										
											2017-07-27 17:09:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         http = Net::HTTP.new(uri.host, uri.port) | 
					
						
							|  |  |  |         http.use_ssl = true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-01 20:53:39 +01:00
										 |  |  |         req = Net::HTTP::Put.new(uri, 'Content-Type' => 'application/json') | 
					
						
							| 
									
										
										
										
											2017-07-27 17:09:44 +01:00
										 |  |  |         req.body = message.to_json | 
					
						
							|  |  |  |         response = http.request(req) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-14 15:03:49 -05:00
										 |  |  |         response | 
					
						
							| 
									
										
										
										
											2017-07-27 17:09:44 +01:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def self.generate_matrix_message(post) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         display_name = "@#{post.user.username}" | 
					
						
							|  |  |  |         full_name = post.user.name || "" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if !(full_name.strip.empty?) && (full_name.strip.gsub(' ', '_').casecmp(post.user.username) != 0) && (full_name.strip.gsub(' ', '').casecmp(post.user.username) != 0) | 
					
						
							|  |  |  |           display_name = "#{full_name} @#{post.user.username}" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         message = { | 
					
						
							| 
									
										
										
										
											2018-12-28 15:44:43 -08:00
										 |  |  |           msgtype: SiteSetting.chat_integration_matrix_use_notice ? 'm.notice' : 'm.text', | 
					
						
							| 
									
										
										
										
											2017-08-01 20:53:39 +01:00
										 |  |  |           body: I18n.t('chat_integration.provider.matrix.text_message',                           user: display_name, | 
					
						
							|  |  |  |                                                                                                   post_url: post.full_url, | 
					
						
							|  |  |  |                                                                                                   title: post.topic.title), | 
					
						
							| 
									
										
										
										
											2017-07-27 17:09:44 +01:00
										 |  |  |           format: 'org.matrix.custom.html', | 
					
						
							| 
									
										
										
										
											2017-08-01 20:53:39 +01:00
										 |  |  |           formatted_body: I18n.t('chat_integration.provider.matrix.formatted_message',                           user: display_name, | 
					
						
							|  |  |  |                                                                                                                  post_url: post.full_url, | 
					
						
							|  |  |  |                                                                                                                  title: post.topic.title, | 
					
						
							| 
									
										
										
										
											2017-12-27 03:35:32 -05:00
										 |  |  |                                                                                                                  excerpt: post.excerpt(SiteSetting.chat_integration_matrix_excerpt_length, text_entities: true, strip_links: true, remap_emoji: true)) | 
					
						
							| 
									
										
										
										
											2017-07-27 17:09:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-14 15:03:49 -05:00
										 |  |  |         message | 
					
						
							| 
									
										
										
										
											2017-07-27 17:09:44 +01:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-15 11:45:25 -04:00
										 |  |  |       def self.trigger_notification(post, channel, rule) | 
					
						
							| 
									
										
										
										
											2017-07-27 17:09:44 +01:00
										 |  |  |         message = generate_matrix_message(post) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         response = send_message(channel.data['room_id'], message) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-03 17:42:07 +08:00
										 |  |  |         if !response.kind_of?(Net::HTTPSuccess) | 
					
						
							| 
									
										
										
										
											2017-07-27 17:09:44 +01:00
										 |  |  |           error_key = nil | 
					
						
							|  |  |  |           begin | 
					
						
							| 
									
										
										
										
											2020-09-25 21:08:28 +02:00
										 |  |  |             responseData = JSON.parse(response.body) | 
					
						
							| 
									
										
										
										
											2017-07-27 17:09:44 +01:00
										 |  |  |             if responseData['errcode'] == "M_UNKNOWN_TOKEN" | 
					
						
							|  |  |  |               error_key = 'chat_integration.provider.matrix.errors.unknown_token' | 
					
						
							|  |  |  |             elsif responseData['errcode'] == "M_UNKNOWN" | 
					
						
							|  |  |  |               error_key = 'chat_integration.provider.matrix.errors.unknown_room' | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           ensure | 
					
						
							| 
									
										
										
										
											2021-07-13 14:36:16 -05:00
										 |  |  |             raise ::DiscourseChatIntegration::ProviderError.new info: { error_key: error_key, message: message, response_body: response.body } | 
					
						
							| 
									
										
										
										
											2017-07-27 17:09:44 +01:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2017-08-01 20:53:39 +01:00
										 |  |  | end |