Skip to content

Commit

Permalink
Merge pull request #743 from coopdevs/captcha
Browse files Browse the repository at this point in the history
Add Invisible Captcha
  • Loading branch information
markets authored May 2, 2024
2 parents de9d580 + 3613c61 commit 5a2b32c
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ gem 'pg', '~> 1.4'
gem 'json_translate', '~> 4.0.0'
gem 'devise', '~> 4.9.1'
gem 'devise-i18n', '~> 1.11.0'
gem 'invisible_captcha', '~> 2.3.0'
gem 'http_accept_language', '~> 2.1.1'
gem 'kaminari', '~> 1.2.1'
gem 'simple_form', '~> 5.0.2'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ GEM
has_scope (>= 0.6)
railties (>= 6.0)
responders (>= 2)
invisible_captcha (2.3.0)
rails (>= 5.2)
jmespath (1.6.2)
jquery-rails (4.4.0)
rails-dom-testing (>= 1, < 3)
Expand Down Expand Up @@ -450,6 +452,7 @@ DEPENDENCIES
has_scope (~> 0.7.2)
http_accept_language (~> 2.1.1)
image_processing (~> 1.12)
invisible_captcha (~> 2.3.0)
jquery-rails (~> 4.4.0)
json_translate (~> 4.0.0)
kaminari (~> 1.2.1)
Expand Down
3 changes: 3 additions & 0 deletions app/admin/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
ActiveAdmin.register User do
config.batch_actions = true

action_item :upload_csv, only: :index do
link_to I18n.t("active_admin.users.upload_from_csv"), action: "upload_csv"
end
Expand All @@ -15,6 +17,7 @@
end

index do
selectable_column
column do |user|
link_to image_tag(avatar_url(user, 24)), admin_user_path(user)
end
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class UsersController < ApplicationController
before_action :user_should_be_confirmed, except: %i[signup create please_confirm]
before_action :member_should_exist_and_be_active, except: %i[signup create edit show update please_confirm]

invisible_captcha if: -> { params[:from_signup].present? }

has_scope :tagged_with, as: :tag

def index
Expand Down
8 changes: 4 additions & 4 deletions app/jobs/organization_notifier_job.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# A weekly digest email.
#
# Strategy: go throught all organizations and take latest active posts from last week
# posted by active members. Send an email to all active and online members
# posted by active members. Send an email to all confirmed, active and online members
# with the email notifications enabled with those posts. Group emails by user's locale.

# Schedule defined in config/schedule.yml file.
Expand All @@ -24,11 +24,11 @@ def perform
private

def users_by_locale(organization)
with_notifications = organization.users.online_active.actives.notifications
org_locales = with_notifications.pluck(:locale).uniq
org_users = organization.users.online_active.actives.confirmed.notifications
org_locales = org_users.pluck(:locale).uniq

org_locales.each_with_object({}) do |locale, hash|
hash[locale] = with_notifications.where(locale: locale)
hash[locale] = org_users.where(locale: locale)
end
end
end
1 change: 0 additions & 1 deletion app/mailers/organization_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class OrganizationNotifier < ActionMailer::Base
default from: "\"TimeOverflow\" <[email protected]>"

def recent_posts(posts, locale, users)
# last 10 posts of offers and inquiries
@offers = posts.where(type: "Offer").take(10)
@inquiries = posts.where(type: "Inquiry").take(10)

Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class User < ApplicationRecord
scope :actives, -> { joins(:members).where(members: { active: true }) }
scope :online_active, -> { where("sign_in_count > 0") }
scope :notifications, -> { where(notifications: true) }
scope :confirmed, -> { where.not(confirmed_at: nil) }

validates :username, presence: true
validates :email, presence: true, uniqueness: true
Expand Down
1 change: 1 addition & 0 deletions app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<%= show_error_messages!(@user) %>
<%= simple_form_for @user do |f| %>
<%= f.hidden_field :locale, value: I18n.locale %>
<%= invisible_captcha %>

<div class="form-inputs">
<%= f.input :username %>
Expand Down
1 change: 0 additions & 1 deletion config/initializers/active_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
config.current_user_method = :current_user
config.logout_link_path = :destroy_user_session_path
config.logout_link_method = :delete
config.batch_actions = false
config.comments = false
config.namespace :admin do |admin|
admin.build_menu :utility_navigation do |menu|
Expand Down
4 changes: 4 additions & 0 deletions config/initializers/invisible_captcha.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
InvisibleCaptcha.setup do |config|
config.timestamp_enabled = !Rails.env.test?
config.spinner_enabled = !Rails.env.test?
end
4 changes: 2 additions & 2 deletions spec/mailers/organization_notifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
let(:test_organization) { Fabricate(:organization) }
let!(:offer) { Fabricate(:offer, organization: test_organization) }
let!(:inquiry) { Fabricate(:inquiry, organization: test_organization) }
let(:user) { Fabricate(:user, email: "user@example.com", locale: :en) }
let(:user) { Fabricate(:user, email: "user@timeoverflow.org", locale: :en) }
let(:member) { Fabricate(:member, organization: test_organization, user: user) }

describe "send an email" do
Expand All @@ -17,7 +17,7 @@
let(:mail) { OrganizationNotifier.recent_posts(test_organization.posts, :en, [user]) }

it "receive email only active and online users" do
expect(mail.bcc).to eql(["user@example.com"])
expect(mail.bcc).to eql(["user@timeoverflow.org"])
end
it "to should be null" do
expect(mail.to).to be_nil
Expand Down

0 comments on commit 5a2b32c

Please sign in to comment.