-
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.
- Loading branch information
Showing
40 changed files
with
907 additions
and
136 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
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
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,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
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,26 @@ | ||
class AlegreCallbackError < StandardError | ||
end | ||
|
||
module AlegreWebhooks | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
def valid_request?(request) | ||
token = request.params['token'] || request.query_parameters['token'] | ||
!token.blank? && token == CheckConfig.get('alegre_token') | ||
end | ||
|
||
def webhook(request) | ||
begin | ||
doc_id = request.params.dig('data', 'requested', 'body', 'id') | ||
raise 'Unexpected params format' if doc_id.blank? | ||
redis = Redis.new(REDIS_CONFIG) | ||
key = "alegre:webhook:#{doc_id}" | ||
redis.lpush(key, request.params.to_json) | ||
redis.expire(key, 1.day.to_i) | ||
rescue StandardError => e | ||
CheckSentry.notify(AlegreCallbackError.new(e.message), { alegre_response: request.params }) | ||
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
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
Oops, something went wrong.