2020-02-02 19:09:00 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: channel_twitter_profiles
|
|
|
|
#
|
|
|
|
# id :bigint not null, primary key
|
2021-12-15 18:54:50 +00:00
|
|
|
# tweets_enabled :boolean default(TRUE)
|
2020-02-02 19:09:00 +00:00
|
|
|
# twitter_access_token :string not null
|
|
|
|
# twitter_access_token_secret :string not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# account_id :integer not null
|
|
|
|
# profile_id :string not null
|
|
|
|
#
|
2020-04-05 16:41:27 +00:00
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_channel_twitter_profiles_on_account_id_and_profile_id (account_id,profile_id) UNIQUE
|
|
|
|
#
|
2020-02-02 19:09:00 +00:00
|
|
|
|
|
|
|
class Channel::TwitterProfile < ApplicationRecord
|
2021-09-10 20:01:17 +00:00
|
|
|
include Channelable
|
|
|
|
|
2020-02-02 19:09:00 +00:00
|
|
|
self.table_name = 'channel_twitter_profiles'
|
|
|
|
|
|
|
|
validates :profile_id, uniqueness: { scope: :account_id }
|
|
|
|
|
|
|
|
before_destroy :unsubscribe
|
|
|
|
|
2021-12-15 18:54:50 +00:00
|
|
|
EDITABLE_ATTRS = [:tweets_enabled].freeze
|
|
|
|
|
2020-09-04 13:43:47 +00:00
|
|
|
def name
|
|
|
|
'Twitter'
|
|
|
|
end
|
|
|
|
|
2020-02-09 10:17:48 +00:00
|
|
|
def create_contact_inbox(profile_id, name, additional_attributes)
|
2020-02-02 19:09:00 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
2020-02-09 10:17:48 +00:00
|
|
|
contact = inbox.account.contacts.create!(additional_attributes: additional_attributes, name: name)
|
2020-02-02 19:09:00 +00:00
|
|
|
::ContactInbox.create!(
|
|
|
|
contact_id: contact.id,
|
|
|
|
inbox_id: inbox.id,
|
|
|
|
source_id: profile_id
|
|
|
|
)
|
|
|
|
rescue StandardError => e
|
2020-05-14 17:21:07 +00:00
|
|
|
Rails.logger.info e
|
2020-02-02 19:09:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-11 08:57:38 +00:00
|
|
|
def twitter_client
|
|
|
|
Twitty::Facade.new do |config|
|
|
|
|
config.consumer_key = ENV.fetch('TWITTER_CONSUMER_KEY', nil)
|
|
|
|
config.consumer_secret = ENV.fetch('TWITTER_CONSUMER_SECRET', nil)
|
|
|
|
config.access_token = twitter_access_token
|
|
|
|
config.access_token_secret = twitter_access_token_secret
|
|
|
|
config.base_url = 'https://api.twitter.com'
|
|
|
|
config.environment = ENV.fetch('TWITTER_ENVIRONMENT', '')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-02 19:09:00 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def unsubscribe
|
2020-05-14 17:21:07 +00:00
|
|
|
### Fix unsubscription with new endpoint
|
|
|
|
unsubscribe_response = twitter_client.remove_subscription(user_id: profile_id)
|
2020-09-08 05:54:08 +00:00
|
|
|
Rails.logger.info "TWITTER_UNSUBSCRIBE: #{unsubscribe_response.body}"
|
2020-05-14 17:21:07 +00:00
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.info e
|
2020-02-02 19:09:00 +00:00
|
|
|
end
|
|
|
|
end
|