| 
									
										
										
										
											2020-07-29 21:50:02 +05:30
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-13 14:36:16 -05:00
										 |  |  | module DiscourseChatIntegration::Provider::TeamsProvider | 
					
						
							| 
									
										
										
										
											2020-07-29 21:50:02 +05:30
										 |  |  |   PROVIDER_NAME = "teams".freeze | 
					
						
							|  |  |  |   PROVIDER_ENABLED_SETTING = :chat_integration_teams_enabled | 
					
						
							|  |  |  |   CHANNEL_PARAMETERS = [ | 
					
						
							|  |  |  |     { key: "name", regex: '^\S+$', unique: true }, | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |     { key: "webhook_url", regex: '^https:\/\/\S+$', unique: true, hidden: true }, | 
					
						
							| 
									
										
										
										
											2020-07-29 21:50:02 +05:30
										 |  |  |   ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def self.trigger_notification(post, channel, rule) | 
					
						
							|  |  |  |     message = get_message(post) | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |     uri = URI(channel.data["webhook_url"]) | 
					
						
							| 
									
										
										
										
											2020-07-29 21:50:02 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-01 17:36:56 +00:00
										 |  |  |     http = FinalDestination::HTTP.new(uri.host, uri.port) | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |     http.use_ssl = (uri.scheme == "https") | 
					
						
							| 
									
										
										
										
											2020-07-29 21:50:02 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |     req = Net::HTTP::Post.new(uri, "Content-Type" => "application/json") | 
					
						
							| 
									
										
										
										
											2020-07-29 21:50:02 +05:30
										 |  |  |     req.body = message.to_json | 
					
						
							|  |  |  |     response = http.request(req) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     unless response.kind_of? Net::HTTPSuccess | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |       if response.body.include?("Invalid webhook URL") | 
					
						
							|  |  |  |         error_key = "chat_integration.provider.teams.errors.invalid_channel" | 
					
						
							| 
									
										
										
										
											2020-07-29 21:50:02 +05:30
										 |  |  |       else | 
					
						
							|  |  |  |         error_key = nil | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |       raise ::DiscourseChatIntegration::ProviderError.new info: { | 
					
						
							|  |  |  |                                                             error_key: error_key, | 
					
						
							|  |  |  |                                                             request: req.body, | 
					
						
							|  |  |  |                                                             response_code: response.code, | 
					
						
							|  |  |  |                                                             response_body: response.body, | 
					
						
							|  |  |  |                                                           } | 
					
						
							| 
									
										
										
										
											2020-07-29 21:50:02 +05:30
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def self.get_message(post) | 
					
						
							|  |  |  |     display_name = "@#{post.user.username}" | 
					
						
							| 
									
										
										
										
											2022-06-16 13:12:56 -03:00
										 |  |  |     full_name = | 
					
						
							|  |  |  |       if SiteSetting.enable_names && post.user.name.present? | 
					
						
							|  |  |  |         post.user.name | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         "" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-07-29 21:50:02 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     topic = post.topic | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |     category = "" | 
					
						
							| 
									
										
										
										
											2020-07-29 21:50:02 +05:30
										 |  |  |     if topic.category&.uncategorized? | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |       category = "[#{I18n.t("uncategorized_category_name")}]" | 
					
						
							| 
									
										
										
										
											2020-07-29 21:50:02 +05:30
										 |  |  |     elsif topic.category | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |       category = | 
					
						
							|  |  |  |         ( | 
					
						
							|  |  |  |           if (topic.category.parent_category) | 
					
						
							|  |  |  |             "[#{topic.category.parent_category.name}/#{topic.category.name}]" | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             "[#{topic.category.name}]" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-07-29 21:50:02 +05:30
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     message = { | 
					
						
							|  |  |  |       "@type": "MessageCard", | 
					
						
							| 
									
										
										
										
											2022-12-29 12:31:05 +00:00
										 |  |  |       summary: topic.title, | 
					
						
							|  |  |  |       sections: [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           activityTitle: | 
					
						
							|  |  |  |             "[#{topic.title} #{category} #{topic.tags.present? ? topic.tags.map(&:name).join(", ") : ""}](#{post.full_url})", | 
					
						
							|  |  |  |           activitySubtitle: | 
					
						
							|  |  |  |             post.excerpt( | 
					
						
							|  |  |  |               SiteSetting.chat_integration_teams_excerpt_length, | 
					
						
							|  |  |  |               text_entities: true, | 
					
						
							|  |  |  |               strip_links: true, | 
					
						
							|  |  |  |               remap_emoji: true, | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |           activityImage: post.user.small_avatar_url, | 
					
						
							|  |  |  |           facts: [{ name: full_name, value: display_name }], | 
					
						
							|  |  |  |           markdown: true, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       ], | 
					
						
							| 
									
										
										
										
											2020-07-29 21:50:02 +05:30
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     message | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |