2021-05-05 08:50:54 +00:00
|
|
|
from datetime import datetime, timedelta
|
2020-12-09 17:48:15 +00:00
|
|
|
from os import path, remove
|
2020-12-21 01:04:27 +00:00
|
|
|
from io import BytesIO
|
|
|
|
from base64 import b64encode
|
2022-09-01 06:28:48 +00:00
|
|
|
from sys import audit
|
2020-12-21 01:04:27 +00:00
|
|
|
from qrcode import make as qrcode_make
|
2020-08-08 06:12:33 +00:00
|
|
|
from flask import render_template, Blueprint, request, session, flash
|
2020-07-30 08:36:31 +00:00
|
|
|
from flask import send_from_directory, redirect, url_for, current_app
|
2020-07-29 00:24:59 +00:00
|
|
|
from werkzeug.utils import secure_filename
|
2020-09-24 04:55:00 +00:00
|
|
|
from secrets import token_urlsafe
|
2020-08-10 05:47:53 +00:00
|
|
|
from suchwow import wownero
|
2020-11-22 20:39:21 +00:00
|
|
|
from suchwow import config
|
2021-11-24 05:55:38 +00:00
|
|
|
from suchwow.models import Post, Profile, Comment, Ban
|
2020-12-04 07:17:58 +00:00
|
|
|
from suchwow.utils.decorators import login_required, profile_required, moderator_required
|
2021-05-05 08:50:54 +00:00
|
|
|
from suchwow.utils.helpers import allowed_file, is_moderator, get_session_user
|
2022-09-01 06:28:48 +00:00
|
|
|
from suchwow.utils.helpers import post_webhook, audit_event
|
2020-12-10 21:16:41 +00:00
|
|
|
from suchwow.discord import post_discord_webhook
|
2020-07-29 00:24:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
bp = Blueprint("post", "post")
|
|
|
|
|
|
|
|
@bp.route("/post/<id>")
|
|
|
|
def read(id):
|
2020-12-21 01:04:27 +00:00
|
|
|
_address_qr = BytesIO()
|
|
|
|
qr_code = None
|
2020-07-29 00:24:59 +00:00
|
|
|
if Post.filter(id=id):
|
2020-08-10 05:47:53 +00:00
|
|
|
wallet = wownero.Wallet()
|
|
|
|
post = Post.get(id=id)
|
2020-12-04 07:17:58 +00:00
|
|
|
if not post.approved:
|
|
|
|
if not is_moderator(get_session_user()):
|
2021-11-08 09:04:58 +00:00
|
|
|
flash("That post has not been approved.", "is-warning")
|
2020-12-04 07:17:58 +00:00
|
|
|
return redirect("/")
|
2020-08-10 05:47:53 +00:00
|
|
|
if wallet.connected:
|
2020-10-06 08:41:05 +00:00
|
|
|
address = wallet.get_address(account=post.account_index)
|
2020-08-10 22:11:53 +00:00
|
|
|
transfers = wallet.transfers(account=post.account_index)
|
2020-12-21 01:04:27 +00:00
|
|
|
qr_uri = f'wownero:{address}?tx_description=suchwow%20post%20{post.id}'
|
|
|
|
address_qr = qrcode_make(qr_uri).save(_address_qr)
|
|
|
|
qr_code = b64encode(_address_qr.getvalue()).decode()
|
2020-08-10 05:47:53 +00:00
|
|
|
else:
|
|
|
|
address = "?"
|
2020-08-10 22:11:53 +00:00
|
|
|
transfers = "?"
|
|
|
|
return render_template(
|
|
|
|
"post/read.html",
|
|
|
|
post=post,
|
|
|
|
address=address,
|
2020-12-21 01:04:27 +00:00
|
|
|
transfers=transfers,
|
|
|
|
qr_code=qr_code
|
2020-08-10 22:11:53 +00:00
|
|
|
)
|
2020-07-29 00:24:59 +00:00
|
|
|
else:
|
2021-11-08 09:04:58 +00:00
|
|
|
flash("No meme there, brah", "is-warning")
|
2022-09-01 06:02:31 +00:00
|
|
|
return redirect(url_for("main.index"))
|
2020-07-29 00:24:59 +00:00
|
|
|
|
|
|
|
@bp.route("/post/create", methods=["GET", "POST"])
|
|
|
|
@login_required
|
2020-08-10 06:59:45 +00:00
|
|
|
@profile_required
|
2020-07-29 00:24:59 +00:00
|
|
|
def create():
|
2021-11-24 05:55:38 +00:00
|
|
|
submitter = get_session_user()
|
|
|
|
u = Profile.filter(username=submitter)
|
|
|
|
banned = Ban.filter(user=u).first()
|
|
|
|
if banned:
|
|
|
|
flash(f"You can't post: {banned.reason}", "is-danger")
|
|
|
|
return redirect("/")
|
2020-07-29 00:24:59 +00:00
|
|
|
if request.method == "POST":
|
|
|
|
post_title = request.form.get("title")
|
|
|
|
# check if the post request has the file part
|
|
|
|
if "file" not in request.files:
|
2021-11-08 09:04:58 +00:00
|
|
|
flash("You didn't upload a caliente meme, bro! You're fuckin up!", "is-danger")
|
2020-07-29 00:24:59 +00:00
|
|
|
return redirect(request.url)
|
|
|
|
file = request.files["file"]
|
|
|
|
# if user does not select file, browser also
|
|
|
|
# submit an empty part without filename
|
|
|
|
if file.filename == "":
|
2021-11-08 09:04:58 +00:00
|
|
|
flash("You didn't upload a caliente meme, bro! You're fuckin up!", "is-danger")
|
2020-07-29 00:24:59 +00:00
|
|
|
return redirect(request.url)
|
2020-12-07 18:05:22 +00:00
|
|
|
if post_title == "":
|
2021-11-08 09:04:58 +00:00
|
|
|
flash("You didn't give your meme a spicy title, bro! You're fuckin up!", "is-danger")
|
2020-07-29 00:24:59 +00:00
|
|
|
return redirect(request.url)
|
|
|
|
if file and allowed_file(file.filename):
|
2020-09-24 04:55:00 +00:00
|
|
|
filename = "{}-{}".format(
|
|
|
|
token_urlsafe(12),
|
|
|
|
secure_filename(file.filename)
|
|
|
|
)
|
2020-08-10 05:47:53 +00:00
|
|
|
save_path_base = path.join(current_app.config["DATA_FOLDER"], "uploads")
|
|
|
|
save_path = path.join(save_path_base, filename)
|
2020-07-29 00:24:59 +00:00
|
|
|
file.save(save_path)
|
2020-08-10 05:47:53 +00:00
|
|
|
try:
|
|
|
|
wallet = wownero.Wallet()
|
|
|
|
account_index = wallet.new_account()
|
2022-02-18 22:20:05 +00:00
|
|
|
in_use = Post.select().where(Post.account_index == account_index).first()
|
|
|
|
if in_use:
|
|
|
|
flash("Suchwow wallet is fucked up! Try again later.", "is-danger")
|
|
|
|
return redirect(request.url)
|
2020-08-10 05:47:53 +00:00
|
|
|
except:
|
2021-11-08 09:04:58 +00:00
|
|
|
flash("Suchwow wallet is fucked up! Try again later.", "is-danger")
|
2021-04-15 17:47:27 +00:00
|
|
|
return redirect(request.url)
|
2020-07-29 00:24:59 +00:00
|
|
|
post = Post(
|
|
|
|
title=post_title,
|
2020-08-08 06:12:33 +00:00
|
|
|
text=request.form.get("text", ""),
|
2020-11-22 20:39:21 +00:00
|
|
|
submitter=submitter,
|
2020-07-29 00:24:59 +00:00
|
|
|
image_name=filename,
|
2020-08-10 05:47:53 +00:00
|
|
|
account_index=account_index,
|
2020-07-29 00:24:59 +00:00
|
|
|
address_index=0
|
|
|
|
)
|
|
|
|
post.save()
|
2021-01-29 06:01:42 +00:00
|
|
|
post.save_thumbnail()
|
2020-12-14 06:28:23 +00:00
|
|
|
url = url_for('post.read', id=post.id, _external=True)
|
2022-09-01 06:28:48 +00:00
|
|
|
audit_event(f'Created new post {post.id}')
|
2021-11-08 09:04:58 +00:00
|
|
|
flash("New post created and pending approval!", "is-success")
|
2022-09-01 06:02:31 +00:00
|
|
|
return redirect(url_for("main.index"))
|
2020-07-30 08:36:31 +00:00
|
|
|
return render_template("post/create.html")
|
|
|
|
|
2020-12-04 07:17:58 +00:00
|
|
|
@bp.route("/post/<id>/approve")
|
|
|
|
@moderator_required
|
|
|
|
def approve(id):
|
|
|
|
post = Post.get(id=id)
|
2020-12-14 06:28:23 +00:00
|
|
|
url = url_for('post.read', id=post.id, _external=True)
|
2020-12-04 07:17:58 +00:00
|
|
|
if post:
|
2020-12-14 06:28:23 +00:00
|
|
|
if not post.approved:
|
|
|
|
post.approved = True
|
|
|
|
post.save()
|
2021-11-08 09:04:58 +00:00
|
|
|
flash("Approved", "is-success")
|
2022-09-01 06:28:48 +00:00
|
|
|
audit_event(f'Approved post {post.id}')
|
2020-12-14 06:28:23 +00:00
|
|
|
if current_app.config["DEBUG"] is False:
|
2021-05-12 04:12:18 +00:00
|
|
|
post_discord_webhook(post)
|
2022-09-01 06:02:31 +00:00
|
|
|
return redirect(url_for("mod.pending_posts"))
|
2020-12-04 07:17:58 +00:00
|
|
|
else:
|
2021-11-08 09:04:58 +00:00
|
|
|
flash("You can't approve this", "is-success")
|
2022-09-01 06:02:31 +00:00
|
|
|
return redirect(url_for("main.index"))
|
2020-12-04 07:17:58 +00:00
|
|
|
|
2020-09-24 05:10:24 +00:00
|
|
|
@bp.route("/post/<id>/delete")
|
|
|
|
@login_required
|
|
|
|
def delete(id):
|
|
|
|
filtered = Post.filter(id=id)
|
2020-12-04 07:30:49 +00:00
|
|
|
user = get_session_user()
|
2020-12-07 17:29:34 +00:00
|
|
|
is_mod = is_moderator(user)
|
2020-09-24 05:10:24 +00:00
|
|
|
if filtered:
|
|
|
|
post = filtered.first()
|
2020-12-07 17:29:34 +00:00
|
|
|
if user == post.submitter or is_mod:
|
2020-12-09 17:48:15 +00:00
|
|
|
save_path_base = path.join(current_app.config["DATA_FOLDER"], "uploads")
|
|
|
|
save_path = path.join(save_path_base, post.image_name)
|
|
|
|
remove(save_path)
|
2022-09-01 06:28:48 +00:00
|
|
|
audit_event(f'Deleted post {post.id}')
|
2020-09-24 05:10:24 +00:00
|
|
|
post.delete_instance()
|
2021-11-08 09:04:58 +00:00
|
|
|
flash("Deleted that shit, brah!", "is-success")
|
2020-12-07 17:29:34 +00:00
|
|
|
if is_mod:
|
2022-09-01 06:02:31 +00:00
|
|
|
return redirect(url_for("mod.pending_posts"))
|
2020-12-07 17:29:34 +00:00
|
|
|
else:
|
2022-09-01 06:02:31 +00:00
|
|
|
return redirect(url_for("main.index"))
|
2020-09-24 05:10:24 +00:00
|
|
|
else:
|
2021-11-08 09:04:58 +00:00
|
|
|
flash("You can't delete a meme you don't own, brah", "is-warning")
|
2020-09-24 05:10:24 +00:00
|
|
|
return redirect(url_for("post.read", id=post.id))
|
|
|
|
else:
|
2021-11-08 09:04:58 +00:00
|
|
|
flash("No meme there, brah", "is-warning")
|
2022-09-01 06:02:31 +00:00
|
|
|
return redirect(url_for("main.index"))
|
2020-09-24 05:10:24 +00:00
|
|
|
|
2020-07-30 08:36:31 +00:00
|
|
|
@bp.route("/uploads/<path:filename>")
|
|
|
|
def uploaded_file(filename):
|
2020-08-10 05:47:53 +00:00
|
|
|
file_path = path.join(current_app.config["DATA_FOLDER"], "uploads")
|
2021-01-29 06:08:36 +00:00
|
|
|
return send_from_directory(file_path, filename)
|