Skip to content

Commit

Permalink
Merge pull request #2242 from brave-intl/staging
Browse files Browse the repository at this point in the history
Release 2019-09-19
  • Loading branch information
yachtcaptain23 authored Sep 19, 2019
2 parents f2fcfa1 + f3627df commit 3917b81
Show file tree
Hide file tree
Showing 38 changed files with 318 additions and 194 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ AllCops:
- app/controllers/admin/publishers_controller.rb
- app/controllers/admin/publishers/publisher_status_updates_controller.rb
- app/controllers/publishers/cases_controller.rb
- app/controllers/publishers/uphold_controller.rb
- app/controllers/publishers/**
- app/services/uphold/**/*
- app/jobs/create_uphold_cards_job.rb
- app/jobs/create_uphold_channel_card_job.rb
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -608,4 +608,4 @@ RUBY VERSION
ruby 2.5.5p157

BUNDLED WITH
2.0.1
2.0.2
3 changes: 1 addition & 2 deletions app/assets/stylesheets/admin/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
grid-gap: 32px;
align-items: center;
}

// Bootstrap overrides. Could be refined
.btn-default {
color: $braveGray-2 !important;
Expand Down Expand Up @@ -58,7 +58,6 @@
bottom: 0;
background: $braveGray-2;
margin: 60px 0 0 0;
padding: 24px 0;
overflow: auto;

.sidebar-menu {
Expand Down
30 changes: 30 additions & 0 deletions app/controllers/admin/uphold_status_reports_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'csv'

module Admin
class UpholdStatusReportsController < AdminController
def index
@uphold_status_reports = UpholdStatusReport.
group('(EXTRACT(YEAR FROM created_at))::integer').
group('(EXTRACT(MONTH FROM created_at))::integer').
order('2 DESC, 3 DESC').count
end

def show
date = DateTime.strptime(params[:id], "%Y-%m")
start_date = date.at_beginning_of_month
end_date = date.at_end_of_month

uphold_status_reports = UpholdStatusReport.where("created_at >= :start AND created_at <= :finish", start: start_date, finish: end_date)

generated = []
generated << ["publisher id", "publisher created at", "uphold id", "uphold connected time"].to_csv

uphold_status_reports.each do |report|
generated << [report.publisher_id, report.publisher.created_at, report.uphold_id, report.created_at].to_csv
end

send_data generated.join(''), filename: "uphold-#{params[:id]}.csv"
end
end
end

17 changes: 9 additions & 8 deletions app/controllers/api/v1/public/channels_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
class Api::V1::Public::ChannelsController < Api::V1::Public::BaseController
def channels
channels_json = Rails.cache.fetch('browser_channels_json', race_condition_ttl: 30) do
require 'sentry-raven'
Raven.capture_message("Failed to use redis cache for /api/public/channels, using DB instead.")
channels_json = JsonBuilders::ChannelsJsonBuilder.new.build
end
render(json: channels_json, status: 200)
end
include BrowserChannelsDynoCaching
@@cached_payload ||= nil
REDIS_KEY = 'browser_channels_json'.freeze

def totals
render(json: Channel.statistical_totals, status: 200)
end

private

def dyno_expiration_key
"browser_v1_channels_expiration:#{ENV['DYNO']}"
end
end
17 changes: 9 additions & 8 deletions app/controllers/api/v2/public/channels_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
class Api::V2::Public::ChannelsController < Api::V2::Public::BaseController
def channels
channels_json = Rails.cache.fetch('browser_channels_json_v2', race_condition_ttl: 30) do
require 'sentry-raven'
Raven.capture_message("Failed to use redis cache for /api/public/channels V2, using DB instead.")
channels_json = JsonBuilders::ChannelsJsonBuilderV2.new.build
end
render(json: channels_json, status: 200)
end
include BrowserChannelsDynoCaching
@@cached_payload = nil
REDIS_KEY = 'browser_channels_json_v2'.freeze

def totals
render(json: Channel.statistical_totals, status: 200)
end

private

def dyno_expiration_key
"browser_v3_channels_expiration:#{ENV['DYNO']}"
end
end
16 changes: 9 additions & 7 deletions app/controllers/api/v3/public/channels_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
require 'sentry-raven'

class Api::V3::Public::ChannelsController < Api::V3::Public::BaseController
def channels
channels_json = Rails.cache.fetch('browser_channels_json_v3', race_condition_ttl: 30) do
Raven.capture_message("Failed to use redis cache for /api/public/channels V3, using DB instead.")
channels_json = JsonBuilders::ChannelsJsonBuilderV3.new.build
end
render(json: channels_json, status: 200)
end
include BrowserChannelsDynoCaching
@@cached_payload = nil
REDIS_KEY = 'browser_channels_json_v3'.freeze

def totals
statistical_totals_json = Rails.cache.fetch(CacheBrowserChannelsJsonJobV3::TOTALS_CACHE_KEY, race_condition_ttl: 30)
render(json: statistical_totals_json, status: 200)
end

private

def dyno_expiration_key
"browser_v3_channels_expiration:#{ENV['DYNO']}"
end
end
41 changes: 41 additions & 0 deletions app/controllers/concerns/browser_channels_dyno_caching.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module BrowserChannelsDynoCaching
extend ActiveSupport::Concern
require 'sentry-raven'

def channels
if dyno_cache_expired? || invalid_dyno_cache?
update_dyno_cache
end
render(json: self.class.class_variable_get(klass_dyno_cache), status: 200)
end

private

def dyno_cache_expired?
expiration_time = Rails.cache.fetch(dyno_expiration_key)
return expiration_time.nil? || Time.parse(expiration_time) <= Time.now
end

def invalid_dyno_cache?
cached_dyno_value = self.class.class_variable_get(klass_dyno_cache)
cached_dyno_value.nil? || !cached_dyno_value.is_a?(String)
end

def update_dyno_cache
redis_value = Rails.cache.fetch(self.class::REDIS_KEY, race_condition_ttl: 30) do
Raven.capture_message("Failed to use redis cache for Dyno cache: #{klass_dyno_cache}, continuing to read from cache instead")
end
if redis_value.present?
self.class.class_variable_set(klass_dyno_cache, redis_value)
Rails.cache.write(dyno_expiration_key, 1.hour.from_now.to_s, expires_in: 1.hour.from_now )
end
end

def dyno_expiration_key
raise "Define me for dyno_expiration_name!"
end

def klass_dyno_cache
:@@cached_payload
end
end
9 changes: 6 additions & 3 deletions app/controllers/publishers/promo_registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
class Publishers::PromoRegistrationsController < PublishersController
def for_referral_code
promo_registration = current_publisher.admin? ?
PromoRegistration.find_by(referral_code: params[:referral_code]) :
current_publisher.promo_registrations.find_by(referral_code: params[:referral_code])
promo_registration =
if current_publisher.admin?
PromoRegistration.find_by(referral_code: params[:referral_code])
else
current_publisher.promo_registrations.find_by(referral_code: params[:referral_code])
end
render :unauthorized and return if promo_registration.nil?
render json: promo_registration.stats_by_date.to_json
end
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/publishers/site_banners_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Publishers::SiteBannersController < ApplicationController

def show
if site_banner
render json:site_banner.read_only_react_property
render json: site_banner.read_only_react_property
else
render(json: nil.to_json)
end
Expand Down Expand Up @@ -86,16 +86,16 @@ def image_properties(attachment_type:)
)
rescue OutsidePaddingRangeError => e
logger.error "Outside padding range #{e.message}"
LogException.perform(StandardError.new("File size too big for #{attachment_type}"), params: {publisher_id: current_publisher.id})
raise StandardError.new("File size too big for #{attachment_type}")
LogException.perform(StandardError.new("File size too big for #{attachment_type}"), params: { publisher_id: current_publisher.id })
raise StandardError.new("File size too big for #{attachment_type}") # rubocop:disable Style/RaiseArgs
end

new_filename = generate_filename(source_image_path: padded_resized_jpg_path)

{
io: open(padded_resized_jpg_path),
filename: new_filename + ".jpg",
content_type: "image/jpg"
content_type: "image/jpg",
}
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Publishers
class TwoFactorAuthenticationsRemovalsController < ApplicationController
include TwoFactorAuth

def new
end

def create
publisher = pending_2fa_current_publisher

publisher.register_for_2fa_removal if publisher.two_factor_authentication_removal.blank?
publisher.reload

MailerServices::TwoFactorAuthenticationRemovalReminderEmailer.new(publisher: publisher).perform
redirect_to two_factor_authentications_path, flash: { notice: t("publishers.two_factor_authentication_removal.request_success") }
end

def destroy
publisher = pending_2fa_current_publisher

publisher.two_factor_authentication_removal.destroy if publisher.two_factor_authentication_removal.present?

redirect_to two_factor_authentications_path, flash: { notice: t("publishers.two_factor_authentication_removal.confirm_cancel_flash") }
end
end
end
15 changes: 15 additions & 0 deletions app/controllers/publishers/uphold_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def create

ExchangeUpholdCodeForAccessTokenJob.perform_now(publisher_id: current_publisher.id)

connection.reload
create_uphold_report!(connection)

redirect_to(home_publishers_path)
rescue UpholdError, Faraday::Error => e
Rails.logger.info("Uphold Error: #{e.message}")
Expand All @@ -95,6 +98,18 @@ def destroy

class UpholdError < StandardError; end

def create_uphold_report!(connection)
uphold_id = connection.uphold_details&.id
return if uphold_id.blank?
# Return if we've already created a report for this id
return if UpholdStatusReport.find_by(uphold_id: uphold_id).present?

UpholdStatusReport.create(
publisher: current_publisher,
uphold_id: uphold_id
)
end

def validate_uphold!(connection)
# Ensure the uphold_state_token has been set. If not send back to try again
raise UpholdError.new, t('.missing_state') if connection&.uphold_state_token.blank? && !connection.uphold_verified?
Expand Down
59 changes: 2 additions & 57 deletions app/controllers/publishers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
class PublishersController < ApplicationController
# Number of requests to #create before we present a captcha.

include PublishersHelper
include PromosHelper

Expand All @@ -13,17 +11,11 @@ class PublishersController < ApplicationController
:statement,
:statements,
:update,
:uphold_status,
:uphold_verified,
].freeze

