mentions_controller.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class MentionsController < ApplicationController
  3. prepend_before_action -> { authorize! }
  4. prepend_before_action { authentication_check }
  5. # GET /api/v1/mentions
  6. def list
  7. list = Mention.where(condition).order(created_at: :desc)
  8. if response_full?
  9. assets = {}
  10. item_ids = []
  11. list.each do |item|
  12. item_ids.push item.id
  13. assets = item.assets(assets)
  14. end
  15. render json: {
  16. record_ids: item_ids,
  17. assets: assets,
  18. }, status: :ok
  19. return
  20. end
  21. # return result
  22. render json: {
  23. mentions: list,
  24. }
  25. end
  26. # POST /api/v1/mentions
  27. def create
  28. success = Mention.create!(
  29. mentionable: mentionable!,
  30. user: current_user,
  31. )
  32. if success
  33. render json: success, status: :created
  34. else
  35. render json: success.errors, status: :unprocessable_entity
  36. end
  37. end
  38. # DELETE /api/v1/mentions
  39. def destroy
  40. success = Mention.find_by(user: current_user, id: params[:id]).destroy
  41. if success
  42. render json: success, status: :ok
  43. else
  44. render json: success.errors, status: :unprocessable_entity
  45. end
  46. end
  47. private
  48. def mentionable_type!
  49. @mentionable_type ||= begin
  50. raise __("The parameter 'mentionable_type' is invalid.") if 'Ticket'.freeze != params[:mentionable_type]
  51. params[:mentionable_type]
  52. end
  53. end
  54. def mentionable!
  55. object = mentionable_type!.constantize.find(params[:mentionable_id])
  56. authorize!(object, :agent_read_access?)
  57. object
  58. end
  59. def fill_condition_mentionable(condition)
  60. condition[:mentionable_type] = mentionable_type!
  61. return if params[:mentionable_id].blank?
  62. condition[:mentionable_id] = params[:mentionable_id]
  63. end
  64. def fill_condition_id(condition)
  65. return if params[:id].blank?
  66. condition[:id] = params[:id]
  67. end
  68. def fill_condition_user(condition)
  69. return if params[:user_id].blank?
  70. condition[:user] = User.find(params[:user_id])
  71. end
  72. def condition
  73. condition = {}
  74. fill_condition_id(condition)
  75. fill_condition_user(condition)
  76. return condition if params[:mentionable_type].blank?
  77. mentionable!
  78. fill_condition_mentionable(condition)
  79. condition
  80. end
  81. end