Add user tripcodes

Users can separate their name and a password with ## to create the
illusion of consistent posting.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-07-20 00:28:02 -07:00
parent 8f4b6d7aea
commit c53111ea60
6 changed files with 54 additions and 7 deletions

View File

@@ -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):
"""

View File

@@ -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),
),
]

View File

@@ -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

View File

@@ -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;
}

View File

@@ -24,10 +24,9 @@
{% if post.subject %}
<span class="post_subject">{{post.subject}}</span>
{% endif %}
{% blocktranslate with post_name=post.name|default:"Anonymous" %}
by
<span class="post_name">{{post_name}}</span>
{% endblocktranslate %}
{# No spaces between the name and the tripcode #}
<span class="post_name">{{post.name|default:"Anonymous"}}</span>{% if post.tripcode %}<span class="post_tripcode">!!{{post.tripcode}}</span>{% endif %}
{% if post.capcode %}
<span class="post_capcode" style="color: {{post.capcode.color}};">{{post.capcode}}</span>
{% endif %}

View File

@@ -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]