res_users_settings.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models
  4. class ResUsersSettings(models.Model):
  5. _name = 'res.users.settings'
  6. _description = 'User Settings'
  7. _rec_name = 'user_id'
  8. user_id = fields.Many2one('res.users', string="User", required=True, readonly=True, ondelete='cascade')
  9. is_discuss_sidebar_category_channel_open = fields.Boolean(string="Is discuss sidebar category channel open?", default=True)
  10. is_discuss_sidebar_category_chat_open = fields.Boolean(string="Is discuss sidebar category chat open?", default=True)
  11. # RTC
  12. push_to_talk_key = fields.Char(string="Push-To-Talk shortcut", help="String formatted to represent a key with modifiers following this pattern: shift.ctrl.alt.key, e.g: truthy.1.true.b")
  13. use_push_to_talk = fields.Boolean(string="Use the push to talk feature", default=False)
  14. voice_active_duration = fields.Integer(string="Duration of voice activity in ms", help="How long the audio broadcast will remain active after passing the volume threshold")
  15. volume_settings_ids = fields.One2many('res.users.settings.volumes', 'user_setting_id', string="Volumes of other partners")
  16. _sql_constraints = [
  17. ('unique_user_id', 'UNIQUE(user_id)', 'One user should only have one mail user settings.')
  18. ]
  19. @api.model
  20. def _find_or_create_for_user(self, user):
  21. settings = user.sudo().res_users_settings_ids
  22. if not settings:
  23. settings = self.sudo().create({'user_id': user.id})
  24. return settings
  25. def _res_users_settings_format(self, fields_to_format=None):
  26. self.ensure_one()
  27. if not fields_to_format:
  28. fields_to_format = [name for name, field in self._fields.items() if name == 'id' or not field.automatic]
  29. res = self._read_format(fnames=fields_to_format)[0]
  30. if 'user_id' in fields_to_format:
  31. res['user_id'] = {'id': self.user_id.id}
  32. if 'volume_settings_ids' in fields_to_format:
  33. volume_settings = self.volume_settings_ids._discuss_users_settings_volume_format()
  34. res['volume_settings_ids'] = [('insert', volume_settings)]
  35. return res
  36. def set_res_users_settings(self, new_settings):
  37. self.ensure_one()
  38. changed_settings = {}
  39. for setting in new_settings.keys():
  40. if setting in self._fields and new_settings[setting] != self[setting]:
  41. changed_settings[setting] = new_settings[setting]
  42. self.write(changed_settings)
  43. self.env['bus.bus']._sendone(self.user_id.partner_id, 'res.users.settings/insert', self._res_users_settings_format([*changed_settings.keys(), 'id']))
  44. def set_volume_setting(self, partner_id, volume, guest_id=None):
  45. """
  46. Saves the volume of a guest or a partner.
  47. Either partner_id or guest_id must be specified.
  48. :param float volume: the selected volume between 0 and 1
  49. :param int partner_id:
  50. :param int guest_id:
  51. """
  52. self.ensure_one()
  53. volume_setting = self.env['res.users.settings.volumes'].search([
  54. ('user_setting_id', '=', self.id), ('partner_id', '=', partner_id), ('guest_id', '=', guest_id)
  55. ])
  56. if volume_setting:
  57. volume_setting.volume = volume
  58. else:
  59. volume_setting = self.env['res.users.settings.volumes'].create({
  60. 'user_setting_id': self.id,
  61. 'volume': volume,
  62. 'partner_id': partner_id,
  63. 'guest_id': guest_id,
  64. })
  65. self.env['bus.bus']._sendone(self.user_id.partner_id, 'res.users.settings.volumes/insert', volume_setting._discuss_users_settings_volume_format())