Compare commits

..
6 changed files with 14 additions and 27 deletions
+3 -5
View File
@@ -72,11 +72,9 @@ class ContactIdentifyAction
def merge_contacts?(existing_contact, key)
return if existing_contact.blank?
# Never merge into a contact that already owns an identifier unless the request
# supplies the matching one. This also covers the blank-identifier case: an
# unverified/anonymous request must not take over an identified contact just by
# matching its email or phone_number. Merges into non-identified contacts
# (anonymous dedup) are still allowed.
return true if params[:identifier].blank?
# we want to prevent merging contacts with different identifiers
if existing_contact.identifier.present? && existing_contact.identifier != params[:identifier]
# we will remove attribute from update list
@attributes_to_update.delete(key)
@@ -38,7 +38,7 @@ module InstagramConcern
end
def fetch_instagram_user_details(access_token)
endpoint = 'https://graph.instagram.com/v22.0/me'
endpoint = "https://graph.instagram.com/#{GlobalConfigService.load('INSTAGRAM_API_VERSION', 'v22.0')}/me"
params = {
fields: 'id,username,user_id,name,profile_picture_url,account_type',
access_token: access_token
+8 -2
View File
@@ -45,7 +45,7 @@ class Channel::Instagram < ApplicationRecord
def subscribe
# ref https://developers.facebook.com/docs/instagram-platform/webhooks#enable-subscriptions
HTTParty.post(
"https://graph.instagram.com/v22.0/#{instagram_id}/subscribed_apps",
"#{base_uri}/#{instagram_id}/subscribed_apps",
query: {
subscribed_fields: %w[messages message_reactions messaging_seen],
access_token: access_token
@@ -58,7 +58,7 @@ class Channel::Instagram < ApplicationRecord
def unsubscribe
HTTParty.delete(
"https://graph.instagram.com/v22.0/#{instagram_id}/subscribed_apps",
"#{base_uri}/#{instagram_id}/subscribed_apps",
query: {
access_token: access_token
}
@@ -72,4 +72,10 @@ class Channel::Instagram < ApplicationRecord
def access_token
Instagram::RefreshOauthTokenService.new(channel: self).access_token
end
private
def base_uri
"https://graph.instagram.com/#{GlobalConfigService.load('INSTAGRAM_API_VERSION', 'v22.0')}"
end
end
@@ -13,7 +13,7 @@ class Instagram::SendOnInstagramService < Instagram::BaseSendService
instagram_id = channel.instagram_id.presence || 'me'
response = HTTParty.post(
"https://graph.instagram.com/v22.0/#{instagram_id}/messages",
"https://graph.instagram.com/#{GlobalConfigService.load('INSTAGRAM_API_VERSION', 'v22.0')}/#{instagram_id}/messages",
body: message_content,
query: query
)
@@ -107,24 +107,6 @@ describe ContactIdentifyAction do
end
end
context 'when a request without an identifier matches an already identified contact' do
it 'does not merge into the identified contact via a matching email' do
victim = create(:contact, account: account, identifier: 'victim_id', email: 'victim@test.com', name: 'Victim')
params = { email: 'victim@test.com', name: 'Attacker' }
result = described_class.new(contact: contact, params: params).perform
expect(result.id).not_to eq victim.id
expect(victim.reload.name).to eq 'Victim'
end
it 'does not merge into the identified contact via a matching phone_number' do
victim = create(:contact, account: account, identifier: 'victim_id', phone_number: '+919999888877', name: 'Victim')
params = { phone_number: '+919999888877', name: 'Attacker' }
result = described_class.new(contact: contact, params: params).perform
expect(result.id).not_to eq victim.id
expect(victim.reload.name).to eq 'Victim'
end
end
context 'when contacts with blank identifiers exist and identify action is called with blank identifier' do
it 'updates the attributes of contact passed in to identify action' do
create(:contact, account: account, identifier: '')
@@ -12,6 +12,7 @@ RSpec.describe InstagramConcern do
before do
allow(GlobalConfigService).to receive(:load).with('INSTAGRAM_APP_ID', nil).and_return(client_id)
allow(GlobalConfigService).to receive(:load).with('INSTAGRAM_APP_SECRET', nil).and_return(client_secret)
allow(GlobalConfigService).to receive(:load).with('INSTAGRAM_API_VERSION', 'v22.0').and_return('v22.0')
allow(Rails.logger).to receive(:error)
end