| 
									
										
										
										
											2019-05-13 10:37:49 +08:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-13 14:36:16 -05:00
										 |  |  | module DiscourseChatIntegration::Provider::MattermostProvider | 
					
						
							|  |  |  |   class MattermostCommandController < DiscourseChatIntegration::Provider::HookController | 
					
						
							|  |  |  |     requires_provider ::DiscourseChatIntegration::Provider::MattermostProvider::PROVIDER_NAME | 
					
						
							| 
									
										
										
										
											2017-07-26 13:09:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 10:44:32 +08:00
										 |  |  |     before_action :mattermost_token_valid?, only: :command | 
					
						
							| 
									
										
										
										
											2017-07-26 13:09:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 10:44:32 +08:00
										 |  |  |     skip_before_action :check_xhr, | 
					
						
							| 
									
										
										
										
											2017-07-26 13:09:05 +01:00
										 |  |  |                        :preload_json, | 
					
						
							|  |  |  |                        :verify_authenticity_token, | 
					
						
							|  |  |  |                        :redirect_to_login_if_required, | 
					
						
							|  |  |  |                        only: :command | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def command | 
					
						
							|  |  |  |       text = process_command(params) | 
					
						
							| 
									
										
										
										
											2017-08-01 20:53:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |       render json: { response_type: "ephemeral", text: text } | 
					
						
							| 
									
										
										
										
											2017-07-26 13:09:05 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def process_command(params) | 
					
						
							|  |  |  |       tokens = params[:text].split(" ") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # channel name fix | 
					
						
							|  |  |  |       channel_id = | 
					
						
							|  |  |  |         case params[:channel_name] | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |         when "directmessage" | 
					
						
							| 
									
										
										
										
											2017-07-26 13:09:05 +01:00
										 |  |  |           "@#{params[:user_name]}" | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |         when "privategroup" | 
					
						
							| 
									
										
										
										
											2017-07-26 13:09:05 +01:00
										 |  |  |           params[:channel_id] | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           "##{params[:channel_name]}" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-13 14:36:16 -05:00
										 |  |  |       provider = DiscourseChatIntegration::Provider::MattermostProvider::PROVIDER_NAME | 
					
						
							| 
									
										
										
										
											2017-07-26 13:09:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |       channel = | 
					
						
							|  |  |  |         DiscourseChatIntegration::Channel | 
					
						
							|  |  |  |           .with_provider(provider) | 
					
						
							|  |  |  |           .with_data_value("identifier", channel_id) | 
					
						
							|  |  |  |           .first | 
					
						
							| 
									
										
										
										
											2017-07-26 13:09:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       # Create channel if doesn't exist | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |       channel ||= | 
					
						
							|  |  |  |         DiscourseChatIntegration::Channel.create!( | 
					
						
							|  |  |  |           provider: provider, | 
					
						
							|  |  |  |           data: { | 
					
						
							|  |  |  |             identifier: channel_id, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2017-07-26 13:09:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-13 14:36:16 -05:00
										 |  |  |       ::DiscourseChatIntegration::Helper.process_command(channel, tokens) | 
					
						
							| 
									
										
										
										
											2017-07-26 13:09:05 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def mattermost_token_valid? | 
					
						
							|  |  |  |       params.require(:token) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if SiteSetting.chat_integration_mattermost_incoming_webhook_token.blank? || | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |            SiteSetting.chat_integration_mattermost_incoming_webhook_token != params[:token] | 
					
						
							| 
									
										
										
										
											2017-07-26 13:09:05 +01:00
										 |  |  |         raise Discourse::InvalidAccess.new | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   class MattermostEngine < ::Rails::Engine | 
					
						
							| 
									
										
										
										
											2021-07-13 14:36:16 -05:00
										 |  |  |     engine_name DiscourseChatIntegration::PLUGIN_NAME + "-mattermost" | 
					
						
							|  |  |  |     isolate_namespace DiscourseChatIntegration::Provider::MattermostProvider | 
					
						
							| 
									
										
										
										
											2017-07-26 13:09:05 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |   MattermostEngine.routes.draw { post "command" => "mattermost_command#command" } | 
					
						
							| 
									
										
										
										
											2017-07-26 13:09:05 +01:00
										 |  |  | end |