-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block WhatsApp user if WhatsApp API reports a "pair rate limit hit" e…
…rror If WhatsApp Cloud API reports an error of type "pair rate limit hit" when we try to send a message to a WhatsApp user, block that user automatically, so we don't need to answer them again. It helps with SPAM and abuse. Reference: CV2-3489.
- Loading branch information
Showing
9 changed files
with
118 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class BlockedTiplineUser < ApplicationRecord | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require 'active_support/concern' | ||
|
||
module SmoochBlocking | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
def ban_user(message) | ||
unless message.nil? | ||
uid = message['authorId'] | ||
self.block_user(uid) | ||
end | ||
end | ||
|
||
def block_user_from_error_code(uid, error_code) | ||
self.block_user(uid) if error_code == 131056 # Error of type "pair rate limit hit" | ||
end | ||
|
||
def block_user(uid) | ||
begin | ||
block = BlockedTiplineUser.new(uid: uid) | ||
block.skip_check_ability = true | ||
block.save! | ||
Rails.logger.info("[Smooch Bot] Blocked user #{uid}") | ||
Rails.cache.write("smooch:banned:#{uid}", Time.now.to_i) | ||
rescue ActiveRecord::RecordNotUnique | ||
# User already blocked | ||
Rails.logger.info("[Smooch Bot] User #{uid} already blocked") | ||
end | ||
end | ||
|
||
def unblock_user(uid) | ||
BlockedTiplineUser.where(uid: uid).last.destroy! | ||
Rails.logger.info("[Smooch Bot] Unblocked user #{uid}") | ||
Rails.cache.delete("smooch:banned:#{uid}") | ||
end | ||
|
||
def user_blocked?(uid) | ||
!uid.blank? && (!Rails.cache.read("smooch:banned:#{uid}").nil? || BlockedTiplineUser.where(uid: uid).exists?) | ||
end | ||
|
||
def user_banned?(payload) | ||
uid = payload.dig('appUser', '_id') | ||
self.user_blocked?(uid) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CreateBlockedTiplineUsers < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :blocked_tipline_users do |t| | ||
t.string :uid, null: false | ||
t.timestamps | ||
end | ||
add_index :blocked_tipline_users, :uid, unique: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require_relative '../test_helper' | ||
|
||
class BlockedTiplineUserTest < ActiveSupport::TestCase | ||
def setup | ||
end | ||
|
||
def teardown | ||
end | ||
|
||
test 'should create blocked tipline user' do | ||
assert_difference 'BlockedTiplineUser.count' do | ||
create_blocked_tipline_user | ||
end | ||
end | ||
|
||
test 'should not create blocked tipline user if UID is blank' do | ||
assert_no_difference 'BlockedTiplineUser.count' do | ||
assert_raises ActiveRecord::NotNullViolation do | ||
create_blocked_tipline_user uid: nil | ||
end | ||
end | ||
end | ||
|
||
test 'should not block the same user more than once' do | ||
uid = random_string | ||
create_blocked_tipline_user uid: uid | ||
assert_no_difference 'BlockedTiplineUser.count' do | ||
assert_raises ActiveRecord::RecordNotUnique do | ||
create_blocked_tipline_user uid: uid | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters