2020-02-05 13:20:38 +00:00
|
|
|
class Twitter::WebhooksBaseService
|
|
|
|
private
|
|
|
|
|
|
|
|
def profile_id
|
|
|
|
payload[:for_user_id]
|
|
|
|
end
|
|
|
|
|
2020-02-09 10:17:48 +00:00
|
|
|
def additional_contact_attributes(user)
|
|
|
|
{
|
|
|
|
screen_name: user['screen_name'],
|
|
|
|
location: user['location'],
|
|
|
|
url: user['url'],
|
|
|
|
description: user['description'],
|
|
|
|
followers_count: user['followers_count'],
|
|
|
|
friends_count: user['friends_count']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-02-05 13:20:38 +00:00
|
|
|
def set_inbox
|
|
|
|
twitter_profile = ::Channel::TwitterProfile.find_by(profile_id: profile_id)
|
|
|
|
@inbox = ::Inbox.find_by!(channel: twitter_profile)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_or_create_contact(user)
|
|
|
|
@contact_inbox = @inbox.contact_inboxes.where(source_id: user['id']).first
|
|
|
|
@contact = @contact_inbox.contact if @contact_inbox
|
|
|
|
return if @contact
|
|
|
|
|
2020-02-09 10:17:48 +00:00
|
|
|
@contact_inbox = @inbox.channel.create_contact_inbox(
|
|
|
|
user['id'], user['name'], additional_contact_attributes(user)
|
|
|
|
)
|
2020-02-05 13:20:38 +00:00
|
|
|
@contact = @contact_inbox.contact
|
|
|
|
avatar_resource = LocalResource.new(user['profile_image_url'])
|
|
|
|
@contact.avatar.attach(
|
|
|
|
io: avatar_resource.file,
|
|
|
|
filename: avatar_resource.tmp_filename,
|
|
|
|
content_type: avatar_resource.encoding
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|