Feat: Manage conversation for tweets based on the tweet flag (#3353)
Add tweet conversation only if tweets are enabled. Fixes #1961
This commit is contained in:
@@ -124,19 +124,6 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def get_channel_attributes(channel_type)
|
def get_channel_attributes(channel_type)
|
||||||
case channel_type
|
channel_type.constantize::EDITABLE_ATTRS.presence || []
|
||||||
when 'Channel::WebWidget'
|
|
||||||
Channel::WebWidget::EDITABLE_ATTRS
|
|
||||||
when 'Channel::Api'
|
|
||||||
Channel::Api::EDITABLE_ATTRS
|
|
||||||
when 'Channel::Email'
|
|
||||||
Channel::Email::EDITABLE_ATTRS
|
|
||||||
when 'Channel::Telegram'
|
|
||||||
Channel::Telegram::EDITABLE_ATTRS
|
|
||||||
when 'Channel::Line'
|
|
||||||
Channel::Line::EDITABLE_ATTRS
|
|
||||||
else
|
|
||||||
[]
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -47,7 +47,10 @@
|
|||||||
},
|
},
|
||||||
"TWITTER": {
|
"TWITTER": {
|
||||||
"HELP": "To add your Twitter profile as a channel, you need to authenticate your Twitter Profile by clicking on 'Sign in with Twitter' ",
|
"HELP": "To add your Twitter profile as a channel, you need to authenticate your Twitter Profile by clicking on 'Sign in with Twitter' ",
|
||||||
"ERROR_MESSAGE": "There was an error connecting to Twitter, please try again"
|
"ERROR_MESSAGE": "There was an error connecting to Twitter, please try again",
|
||||||
|
"TWEETS": {
|
||||||
|
"ENABLE": "Create conversations from mentioned Tweets"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"WEBSITE_CHANNEL": {
|
"WEBSITE_CHANNEL": {
|
||||||
"TITLE": "Website channel",
|
"TITLE": "Website channel",
|
||||||
|
|||||||
@@ -32,6 +32,14 @@
|
|||||||
:label="inboxNameLabel"
|
:label="inboxNameLabel"
|
||||||
:placeholder="inboxNamePlaceHolder"
|
:placeholder="inboxNamePlaceHolder"
|
||||||
/>
|
/>
|
||||||
|
<label for="toggle-business-hours" class="toggle-input-wrap" v-if="isATwitterInbox">
|
||||||
|
<input
|
||||||
|
v-model="tweetsEnabled"
|
||||||
|
type="checkbox"
|
||||||
|
name="toggle-business-hours"
|
||||||
|
/>
|
||||||
|
{{ $t('INBOX_MGMT.ADD.TWITTER.TWEETS.ENABLE') }}
|
||||||
|
</label>
|
||||||
<woot-input
|
<woot-input
|
||||||
v-if="isAPIInbox"
|
v-if="isAPIInbox"
|
||||||
v-model.trim="webhookUrl"
|
v-model.trim="webhookUrl"
|
||||||
@@ -401,6 +409,7 @@ export default {
|
|||||||
avatarUrl: '',
|
avatarUrl: '',
|
||||||
selectedAgents: [],
|
selectedAgents: [],
|
||||||
greetingEnabled: true,
|
greetingEnabled: true,
|
||||||
|
tweetsEnabled: true,
|
||||||
hmacMandatory: null,
|
hmacMandatory: null,
|
||||||
greetingMessage: '',
|
greetingMessage: '',
|
||||||
autoAssignment: false,
|
autoAssignment: false,
|
||||||
@@ -564,6 +573,7 @@ export default {
|
|||||||
this.selectedInboxName = this.inbox.name;
|
this.selectedInboxName = this.inbox.name;
|
||||||
this.webhookUrl = this.inbox.webhook_url;
|
this.webhookUrl = this.inbox.webhook_url;
|
||||||
this.greetingEnabled = this.inbox.greeting_enabled || false;
|
this.greetingEnabled = this.inbox.greeting_enabled || false;
|
||||||
|
this.tweetsEnabled = this.inbox.tweets_enabled || false;
|
||||||
this.hmacMandatory = this.inbox.hmac_mandatory || false;
|
this.hmacMandatory = this.inbox.hmac_mandatory || false;
|
||||||
this.greetingMessage = this.inbox.greeting_message || '';
|
this.greetingMessage = this.inbox.greeting_message || '';
|
||||||
this.autoAssignment = this.inbox.enable_auto_assignment;
|
this.autoAssignment = this.inbox.enable_auto_assignment;
|
||||||
@@ -622,6 +632,7 @@ export default {
|
|||||||
selectedFeatureFlags: this.selectedFeatureFlags,
|
selectedFeatureFlags: this.selectedFeatureFlags,
|
||||||
reply_time: this.replyTime || 'in_a_few_minutes',
|
reply_time: this.replyTime || 'in_a_few_minutes',
|
||||||
hmac_mandatory: this.hmacMandatory,
|
hmac_mandatory: this.hmacMandatory,
|
||||||
|
tweets_enabled: this.tweetsEnabled,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
if (this.avatarFile) {
|
if (this.avatarFile) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
# Table name: channel_twitter_profiles
|
# Table name: channel_twitter_profiles
|
||||||
#
|
#
|
||||||
# id :bigint not null, primary key
|
# id :bigint not null, primary key
|
||||||
|
# tweets_enabled :boolean default(TRUE)
|
||||||
# twitter_access_token :string not null
|
# twitter_access_token :string not null
|
||||||
# twitter_access_token_secret :string not null
|
# twitter_access_token_secret :string not null
|
||||||
# created_at :datetime not null
|
# created_at :datetime not null
|
||||||
@@ -24,6 +25,8 @@ class Channel::TwitterProfile < ApplicationRecord
|
|||||||
|
|
||||||
before_destroy :unsubscribe
|
before_destroy :unsubscribe
|
||||||
|
|
||||||
|
EDITABLE_ATTRS = [:tweets_enabled].freeze
|
||||||
|
|
||||||
def name
|
def name
|
||||||
'Twitter'
|
'Twitter'
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -87,6 +87,10 @@ class Inbox < ApplicationRecord
|
|||||||
channel_type == 'Channel::TwilioSms'
|
channel_type == 'Channel::TwilioSms'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def twitter?
|
||||||
|
channel_type == 'Channel::TwitterProfile'
|
||||||
|
end
|
||||||
|
|
||||||
def inbox_type
|
def inbox_type
|
||||||
channel.name
|
channel.name
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,7 +3,8 @@ class Twitter::TweetParserService < Twitter::WebhooksBaseService
|
|||||||
|
|
||||||
def perform
|
def perform
|
||||||
set_inbox
|
set_inbox
|
||||||
return if message_already_exist? || user_has_blocked?
|
|
||||||
|
return if !tweets_enabled? || message_already_exist? || user_has_blocked?
|
||||||
|
|
||||||
create_message
|
create_message
|
||||||
end
|
end
|
||||||
@@ -38,6 +39,10 @@ class Twitter::TweetParserService < Twitter::WebhooksBaseService
|
|||||||
payload['user_has_blocked'] == true
|
payload['user_has_blocked'] == true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def tweets_enabled?
|
||||||
|
@inbox.channel.tweets_enabled?
|
||||||
|
end
|
||||||
|
|
||||||
def parent_tweet_id
|
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']
|
tweet_data['in_reply_to_status_id_str'].nil? ? tweet_data['id'].to_s : tweet_data['in_reply_to_status_id_str']
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ json.working_hours resource.weekly_schedule
|
|||||||
json.timezone resource.timezone
|
json.timezone resource.timezone
|
||||||
json.callback_webhook_url resource.callback_webhook_url
|
json.callback_webhook_url resource.callback_webhook_url
|
||||||
|
|
||||||
|
json.tweets_enabled resource.channel.try(:tweets_enabled) if resource.twitter?
|
||||||
|
|
||||||
## Channel specific settings
|
## Channel specific settings
|
||||||
## TODO : Clean up and move the attributes into channel sub section
|
## TODO : Clean up and move the attributes into channel sub section
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
class AddTweetEnabledFlagToTwitterChannel < ActiveRecord::Migration[6.1]
|
||||||
|
def change
|
||||||
|
add_column :channel_twitter_profiles, :tweets_enabled, :boolean, default: true
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -242,6 +242,7 @@ ActiveRecord::Schema.define(version: 2021_12_08_085931) do
|
|||||||
t.integer "account_id", null: false
|
t.integer "account_id", null: false
|
||||||
t.datetime "created_at", precision: 6, null: false
|
t.datetime "created_at", precision: 6, null: false
|
||||||
t.datetime "updated_at", precision: 6, null: false
|
t.datetime "updated_at", precision: 6, null: false
|
||||||
|
t.boolean "tweets_enabled", default: true
|
||||||
t.index ["account_id", "profile_id"], name: "index_channel_twitter_profiles_on_account_id_and_profile_id", unique: true
|
t.index ["account_id", "profile_id"], name: "index_channel_twitter_profiles_on_account_id_and_profile_id", unique: true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
class Webhooks::Twitter
|
class Webhooks::Twitter
|
||||||
SUPPORTED_EVENTS = [:direct_message_events, :tweet_create_events].freeze
|
SUPPORTED_EVENTS = [:direct_message_events, :tweet_create_events].freeze
|
||||||
|
EDITABLE_ATTRS = [:tweets_enabled].freeze
|
||||||
|
|
||||||
attr_accessor :params, :account
|
attr_accessor :params, :account
|
||||||
|
|
||||||
|
|||||||
@@ -362,6 +362,19 @@ RSpec.describe 'Inboxes API', type: :request do
|
|||||||
expect(api_channel.reload.webhook_url).to eq('webhook.test')
|
expect(api_channel.reload.webhook_url).to eq('webhook.test')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'updates twitter inbox when administrator' do
|
||||||
|
api_channel = create(:channel_twitter_profile, account: account, tweets_enabled: true)
|
||||||
|
api_inbox = create(:inbox, channel: api_channel, account: account)
|
||||||
|
|
||||||
|
patch "/api/v1/accounts/#{account.id}/inboxes/#{api_inbox.id}",
|
||||||
|
headers: admin.create_new_auth_token,
|
||||||
|
params: { channel: { tweets_enabled: false } },
|
||||||
|
as: :json
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
expect(api_channel.reload.tweets_enabled).to eq(false)
|
||||||
|
end
|
||||||
|
|
||||||
it 'updates email inbox when administrator' do
|
it 'updates email inbox when administrator' do
|
||||||
email_channel = create(:channel_email, account: account)
|
email_channel = create(:channel_email, account: account)
|
||||||
email_inbox = create(:inbox, channel: email_channel, account: account)
|
email_inbox = create(:inbox, channel: email_channel, account: account)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ describe Webhooks::Twitter do
|
|||||||
|
|
||||||
let!(:account) { create(:account) }
|
let!(:account) { create(:account) }
|
||||||
# FIX ME: recipient id is set to 1 inside event factories
|
# FIX ME: recipient id is set to 1 inside event factories
|
||||||
let!(:twitter_channel) { create(:channel_twitter_profile, account: account, profile_id: '1') }
|
let!(:twitter_channel) { create(:channel_twitter_profile, account: account, profile_id: '1', tweets_enabled: true) }
|
||||||
let!(:twitter_inbox) { create(:inbox, channel: twitter_channel, account: account, greeting_enabled: false) }
|
let!(:twitter_inbox) { create(:inbox, channel: twitter_channel, account: account, greeting_enabled: false) }
|
||||||
let!(:dm_params) { build(:twitter_message_create_event).with_indifferent_access }
|
let!(:dm_params) { build(:twitter_message_create_event).with_indifferent_access }
|
||||||
let!(:tweet_params) { build(:tweet_create_event).with_indifferent_access }
|
let!(:tweet_params) { build(:tweet_create_event).with_indifferent_access }
|
||||||
@@ -32,6 +32,27 @@ describe Webhooks::Twitter do
|
|||||||
|
|
||||||
it 'creates incoming message in the twitter inbox' do
|
it 'creates incoming message in the twitter inbox' do
|
||||||
twitter_webhook.new(tweet_params).consume
|
twitter_webhook.new(tweet_params).consume
|
||||||
|
twitter_inbox.reload
|
||||||
|
expect(twitter_inbox.contacts.count).to be 1
|
||||||
|
expect(twitter_inbox.conversations.count).to be 1
|
||||||
|
expect(twitter_inbox.messages.count).to be 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with tweet_enabled flag disabled' do
|
||||||
|
before do
|
||||||
|
twitter_channel.update(tweets_enabled: false)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not create incoming message in the twitter inbox for tweet' do
|
||||||
|
twitter_webhook.new(tweet_params).consume
|
||||||
|
expect(twitter_inbox.contacts.count).to be 0
|
||||||
|
expect(twitter_inbox.conversations.count).to be 0
|
||||||
|
expect(twitter_inbox.messages.count).to be 0
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'creates incoming message in the twitter inbox' do
|
||||||
|
twitter_webhook.new(dm_params).consume
|
||||||
expect(twitter_inbox.contacts.count).to be 1
|
expect(twitter_inbox.contacts.count).to be 1
|
||||||
expect(twitter_inbox.conversations.count).to be 1
|
expect(twitter_inbox.conversations.count).to be 1
|
||||||
expect(twitter_inbox.messages.count).to be 1
|
expect(twitter_inbox.messages.count).to be 1
|
||||||
|
|||||||
Reference in New Issue
Block a user