2018-08-15 17:41:51 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
Individual functions for handling WebSocket events. Gets called by the
|
|
|
|
QuestConsumer object in consumers.py.
|
|
|
|
"""
|
2018-08-20 07:23:33 -04:00
|
|
|
import re
|
2018-08-16 15:43:43 -04:00
|
|
|
import time
|
2018-08-15 17:41:51 -04:00
|
|
|
import types
|
|
|
|
|
2018-08-16 15:43:43 -04:00
|
|
|
import bleach
|
|
|
|
|
2018-08-20 06:57:36 -04:00
|
|
|
from quest.models import Message, Quest
|
|
|
|
|
2018-08-15 17:41:51 -04:00
|
|
|
def message(socket, data):
|
|
|
|
"""
|
|
|
|
Gets called when the server receives a 'message' event.
|
|
|
|
"""
|
2018-08-20 06:57:36 -04:00
|
|
|
# TODO: validation
|
2018-08-15 17:41:51 -04:00
|
|
|
message = data.get('message')
|
2018-08-20 06:57:36 -04:00
|
|
|
quest_id = data.get('quest_id')
|
2018-08-16 15:43:43 -04:00
|
|
|
|
|
|
|
message = message.strip()
|
|
|
|
if not message:
|
|
|
|
return
|
|
|
|
tags = ["b", "code", "i", "s"]
|
|
|
|
message = bleach.clean(message, tags=tags)
|
2018-08-20 07:23:33 -04:00
|
|
|
|
|
|
|
lines = []
|
|
|
|
for line in message.splitlines():
|
|
|
|
if line.startswith(">") and not line.startswith(">>"):
|
|
|
|
line = '<span class="greenText">' + line + '</span>'
|
|
|
|
lines.append(line)
|
|
|
|
message = "<br>".join(lines)
|
|
|
|
|
|
|
|
quotes = re.findall(r">>\d+", message)
|
|
|
|
for quote in quotes:
|
|
|
|
msg_id = quote.replace(">>", "")
|
|
|
|
msg = '<a class="quotelink" '
|
|
|
|
msg += 'href="javascript:scrollToMsg(\'' + msg_id + '\')" '
|
|
|
|
msg += 'onmousemove="showPreview(event, \'' + msg_id + '\')" '
|
|
|
|
msg += 'onmouseout="clearPreview()">' + quote + '</a>'
|
|
|
|
message = message.replace(quote, msg)
|
|
|
|
|
2018-08-20 06:57:36 -04:00
|
|
|
user = socket.scope['user']
|
|
|
|
|
|
|
|
m = Message(
|
|
|
|
quest=Quest.objects.get(id=quest_id),
|
|
|
|
message=message)
|
|
|
|
if user.username:
|
|
|
|
m.user = user
|
|
|
|
m.save()
|
2018-08-15 17:41:51 -04:00
|
|
|
|
|
|
|
data = {}
|
2018-08-20 06:57:36 -04:00
|
|
|
data['message_id'] = m.id
|
2018-08-16 15:43:43 -04:00
|
|
|
data['message'] = message
|
2018-08-20 06:57:36 -04:00
|
|
|
data['date'] = int(time.time())
|
|
|
|
data['name'] = user.username
|
2018-08-15 17:41:51 -04:00
|
|
|
socket.send('message', data)
|
|
|
|
|
|
|
|
|
|
|
|
events = {}
|
|
|
|
for obj in dir():
|
|
|
|
if type(locals()[obj]) == types.FunctionType:
|
|
|
|
events[locals()[obj].__name__] = locals()[obj]
|