From c53111ea60b22503bff8ffa2033d622363721edb Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Wed, 20 Jul 2022 00:28:02 -0700 Subject: [PATCH] Add user tripcodes Users can separate their name and a password with ## to create the illusion of consistent posting. Signed-off-by: Alek Ratzloff --- board/forms.py | 11 ++++++++++- board/migrations/0002_post_tripcode.py | 18 ++++++++++++++++++ board/models.py | 2 ++ board/static/board/style.css | 8 ++++++++ board/templates/board/post_snippet.html | 5 ++--- board/utils.py | 17 ++++++++++++++--- 6 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 board/migrations/0002_post_tripcode.py diff --git a/board/forms.py b/board/forms.py index 07391b6..cbac793 100644 --- a/board/forms.py +++ b/board/forms.py @@ -8,9 +8,11 @@ from django.forms.models import fields_for_model from django.utils import timezone from django.utils.translation import gettext as _ from guardian.shortcuts import get_objects_for_user -from board.models import Ban, Post, Report, ReportReason, ReportRecord from hcaptcha.fields import hCaptchaField +from board.models import Ban, Post, Report, ReportReason, ReportRecord +from board.utils import generate_tripcode + class PostForm(ModelForm): """ @@ -42,6 +44,13 @@ class PostForm(ModelForm): if not self.user or not self.user.has_perm("board.use_capcode", capcode): raise ValidationError(_("Could not create post")) + def clean_name(self): + name = self.cleaned_data["name"] + if "##" in name: + name, password = [v.strip() for v in name.split("##", 1)] + self.instance.tripcode = generate_tripcode(password) + return name + class ReplyForm(PostForm): """ diff --git a/board/migrations/0002_post_tripcode.py b/board/migrations/0002_post_tripcode.py new file mode 100644 index 0000000..cfa7b4e --- /dev/null +++ b/board/migrations/0002_post_tripcode.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1b1 on 2022-07-20 05:50 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("board", "0001_initial"), + ] + + operations = [ + migrations.AddField( + model_name="post", + name="tripcode", + field=models.CharField(blank=True, max_length=10), + ), + ] diff --git a/board/models.py b/board/models.py index 5bf0015..1221109 100644 --- a/board/models.py +++ b/board/models.py @@ -90,6 +90,8 @@ class Post(models.Model): ) # User's supplied name for this post name = models.CharField(max_length=255, null=True, blank=True) + # User's tripcode, already calculated + tripcode = models.CharField(max_length=10, blank=True) # User's supplied subject for this post subject = models.CharField(max_length=255, null=True, blank=True) # Text of this post diff --git a/board/static/board/style.css b/board/static/board/style.css index 2a81c08..8293e8b 100644 --- a/board/static/board/style.css +++ b/board/static/board/style.css @@ -2,7 +2,9 @@ --body: #ededed; --highlight: #555; --border-color: #555; + --name: #060; --quote: #595; + --subject: #333; --activated: #888; --post-background: #d9d9d9; --reply-background: #eee; @@ -102,13 +104,19 @@ th { } .post_subject { + color: var(--subject); font-weight: bold; } .post_name { + color: var(--name); font-weight: bold; } +.post_tripcode { + color: var(--name); +} + .post_stats { float: right; } diff --git a/board/templates/board/post_snippet.html b/board/templates/board/post_snippet.html index 22d47f1..b4e11fe 100644 --- a/board/templates/board/post_snippet.html +++ b/board/templates/board/post_snippet.html @@ -24,10 +24,9 @@ {% if post.subject %} {{post.subject}} {% endif %} - {% blocktranslate with post_name=post.name|default:"Anonymous" %} by - {{post_name}} - {% endblocktranslate %} + {# No spaces between the name and the tripcode #} + {{post.name|default:"Anonymous"}}{% if post.tripcode %}!!{{post.tripcode}}{% endif %} {% if post.capcode %} {{post.capcode}} {% endif %} diff --git a/board/utils.py b/board/utils.py index 8bc7a63..8b27120 100644 --- a/board/utils.py +++ b/board/utils.py @@ -1,9 +1,12 @@ -from typing import Optional, TYPE_CHECKING -from django.utils import timezone -from django.conf import settings +import base64 +import hashlib import ipaddress import random import string +from typing import Optional, TYPE_CHECKING + +from django.utils import timezone +from django.conf import settings from board.models import Ban, RangeBan @@ -56,3 +59,11 @@ def generate_user_token() -> str: User tokens need not be secure so this is a simple implementation. """ return "".join(random.choices(string.ascii_letters, k=settings.USER_TOKEN_LENGTH)) + + +def generate_tripcode(value: str) -> str: + hasher = hashlib.sha256() + hasher.update(settings.TRIPCODE_SALT.encode()) + hasher.update(value.encode()) + trip = base64.b64encode(hasher.digest()).decode("ascii") + return trip[0:10]