mirror of
https://github.com/mastodon/mastodon.git
synced 2024-08-20 21:08:15 -07:00
Compare commits
5 commits
14104ad987
...
ac30d45992
Author | SHA1 | Date | |
---|---|---|---|
|
ac30d45992 | ||
|
a4812f6ef5 | ||
|
7ab706061d | ||
|
bd4c5f792e | ||
|
c4f7b8b625 |
3 changed files with 55 additions and 19 deletions
|
@ -8,37 +8,39 @@ class Api::V1::MarkersController < Api::BaseController
|
|||
|
||||
def index
|
||||
with_read_replica do
|
||||
@markers = current_user.markers.where(timeline: Array(params[:timeline])).index_by(&:timeline)
|
||||
@markers = current_user_markers
|
||||
end
|
||||
|
||||
render json: serialize_map(@markers)
|
||||
render json: marker_timeline_presenter, serializer: REST::MarkerTimelineSerializer
|
||||
end
|
||||
|
||||
def create
|
||||
Marker.transaction do
|
||||
@markers = {}
|
||||
|
||||
resource_params.each_pair do |timeline, timeline_params|
|
||||
@markers[timeline] = current_user.markers.find_or_create_by(timeline: timeline)
|
||||
@markers[timeline].update!(timeline_params)
|
||||
end
|
||||
end
|
||||
|
||||
render json: serialize_map(@markers)
|
||||
@markers = create_markers_from_params
|
||||
render json: marker_timeline_presenter, serializer: REST::MarkerTimelineSerializer
|
||||
rescue ActiveRecord::StaleObjectError
|
||||
render json: { error: 'Conflict during update, please try again' }, status: 409
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def serialize_map(map)
|
||||
serialized = {}
|
||||
def marker_timeline_presenter
|
||||
MarkerTimelinePresenter.new(@markers)
|
||||
end
|
||||
|
||||
map.each_pair do |key, value|
|
||||
serialized[key] = ActiveModelSerializers::SerializableResource.new(value, serializer: REST::MarkerSerializer).as_json
|
||||
def current_user_markers
|
||||
current_user.markers.where(timeline: Array(params[:timeline]))
|
||||
end
|
||||
|
||||
def create_markers_from_params
|
||||
[].tap do |markers|
|
||||
Marker.transaction do
|
||||
resource_params.each_pair do |timeline, timeline_params|
|
||||
current_user.markers.find_or_create_by(timeline: timeline).tap do |marker|
|
||||
marker.update!(timeline_params)
|
||||
markers << marker
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Oj.dump(serialized)
|
||||
end
|
||||
|
||||
def resource_params
|
||||
|
|
23
app/presenters/marker_timeline_presenter.rb
Normal file
23
app/presenters/marker_timeline_presenter.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class MarkerTimelinePresenter
|
||||
include ActiveModel::Model
|
||||
|
||||
alias read_attribute_for_serialization send
|
||||
|
||||
attr_reader :markers
|
||||
|
||||
def initialize(markers)
|
||||
@markers = markers
|
||||
end
|
||||
|
||||
Marker::TIMELINES.each do |timeline|
|
||||
define_method timeline.to_sym do
|
||||
markers.find { |marker| marker.timeline == timeline }
|
||||
end
|
||||
end
|
||||
|
||||
def timeline_present?(value)
|
||||
markers.map(&:timeline).include?(value)
|
||||
end
|
||||
end
|
11
app/serializers/rest/marker_timeline_serializer.rb
Normal file
11
app/serializers/rest/marker_timeline_serializer.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class REST::MarkerTimelineSerializer < ActiveModel::Serializer
|
||||
Marker::TIMELINES.each do |timeline|
|
||||
has_one timeline.to_sym,
|
||||
if: -> { timeline_present?(timeline) },
|
||||
serializer: REST::MarkerSerializer
|
||||
end
|
||||
|
||||
delegate :timeline_present?, to: :object
|
||||
end
|
Loading…
Reference in a new issue