online_notifications_controller.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. online_notifications = OnlineNotification.list(current_user, 200)
  40. if response_expand?
  41. list = []
  42. online_notifications.each do |item|
  43. list.push item.attributes_with_association_names
  44. end
  45. render json: list, status: :ok
  46. return
  47. end
  48. if response_full?
  49. assets = {}
  50. item_ids = []
  51. online_notifications.each do |item|
  52. item_ids.push item.id
  53. assets = item.assets(assets)
  54. end
  55. render json: {
  56. record_ids: item_ids,
  57. assets: assets,
  58. }, status: :ok
  59. return
  60. end
  61. all = []
  62. online_notifications.each do |item|
  63. all.push item.attributes_with_association_ids
  64. end
  65. render json: all, status: :ok
  66. end
  67. =begin
  68. Resource:
  69. GET /api/v1/online_notifications/{id}
  70. Payload:
  71. {
  72. "id": "123",
  73. }
  74. Response:
  75. {
  76. "id": 1,
  77. "name": "some_name",
  78. ...
  79. }
  80. Test:
  81. curl http://localhost/api/v1/online_notifications/#{id} -v -u #{login}:#{password}
  82. =end
  83. def show
  84. return if !access?
  85. model_show_render(OnlineNotification, params)
  86. end
  87. =begin
  88. Resource:
  89. PUT /api/v1/online_notifications/{id}
  90. Payload:
  91. {
  92. "name": "some name",
  93. "options":{"a":1,"b":2},
  94. }
  95. Response:
  96. {
  97. "id": 1,
  98. "name": "some_name",
  99. ...
  100. }
  101. Test:
  102. 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"}'
  103. =end
  104. def update
  105. return if !access?
  106. model_update_render(OnlineNotification, params)
  107. end
  108. =begin
  109. Resource:
  110. DELETE /api/v1/online_notifications/{id}.json
  111. Response:
  112. {}
  113. Test:
  114. curl http://localhost/api/v1/online_notifications/{id}.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X DELETE
  115. =end
  116. def destroy
  117. return if !access?
  118. model_destroy_render(OnlineNotification, params)
  119. end
  120. =begin
  121. Resource:
  122. PUT /api/v1/online_notifications/mark_all_as_read
  123. Payload:
  124. {}
  125. Response:
  126. {}
  127. Test:
  128. curl http://localhost/api/v1/online_notifications/mark_all_as_read -v -u #{login}:#{password} -X POST -d '{}'
  129. =end
  130. def mark_all_as_read
  131. notifications = OnlineNotification.list(current_user, 200)
  132. notifications.each do |notification|
  133. if !notification['seen']
  134. OnlineNotification.seen(id: notification['id'])
  135. end
  136. end
  137. render json: {}, status: :ok
  138. end
  139. private
  140. def access?
  141. notification = OnlineNotification.find(params[:id])
  142. if notification.user_id != current_user.id
  143. response_access_deny
  144. return false
  145. end
  146. true
  147. end
  148. end