fix: Automatically remove expired story mention (#5300)
When a user mentions the connected Instagram page in a story, the story's content is downloaded in Chatwoot, then if the user deletes the story, the content persists in the platform. fixes: #5258
This commit is contained in:
@@ -46,6 +46,7 @@ class Messages::Messenger::MessageBuilder
|
||||
end
|
||||
|
||||
def update_attachment_file_type(attachment)
|
||||
return if @message.reload.attachments.blank?
|
||||
return unless attachment.file_type == 'share' || attachment.file_type == 'story_mention'
|
||||
|
||||
attachment.file_type = file_type(attachment.file&.content_type)
|
||||
@@ -62,6 +63,7 @@ class Messages::Messenger::MessageBuilder
|
||||
story_sender = result['from']['username']
|
||||
message.content_attributes[:story_sender] = story_sender
|
||||
message.content_attributes[:story_id] = story_id
|
||||
message.content_attributes[:image_type] = 'story_mention'
|
||||
message.content = I18n.t('conversations.messages.instagram_story_content', story_sender: story_sender)
|
||||
message.save!
|
||||
end
|
||||
@@ -74,6 +76,7 @@ class Messages::Messenger::MessageBuilder
|
||||
raise
|
||||
rescue Koala::Facebook::ClientError => e
|
||||
# The exception occurs when we are trying fetch the deleted story or blocked story.
|
||||
@message.attachments.destroy_all
|
||||
@message.update(content: I18n.t('conversations.messages.instagram_deleted_story_content'))
|
||||
Rails.logger.error e
|
||||
{}
|
||||
|
||||
@@ -54,19 +54,6 @@
|
||||
size="16"
|
||||
/>
|
||||
</button>
|
||||
<a
|
||||
v-if="hasInstagramStory && (isIncoming || isOutgoing) && linkToStory"
|
||||
:href="linkToStory"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer nofollow"
|
||||
>
|
||||
<fluent-icon
|
||||
v-tooltip.top-start="$t('CHAT_LIST.LINK_TO_STORY')"
|
||||
icon="open"
|
||||
class="action--icon cursor-pointer"
|
||||
size="16"
|
||||
/>
|
||||
</a>
|
||||
<a
|
||||
v-if="isATweet && (isOutgoing || isIncoming) && linkToTweet"
|
||||
:href="linkToTweet"
|
||||
|
||||
@@ -70,12 +70,15 @@ class Attachment < ApplicationRecord
|
||||
private
|
||||
|
||||
def file_metadata
|
||||
{
|
||||
metadata = {
|
||||
extension: extension,
|
||||
data_url: file_url,
|
||||
thumb_url: thumb_url,
|
||||
file_size: file.byte_size
|
||||
}
|
||||
|
||||
metadata[:data_url] = metadata[:thumb_url] = external_url if message.instagram_story_mention?
|
||||
metadata
|
||||
end
|
||||
|
||||
def location_metadata
|
||||
|
||||
@@ -63,4 +63,21 @@ class Channel::FacebookPage < ApplicationRecord
|
||||
Rails.logger.debug { "Rescued: #{e.inspect}" }
|
||||
true
|
||||
end
|
||||
|
||||
# TODO: We will be removing this code after instagram_manage_insights is implemented
|
||||
def fetch_instagram_story_link(message)
|
||||
k = Koala::Facebook::API.new(page_access_token)
|
||||
result = k.get_object(message.source_id, fields: %w[story]) || {}
|
||||
story_link = result['story']['mention']['link']
|
||||
# If the story is expired then it raises the ClientError and if the story is deleted with valid story-id it responses with nil
|
||||
delete_instagram_story(message) if story_link.blank?
|
||||
story_link
|
||||
rescue Koala::Facebook::ClientError => e
|
||||
delete_instagram_story(message)
|
||||
end
|
||||
|
||||
def delete_instagram_story(message)
|
||||
message.update(content: I18n.t('conversations.messages.instagram_deleted_story_content'), content_attributes: {})
|
||||
message.attachments.destroy_all
|
||||
end
|
||||
end
|
||||
|
||||
@@ -16,4 +16,8 @@ module MessageFilterHelpers
|
||||
def email_reply_summarizable?
|
||||
incoming? || outgoing? || input_csat?
|
||||
end
|
||||
|
||||
def instagram_story_mention?
|
||||
inbox.instagram? && try(:content_attributes)[:image_type] == 'story_mention'
|
||||
end
|
||||
end
|
||||
|
||||
@@ -78,6 +78,10 @@ class Inbox < ApplicationRecord
|
||||
channel_type == 'Channel::FacebookPage'
|
||||
end
|
||||
|
||||
def instagram?
|
||||
facebook? && channel.instagram_id.present?
|
||||
end
|
||||
|
||||
def web_widget?
|
||||
channel_type == 'Channel::WebWidget'
|
||||
end
|
||||
|
||||
@@ -107,10 +107,20 @@ class Message < ApplicationRecord
|
||||
conversation: { assignee_id: conversation.assignee_id }
|
||||
)
|
||||
data.merge!(echo_id: echo_id) if echo_id.present?
|
||||
validate_instagram_story if instagram_story_mention?
|
||||
data.merge!(attachments: attachments.map(&:push_event_data)) if attachments.present?
|
||||
merge_sender_attributes(data)
|
||||
end
|
||||
|
||||
# TODO: We will be removing this code after instagram_manage_insights is implemented
|
||||
# Better logic is to listen to webhook and remove stories proactively rather than trying
|
||||
# a fetch every time a message is returned
|
||||
def validate_instagram_story
|
||||
inbox.channel.fetch_instagram_story_link(self)
|
||||
# we want to reload the message in case the story has expired and data got removed
|
||||
reload
|
||||
end
|
||||
|
||||
def merge_sender_attributes(data)
|
||||
data.merge!(sender: sender.push_event_data) if sender && !sender.is_a?(AgentBot)
|
||||
data.merge!(sender: sender.push_event_data(inbox)) if sender.is_a?(AgentBot)
|
||||
|
||||
Reference in New Issue
Block a user