2024-02-02 17:10:48 +13:00
|
|
|
from random import randint
|
|
|
|
|
2024-05-30 21:54:25 +12:00
|
|
|
from flask import redirect, url_for, flash, request, make_response, session, Markup, current_app, abort, g
|
2023-11-29 22:12:55 +13:00
|
|
|
from flask_login import login_user, logout_user, current_user, login_required
|
|
|
|
from flask_babel import _
|
|
|
|
|
2024-02-02 17:22:32 +13:00
|
|
|
from app import db, constants, cache
|
2024-02-02 17:10:48 +13:00
|
|
|
from app.inoculation import inoculation
|
2024-02-02 16:52:23 +13:00
|
|
|
from app.models import Post, Domain, Community, DomainBlock
|
2023-11-29 22:12:55 +13:00
|
|
|
from app.domain import bp
|
2024-03-12 19:33:26 +13:00
|
|
|
from app.utils import render_template, permission_required, joined_communities, moderating_communities, \
|
2024-05-30 21:54:25 +12:00
|
|
|
user_filters_posts, blocked_domains, blocked_instances, menu_topics
|
2024-03-12 20:06:24 +13:00
|
|
|
from sqlalchemy import desc, or_
|
2023-11-29 22:12:55 +13:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/d/<domain_id>', methods=['GET'])
|
|
|
|
def show_domain(domain_id):
|
2024-02-02 16:52:23 +13:00
|
|
|
page = request.args.get('page', 1, type=int)
|
|
|
|
|
2023-11-29 22:12:55 +13:00
|
|
|
if '.' in domain_id:
|
|
|
|
domain = Domain.query.filter_by(name=domain_id, banned=False).first()
|
|
|
|
else:
|
|
|
|
domain = Domain.query.get_or_404(domain_id)
|
|
|
|
if domain.banned:
|
|
|
|
domain = None
|
|
|
|
if domain:
|
2024-06-28 13:22:15 +02:00
|
|
|
if current_user.is_anonymous or current_user.ignore_bots == 1:
|
2024-01-01 11:38:24 +13:00
|
|
|
posts = Post.query.join(Community, Community.id == Post.community_id).\
|
2024-06-02 16:45:21 +12:00
|
|
|
filter(Post.from_bot == False, Post.domain_id == domain.id, Community.banned == False, Post.deleted == False).\
|
2024-02-23 20:23:59 +13:00
|
|
|
order_by(desc(Post.posted_at))
|
2023-11-29 22:12:55 +13:00
|
|
|
else:
|
2024-06-02 16:45:21 +12:00
|
|
|
posts = Post.query.join(Community).filter(Post.domain_id == domain.id, Community.banned == False, Post.deleted == False).order_by(desc(Post.posted_at))
|
2024-02-02 17:10:48 +13:00
|
|
|
|
|
|
|
if current_user.is_authenticated:
|
2024-03-13 11:17:04 +13:00
|
|
|
instance_ids = blocked_instances(current_user.id)
|
|
|
|
if instance_ids:
|
|
|
|
posts = posts.filter(or_(Post.instance_id.not_in(instance_ids), Post.instance_id == None))
|
2024-02-02 17:10:48 +13:00
|
|
|
content_filters = user_filters_posts(current_user.id)
|
|
|
|
else:
|
|
|
|
content_filters = {}
|
2024-02-02 16:52:23 +13:00
|
|
|
# pagination
|
|
|
|
posts = posts.paginate(page=page, per_page=100, error_out=False)
|
2024-02-02 17:00:12 +13:00
|
|
|
next_url = url_for('domain.show_domain', domain_id=domain_id, page=posts.next_num) if posts.has_next else None
|
|
|
|
prev_url = url_for('domain.show_domain', domain_id=domain_id, page=posts.prev_num) if posts.has_prev and page != 1 else None
|
2023-11-29 22:12:55 +13:00
|
|
|
return render_template('domain/domain.html', domain=domain, title=domain.name, posts=posts,
|
2024-02-02 16:52:23 +13:00
|
|
|
POST_TYPE_IMAGE=constants.POST_TYPE_IMAGE, POST_TYPE_LINK=constants.POST_TYPE_LINK,
|
2024-04-16 20:59:58 +12:00
|
|
|
POST_TYPE_VIDEO=constants.POST_TYPE_VIDEO,
|
2024-02-02 17:10:48 +13:00
|
|
|
next_url=next_url, prev_url=prev_url,
|
|
|
|
content_filters=content_filters,
|
|
|
|
moderating_communities=moderating_communities(current_user.get_id()),
|
|
|
|
joined_communities=joined_communities(current_user.get_id()),
|
2024-05-30 21:54:25 +12:00
|
|
|
menu_topics=menu_topics(), site=g.site,
|
2024-02-02 17:10:48 +13:00
|
|
|
inoculation=inoculation[randint(0, len(inoculation) - 1)]
|
|
|
|
)
|
2023-11-29 22:12:55 +13:00
|
|
|
else:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/domains', methods=['GET'])
|
|
|
|
def domains():
|
2024-02-02 16:52:23 +13:00
|
|
|
page = request.args.get('page', 1, type=int)
|
|
|
|
search = request.args.get('search', '')
|
|
|
|
|
2024-03-10 20:17:16 +01:00
|
|
|
domains = Domain.query.filter_by(banned=False)
|
2024-02-02 16:52:23 +13:00
|
|
|
if search != '':
|
|
|
|
domains = domains.filter(Domain.name.ilike(f'%{search}%'))
|
|
|
|
domains = domains.order_by(Domain.name)
|
|
|
|
domains = domains.paginate(page=page, per_page=100, error_out=False)
|
|
|
|
|
2024-03-10 20:17:16 +01:00
|
|
|
ban_visibility_permission = False
|
|
|
|
|
|
|
|
if not current_user.is_anonymous:
|
|
|
|
if not current_user.created_recently() and current_user.reputation > 100 or current_user.is_admin():
|
|
|
|
ban_visibility_permission = True
|
|
|
|
|
2024-02-02 16:58:30 +13:00
|
|
|
next_url = url_for('domain.domains', page=domains.next_num) if domains.has_next else None
|
|
|
|
prev_url = url_for('domain.domains', page=domains.prev_num) if domains.has_prev and page != 1 else None
|
2024-02-02 16:52:23 +13:00
|
|
|
|
|
|
|
return render_template('domain/domains.html', title='All known domains', domains=domains,
|
2024-03-10 20:17:16 +01:00
|
|
|
next_url=next_url, prev_url=prev_url, search=search, ban_visibility_permission=ban_visibility_permission)
|
|
|
|
|
2024-03-11 20:14:12 +13:00
|
|
|
|
2024-03-10 20:17:16 +01:00
|
|
|
@bp.route('/domains/banned', methods=['GET'])
|
|
|
|
@login_required
|
2024-03-12 19:33:26 +13:00
|
|
|
def domains_blocked_list():
|
2024-03-11 20:14:12 +13:00
|
|
|
if not current_user.trustworthy():
|
|
|
|
abort(404)
|
|
|
|
|
2024-03-10 20:17:16 +01:00
|
|
|
page = request.args.get('page', 1, type=int)
|
|
|
|
search = request.args.get('search', '')
|
|
|
|
|
|
|
|
domains = Domain.query.filter_by(banned=True)
|
|
|
|
if search != '':
|
|
|
|
domains = domains.filter(Domain.name.ilike(f'%{search}%'))
|
|
|
|
domains = domains.order_by(Domain.name)
|
|
|
|
domains = domains.paginate(page=page, per_page=100, error_out=False)
|
|
|
|
|
|
|
|
next_url = url_for('domain.domains', page=domains.next_num) if domains.has_next else None
|
|
|
|
prev_url = url_for('domain.domains', page=domains.prev_num) if domains.has_prev and page != 1 else None
|
|
|
|
|
2024-03-11 20:14:12 +13:00
|
|
|
return render_template('domain/domains_blocked.html', title='Domains blocked on this instance', domains=domains,
|
|
|
|
next_url=next_url, prev_url=prev_url, search=search)
|
2024-02-02 16:52:23 +13:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/d/<int:domain_id>/block')
|
|
|
|
@login_required
|
|
|
|
def domain_block(domain_id):
|
|
|
|
domain = Domain.query.get_or_404(domain_id)
|
|
|
|
block = DomainBlock.query.filter_by(user_id=current_user.id, domain_id=domain_id).first()
|
|
|
|
if not block:
|
|
|
|
block = DomainBlock(user_id=current_user.id, domain_id=domain_id)
|
|
|
|
db.session.add(block)
|
|
|
|
db.session.commit()
|
2024-02-02 17:22:32 +13:00
|
|
|
cache.delete_memoized(blocked_domains, current_user.id)
|
2024-02-02 16:52:23 +13:00
|
|
|
flash(_('%(name)s blocked.', name=domain.name))
|
|
|
|
return redirect(url_for('domain.show_domain', domain_id=domain.id))
|
|
|
|
|
2023-11-29 22:12:55 +13:00
|
|
|
|
2024-02-02 16:52:23 +13:00
|
|
|
@bp.route('/d/<int:domain_id>/unblock')
|
|
|
|
@login_required
|
|
|
|
def domain_unblock(domain_id):
|
|
|
|
domain = Domain.query.get_or_404(domain_id)
|
|
|
|
block = DomainBlock.query.filter_by(user_id=current_user.id, domain_id=domain_id).first()
|
2024-02-02 17:22:32 +13:00
|
|
|
if block:
|
2024-02-02 16:52:23 +13:00
|
|
|
db.session.delete(block)
|
|
|
|
db.session.commit()
|
2024-02-02 17:22:32 +13:00
|
|
|
cache.delete_memoized(blocked_domains, current_user.id)
|
2024-02-02 16:52:23 +13:00
|
|
|
flash(_('%(name)s un-blocked.', name=domain.name))
|
|
|
|
return redirect(url_for('domain.show_domain', domain_id=domain.id))
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/d/<int:domain_id>/ban')
|
|
|
|
@login_required
|
|
|
|
@permission_required('manage users')
|
|
|
|
def domain_ban(domain_id):
|
|
|
|
domain = Domain.query.get_or_404(domain_id)
|
|
|
|
if domain:
|
|
|
|
domain.banned = True
|
|
|
|
db.session.commit()
|
|
|
|
domain.purge_content()
|
|
|
|
flash(_('%(name)s banned for all users and all content deleted.', name=domain.name))
|
|
|
|
return redirect(url_for('domain.domains'))
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/d/<int:domain_id>/unban')
|
|
|
|
@login_required
|
|
|
|
@permission_required('manage users')
|
|
|
|
def domain_unban(domain_id):
|
|
|
|
domain = Domain.query.get_or_404(domain_id)
|
|
|
|
if domain:
|
2024-02-02 17:22:32 +13:00
|
|
|
domain.banned = False
|
2024-02-02 16:52:23 +13:00
|
|
|
db.session.commit()
|
|
|
|
flash(_('%(name)s un-banned for all users.', name=domain.name))
|
|
|
|
return redirect(url_for('domain.show_domain', domain_id=domain.id))
|