Skip to content

Commit

Permalink
Routine weeding of the codebase (#163)
Browse files Browse the repository at this point in the history
* Routine weeding of the codebase
* fix the spec
  • Loading branch information
sojan-official committed Oct 20, 2019
1 parent 94c6d6d commit 2099dc0
Show file tree
Hide file tree
Showing 16 changed files with 259 additions and 275 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -52,7 +52,7 @@ foreman start -f Procfile.dev

```bash
http://localhost:3000
user name: larry@google.com
user name: john@acme.inc
password: 123456
```

Expand Down
3 changes: 0 additions & 3 deletions app/models/channel.rb

This file was deleted.

26 changes: 26 additions & 0 deletions app/models/channel/facebook_page.rb
@@ -0,0 +1,26 @@
module Channel
class FacebookPage < ApplicationRecord
self.table_name = 'channel_facebook_pages'

validates :account_id, presence: true
validates_uniqueness_of :page_id, scope: :account_id
mount_uploader :avatar, AvatarUploader
belongs_to :account

has_one :inbox, as: :channel, dependent: :destroy

before_destroy :unsubscribe

def name
`Facebook`
end

private

def unsubscribe
Facebook::Messenger::Subscriptions.unsubscribe(access_token: page_access_token)
rescue => e
true
end
end
end
8 changes: 8 additions & 0 deletions app/models/channel/web_widget.rb
@@ -0,0 +1,8 @@
module Channel
class WebWidget < ApplicationRecord
self.table_name = 'channel_web_widgets'

belongs_to :account
has_one :inbox, as: :channel, dependent: :destroy
end
end
4 changes: 0 additions & 4 deletions app/models/channel/widget.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/inbox.rb
Expand Up @@ -25,7 +25,7 @@ def remove_member(user_id)
end

def facebook?
channel.class.name.to_s == 'FacebookPage'
channel.class.name.to_s == 'Channel::FacebookPage'
end

def next_available_agent
Expand Down
8 changes: 4 additions & 4 deletions app/services/facebook/send_reply_service.rb
Expand Up @@ -4,7 +4,7 @@ class SendReplyService

def perform
return if message.private
return if inbox.channel.class.to_s != 'FacebookPage'
return if inbox.channel.class.to_s != 'Channel::FacebookPage'
return unless outgoing_message_from_chatwoot?

Bot.deliver(delivery_params, access_token: message.channel_token)
Expand Down Expand Up @@ -55,14 +55,14 @@ def twenty_four_hour_window_over?

is_after_24_hours = (Time.current - last_incoming_message.created_at) / 3600 >= 24

false unless is_after_24_hours
return false unless is_after_24_hours

false if last_incoming_message && has_sent_first_outgoing_message_after_24_hours?(last_incoming_message.id)
return false if last_incoming_message && sent_first_outgoing_message_after_24_hours?(last_incoming_message.id)

true
end

def has_sent_first_outgoing_message_after_24_hours?(last_incoming_message_id)
def sent_first_outgoing_message_after_24_hours?(last_incoming_message_id)
# we can send max 1 message after 24 hour window
conversation.messages.outgoing.where('id > ?', last_incoming_message_id).count == 1
end
Expand Down
6 changes: 3 additions & 3 deletions config/initializers/bot.rb
Expand Up @@ -26,7 +26,7 @@ def app_id
end
end

class ExampleProvider < Facebook::Messenger::Configuration::Providers::Base
class ChatwootFbProvider < Facebook::Messenger::Configuration::Providers::Base
def valid_verify_token?(_verify_token)
ENV['fb_verify_token']
end
Expand All @@ -36,7 +36,7 @@ def app_secret_for(_page_id)
end

def access_token_for(page_id)
FacebookPage.where(page_id: page_id).last.access_token
Channel::FacebookPage.where(page_id: page_id).last.access_token
end

private
Expand All @@ -47,5 +47,5 @@ def bot
end

Facebook::Messenger.configure do |config|
config.provider = ExampleProvider.new
config.provider = ChatwootFbProvider.new
end
7 changes: 7 additions & 0 deletions db/migrate/20191020085608_rename_old_tables.rb
@@ -0,0 +1,7 @@
class RenameOldTables < ActiveRecord::Migration[6.1]
def change
drop_table :channels
rename_table :facebook_pages, :channel_facebook_pages
rename_table :channel_widgets, :channel_web_widgets
end
end

0 comments on commit 2099dc0

Please sign in to comment.