<commit_before># -*- coding: utf-8 -*-

from datetime import datetime

from django.conf import settings
from django.utils.html import urlize
from django.utils.importlib import import_module

from sockjs.tornado import SockJSConnection


class ChatboxSocketHandler(SockJSConnection):
    """
    Websocket handler for the chatbox.
    """
    clients = set()

    def on_open(self, info):
        """
        Checks connecting user and adds his client to the clients list.
        """
        from openslides.participant.models import User

        # get the session (compatible with other auth-backends)
        engine = import_module(settings.SESSION_ENGINE)
        try:
            session_key = info.get_cookie(settings.SESSION_COOKIE_NAME).value
            session = engine.SessionStore(session_key)
            pk = session.get_decoded().get('_auth_user_id')
        except AttributeError:
            return False

        try:
            self.user = User.objects.get(pk)
        except User.DoesNotExist:
            return False

        if self.user.has_perm('core.can_use_chat'):
            self.clients.add(self)
            return True
        else:
            return False

    def on_message(self, message):
        """
        Sends the given message to all clients.

        Also appends the message to the cache and removes old messages if there
        are more than 100.
        """
        if self.user.has_perm('core.can_use_chat') and message:
            message_object = ChatMessage(person=self.user, message=message)
            chat_messages.append(message_object)
            if len(chat_messages) > 100:<commit_msg>Fixed bug in chatbox.<commit_after># -*- coding: utf-8 -*-

from datetime import datetime

from django.conf import settings
from django.utils.html import urlize
from django.utils.importlib import import_module

from sockjs.tornado import SockJSConnection


class ChatboxSocketHandler(SockJSConnection):
    """
    Websocket handler for the chatbox.
    """
    clients = set()

    def on_open(self, info):
        """
        Checks connecting user and adds his client to the clients list.
        """
        from openslides.participant.models import User

        # get the session (compatible with other auth-backends)
        engine = import_module(settings.SESSION_ENGINE)
        try:
            session_key = info.get_cookie(settings.SESSION_COOKIE_NAME).value
            session_store = engine.SessionStore(session_key)
            pk = session_store.get('_auth_user_id')
        except AttributeError:
            return False

        try:
            self.user = User.objects.get(pk=pk)
        except User.DoesNotExist:
            return False

        if self.user.has_perm('core.can_use_chat'):
            self.clients.add(self)
            return True
        else:
            return False

    def on_message(self, message):
        """
        Sends the given message to all clients.

        Also appends the message to the cache and removes old messages if there
        are more than 100.
        """
        if self.user.has_perm('core.can_use_chat') and message:
            message_object = ChatMessage(person=self.user, message=message)
            chat_messages.append(message_object)
            if len(chat_messages) > 100:
{'fraction_non_alphanumeric': 0.07776096822995461, 'fraction_numerical': 0.004236006051437216, 'mean_word_length': 2.286282306163022, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7527141', 'n_tokens_mistral': 914, 'n_tokens_neox': 879, 'n_words': 248}