2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-03-16 08:59:32 +00:00
|
|
|
|
|
|
|
class MentionsController < ApplicationController
|
|
|
|
prepend_before_action -> { authorize! }
|
|
|
|
prepend_before_action { authentication_check }
|
|
|
|
|
|
|
|
# GET /api/v1/mentions
|
|
|
|
def list
|
|
|
|
list = Mention.where(condition).order(created_at: :desc)
|
|
|
|
|
|
|
|
if response_full?
|
|
|
|
assets = {}
|
|
|
|
item_ids = []
|
|
|
|
list.each do |item|
|
|
|
|
item_ids.push item.id
|
|
|
|
assets = item.assets(assets)
|
|
|
|
end
|
|
|
|
render json: {
|
|
|
|
record_ids: item_ids,
|
|
|
|
assets: assets,
|
|
|
|
}, status: :ok
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# return result
|
|
|
|
render json: {
|
|
|
|
mentions: list,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# POST /api/v1/mentions
|
|
|
|
def create
|
|
|
|
success = Mention.create!(
|
|
|
|
mentionable: mentionable!,
|
|
|
|
user: current_user,
|
|
|
|
)
|
|
|
|
if success
|
|
|
|
render json: success, status: :created
|
|
|
|
else
|
|
|
|
render json: success.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# DELETE /api/v1/mentions
|
|
|
|
def destroy
|
|
|
|
success = Mention.find_by(user: current_user, id: params[:id]).destroy
|
|
|
|
if success
|
|
|
|
render json: success, status: :ok
|
|
|
|
else
|
|
|
|
render json: success.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-07-20 13:31:46 +00:00
|
|
|
def mentionable_type!
|
|
|
|
@mentionable_type ||= begin
|
2021-11-15 15:58:19 +00:00
|
|
|
raise __('Invalid mentionable_type!') if 'Ticket'.freeze != params[:mentionable_type]
|
2021-03-16 08:59:32 +00:00
|
|
|
|
2021-07-20 13:31:46 +00:00
|
|
|
params[:mentionable_type]
|
|
|
|
end
|
2021-03-16 08:59:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def mentionable!
|
2021-07-20 13:31:46 +00:00
|
|
|
object = mentionable_type!.constantize.find(params[:mentionable_id])
|
2021-06-17 07:47:53 +00:00
|
|
|
authorize!(object, :agent_read_access?)
|
2021-03-16 08:59:32 +00:00
|
|
|
object
|
|
|
|
end
|
|
|
|
|
|
|
|
def fill_condition_mentionable(condition)
|
2021-07-20 13:31:46 +00:00
|
|
|
condition[:mentionable_type] = mentionable_type!
|
2021-03-16 08:59:32 +00:00
|
|
|
return if params[:mentionable_id].blank?
|
|
|
|
|
|
|
|
condition[:mentionable_id] = params[:mentionable_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def fill_condition_id(condition)
|
|
|
|
return if params[:id].blank?
|
|
|
|
|
|
|
|
condition[:id] = params[:id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def fill_condition_user(condition)
|
|
|
|
return if params[:user_id].blank?
|
|
|
|
|
|
|
|
condition[:user] = User.find(params[:user_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def condition
|
|
|
|
condition = {}
|
|
|
|
fill_condition_id(condition)
|
|
|
|
fill_condition_user(condition)
|
|
|
|
|
|
|
|
return condition if params[:mentionable_type].blank?
|
|
|
|
|
|
|
|
mentionable!
|
|
|
|
fill_condition_mentionable(condition)
|
|
|
|
condition
|
|
|
|
end
|
|
|
|
end
|