47 lines
965 B
Python
47 lines
965 B
Python
#!/usr/bin/env python3
|
|
"""
|
|
Individual functions for handling WebSocket events. Gets called by the
|
|
QuestConsumer object in consumers.py.
|
|
"""
|
|
import time
|
|
import types
|
|
|
|
import bleach
|
|
|
|
from quest.models import Message, Quest
|
|
|
|
def message(socket, data):
|
|
"""
|
|
Gets called when the server receives a 'message' event.
|
|
"""
|
|
# TODO: validation
|
|
message = data.get('message')
|
|
quest_id = data.get('quest_id')
|
|
|
|
message = message.strip()
|
|
if not message:
|
|
return
|
|
tags = ["b", "code", "i", "s"]
|
|
message = bleach.clean(message, tags=tags)
|
|
user = socket.scope['user']
|
|
|
|
m = Message(
|
|
quest=Quest.objects.get(id=quest_id),
|
|
message=message)
|
|
if user.username:
|
|
m.user = user
|
|
m.save()
|
|
|
|
data = {}
|
|
data['message_id'] = m.id
|
|
data['message'] = message
|
|
data['date'] = int(time.time())
|
|
data['name'] = user.username
|
|
socket.send('message', data)
|
|
|
|
|
|
events = {}
|
|
for obj in dir():
|
|
if type(locals()[obj]) == types.FunctionType:
|
|
events[locals()[obj].__name__] = locals()[obj]
|