before_action :authenticate_via_token, only: %i(show)
before_action :authenticate_publisher!, except: %i(
two_factor_authentication_removal
request_two_factor_authentication_removal
confirm_two_factor_authentication_removal
cancel_two_factor_authentication_removal
)
before_action :authenticate_publisher!

before_action :require_publisher_email_not_verified_through_youtube_auth,
except: %i(update_email change_email)

Expand Down Expand Up @@ -104,53 +96,6 @@ def update
end
end

def request_two_factor_authentication_removal
publisher = Publisher.by_email_case_insensitive(params[:email]).first
flash[:notice] = t("publishers.two_factor_authentication_removal.request_success")
if publisher
if publisher.two_factor_authentication_removal.blank?
MailerServices::TwoFactorAuthenticationRemovalRequestEmailer.new(publisher: publisher).perform
elsif !publisher.two_factor_authentication_removal.removal_completed
MailerServices::TwoFactorAuthenticationRemovalCancellationEmailer.new(publisher: publisher).perform
end
end
redirect_to two_factor_authentication_removal_publishers_path
end

def cancel_two_factor_authentication_removal
sign_out(current_publisher) if current_publisher

publisher = Publisher.find(params[:id])
token = params[:token]

if PublisherTokenAuthenticator.new(publisher: publisher, token: token, confirm_email: publisher.email).perform
publisher.two_factor_authentication_removal.destroy if publisher.two_factor_authentication_removal.present?
flash[:notice] = t("publishers.two_factor_authentication_removal.confirm_cancel_flash")
redirect_to(root_path)
else
flash[:notice] = t("publishers.shared.error")
redirect_to(root_path)
end
end

