<commit_before>class FbAppPlugin::Auth < OauthClientPlugin::Auth

  module Status
    Connected = 'connected'
    NotAuthorized = 'not_authorized'
    Unknown = 'unknown'
  end

  settings_items :signed_request
  settings_items :fb_user

  attr_accessible :provider_user_id, :signed_request

  before_create :update_user
  before_create :exchange_token
  after_create :schedule_exchange_token
  after_destroy :destroy_page_tabs
  before_validation :set_enabled

  validates_presence_of :provider_user_id
  validates_uniqueness_of :provider_user_id, scope: :profile_id

  def self.parse_signed_request signed_request, credentials = FbAppPlugin.page_tab_app_credentials
    secret = credentials[:secret] rescue ''
    request = Facebook::SignedRequest.new signed_request, secret: secret
    request.data
  end

  def status
    if self.access_token.present? and self.not_expired? then Status::Connected else Status::NotAuthorized end
  end
  def not_authorized?
    self.status == Status::NotAuthorized
  end
  def connected?
    self.status == Status::Connected
  end

  def exchange_token
    app_id = FbAppPlugin.timeline_app_credentials[:id]
    app_secret = FbAppPlugin.timeline_app_credentials[:secret]
    fb_auth = FbGraph2::Auth.new app_id, app_secret
    fb_auth.fb_exchange_token = self.access_token

    access_token = fb_auth.access_token!
    self.access_token = access_token.access_token
    self.expires_in = access_token.expires_in
    # refresh user and its stored access token
    self.fetch_user
  end

  def exchange_token!
    self.exchange_token
    self.save!
  end

  def signed_request_data
    self.class.parse_signed_request self.signed_request
  end

  def fetch_user
    fb_user = FbGraph2::User.me self.access_token
    self.fb_user = fb_user.fetch
  end
  def update_user
    self.fb_user = self.fetch_user
  end

  protected

  def destroy_page_tabs
    self.profile.fb_app_page_tabs.destroy_all
  end

  def exchange_token_and_reschedule!
    self.exchange_token!
    self.schedule_exchange_token
  end

  def schedule_exchange_token
    self.delay(run_at: self.expires_at - 2.weeks).exchange_token_and_reschedule!
  end

  def set_enabled
    self.enabled = self.not_expired?
  end

end
<commit_msg>fb_app: don't try to reschedule expired auths<commit_after>class FbAppPlugin::Auth < OauthClientPlugin::Auth

  module Status
    Connected = 'connected'
    NotAuthorized = 'not_authorized'
    Unknown = 'unknown'
  end

  settings_items :signed_request
  settings_items :fb_user

  attr_accessible :provider_user_id, :signed_request

  before_create :update_user
  before_create :exchange_token
  after_create :schedule_exchange_token
  after_destroy :destroy_page_tabs
  before_validation :set_enabled

  validates_presence_of :provider_user_id
  validates_uniqueness_of :provider_user_id, scope: :profile_id

  def self.parse_signed_request signed_request, credentials = FbAppPlugin.page_tab_app_credentials
    secret = credentials[:secret] rescue ''
    request = Facebook::SignedRequest.new signed_request, secret: secret
    request.data
  end

  def status
    if self.access_token.present? and self.not_expired? then Status::Connected else Status::NotAuthorized end
  end
  def not_authorized?
    self.status == Status::NotAuthorized
  end
  def connected?
    self.status == Status::Connected
  end

  def exchange_token
    app_id = FbAppPlugin.timeline_app_credentials[:id]
    app_secret = FbAppPlugin.timeline_app_credentials[:secret]
    fb_auth = FbGraph2::Auth.new app_id, app_secret
    fb_auth.fb_exchange_token = self.access_token

    access_token = fb_auth.access_token!
    self.access_token = access_token.access_token
    self.expires_in = access_token.expires_in
    # refresh user and its stored access token
    self.fetch_user
  end

  def exchange_token!
    self.exchange_token
    self.save!
  end

  def signed_request_data
    self.class.parse_signed_request self.signed_request
  end

  def fetch_user
    fb_user = FbGraph2::User.me self.access_token
    self.fb_user = fb_user.fetch
  end
  def update_user
    self.fb_user = self.fetch_user
  end

  protected

  def destroy_page_tabs
    self.profile.fb_app_page_tabs.destroy_all
  end

  def exchange_token_and_reschedule!
    self.exchange_token!
    self.schedule_exchange_token
  end

  def schedule_exchange_token
    return unless self.expires_at < Time.now
    self.delay(run_at: self.expires_at - 2.weeks).exchange_token_and_reschedule!
  end

  def set_enabled
    self.enabled = self.not_expired?
  end

end

{'fraction_non_alphanumeric': 0.061440211407179035, 'fraction_numerical': 0.0013212948689715921, 'mean_word_length': 4.269141531322505, 'pattern_counts': {'":': 0, '<': 6, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5820723', 'n_tokens_mistral': 1528, 'n_tokens_neox': 1457, 'n_words': 298}