OpenSlides/openslides/core/chatbox.py

111 lines
3.3 KiB
Python
Raw Normal View History

2013-12-09 18:03:47 +01:00
from datetime import datetime
from django.conf import settings
from django.utils.html import urlize
from django.utils.importlib import import_module
2013-12-09 18:03:47 +01:00
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.users.models import User
2013-12-09 18:03:47 +01:00
# 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
2015-01-21 17:22:06 +01:00
session_store = engine.SessionStore(session_key)
pk = session_store.get('_auth_user_id')
except AttributeError:
return False
2013-12-09 18:03:47 +01:00
try:
2015-01-21 17:22:06 +01:00
self.user = User.objects.get(pk=pk)
2013-12-09 18:03:47 +01:00
except User.DoesNotExist:
return False
if self.user.has_perm('core.can_use_chat'):
self.clients.add(self)
return True
2013-12-09 18:03:47 +01:00
else:
return False
2013-12-09 18:03:47 +01:00
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:
2013-12-09 18:03:47 +01:00
message_object = ChatMessage(person=self.user, message=message)
chat_messages.append(message_object)
if len(chat_messages) > 100:
chat_messages.pop(0)
self.broadcast(
self.clients,
'%s %s %s' % (message_object.html_time(),
message_object.html_person(),
urlize(message_object.message)))
2013-12-09 18:03:47 +01:00
def on_close(self):
"""
Removes client from the clients list.
"""
self.clients.remove(self)
class ChatMessage(object):
"""
Class for all chat messages. They are stored in the chat_messages object.
The argument person has to be a Person object, the argument message has to
be the message as string. The argument color can be a three-tuple of RGB
color values. Default is black (0, 0, 0).
"""
def __init__(self, person, message, color=None):
self.person = person
self.message = message
self.color = color or (0, 0, 0)
self.time = datetime.now()
def html_time(self):
2013-12-09 18:03:47 +01:00
"""
Returns the message time in html style.
2013-12-09 18:03:47 +01:00
"""
return '<small class="grey">%s</small>' % self.time.strftime('%H:%M')
def html_person(self):
"""
Returns the message sender name in html style.
"""
return "<span style='color:%(color)s;'>%(person)s:</span>" % {
2013-12-09 18:03:47 +01:00
'color': 'rgb(%d,%d,%d)' % self.color,
'person': self.person.clean_name}
2013-12-09 18:03:47 +01:00
chat_messages = []
"""
Cache with all messages during livetime of the server.
"""
def chat_messages_context_processor(request):
"""
Adds all chat messages to the request context as template context processor.
Returns None if the request user has no permission to use the chat.
2013-12-09 18:03:47 +01:00
"""
if request.user.has_perm('core.can_use_chat'):
2013-12-09 18:03:47 +01:00
value = chat_messages
else:
value = None
return {'chat_messages': value}