Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin' into weblate-translations
Browse files Browse the repository at this point in the history
  • Loading branch information
darccio committed Apr 21, 2023
2 parents 3a47024 + 6794a4b commit 78cd0e7
Show file tree
Hide file tree
Showing 40 changed files with 290 additions and 124 deletions.
4 changes: 2 additions & 2 deletions app/controllers/consultations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def create
result.tokens.each do |token|
next if token.blank?

message << I18n.t('consultations.consultation_token_created', token:, role: token.translated_role)
message << I18n.t('consultations.consultation_token_created', token:, role: token.translated_role.titleize)
end

success(message.join(' '))
Expand Down Expand Up @@ -73,7 +73,7 @@ def create_params
end

def update_params
params.require(:consultation).permit(:title, :description, :status, config: %i[mode ballot])
params.require(:consultation).permit(:title, :description, :status, config: %i[mode ballot distribution])
end

def event
Expand Down
17 changes: 17 additions & 0 deletions app/controllers/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ def self.included(clazz)
error(e.message)
redirect_back fallback_location: root_path
end

rescue_from InvalidEmailAddress do |e|
emails = e.emails.map { |email| email.inspect }

error("#{e.message} (#{emails.join(', ')})")
redirect_back fallback_location: root_path
end
end
end

Expand Down Expand Up @@ -119,4 +126,14 @@ def initialize(msg = I18n.t('errors.you_already_voted_for_this'), consultation:
@consultation = consultation
end
end

class InvalidEmailAddress < ActionController::BadRequest
attr_reader :emails

def initialize(msg = I18n.t('errors.invalid_email'), emails:)
super(msg)

@emails = emails
end
end
end
46 changes: 24 additions & 22 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,25 @@ def next_question
def create_tokens
authorize event

params = create_token_params
role = params.fetch(:role, :voter).to_sym
multiple = params.fetch(:multiple, '0').to_i.positive?

Token.transaction do
if multiple
lines = params[:value].open
CSV.new(lines).read.each do |line|
CreateToken.call(
identifier: line.first,
role:,
aliased: params.fetch(:aliased, '0').to_i == 1,
send_magic_link: params.fetch(:send, '0').to_i == 1,
event:
)
end
identifiers = CSV.new(lines).read.map(&:first)

result = BulkCreateTokens.result(identifiers:, role:, aliased:, send_magic_link:, event:)
raise result.error if result.failure?

success(I18n.t('events.tokens_created_or_enabled'))

result = RedirectBySession.call(Context.to_h)
redirect_to result.destination
else
identifier = params[:value].presence
send_magic_link = params.fetch(:send, '0').to_i == 1
send_magic_link ||= EmailValidator.valid?(identifier, mode: :strict)

result = CreateToken.result(
identifier:,
role:,
aliased: params.fetch(:aliased, '0').to_i == 1,
send_magic_link:,
event:
)
send_magic_link = false
send_magic_link = EmailValidator.valid?(identifier, mode: :strict) if consultation.config.distribution == 'email'

result = CreateToken.result(identifier:, role:, aliased:, send_magic_link:, event:)

if result.skip
success(I18n.t('events.token_already_issued'))
Expand Down Expand Up @@ -143,6 +129,22 @@ def create_token_params
params.permit(:value, :aliased, :multiple, :send, :role)
end

def role
create_token_params.fetch(:role, :voter).to_sym
end

def multiple
create_token_params.fetch(:multiple, 'false') == 'true'
end

def aliased
create_token_params.fetch(:aliased, '0').to_i == 1
end

def send_magic_link
create_token_params.fetch(:send, '0').to_i == 1
end

def update_token_params
params.require(:status)
end
Expand Down
30 changes: 30 additions & 0 deletions app/interactors/bulk_create_tokens.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

class BulkCreateTokens < Actor
input :identifiers, type: Array
input :role, type: Symbol, in: Token.roles.keys.map(&:to_sym)
input :event, type: Event, allow_nil: true, default: nil
input :aliased, type: [TrueClass, FalseClass], default: false
input :send_magic_link, type: [TrueClass, FalseClass], default: false

def call
validate
create_tokens
end

private

def validate
return if identifiers.blank?
return unless send_magic_link

invalid_identifiers = identifiers.reject { |identifier| EmailValidator.valid?(identifier, mode: :strict) }
fail!(error: Errors::InvalidEmailAddress.new(emails: invalid_identifiers)) if invalid_identifiers.present?
end

def create_tokens
identifiers.each do |identifier|
CreateToken.call(identifier:, role:, aliased:, send_magic_link:, event:)
end
end
end
6 changes: 1 addition & 5 deletions app/interactors/cast_votes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,9 @@ def cast_vote(vote_params)
question = vote_params[:question]

envelope = Envelope.new(question, current_user, **vote_params.to_h.symbolize_keys)
envelope.save(async: async?)
envelope.save_later

envelope.receipt.save!
self.receipts << envelope.receipt
end

def async?
Rails.configuration.x.asembleo.async_vote
end
end
2 changes: 1 addition & 1 deletion app/interactors/create_magic_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ def user
end

def deliver
SessionsMailer.magic_link_email(email, token).deliver_later
SessionsMailer.magic_link_email(email, token.class.name, token.id, token.to_hash, scope: :global).deliver_later
end
end
3 changes: 2 additions & 1 deletion app/interactors/create_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def issue_receipt
end

def deliver
SessionsMailer.magic_link_email(identifier, token.reload).deliver_later
token.reload
SessionsMailer.magic_link_email(identifier, token.class.name, token.id, token.to_hash, scope:).deliver_later
end
end
3 changes: 2 additions & 1 deletion app/interactors/redirect_by_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def active_question

join_condition = [
receipts[:question_id].eq(questions[:question_id]),
receipts[:token_id].eq(identity.id)
receipts[:voter_type].eq(identity.class.name),
receipts[:voter_id].eq(identity.id)
]
where = {
status: :opened,
Expand Down
8 changes: 5 additions & 3 deletions app/jobs/vote_cast_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ class VoteCastJob < ApplicationJob
queue_as :default

# TODO: Vote hash chaining
def perform(question_id, token_id, value, created_at)
def perform(question_id, token_class, token_id, value, created_at)
question = Question.find(question_id)
token = Token.find(token_id)

token_class = token_class.constantize
token = token_class.find(token_id)

envelope = Envelope.new(question, token, value:, created_at:)
envelope.save
envelope.save!
end
end
15 changes: 9 additions & 6 deletions app/mailers/sessions_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

class SessionsMailer < ApplicationMailer
def magic_link_email(to, token)
@token = token
def magic_link_email(to, voter_type, voter_id, voter_hash, scope: :consultation)
@scope = scope.to_sym
@voter_hash = voter_hash
raise Errors::InvalidEmail unless EmailValidator.valid?(to, mode: :strict)

tag = TokenTag.sent
Expand All @@ -14,20 +15,22 @@ def magic_link_email(to, token)

raise e
ensure
token.tags << tag
token.save!
return unless voter_type == 'Token'

tag.token_id = voter_id
tag.save!
end

private

def template_name
return 'user_magic_link_email' if @token.scope == 'global'
return 'user_magic_link_email' if @scope == :global

'magic_link_email'
end

def subject
return I18n.t('sessions_mailer.user_magic_link_email.subject') if @token.scope == 'global'
return I18n.t('sessions_mailer.user_magic_link_email.subject') if @scope == :global

I18n.t('sessions_mailer.magic_link_email.subject')
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/consultation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ class Config

enum :mode, %i[synchronous asynchronous], default: :synchronous
enum :ballot, %i[open secret], default: :secret
enum :distribution, %i[manual email], default: :manual

translate_enum :mode
translate_enum :ballot
translate_enum :distribution
end

has_many :events, dependent: :destroy
Expand Down
15 changes: 8 additions & 7 deletions app/models/envelope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,25 @@ def receipt
return @receipt if defined?(@receipt)

@receipt ||= Receipt.new.tap do |r|
r.token_id = token.id
r.voter = token
r.question = question
r.created_at = created_at
r.fingerprint = FingerprintService.generate(r, token.to_hash, values)
end
end

def save(async: false)
if async
VoteCastJob.perform_later(question.id, token.id, values, receipt.created_at)
else
votes.map(&:save!)
end
def save_later
VoteCastJob.perform_later(question.id, token.class.name, token.id, values, receipt.created_at)
end

def save!
votes.each(&:save!)
end

private

def vote_alias
return unless token.is_a?(Token)
return unless consultation.ballot == 'open'

token.on_behalf_of || token.alias || token.to_hash
Expand Down
4 changes: 2 additions & 2 deletions app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def valid_option?(value)
options.exists?(value:)
end

def voted?(token)
receipts.exists?(token:)
def voted?(voter)
receipts.exists?(voter:)
end

def short_description
Expand Down
2 changes: 1 addition & 1 deletion app/models/receipt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
class Receipt < ApplicationRecord
has_paper_trail

belongs_to :token
belongs_to :voter, polymorphic: true
belongs_to :question
end
2 changes: 1 addition & 1 deletion app/models/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Token < ApplicationRecord
belongs_to :consultation, optional: true
belongs_to :event, optional: true

has_many :receipts, dependent: :destroy
has_many :receipts, dependent: :destroy, as: :voter
has_many :tags, foreign_key: 'token_id', class_name: 'TokenTag', dependent: :destroy, inverse_of: :token

enum role: { voter: 0, manager: 1, admin: 2 }
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class User < ApplicationRecord
has_paper_trail

has_many :receipts, foreign_key: 'token_id', dependent: :destroy
has_many :receipts, dependent: :destroy, as: :voter

enum :role, { voter: 0, manager: 1, admin: 2 }, default: :voter
enum :status, { enabled: 1, disabled: 0 }, default: :disabled
Expand Down
7 changes: 5 additions & 2 deletions app/services/markdown_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ def self.render(text, plain_text: false)
ActionController::Base.helpers.sanitize(content)
end

def header(text, _header_level)
%(<strong>#{text}</strong>)
def header(text, header_level)
new_header_level = header_level + 2
new_header_level = 6 if new_header_level > 6

%(<h#{new_header_level}>#{text}</h#{new_header_level}>)
end
end
2 changes: 1 addition & 1 deletion app/views/application/_tokens.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="card mt-3">
<div class="card-header d-flex justify-content-between align-items-center">
<div class="col">
<strong><%= t('activerecord.models.token', count: 2) %></strong>
<strong><%= t('activerecord.models.token', count: 2) %> (<%= "#{@consultation.tokens.voter.count} #{t('attributes.role_list.voter', count: 2)}" %>)</strong>
</div>
<div class="col-auto">
<%= form_with url: { controller: :events, action: :create_tokens, id: @event.id }, class: 'row row-cols-auto g-1 align-items-center', local: true do |f| %>
Expand Down
Loading

0 comments on commit 78cd0e7

Please sign in to comment.