File: main.py

package info (click to toggle)
odoo 18.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 878,716 kB
  • sloc: javascript: 927,937; python: 685,670; xml: 388,524; sh: 1,033; sql: 415; makefile: 26
file content (59 lines) | stat: -rw-r--r-- 2,142 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from werkzeug.exceptions import NotFound

from odoo import http
from odoo.http import request


class WebsiteJitsiController(http.Controller):

    @http.route(["/jitsi/update_status"], type="json", auth="public")
    def jitsi_update_status(self, room_name, participant_count, joined):
        """ Update room status: participant count, max reached

        Use the SQL keywords "FOR UPDATE SKIP LOCKED" in order to skip if the row
        is locked (instead of raising an exception, wait for a moment and retry).
        This endpoint may be called multiple times and we don't care having small
        errors in participant count compared to performance issues.

        :raise ValueError: wrong participant count
        :raise NotFound: wrong room name
        """
        if participant_count < 0:
            raise ValueError()

        chat_room = self._chat_room_exists(room_name)
        if not chat_room:
            raise NotFound()

        request.env.cr.execute(
            """
            WITH req AS (
                SELECT id
                  FROM chat_room
                  -- Can not update the chat room if we do not have its name
                 WHERE name = %s
                   FOR UPDATE SKIP LOCKED
            )
            UPDATE chat_room AS wcr
               SET participant_count = %s,
                   last_activity = NOW(),
                   max_participant_reached = GREATEST(max_participant_reached, %s)
              FROM req
             WHERE wcr.id = req.id;
            """,
            [room_name, participant_count, participant_count]
        )

    @http.route(["/jitsi/is_full"], type="json", auth="public")
    def jitsi_is_full(self, room_name):
        return self._chat_room_exists(room_name).is_full

    # ------------------------------------------------------------
    # TOOLS
    # ------------------------------------------------------------

    def _chat_room_exists(self, room_name):
        return request.env["chat.room"].sudo().search([("name", "=", room_name)], limit=1)