|
| 1 | +module PagerTree::Integrations |
| 2 | + class Meta::Workplace::V3 < Integration |
| 3 | + OPTIONS = [ |
| 4 | + {key: :incoming_enabled, type: :boolean, default: false}, |
| 5 | + {key: :outgoing_enabled, type: :boolean, default: true}, |
| 6 | + {key: :access_token, type: :string, default: nil}, |
| 7 | + {key: :app_secret, type: :string, default: nil}, |
| 8 | + {key: :group_id, type: :string, default: nil}, |
| 9 | + {key: :outgoing_rules, type: :string, default: nil} |
| 10 | + ] |
| 11 | + store_accessor :options, *OPTIONS.map { |x| x[:key] }.map(&:to_s), prefix: "option" |
| 12 | + |
| 13 | + validates :option_incoming_enabled, inclusion: {in: [true, false]} |
| 14 | + validates :option_outgoing_enabled, inclusion: {in: [true, false]} |
| 15 | + validates :option_access_token, presence: true |
| 16 | + # validates :option_app_secret, presence: true |
| 17 | + validates :option_group_id, presence: true |
| 18 | + |
| 19 | + after_initialize do |
| 20 | + self.option_incoming_enabled = false if option_incoming_enabled.nil? |
| 21 | + self.option_outgoing_enabled = true if option_outgoing_enabled.nil? |
| 22 | + self.option_outgoing_rules ||= "" |
| 23 | + end |
| 24 | + |
| 25 | + def endpoint |
| 26 | + super + "/g" |
| 27 | + end |
| 28 | + |
| 29 | + def adapter_should_block_incoming?(request) |
| 30 | + should_block = false |
| 31 | + |
| 32 | + if option_app_secret.present? && request.headers["x-hub-signature-256"].present? |
| 33 | + signature = request.headers["x-hub-signature-256"].delete_prefix("sha256=") |
| 34 | + data = request.body.read |
| 35 | + digest = OpenSSL::HMAC.hexdigest("SHA256", option_app_secret, data) |
| 36 | + should_block = signature != digest |
| 37 | + end |
| 38 | + |
| 39 | + should_block |
| 40 | + end |
| 41 | + |
| 42 | + def adapter_supports_incoming? |
| 43 | + # TODO - change this to true once we are ready to support this. The code currently works, but we don't want to confuse our users. |
| 44 | + # Let's work with them to see what they want the functionality to look like |
| 45 | + false |
| 46 | + end |
| 47 | + |
| 48 | + def adapter_supports_outgoing? |
| 49 | + true |
| 50 | + end |
| 51 | + |
| 52 | + def adapter_show_outgoing_webhook_delivery? |
| 53 | + true |
| 54 | + end |
| 55 | + |
| 56 | + def adapter_incoming_can_defer? |
| 57 | + false |
| 58 | + end |
| 59 | + |
| 60 | + def adapter_supports_title_template? |
| 61 | + false |
| 62 | + end |
| 63 | + |
| 64 | + def adapter_supports_description_template? |
| 65 | + false |
| 66 | + end |
| 67 | + |
| 68 | + def adapter_thirdparty_id |
| 69 | + [_entry&.dig("id"), _entry&.dig("time")].compact_blank.join("-") |
| 70 | + end |
| 71 | + |
| 72 | + def adapter_action |
| 73 | + if option_incoming_enabled && adapter_incoming_request_params.dig("object") == "page" && _entry_changes&.dig("field") == "mention" |
| 74 | + :create |
| 75 | + else |
| 76 | + :other |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + def adapter_response_incoming |
| 81 | + if adapter_incoming_request_params["hub.mode"] == "subscribe" |
| 82 | + adapter_controller&.render(plain: adapter_incoming_request_params["hub.challenge"]) |
| 83 | + else |
| 84 | + adapter_controller&.head(:ok) |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + def adapter_process_create |
| 89 | + # send back an ok |
| 90 | + post_id = _entry_changes&.dig("value", "post_id") |
| 91 | + _post_comment(post_id, "Ok") if post_id.present? |
| 92 | + |
| 93 | + Alert.new( |
| 94 | + title: _title, |
| 95 | + thirdparty_id: adapter_thirdparty_id |
| 96 | + ) |
| 97 | + end |
| 98 | + |
| 99 | + def adapter_process_other |
| 100 | + end |
| 101 | + |
| 102 | + def adapter_outgoing_interest?(event_name) |
| 103 | + option_outgoing_enabled && [ |
| 104 | + "alert_assigned", |
| 105 | + "alert_acknowledged", |
| 106 | + "alert_rejected", |
| 107 | + "alert_resolved", |
| 108 | + "alert_dropped", |
| 109 | + "alert_handoff", |
| 110 | + "comment_created" |
| 111 | + ].include?(event_name.to_s) |
| 112 | + end |
| 113 | + |
| 114 | + def adapter_process_outgoing |
| 115 | + event_type = adapter_outgoing_event.event_name.to_s |
| 116 | + message = _generate_message(event_type) |
| 117 | + |
| 118 | + return unless message.present? |
| 119 | + |
| 120 | + if event_type == "alert_assigned" |
| 121 | + _post_message(option_group_id, message) |
| 122 | + else |
| 123 | + post_id = _alert.meta["#{id}_meta_workplace_post_id"] |
| 124 | + |
| 125 | + if post_id.blank? |
| 126 | + outgoing_webhook_delivery_id = _alert.meta["#{id}_meta_workplace_outgoing_webhook_delivery_id"] |
| 127 | + outgoing_webhook_delivery = OutgoingWebhookDelivery.find_by(id: outgoing_webhook_delivery_id) |
| 128 | + post_id = begin |
| 129 | + JSON.parse(outgoing_webhook_delivery.responses.first.dig("body"))["id"] |
| 130 | + rescue |
| 131 | + nil |
| 132 | + end |
| 133 | + |
| 134 | + if post_id.present? |
| 135 | + _alert.meta["#{id}_meta_workplace_post_id"] = post_id |
| 136 | + _alert.save! |
| 137 | + end |
| 138 | + end |
| 139 | + |
| 140 | + _post_comment(post_id, message) if post_id.present? |
| 141 | + end |
| 142 | + rescue => e |
| 143 | + Rails.logger.error "[Workplace] Error processing outgoing event: #{e.message}" |
| 144 | + end |
| 145 | + |
| 146 | + private |
| 147 | + |
| 148 | + # INCOMING |
| 149 | + def _entry |
| 150 | + @_entry ||= adapter_incoming_request_params&.dig("entry", 0) |
| 151 | + end |
| 152 | + |
| 153 | + def _entry_changes |
| 154 | + @_entry_changes ||= _entry&.dig("changes", 0) |
| 155 | + end |
| 156 | + |
| 157 | + def _title |
| 158 | + _entry_changes&.dig("value", "message") |
| 159 | + end |
| 160 | + |
| 161 | + def _additional_datums |
| 162 | + [] |
| 163 | + end |
| 164 | + |
| 165 | + # OUTGOING |
| 166 | + def _alert |
| 167 | + @_alert ||= adapter_outgoing_event.alert |
| 168 | + end |
| 169 | + |
| 170 | + def _post_message(group_id, message) |
| 171 | + # This operation needs to happen immediately because we need to attach the post_id to the alert |
| 172 | + url = "https://graph.facebook.com/#{group_id}/feed?message=#{CGI.escape(message)}&formatting=MARKDOWN" |
| 173 | + outgoing_webhook_delivery = _send_outgoing(url) |
| 174 | + _alert.meta["#{id}_meta_workplace_outgoing_webhook_delivery_id"] = outgoing_webhook_delivery.id |
| 175 | + _alert.save! |
| 176 | + |
| 177 | + outgoing_webhook_delivery |
| 178 | + end |
| 179 | + |
| 180 | + def _post_comment(post_id, message) |
| 181 | + url = "https://graph.facebook.com/#{post_id}/comments?message=#{CGI.escape(message)}" |
| 182 | + _send_outgoing(url) |
| 183 | + end |
| 184 | + |
| 185 | + def _send_outgoing(url) |
| 186 | + outgoing_webhook_delivery = OutgoingWebhookDelivery.factory( |
| 187 | + resource: self, |
| 188 | + url: url, |
| 189 | + options: { |
| 190 | + headers: { |
| 191 | + "Authorization" => "Bearer #{option_access_token}" |
| 192 | + } |
| 193 | + } |
| 194 | + ) |
| 195 | + outgoing_webhook_delivery.save! |
| 196 | + outgoing_webhook_delivery.deliver_later |
| 197 | + |
| 198 | + outgoing_webhook_delivery |
| 199 | + end |
| 200 | + |
| 201 | + def _generate_message(event_name) |
| 202 | + case event_name |
| 203 | + when "alert_assigned" |
| 204 | + if _alert.incident? |
| 205 | + "[Incident ##{_alert.tiny_id}](#{Rails.application.routes.url_helpers.try(:alert_url, _alert, script_name: "/#{_alert.account_id}")}) [#{_alert.incident_severity.upcase.dasherize}] #{_alert.incident_message} - #{_alert.title}" |
| 206 | + else |
| 207 | + "[Alert ##{_alert.tiny_id}](#{Rails.application.routes.url_helpers.try(:alert_url, _alert, script_name: "/#{_alert.account_id}")}) #{_alert.title}" |
| 208 | + end |
| 209 | + when "alert_acknowledged" |
| 210 | + acknowledger = adapter_outgoing_event.account_user || adapter_outgoing_event.team |
| 211 | + acknowledger&.name&.present? ? "Acknowledged by #{acknowledger.name}" : "Acknowledged" |
| 212 | + when "alert_rejected" |
| 213 | + rejecter = adapter_outgoing_event.account_user |
| 214 | + rejecter&.name&.present? ? "Rejected by #{rejecter.name}" : "Rejected" |
| 215 | + when "alert_resolved" |
| 216 | + resolver = adapter_outgoing_event.account_user || adapter_outgoing_event.team |
| 217 | + resolver&.name&.present? ? "Resolved by #{resolver.name}" : "Resolved" |
| 218 | + when "alert_dropped" |
| 219 | + "Dropped" |
| 220 | + when "alert_handoff" |
| 221 | + handoff = adapter_outgoing_event.handoff |
| 222 | + "Handed off from #{handoff.source.name} to #{handoff.destination.name}" |
| 223 | + when "comment_created" |
| 224 | + comment = adapter_outgoing_event.comment |
| 225 | + commenter = comment.account_user&.name || "PagerTree" |
| 226 | + "#{commenter} commented: \"#{comment.body.to_plain_text}\"" |
| 227 | + end |
| 228 | + end |
| 229 | + end |
| 230 | +end |
0 commit comments