assets.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. module Chat::Session::Assets
  3. extend ActiveSupport::Concern
  4. =begin
  5. get all assets / related models for this chat
  6. chat = Chat::Session.find(123)
  7. result = Chat::Session.assets(assets_if_exists)
  8. returns
  9. result = {
  10. users: {
  11. 123: user_model_123,
  12. 1234: user_model_1234,
  13. },
  14. chat_sessions: [ chat_session_model1 ]
  15. }
  16. =end
  17. def assets(data)
  18. app_model_chat_session = Chat::Session.to_app_model
  19. app_model_chat = Chat.to_app_model
  20. app_model_user = User.to_app_model
  21. data[ app_model_chat_session ] ||= {}
  22. if !data[ app_model_chat_session ][ id ]
  23. data[ app_model_chat_session ][ id ] = attributes_with_association_ids
  24. data[ app_model_chat_session ][ id ]['messages'] = []
  25. messages.each do |message|
  26. data[ app_model_chat_session ][ id ]['messages'].push message.attributes
  27. end
  28. data[ app_model_chat_session ][ id ]['tags'] = tag_list
  29. end
  30. if !data[ app_model_chat ] || !data[ app_model_chat ][ chat_id ]
  31. chat = Chat.lookup(id: chat_id)
  32. if chat
  33. data = chat.assets(data)
  34. end
  35. end
  36. %w[created_by_id updated_by_id].each do |local_user_id|
  37. next if !self[ local_user_id ]
  38. next if data[ app_model_user ] && data[ app_model_user ][ self[ local_user_id ] ]
  39. user = User.lookup(id: self[ local_user_id ])
  40. next if !user
  41. data = user.assets(data)
  42. end
  43. data
  44. end
  45. end