Bug: Fix duplicate messages from Twitter DM and tweets (#599)
* Bug: Fix duplicate messages from Twitter DM and tweets
This commit is contained in:
@@ -11,7 +11,9 @@ class Twitter::DirectMessageParserService < Twitter::WebhooksBaseService
|
||||
content: message_create_data['message_data']['text'],
|
||||
account_id: @inbox.account_id,
|
||||
inbox_id: @inbox.id,
|
||||
message_type: outgoing_message? ? :outgoing : :incoming
|
||||
message_type: outgoing_message? ? :outgoing : :incoming,
|
||||
contact_id: @contact.id,
|
||||
source_id: direct_message_data['id']
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
@@ -39,10 +39,16 @@ class Twitter::SendReplyService
|
||||
end
|
||||
|
||||
def send_tweet_reply
|
||||
twitter_client.send_tweet_reply(
|
||||
response = twitter_client.send_tweet_reply(
|
||||
reply_to_tweet_id: conversation.additional_attributes['tweet_id'],
|
||||
tweet: screen_name + message.content
|
||||
)
|
||||
if response.status == '200'
|
||||
tweet_data = response.body
|
||||
message.update!(source_id: tweet_data['id_str'])
|
||||
else
|
||||
Rails.logger 'TWITTER_TWEET_REPLY_ERROR' + response.body
|
||||
end
|
||||
end
|
||||
|
||||
def send_reply
|
||||
|
||||
@@ -3,16 +3,9 @@ class Twitter::TweetParserService < Twitter::WebhooksBaseService
|
||||
|
||||
def perform
|
||||
set_inbox
|
||||
find_or_create_contact(user)
|
||||
set_conversation
|
||||
@conversation.messages.create(
|
||||
account_id: @inbox.account_id,
|
||||
contact_id: @contact.id,
|
||||
content: tweet_text,
|
||||
inbox_id: @inbox.id,
|
||||
message_type: message_type,
|
||||
source_id: tweet_data['id'].to_s
|
||||
)
|
||||
return if message_already_exist?
|
||||
|
||||
create_message
|
||||
end
|
||||
|
||||
private
|
||||
@@ -37,6 +30,10 @@ class Twitter::TweetParserService < Twitter::WebhooksBaseService
|
||||
tweet_data['user']
|
||||
end
|
||||
|
||||
def tweet_id
|
||||
tweet_data['id'].to_s
|
||||
end
|
||||
|
||||
def parent_tweet_id
|
||||
tweet_data['in_reply_to_status_id_str'].nil? ? tweet_data['id'].to_s : tweet_data['in_reply_to_status_id_str']
|
||||
end
|
||||
@@ -66,4 +63,21 @@ class Twitter::TweetParserService < Twitter::WebhooksBaseService
|
||||
|
||||
@conversation = ::Conversation.create!(conversation_params)
|
||||
end
|
||||
|
||||
def message_already_exist?
|
||||
@inbox.messages.find_by(source_id: tweet_id)
|
||||
end
|
||||
|
||||
def create_message
|
||||
find_or_create_contact(user)
|
||||
set_conversation
|
||||
@conversation.messages.create(
|
||||
account_id: @inbox.account_id,
|
||||
contact_id: @contact.id,
|
||||
content: tweet_text,
|
||||
inbox_id: @inbox.id,
|
||||
message_type: message_type,
|
||||
source_id: tweet_id
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user