From 377f20439eeab5b8a9eac7d79dee5a4ac1bffb0e Mon Sep 17 00:00:00 2001 From: iou1name Date: Mon, 8 Oct 2018 10:22:36 -0400 Subject: [PATCH] new post sends chat message --- README.md | 1 + quest/events.py | 62 ++++++++++++++++++++++++++++++++++++ quest/jinja2/quest/chat.html | 3 +- quest/static/quest.css | 11 ++++++- quest/static/quest.js | 2 +- quest/views.py | 2 ++ titivillus/settings.py | 3 ++ todo | 1 - 8 files changed, 81 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 73826a7..06d715a 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ titivillus=# \q 2. Walk the dinosaur 3. Set `STATIC_ROOT` under `settings.py` appropriately 4. Run `python manage.py collectstatic` +5. Create \[super\]user named `Server` and set id in settings.py ## Usage `gunicorn -b localhost:5100 -e SCRIPT_NAME=/titivillus titivillus.wsgi` diff --git a/quest/events.py b/quest/events.py index 2f1415a..c56db70 100644 --- a/quest/events.py +++ b/quest/events.py @@ -11,6 +11,7 @@ import types import random import bleach +from django.conf import settings from django.db import IntegrityError from django.utils.timezone import localtime from django.urls import reverse @@ -18,6 +19,7 @@ from django.urls import reverse from quest.models import * from quest.tools import handle_img from quest.forms import DiceCallForm, PollForm +from user.models import User def message(socket, data): """ @@ -172,6 +174,21 @@ def text_post(socket, data): data['post_id'] = p.id socket.send('new_post', data) + server = User.objects.get(id=settings.SERVER_USER_ID) + m = Message( + quest=quest, + user=server, + message=f"{quest.owner.username} made a new post!", + ) + m.save() + data = {} + data['message_id'] = m.id + data['message'] = m.message + data['date'] = int(time.time()) + data['name'] = server.username + data['anonymize'] = False + socket.send('message', data) + def dice_post(socket, data): """ @@ -226,6 +243,21 @@ def dice_post(socket, data): data['date'] = localtime(p.timestamp).strftime('%Y-%m-%d %H:%M') socket.send('new_post', data) + server = User.objects.get(id=settings.SERVER_USER_ID) + m = Message( + quest=quest, + user=server, + message=f"{quest.owner.username} made a new dice call!", + ) + m.save() + data = {} + data['message_id'] = m.id + data['message'] = m.message + data['date'] = int(time.time()) + data['name'] = server.username + data['anonymize'] = False + socket.send('message', data) + def poll_post(socket, data): """ @@ -269,6 +301,21 @@ def poll_post(socket, data): data['options'] = [(o.id, o.text) for o in options] socket.send('new_post', data) + server = User.objects.get(id=settings.SERVER_USER_ID) + m = Message( + quest=quest, + user=server, + message=f"{quest.owner.username} made a new poll!", + ) + m.save() + data = {} + data['message_id'] = m.id + data['message'] = m.message + data['date'] = int(time.time()) + data['name'] = server.username + data['anonymize'] = False + socket.send('message', data) + def edit_post(socket, data): """ @@ -454,6 +501,21 @@ def new_page(socket, data): data['url'] = reverse('quest:quest', args=[socket.quest_id, p.page_num]) socket.send('new_page', data) + server = User.objects.get(id=settings.SERVER_USER_ID) + m = Message( + quest=quest, + user=server, + message=f"{quest.owner.username} started a new page!", + ) + m.save() + data = {} + data['message_id'] = m.id + data['message'] = m.message + data['date'] = int(time.time()) + data['name'] = server.username + data['anonymize'] = False + socket.send('message', data) + events = {} for obj in dir(): diff --git a/quest/jinja2/quest/chat.html b/quest/jinja2/quest/chat.html index fe505c8..c879a36 100644 --- a/quest/jinja2/quest/chat.html +++ b/quest/jinja2/quest/chat.html @@ -5,10 +5,11 @@ {% for message in chat_messages %}
- {% if quest.owner.username == message.user.username or not quest.anonymize %}{{ message.user.username or quest.anon_name }}{% else %}{{ quest.anon_name }}{% endif %} + {% if quest.owner == message.user or message.user.id == server_user_id or not quest.anonymize %}{{ message.user.username or quest.anon_name }}{% else %}{{ quest.anon_name }}{% endif %} {{ localtime(message.timestamp).strftime('%Y-%m-%d %H:%M:%S') }} No.{{ message.id }}
+
{{ message.message }}

diff --git a/quest/static/quest.css b/quest/static/quest.css index 4757a8c..2236d73 100644 --- a/quest/static/quest.css +++ b/quest/static/quest.css @@ -142,8 +142,17 @@ h3 { width: 100%; } +.msgHeaderHr { + margin-top: 0.2em; + margin-bottom: 0.2em; + margin-left: auto; + margin-right: auto; + border: 0; + border-bottom: 1px solid #ccc; +} + .msgSrvHr { - width: 95%; + width: 50%; margin-top: 0.2em; margin-bottom: 0.2em; margin-left: auto; diff --git a/quest/static/quest.js b/quest/static/quest.js index 415f1fd..6de980f 100644 --- a/quest/static/quest.js +++ b/quest/static/quest.js @@ -52,7 +52,7 @@ socket.events['message'] = function(data) { let msg_str = '
'; msg_str = '
' + ((data.name && !data.anonymize) ? data.name : anon_name) + ' '; msg_str += '' + strftime(data.date) + ' '; - msg_str += 'No.' + data.message_id + '
'; + msg_str += 'No.' + data.message_id + '
'; msg_str += '
' + data.message + '

'; let mbox = document.getElementById('chatWindow'); diff --git a/quest/views.py b/quest/views.py index d571c66..a0d8bd0 100644 --- a/quest/views.py +++ b/quest/views.py @@ -9,6 +9,7 @@ from django.views.decorators.http import require_POST from django.contrib import messages from django.shortcuts import render, redirect from django.db.models import F +from django.conf import settings from .models import Quest, DiceRoll, PollOption, PollVote, Page, Post from .forms import EditQuestForm, QuestForm, PostForm @@ -70,6 +71,7 @@ def quest(request, quest_id, page_num='0'): poll_options = PollOption.objects.filter(poll__post__quest=quest) poll_votes = PollVote.objects.filter(option__poll__post__quest=quest) ip_address = request.META['REMOTE_ADDR'] + server_user_id = settings.SERVER_USER_ID context = locals() if page_num == '0': return render(request, 'quest/quest_homepage.html', context) diff --git a/titivillus/settings.py b/titivillus/settings.py index 58467f9..950493a 100644 --- a/titivillus/settings.py +++ b/titivillus/settings.py @@ -163,3 +163,6 @@ IMG_SVR_URL = "https://img.steelbea.me/" # Taggit TAGGIT_CASE_INSENSITIVE = True + +# Server user id +SERVER_USER_ID = 3 diff --git a/todo b/todo index 0be5d78..1274fdc 100644 --- a/todo +++ b/todo @@ -11,7 +11,6 @@ Quote backlinks Improvements: More options for text posts (lists and so on) More rigorous input checking in events.py -New post displays chat message Poll vote highlights entire option Total voters per poll Chat archives