online_notifications_controller.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class OnlineNotificationsController < ApplicationController
  3. prepend_before_action :authentication_check
  4. =begin
  5. Format:
  6. JSON
  7. Example:
  8. {
  9. "id":1,
  10. "name":"some template",
  11. "user_id": null,
  12. "options":{"a":1,"b":2},
  13. "updated_at":"2012-09-14T17:51:53Z",
  14. "created_at":"2012-09-14T17:51:53Z",
  15. "updated_by_id":2.
  16. "created_by_id":2,
  17. }
  18. =end
  19. =begin
  20. Resource:
  21. GET /api/v1/templates.json
  22. Response:
  23. [
  24. {
  25. "id": 1,
  26. "name": "some_name1",
  27. ...
  28. },
  29. {
  30. "id": 2,
  31. "name": "some_name2",
  32. ...
  33. }
  34. ]
  35. Test:
  36. curl http://localhost/api/v1/online_notifications.json -v -u #{login}:#{password}
  37. =end
  38. def index
  39. if params[:full]
  40. render json: OnlineNotification.list_full(current_user, 200)
  41. return
  42. end
  43. notifications = OnlineNotification.list(current_user, 200)
  44. model_index_render_result(notifications)
  45. end
  46. =begin
  47. Resource:
  48. GET /api/v1/online_notifications/{id}
  49. Payload:
  50. {
  51. "id": "123",
  52. }
  53. Response:
  54. {
  55. "id": 1,
  56. "name": "some_name",
  57. ...
  58. }
  59. Test:
  60. curl http://localhost/api/v1/online_notifications/#{id} -v -u #{login}:#{password}
  61. =end
  62. def show
  63. return if !access?
  64. model_show_render(OnlineNotification, params)
  65. end
  66. =begin
  67. Resource:
  68. PUT /api/v1/online_notifications/{id}
  69. Payload:
  70. {
  71. "name": "some name",
  72. "options":{"a":1,"b":2},
  73. }
  74. Response:
  75. {
  76. "id": 1,
  77. "name": "some_name",
  78. ...
  79. }
  80. Test:
  81. curl http://localhost/api/v1/online_notifications -v -u #{login}:#{password} -H "Content-Type: application/json" -X PUT -d '{"name": "some_name","active": true, "note": "some note"}'
  82. =end
  83. def update
  84. return if !access?
  85. model_update_render(OnlineNotification, params)
  86. end
  87. =begin
  88. Resource:
  89. DELETE /api/v1/online_notifications/{id}.json
  90. Response:
  91. {}
  92. Test:
  93. curl http://localhost/api/v1/online_notifications/{id}.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X DELETE
  94. =end
  95. def destroy
  96. return if !access?
  97. model_destroy_render(OnlineNotification, params)
  98. end
  99. =begin
  100. Resource:
  101. PUT /api/v1/online_notifications/mark_all_as_read
  102. Payload:
  103. {}
  104. Response:
  105. {}
  106. Test:
  107. curl http://localhost/api/v1/online_notifications/mark_all_as_read -v -u #{login}:#{password} -X POST -d '{}'
  108. =end
  109. def mark_all_as_read
  110. notifications = OnlineNotification.list(current_user, 200)
  111. notifications.each do |notification|
  112. if !notification['seen']
  113. OnlineNotification.seen( id: notification['id'] )
  114. end
  115. end
  116. render json: {}, status: :ok
  117. end
  118. private
  119. def access?
  120. notification = OnlineNotification.find(params[:id])
  121. if notification.user_id != current_user.id
  122. response_access_deny
  123. return false
  124. end
  125. true
  126. end
  127. end