assets.rb 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://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. if !data[ app_model_chat_session ]
  20. data[ app_model_chat_session ] = {}
  21. end
  22. return data 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. app_model_chat = Chat.to_app_model
  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. app_model_user = User.to_app_model
  37. %w[created_by_id updated_by_id].each do |local_user_id|
  38. next if !self[ local_user_id ]
  39. next if data[ app_model_user ] && data[ app_model_user ][ self[ local_user_id ] ]
  40. user = User.lookup(id: self[ local_user_id ])
  41. next if !user
  42. data = user.assets(data)
  43. end
  44. data
  45. end
  46. end