<commit_before>class PresenceChannel < ApplicationCable::Channel
  def subscribed
    @app      = App.find_by(key: params[:app])

    get_user_data

    visitor = nil
    # TODO , change this when leads can have mail
    if @user_data[:email].blank?
      visitor = get_user_by_session
      @app_user = visitor 
    else
      @app_user = @app.app_users
                  .where("email =?", @user_data[:email])
                  .first
    end

    @key      = "presence:#{@app.key}-#{visitor.present? ? visitor.session_id : @app_user.email}"
    stream_from @key
    pingback
  end

  def pingback
    @app_user.online! if @app_user.offline?
  end

  def unsubscribed
    @app_user.offline! if @app_user.online?
    # Any cleanup needed when channel is unsubscribed
  end
<commit_msg>Update presence_channel.rb<commit_after>class PresenceChannel < ApplicationCable::Channel
  after_unsubscribe :offline

  def subscribed

    @app      = App.find_by(key: params[:app])

    get_user_data

    visitor = nil
    @key    = "presence:#{@app.key}-"

    # TODO , change this when leads can have mail
    if @user_data[:email].blank?
      visitor = get_user_by_session
      @app_user = visitor 
      @key = @key + visitor.session_id
    else
      @app_user = @app.app_users
                  .where("email =?", @user_data[:email])
                  .first
      @key = @key + @app_user.email
    end

    stream_from @key
    pingback
  end

  def pingback
    @app_user.online! if @app_user.offline?
  end

  def offline
    puts "subs #{Redis.new.pubsub("CHANNELS", @key).size}"
    @app_user.offline! 
    #if Redis.new.pubsub("CHANNELS", @key).size == 1 && @app_user.online?
  end

{'fraction_non_alphanumeric': 0.11150652431791222, 'fraction_numerical': 0.0005931198102016608, 'mean_word_length': 2.5366876310272537, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '3690982', 'n_tokens_mistral': 581, 'n_tokens_neox': 558, 'n_words': 125}