Files
grecia/app/controllers/legislation/annotations_controller.rb
Javi Martín f7e2d724dd Replace ahoy events with real data
We were tracking some events with Ahoy, but in an inconsistent way. For
example, we were tracking when a debate was created, but (probably
accidentally) we were only tracking proposals when they were created
from the management section. For budget investments and their supports,
we weren't using Ahoy events but checking their database tables instead.
And we were only using ahoy events for the charts; for the other stats,
we were using the real data.

While we could actually fix these issues and start tracking events
correctly, existing production data would remain broken because we
didn't track a certain event when it happened. And, besides, why should
we bother, for instance, to track when a debate is created, when we can
instead access that information in the debates table?

There are probably some features related to tracking an event and their
visits, but we weren't using them, and we were storing more user data
than we needed to.

So we're removing the track events, allowing us to simplify the code and
make it more consistent. We aren't removing the `ahoy_events` table in
case existing Consul Democracy installations use it, but we'll remove it
after releasing version 2.2.0 and adding a warning in the release notes.

This change fixes the proposal created chart, since we were only
tracking proposals created in the management section, and opens the
possibility to add more charts in the future using data we didn't track
with Ahoy.

Also note the "Level 2 user Graph" test wasn't testing the graph, so
we're changing it in order to test it. We're also moving it next to the
other graphs test and, since we were tracking the event when we were
confirming the phone, we're renaming to "Level 3 users".

Finally, note that, since we were tracking events when something was
created, we're including the `with_hidden` scope. This is also
consistent with the other stats shown in the admin section as well as
the public stats.
2024-05-09 14:28:32 +02:00

110 lines
3.2 KiB
Ruby

class Legislation::AnnotationsController < Legislation::BaseController
skip_before_action :verify_authenticity_token
before_action :authenticate_user!, only: [:create, :new_comment]
before_action :convert_ranges_parameters, only: [:create]
load_and_authorize_resource :process
load_and_authorize_resource :draft_version, through: :process
load_and_authorize_resource through: :draft_version
has_orders %w[most_voted newest oldest], only: :show
def index
end
def show
@commentable = @annotation
if params[:sub_annotation_ids].present?
@sub_annotations = Legislation::Annotation.where(id: params[:sub_annotation_ids].split(","))
annotations = [@commentable, @sub_annotations]
else
annotations = [@commentable]
end
@comment_tree = MergedCommentTree.new(annotations, params[:page], @current_order)
set_comment_flags(@comment_tree.comments)
end
def create
if !@process.allegations_phase.open? || @draft_version.final_version?
render(json: {}, status: :not_found) && return
end
existing_annotation = @draft_version.annotations.find_by(
range_start: annotation_params[:ranges].first[:start],
range_start_offset: annotation_params[:ranges].first[:startOffset].to_i,
range_end: annotation_params[:ranges].first[:end],
range_end_offset: annotation_params[:ranges].first[:endOffset].to_i
)
@annotation = existing_annotation
if @annotation.present?
comment = @annotation.comments.build(body: annotation_params[:text], user: current_user)
if comment.save
render json: @annotation.to_json
else
render json: comment.errors.full_messages, status: :unprocessable_entity
end
else
@annotation = @draft_version.annotations.new(annotation_params)
@annotation.author = current_user
if @annotation.save
render json: @annotation.to_json
else
render json: @annotation.errors.full_messages, status: :unprocessable_entity
end
end
end
def search
@annotations = @annotations.order(Arel.sql("LENGTH(quote) DESC"))
annotations_hash = { total: @annotations.size, rows: @annotations }
render json: annotations_hash.to_json(methods: :weight)
end
def comments
@annotation = @annotations.find(params[:annotation_id])
@comment = @annotation.comments.new
end
def new
respond_to do |format|
format.js
end
end
def new_comment
@annotation = @annotations.find(params[:annotation_id])
@comment = @annotation.comments.new(body: params[:comment][:body], user: current_user)
if @comment.save
@comment = @annotation.comments.new
end
respond_to do |format|
format.js { render :new_comment }
end
end
private
def annotation_params
params
.require(:legislation_annotation)
.permit(allowed_params)
end
def allowed_params
[:quote, :text, ranges: [:start, :startOffset, :end, :endOffset]]
end
def convert_ranges_parameters
annotation = params[:legislation_annotation]
if annotation && annotation[:ranges] && annotation[:ranges].is_a?(String)
params[:legislation_annotation][:ranges] = JSON.parse(annotation[:ranges])
end
rescue JSON::ParserError
end
end