<commit_before>MessageBus.site_id_lookup do
  RailsMultisite::ConnectionManagement.current_db
end

def setup_message_bus_env(env)
  return if env["__mb"]

  host = RailsMultisite::ConnectionManagement.host(env)
  RailsMultisite::ConnectionManagement.with_hostname(host) do
    user = CurrentUser.lookup_from_env(env)
    user_id = user && user.id
    is_admin = !!(user && user.admin?)
    group_ids = if is_admin
      # special rule, admin is allowed access to all groups
      Group.pluck(:id)
    elsif user
      user.groups.pluck('groups.id')
    end

    hash = {
      extra_headers:
        {
          "Access-Control-Allow-Origin" => Discourse.base_url_no_prefix,
          "Access-Control-Allow-Methods" => "GET, POST",
          "Access-Control-Allow-Headers" => "X-SILENCE-LOGGER, X-Shared-Session-Key"
        },
      user_id: user_id,
      group_ids: group_ids,
      is_admin: is_admin

    }<commit_msg>FIX: missing site id lookup causing message bus cross talk in multisite<commit_after>MessageBus.site_id_lookup do |env=nil|
  if env
    setup_message_bus_env(env)
    env["__mb"][:site_id]
  else
    RailsMultisite::ConnectionManagement.current_db
  end
end

def setup_message_bus_env(env)
  return if env["__mb"]

  host = RailsMultisite::ConnectionManagement.host(env)
  RailsMultisite::ConnectionManagement.with_hostname(host) do
    user = CurrentUser.lookup_from_env(env)
    user_id = user && user.id
    is_admin = !!(user && user.admin?)
    group_ids = if is_admin
      # special rule, admin is allowed access to all groups
      Group.pluck(:id)
    elsif user
      user.groups.pluck('groups.id')
    end

    hash = {
      extra_headers:
        {
          "Access-Control-Allow-Origin" => Discourse.base_url_no_prefix,
          "Access-Control-Allow-Methods" => "GET, POST",
          "Access-Control-Allow-Headers" => "X-SILENCE-LOGGER, X-Shared-Session-Key"
        },
      user_id: user_id,
      group_ids: group_ids,
      is_admin: is_admin,
      site_id: RailsMultisite::ConnectionManagement.current_db

    }
{'fraction_non_alphanumeric': 0.10781932977173385, 'fraction_numerical': 0.0, 'mean_word_length': 3.1365461847389557, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '510328', 'n_tokens_mistral': 685, 'n_tokens_neox': 669, 'n_words': 128}