def confirm_two_factor_authentication_removal
sign_out(current_publisher) if current_publisher

publisher = Publisher.find(params[:id])
token = params[:token]

if PublisherTokenAuthenticator.new(publisher: publisher, token: token, confirm_email: publisher.email).perform
publisher.register_for_2fa_removal if publisher.two_factor_authentication_removal.blank?
publisher.reload
MailerServices::TwoFactorAuthenticationRemovalReminderEmailer.new(publisher: publisher).perform
flash[:notice] = t("publishers.two_factor_authentication_removal.confirm_login_flash")
redirect_to(root_path)
else
flash[:notice] = t("publishers.shared.error")
redirect_to(root_path)
end
end

def protect
if current_publisher.nil?
redirect_to root_url and return
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/two_factor_authentications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class TwoFactorAuthenticationsController < ApplicationController

def index
@u2f_enabled = u2f_enabled?(pending_2fa_current_publisher)
@removal = pending_2fa_current_publisher.two_factor_authentication_removal

if !params[:request_totp] && @u2f_enabled
challenge = u2f.challenge
session[:challenge] = challenge
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/cache_browser_channels_json_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def perform
result = nil

loop do
result = Rails.cache.write('browser_channels_json', channels_json)
result = Rails.cache.write(Api::V1::Public::ChannelsController::REDIS_KEY, channels_json)
break if result || retry_count > MAX_RETRY

retry_count += 1
Expand Down
4 changes: 2 additions & 2 deletions app/jobs/cache_browser_channels_json_job_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def perform
result = nil

loop do
result = Rails.cache.write('browser_channels_json_v2', channels_json)
result = Rails.cache.write(Api::V2::Public::ChannelsController::REDIS_KEY, channels_json)
break if result || retry_count > MAX_RETRY

retry_count += 1
Expand All @@ -23,4 +23,4 @@ def perform
Rails.logger.info("CacheBrowserChannelsJsonJob V2 could not update the channels JSON.")
end
end
end
end
Loading

0 comments on commit 3917b81

Please sign in to comment.