Skip to content

Commit

Permalink
Feat: Manage conversation for tweets based on the tweet flag (#3353)
Browse files Browse the repository at this point in the history
Add tweet conversation only if tweets are enabled.

Fixes #1961
  • Loading branch information
tejaswinichile committed Dec 15, 2021
1 parent e2e459a commit 9984edd
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 17 deletions.
15 changes: 1 addition & 14 deletions app/controllers/api/v1/accounts/inboxes_controller.rb
Expand Up @@ -124,19 +124,6 @@ def permitted_params(channel_attributes = [])
end

def get_channel_attributes(channel_type)
case channel_type
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
channel_type.constantize::EDITABLE_ATTRS.presence || []
end
end
5 changes: 4 additions & 1 deletion app/javascript/dashboard/i18n/locale/en/inboxMgmt.json
Expand Up @@ -47,7 +47,10 @@
},
"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": {
"TITLE": "Website channel",
Expand Down
Expand Up @@ -32,6 +32,14 @@
:label="inboxNameLabel"
: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
v-if="isAPIInbox"
v-model.trim="webhookUrl"
Expand Down Expand Up @@ -401,6 +409,7 @@ export default {
avatarUrl: '',
selectedAgents: [],
greetingEnabled: true,
tweetsEnabled: true,
hmacMandatory: null,
greetingMessage: '',
autoAssignment: false,
Expand Down Expand Up @@ -564,6 +573,7 @@ export default {
this.selectedInboxName = this.inbox.name;
this.webhookUrl = this.inbox.webhook_url;
this.greetingEnabled = this.inbox.greeting_enabled || false;
this.tweetsEnabled = this.inbox.tweets_enabled || false;
this.hmacMandatory = this.inbox.hmac_mandatory || false;
this.greetingMessage = this.inbox.greeting_message || '';
this.autoAssignment = this.inbox.enable_auto_assignment;
Expand Down Expand Up @@ -622,6 +632,7 @@ export default {
selectedFeatureFlags: this.selectedFeatureFlags,
reply_time: this.replyTime || 'in_a_few_minutes',
hmac_mandatory: this.hmacMandatory,
tweets_enabled: this.tweetsEnabled,
},
};
if (this.avatarFile) {
Expand Down
3 changes: 3 additions & 0 deletions app/models/channel/twitter_profile.rb
Expand Up @@ -3,6 +3,7 @@
# Table name: channel_twitter_profiles
#
# id :bigint not null, primary key
# tweets_enabled :boolean default(TRUE)
# twitter_access_token :string not null
# twitter_access_token_secret :string not null
# created_at :datetime not null
Expand All @@ -24,6 +25,8 @@ class Channel::TwitterProfile < ApplicationRecord

before_destroy :unsubscribe

EDITABLE_ATTRS = [:tweets_enabled].freeze

def name
'Twitter'
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/inbox.rb
Expand Up @@ -87,6 +87,10 @@ def twilio?
channel_type == 'Channel::TwilioSms'
end

def twitter?
channel_type == 'Channel::TwitterProfile'
end

def inbox_type
channel.name
end
Expand Down
7 changes: 6 additions & 1 deletion app/services/twitter/tweet_parser_service.rb
Expand Up @@ -3,7 +3,8 @@ class Twitter::TweetParserService < Twitter::WebhooksBaseService

def perform
set_inbox
return if message_already_exist? || user_has_blocked?

return if !tweets_enabled? || message_already_exist? || user_has_blocked?

create_message
end
Expand Down Expand Up @@ -38,6 +39,10 @@ def user_has_blocked?
payload['user_has_blocked'] == true
end

def tweets_enabled?
@inbox.channel.tweets_enabled?
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
Expand Down
2 changes: 2 additions & 0 deletions app/views/api/v1/models/_inbox.json.jbuilder
Expand Up @@ -14,6 +14,8 @@ json.working_hours resource.weekly_schedule
json.timezone resource.timezone
json.callback_webhook_url resource.callback_webhook_url

json.tweets_enabled resource.channel.try(:tweets_enabled) if resource.twitter?

## Channel specific settings
## TODO : Clean up and move the attributes into channel sub section

Expand Down
@@ -0,0 +1,5 @@
class AddTweetEnabledFlagToTwitterChannel < ActiveRecord::Migration[6.1]
def change
add_column :channel_twitter_profiles, :tweets_enabled, :boolean, default: true
end
end
1 change: 1 addition & 0 deletions db/schema.rb
Expand Up @@ -242,6 +242,7 @@
t.integer "account_id", null: false
t.datetime "created_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
end

Expand Down
1 change: 1 addition & 0 deletions lib/webhooks/twitter.rb
Expand Up @@ -2,6 +2,7 @@

class Webhooks::Twitter
SUPPORTED_EVENTS = [:direct_message_events, :tweet_create_events].freeze
EDITABLE_ATTRS = [:tweets_enabled].freeze

attr_accessor :params, :account

Expand Down
13 changes: 13 additions & 0 deletions spec/controllers/api/v1/accounts/inboxes_controller_spec.rb
Expand Up @@ -362,6 +362,19 @@
expect(api_channel.reload.webhook_url).to eq('webhook.test')
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
email_channel = create(:channel_email, account: account)
email_inbox = create(:inbox, channel: email_channel, account: account)
Expand Down
23 changes: 22 additions & 1 deletion spec/lib/webhooks/twitter_spec.rb
Expand Up @@ -6,7 +6,7 @@

let!(:account) { create(:account) }
# 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!(:dm_params) { build(:twitter_message_create_event).with_indifferent_access }
let!(:tweet_params) { build(:tweet_create_event).with_indifferent_access }
Expand All @@ -32,6 +32,27 @@

it 'creates incoming message in the twitter inbox' do
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.conversations.count).to be 1
expect(twitter_inbox.messages.count).to be 1
Expand Down

0 comments on commit 9984edd

Please sign in to comment.