0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-08 17:53:55 +08:00
discourse/plugins/discourse-zendesk-plugin/app/controllers/discourse_zendesk_plugin/sync_controller.rb
Arpit Jalan 96a277d2c1
FIX: preserve Zendesk webhook email attribution (#40115)
Previously, Zendesk sync resolved the webhook `email` before checking
whether the incoming comment needed to create a new post, and the
intended attribution behavior was not covered by a request spec.

This change moves that lookup to the post creation path and adds
coverage confirming that a valid Zendesk webhook with a matching `email`
creates the synced comment as that Discourse user.
2026-05-18 21:04:47 +05:30

57 lines
1.8 KiB
Ruby
Vendored

# frozen_string_literal: true
module DiscourseZendeskPlugin
class SyncController < ApplicationController
include DiscourseZendeskPlugin::Helper
requires_plugin PLUGIN_NAME
layout false
before_action :zendesk_token_valid?, only: :webhook
skip_before_action :check_xhr,
:preload_json,
:verify_authenticity_token,
:redirect_to_login_if_required,
only: :webhook
def webhook
unless SiteSetting.zendesk_enabled? && SiteSetting.sync_comments_from_zendesk
return render json: failed_json, status: :unprocessable_entity
end
ticket_id = params[:ticket_id]
raise Discourse::InvalidParameters.new(:ticket_id) if ticket_id.blank?
topic = Topic.find_by_id(params[:topic_id])
raise Discourse::InvalidParameters.new(:topic_id) if topic.blank?
return if !DiscourseZendeskPlugin::Helper.autogeneration_category?(topic.category_id)
latest_comment = get_latest_comment(ticket_id)
if latest_comment.present?
existing_comment =
PostCustomField.where(
name: DiscourseZendeskPlugin::ZENDESK_ID_FIELD,
value: latest_comment.id,
).first
if existing_comment.blank?
user = User.find_by_email(params[:email]) || Discourse.system_user
post = topic.posts.create!(user: user, raw: latest_comment.body)
update_post_custom_fields(post, latest_comment)
end
end
head :no_content
end
private
def zendesk_token_valid?
params.require(:token)
if SiteSetting.zendesk_incoming_webhook_token.blank? ||
SiteSetting.zendesk_incoming_webhook_token != params[:token]
raise Discourse::InvalidAccess.new
end
end
end
end