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 %} +
{{ community.description }}
+{{ community.rules }}