From e0e8ccd6fd826620deafe695b41b6abd4d0038fb Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Sun, 3 Sep 2023 16:30:20 +1200 Subject: [PATCH] add local community --- app/cli.py | 7 ++++++- app/community/forms.py | 6 +++--- app/community/routes.py | 24 +++++++++++++++++++--- app/models.py | 2 +- app/static/js/scripts.js | 28 ++++++++++++++++++++++++++ app/templates/community/add_local.html | 15 ++++++++++++++ app/templates/community/community.html | 5 +++-- app/templates/list_communities.html | 4 ++-- app/utils.py | 26 +++++++++++++++++++++++- 9 files changed, 104 insertions(+), 13 deletions(-) create mode 100644 app/templates/community/add_local.html diff --git a/app/cli.py b/app/cli.py index a9ef11da..e650d9d8 100644 --- a/app/cli.py +++ b/app/cli.py @@ -1,11 +1,13 @@ # if commands in this file are not working (e.g. 'flask translate') make sure you set the FLASK_APP environment variable. # e.g. export FLASK_APP=pyfedi.py - +from flask import json from app import db import click import os +from app.models import Settings, BannedInstances + def register(app): @app.cli.group() @@ -45,5 +47,8 @@ def register(app): db.drop_all() db.configure_mappers() db.create_all() + db.session.append(Settings(name='allow_nsfw', value=json.dumps(False))) + db.session.append(BannedInstances(domain='lemmygrad.ml')) + db.session.append(BannedInstances(domain='gab.com')) db.session.commit() print("Done") diff --git a/app/community/forms.py b/app/community/forms.py index a6837317..434395c2 100644 --- a/app/community/forms.py +++ b/app/community/forms.py @@ -4,9 +4,9 @@ from wtforms.validators import ValidationError, DataRequired, Email, EqualTo, Le from flask_babel import _, lazy_gettext as _l -class AddLocalCommunity(): - name = StringField(_l('Name'), validators=[DataRequired()]) - url = StringField(_l('Url')) +class AddLocalCommunity(FlaskForm): + community_name = StringField(_l('Name'), validators=[DataRequired()]) + url = StringField(_l('Url'), render_kw={'placeholder': '/c/'}) description = TextAreaField(_l('Description')) rules = TextAreaField(_l('Rules')) nsfw = BooleanField('18+ NSFW') diff --git a/app/community/routes.py b/app/community/routes.py index 0c7dd108..9b70b250 100644 --- a/app/community/routes.py +++ b/app/community/routes.py @@ -3,18 +3,36 @@ from flask import render_template, redirect, url_for, flash, request, make_respo from flask_login import login_user, logout_user, current_user from flask_babel import _ from app import db -from app.community.forms import SearchRemoteCommunity +from app.activitypub.signature import RsaKeys +from app.community.forms import SearchRemoteCommunity, AddLocalCommunity from app.community.util import search_for_community from app.constants import SUBSCRIPTION_MEMBER -from app.models import User, Community +from app.models import User, Community, CommunityMember from app.community import bp +from app.utils import get_setting @bp.route('/add_local', methods=['GET', 'POST']) def add_local(): form = AddLocalCommunity() + if get_setting('allow_nsfw', False) is False: + form.nsfw.render_kw = {'disabled': True} + if form.validate_on_submit(): - ... + private_key, public_key = RsaKeys.generate_keypair() + community = Community(title=form.community_name.data, name=form.url.data, description=form.description.data, + rules=form.rules.data, nsfw=form.nsfw.data, private_key=private_key, public_key=public_key, + subscriptions_count=1) + db.session.add(community) + db.session.commit() + membership = CommunityMember(user_id=current_user.id, community_id=community.id, is_moderator=True, + is_owner=True) + db.session.add(membership) + db.session.commit() + flash(_('Your new community has been created.')) + return redirect('/c/' + community.name) + + return render_template('community/add_local.html', title=_('Create community'), form=form) @bp.route('/add_remote', methods=['GET', 'POST']) diff --git a/app/models.py b/app/models.py index 57280ce2..4528d43a 100644 --- a/app/models.py +++ b/app/models.py @@ -159,7 +159,7 @@ class User(UserMixin, db.Model): return jwt.encode( {'reset_password': self.id, 'exp': time() + expires_in}, current_app.config['SECRET_KEY'], - algorithm='HS256').decode('utf-8') + algorithm='HS256') def another_account_using_email(self, email): another_account = User.query.filter(User.email == email, User.id != self.id).first() diff --git a/app/static/js/scripts.js b/app/static/js/scripts.js index e69de29b..2768f7eb 100644 --- a/app/static/js/scripts.js +++ b/app/static/js/scripts.js @@ -0,0 +1,28 @@ +// fires after DOM is ready for manipulation +document.addEventListener("DOMContentLoaded", function () { + setupCommunityNameInput(); +}); + + +// fires after all resources have loaded, including stylesheets and js files +window.addEventListener("load", function () { + +}); + + +function setupCommunityNameInput() { + var communityNameInput = document.getElementById('community_name'); + + if (communityNameInput) { + communityNameInput.addEventListener('keyup', function() { + var urlInput = document.getElementById('url'); + urlInput.value = titleToURL(communityNameInput.value); + }); + } +} + + +function titleToURL(title) { + // Convert the title to lowercase and replace spaces with hyphens + return title.toLowerCase().replace(/\s+/g, '-'); +} \ No newline at end of file diff --git a/app/templates/community/add_local.html b/app/templates/community/add_local.html new file mode 100644 index 00000000..c9a69b4a --- /dev/null +++ b/app/templates/community/add_local.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} +{% from 'bootstrap/form.html' import render_form %} + +{% block app_content %} +
+
+
+
+
{{ _('Create community') }}
+ {{ render_form(form) }} +
+
+
+
+{% endblock %} \ No newline at end of file diff --git a/app/templates/community/community.html b/app/templates/community/community.html index 8a846db0..6379c3d6 100644 --- a/app/templates/community/community.html +++ b/app/templates/community/community.html @@ -43,10 +43,11 @@
-

About community

+

{{ _('About community') }}

- {{ community.description|safe }} +

{{ community.description }}

+

{{ community.rules }}

diff --git a/app/templates/list_communities.html b/app/templates/list_communities.html index 364b73cd..f04a781a 100644 --- a/app/templates/list_communities.html +++ b/app/templates/list_communities.html @@ -14,7 +14,7 @@
- {{ _('Add local') }} + {{ _('Create local') }} {{ _('Add remote') }}
@@ -35,7 +35,7 @@ {% for community in communities %} - + {{ community.display_name() }} {{ community.post_count }} {{ community.post_reply_count }} diff --git a/app/utils.py b/app/utils.py index 2b91f713..7fd7011b 100644 --- a/app/utils.py +++ b/app/utils.py @@ -1,7 +1,12 @@ +import functools + import requests import os -from flask import current_app +from flask import current_app, json + +from app import db +from app.models import Settings # ---------------------------------------------------------------------- @@ -23,3 +28,22 @@ def get_request(uri, params=None, headers=None) -> requests.Response: raise requests.exceptions.RequestException(f"InvalidCodepoint: {str(ex)}") from None return response + + +@functools.lru_cache(maxsize=100) +def get_setting(name: str, default=None): + setting = Settings.query.filter_by(name=name).first() + if setting is None: + return default + else: + return json.loads(setting.value) + + +def set_setting(name: str, value): + setting = Settings.query.filter_by(name=name).first() + if setting is None: + db.session.append(Settings(name=name, value=json.dumps(value))) + else: + setting.value = json.dumps(value) + db.session.commit() + get_setting.cache_clear()