channels_sms_controller.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. class ChannelsSmsController < ApplicationController
  2. prepend_before_action -> { authentication_check && authorize! }, except: [:webhook]
  3. skip_before_action :verify_csrf_token, only: [:webhook]
  4. def index
  5. assets = {}
  6. render json: {
  7. account_channel_ids: channels_data('Sms::Account', assets),
  8. notification_channel_ids: channels_data('Sms::Notification', assets),
  9. config: channels_config,
  10. assets: assets
  11. }
  12. end
  13. def show
  14. model_show_render(Channel, params)
  15. end
  16. def create
  17. model_create_render(Channel, channel_params)
  18. end
  19. def update
  20. model_update_render(Channel, channel_params)
  21. end
  22. def enable
  23. channel.update!(active: true)
  24. render json: channel
  25. end
  26. def disable
  27. channel.update!(active: false)
  28. render json: channel
  29. end
  30. def destroy
  31. channel.destroy!
  32. render json: {}
  33. end
  34. def test
  35. raise 'Missing parameter options.adapter' if params[:options][:adapter].blank?
  36. driver = Channel.driver_class(params[:options][:adapter])
  37. resp = driver.new.send(params[:options], test_options)
  38. render json: { success: resp }
  39. rescue => e
  40. render json: { error: e.inspect, error_human: e.message }
  41. end
  42. def webhook
  43. raise Exceptions::UnprocessableEntity, 'token param missing' if params['token'].blank?
  44. channel = nil
  45. Channel.where(active: true, area: 'Sms::Account').each do |local_channel|
  46. next if local_channel.options[:webhook_token] != params['token']
  47. channel = local_channel
  48. end
  49. if !channel
  50. render(
  51. json: { message: 'channel not found' },
  52. status: :not_found
  53. )
  54. return
  55. end
  56. conten_type, content = channel.process(params.permit!.to_h)
  57. send_data content, type: conten_type
  58. end
  59. private
  60. def channel
  61. @channel ||= Channel.lookup(id: params[:id])
  62. end
  63. def test_options
  64. params.permit(:recipient, :message)
  65. end
  66. def channel_params
  67. raise 'Missing area params' if params[:area].blank?
  68. if ['Sms::Notification', 'Sms::Account'].exclude?(params[:area])
  69. raise "Invalid area '#{params[:area]}'!"
  70. end
  71. raise 'Missing options params' if params[:options].blank?
  72. raise 'Missing options.adapter params' if params[:options][:adapter].blank?
  73. params
  74. end
  75. def channels_config
  76. list = []
  77. Dir.glob(Rails.root.join('app/models/channel/driver/sms/*.rb')).each do |path|
  78. filename = File.basename(path)
  79. require_dependency "channel/driver/sms/#{filename.sub('.rb', '')}"
  80. list.push Channel.driver_class("sms/#{filename}").definition
  81. end
  82. list
  83. end
  84. def channels_data(area, assets)
  85. channel_ids = []
  86. Channel.where(area: area).each do |channel|
  87. assets = channel.assets(assets)
  88. channel_ids.push(channel.id)
  89. end
  90. channel_ids
  91. end
  92. end