Skip to content

Commit

Permalink
CV2-3680: store tipline messages we sent
Browse files Browse the repository at this point in the history
  • Loading branch information
melsawy committed Sep 24, 2023
1 parent 7039a67 commit 149218b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
30 changes: 27 additions & 3 deletions app/models/bot/smooch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -682,12 +682,36 @@ def self.send_error_message(message, is_supported)
def self.send_message_to_user(uid, text, extra = {}, force = false, preview_url = true)
return if self.config['smooch_disabled'] && !force
if RequestStore.store[:smooch_bot_provider] == 'TURN'
self.turnio_send_message_to_user(uid, text, extra, force, preview_url)
response = self.turnio_send_message_to_user(uid, text, extra, force, preview_url)
elsif RequestStore.store[:smooch_bot_provider] == 'CAPI'
self.capi_send_message_to_user(uid, text, extra, force, preview_url)
response = self.capi_send_message_to_user(uid, text, extra, force, preview_url)
else
self.zendesk_send_message_to_user(uid, text, extra, force, preview_url)
response = self.zendesk_send_message_to_user(uid, text, extra, force, preview_url)
end
# store a TiplineMessage
external_id = self.get_id_from_send_response(response)
sent_at = DateTime.now
payload_json = { text: text }.merge(extra).to_json
team_id = self.config['team_id'].to_i
platform = RequestStore.store[:smooch_bot_platform]
self.delay.store_tipline_message(uid, external_id, sent_at, payload_json, team_id, platform)
response
end

def self.store_tipline_message(uid, external_id, sent_at, payload_json, team_id, platform)
payload = JSON.parse(payload_json)
tm = TiplineMessage.new
tm.uid = uid
tm.state = 'sent'
tm.direction = :outgoing
tm.language = self.get_user_language(uid)
tm.platform = platform
tm.sent_at = sent_at
tm.external_id = external_id
tm.payload = payload
tm.team_id = team_id
tm.skip_check_ability = true
tm.save!
end

def self.create_project_media_from_message(message)
Expand Down
2 changes: 2 additions & 0 deletions app/models/tipline_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ def from_smooch_payload(msg, payload, event = nil, language = nil)
when 'message:appUser'
{
direction: :incoming,
state: 'received',
sent_at: parse_timestamp(msg['received']),
platform: Bot::Smooch.get_platform_from_message(msg),
}
when 'message:delivery:channel'
{
direction: :outgoing,
state: 'delivered',
sent_at: parse_timestamp(payload['timestamp']),
platform: Bot::Smooch.get_platform_from_payload(payload),
}
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20230920055719_add_state_column_to_tipline_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AddStateColumnToTiplineMessage < ActiveRecord::Migration[6.1]
def change
add_column :tipline_messages, :state, :string
remove_index :tipline_messages, name: "index_tipline_messages_on_external_id"
add_index :tipline_messages, :external_id
# updated state for existing records
TiplineMessage.where(direction: "incoming").update_all(state: 'received')
TiplineMessage.where(direction: "outgoing").update_all(state: 'delivered')
end
end
8 changes: 5 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_09_14_152816) do
ActiveRecord::Schema.define(version: 2023_09_20_055719) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -639,7 +639,8 @@
t.bigint "team_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["external_id"], name: "index_tipline_messages_on_external_id", unique: true
t.string "state"
t.index ["external_id"], name: "index_tipline_messages_on_external_id"
t.index ["team_id"], name: "index_tipline_messages_on_team_id"
t.index ["uid"], name: "index_tipline_messages_on_uid"
end
Expand Down Expand Up @@ -779,7 +780,8 @@
end

create_table "versions", id: :serial, force: :cascade do |t|
t.string "item_type", null: false
t.string "item_type"
t.string "{:null=>false}"
t.string "item_id", null: false
t.string "event", null: false
t.string "whodunnit"
Expand Down

0 comments on commit 149218b

Please sign in to comment.