-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
twitter_profile.rb
62 lines (52 loc) 路 2.05 KB
/
twitter_profile.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# == Schema Information
#
# Table name: channel_twitter_profiles
#
# id :bigint not null, primary key
# tweets_enabled :boolean default(TRUE)
# 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
#
# Indexes
#
# index_channel_twitter_profiles_on_account_id_and_profile_id (account_id,profile_id) UNIQUE
#
class Channel::TwitterProfile < ApplicationRecord
include Channelable
self.table_name = 'channel_twitter_profiles'
validates :profile_id, uniqueness: { scope: :account_id }
before_destroy :unsubscribe
EDITABLE_ATTRS = [:tweets_enabled].freeze
def name
'Twitter'
end
def create_contact_inbox(profile_id, name, additional_attributes)
::ContactInboxWithContactBuilder.new({
source_id: profile_id,
inbox: inbox,
contact_attributes: { name: name, additional_attributes: additional_attributes }
}).perform
end
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
private
def unsubscribe
### Fix unsubscription with new endpoint
unsubscribe_response = twitter_client.remove_subscription(user_id: profile_id)
Rails.logger.info "TWITTER_UNSUBSCRIBE: #{unsubscribe_response.body}"
rescue StandardError => e
Rails.logger.error e
end
end