mirror of
https://codeberg.org/rimu/pyfedi
synced 2025-01-23 19:36:56 -08:00
add local community
This commit is contained in:
parent
82000c1095
commit
e0e8ccd6fd
9 changed files with 104 additions and 13 deletions
|
@ -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.
|
# 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
|
# e.g. export FLASK_APP=pyfedi.py
|
||||||
|
from flask import json
|
||||||
|
|
||||||
from app import db
|
from app import db
|
||||||
import click
|
import click
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
from app.models import Settings, BannedInstances
|
||||||
|
|
||||||
|
|
||||||
def register(app):
|
def register(app):
|
||||||
@app.cli.group()
|
@app.cli.group()
|
||||||
|
@ -45,5 +47,8 @@ def register(app):
|
||||||
db.drop_all()
|
db.drop_all()
|
||||||
db.configure_mappers()
|
db.configure_mappers()
|
||||||
db.create_all()
|
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()
|
db.session.commit()
|
||||||
print("Done")
|
print("Done")
|
||||||
|
|
|
@ -4,9 +4,9 @@ from wtforms.validators import ValidationError, DataRequired, Email, EqualTo, Le
|
||||||
from flask_babel import _, lazy_gettext as _l
|
from flask_babel import _, lazy_gettext as _l
|
||||||
|
|
||||||
|
|
||||||
class AddLocalCommunity():
|
class AddLocalCommunity(FlaskForm):
|
||||||
name = StringField(_l('Name'), validators=[DataRequired()])
|
community_name = StringField(_l('Name'), validators=[DataRequired()])
|
||||||
url = StringField(_l('Url'))
|
url = StringField(_l('Url'), render_kw={'placeholder': '/c/'})
|
||||||
description = TextAreaField(_l('Description'))
|
description = TextAreaField(_l('Description'))
|
||||||
rules = TextAreaField(_l('Rules'))
|
rules = TextAreaField(_l('Rules'))
|
||||||
nsfw = BooleanField('18+ NSFW')
|
nsfw = BooleanField('18+ NSFW')
|
||||||
|
|
|
@ -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_login import login_user, logout_user, current_user
|
||||||
from flask_babel import _
|
from flask_babel import _
|
||||||
from app import db
|
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.community.util import search_for_community
|
||||||
from app.constants import SUBSCRIPTION_MEMBER
|
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.community import bp
|
||||||
|
from app.utils import get_setting
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/add_local', methods=['GET', 'POST'])
|
@bp.route('/add_local', methods=['GET', 'POST'])
|
||||||
def add_local():
|
def add_local():
|
||||||
form = AddLocalCommunity()
|
form = AddLocalCommunity()
|
||||||
|
if get_setting('allow_nsfw', False) is False:
|
||||||
|
form.nsfw.render_kw = {'disabled': True}
|
||||||
|
|
||||||
if form.validate_on_submit():
|
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'])
|
@bp.route('/add_remote', methods=['GET', 'POST'])
|
||||||
|
|
|
@ -159,7 +159,7 @@ class User(UserMixin, db.Model):
|
||||||
return jwt.encode(
|
return jwt.encode(
|
||||||
{'reset_password': self.id, 'exp': time() + expires_in},
|
{'reset_password': self.id, 'exp': time() + expires_in},
|
||||||
current_app.config['SECRET_KEY'],
|
current_app.config['SECRET_KEY'],
|
||||||
algorithm='HS256').decode('utf-8')
|
algorithm='HS256')
|
||||||
|
|
||||||
def another_account_using_email(self, email):
|
def another_account_using_email(self, email):
|
||||||
another_account = User.query.filter(User.email == email, User.id != self.id).first()
|
another_account = User.query.filter(User.email == email, User.id != self.id).first()
|
||||||
|
|
|
@ -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, '-');
|
||||||
|
}
|
15
app/templates/community/add_local.html
Normal file
15
app/templates/community/add_local.html
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
{% extends "base.html" %}
|
||||||
|
{% from 'bootstrap/form.html' import render_form %}
|
||||||
|
|
||||||
|
{% block app_content %}
|
||||||
|
<div class="row">
|
||||||
|
<div class="col col-login mx-auto">
|
||||||
|
<div class="card mt-5">
|
||||||
|
<div class="card-body p-6">
|
||||||
|
<div class="card-title">{{ _('Create community') }}</div>
|
||||||
|
{{ render_form(form) }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
|
@ -43,10 +43,11 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="card mt-3">
|
<div class="card mt-3">
|
||||||
<div class="card-header">
|
<div class="card-header">
|
||||||
<h2>About community</h2>
|
<h2>{{ _('About community') }}</h2>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
{{ community.description|safe }}
|
<p>{{ community.description }}</p>
|
||||||
|
<p>{{ community.rules }}</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
|
|
||||||
<div class="btn-group">
|
<div class="btn-group">
|
||||||
|
|
||||||
<a href="#" class="btn btn-outline-secondary">{{ _('Add local') }}</a>
|
<a href="{{ url_for('community.add_local') }}" class="btn btn-outline-secondary">{{ _('Create local') }}</a>
|
||||||
<a href="{{ url_for('community.add_remote') }}" class="btn btn-outline-secondary">{{ _('Add remote') }}</a>
|
<a href="{{ url_for('community.add_remote') }}" class="btn btn-outline-secondary">{{ _('Add remote') }}</a>
|
||||||
</div>
|
</div>
|
||||||
<input type="search" placeholder="Find a community" class="form-control">
|
<input type="search" placeholder="Find a community" class="form-control">
|
||||||
|
@ -35,7 +35,7 @@
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for community in communities %}
|
{% for community in communities %}
|
||||||
<tr class="">
|
<tr class="">
|
||||||
<td><a href="/c/{{ community.link() }}"><img src="{{ community.icon_image() }}" class="community_icon rounded-circle" /></a></td>
|
<td><a href="/c/{{ community.link() }}"><img src="{{ community.icon_image() }}" class="community_icon rounded-circle" loading="lazy" /></a></td>
|
||||||
<th scope="row"><a href="/c/{{ community.link() }}">{{ community.display_name() }}</a></th>
|
<th scope="row"><a href="/c/{{ community.link() }}">{{ community.display_name() }}</a></th>
|
||||||
<td>{{ community.post_count }}</td>
|
<td>{{ community.post_count }}</td>
|
||||||
<td>{{ community.post_reply_count }}</td>
|
<td>{{ community.post_reply_count }}</td>
|
||||||
|
|
26
app/utils.py
26
app/utils.py
|
@ -1,7 +1,12 @@
|
||||||
|
import functools
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import os
|
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
|
raise requests.exceptions.RequestException(f"InvalidCodepoint: {str(ex)}") from None
|
||||||
|
|
||||||
return response
|
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()
|
||||||
|
|
Loading…
Add table
Reference in a new issue