main.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from werkzeug.exceptions import NotFound
  4. from odoo import http
  5. from odoo.http import request
  6. class WebsiteJitsiController(http.Controller):
  7. @http.route(["/jitsi/update_status"], type="json", auth="public")
  8. def jitsi_update_status(self, room_name, participant_count, joined):
  9. """ Update room status: participant count, max reached
  10. Use the SQL keywords "FOR UPDATE SKIP LOCKED" in order to skip if the row
  11. is locked (instead of raising an exception, wait for a moment and retry).
  12. This endpoint may be called multiple times and we don't care having small
  13. errors in participant count compared to performance issues.
  14. :raise ValueError: wrong participant count
  15. :raise NotFound: wrong room name
  16. """
  17. if participant_count < 0:
  18. raise ValueError()
  19. chat_room = self._chat_room_exists(room_name)
  20. if not chat_room:
  21. raise NotFound()
  22. request.env.cr.execute(
  23. """
  24. WITH req AS (
  25. SELECT id
  26. FROM chat_room
  27. -- Can not update the chat room if we do not have its name
  28. WHERE name = %s
  29. FOR UPDATE SKIP LOCKED
  30. )
  31. UPDATE chat_room AS wcr
  32. SET participant_count = %s,
  33. last_activity = NOW(),
  34. max_participant_reached = GREATEST(max_participant_reached, %s)
  35. FROM req
  36. WHERE wcr.id = req.id;
  37. """,
  38. [room_name, participant_count, participant_count]
  39. )
  40. @http.route(["/jitsi/is_full"], type="json", auth="public")
  41. def jitsi_is_full(self, room_name):
  42. return self._chat_room_exists(room_name).is_full
  43. # ------------------------------------------------------------
  44. # TOOLS
  45. # ------------------------------------------------------------
  46. def _chat_room_exists(self, room_name):
  47. return request.env["chat.room"].sudo().search([("name", "=", room_name)], limit=1)