27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'app/controllers/collavre/user_creative_preferences_controller.rb', line 27
def update_last_topic
creative = Creative.find(params[:creative_id]).effective_origin
unless creative.has_permission?(Current.user, :read) || creative.user == Current.user
render json: { error: I18n.t("collavre.user_creative_preferences.no_permission") }, status: :forbidden and return
end
if params[:last_topic_id].present?
unless creative.topics.exists?(id: params[:last_topic_id])
render json: { error: I18n.t("collavre.user_creative_preferences.invalid_topic") }, status: :unprocessable_entity and return
end
end
record = UserCreativePreference.find_or_initialize_by(creative_id: creative.id, user_id: Current.user.id)
record.expanded_status ||= {}
record.last_topic_id = params[:last_topic_id].presence
if record.expanded_status.empty? && record.last_topic_id.nil?
record.destroy if record.persisted?
else
record.save!
end
TopicsChannel.broadcast_to(
"user_#{Current.user.id}_creative_#{creative.id}",
{ action: "last_topic_changed", last_topic_id: record.last_topic_id }
)
render json: { success: true }
end
|