MarseyWorld/files/routes/users.py

727 lines
23 KiB
Python
Raw Normal View History

2021-07-23 19:12:10 +00:00
import qrcode
2021-07-21 01:12:26 +00:00
import io
2021-07-24 20:50:36 +00:00
import time
2021-09-21 18:06:41 +00:00
import math
2021-08-04 15:35:10 +00:00
from files.classes.user import ViewerRelationship
from files.helpers.alerts import *
from files.helpers.sanitize import *
from files.helpers.markdown import *
2021-08-21 11:06:28 +00:00
from files.helpers.const import *
2021-08-04 15:35:10 +00:00
from files.mail import *
2021-07-21 01:12:26 +00:00
from flask import *
2021-08-04 15:35:10 +00:00
from files.__main__ import app, limiter
2021-08-11 02:54:52 +00:00
from pusher_push_notifications import PushNotifications
2021-09-19 18:41:36 +00:00
from sqlalchemy.orm import contains_eager
2021-07-21 01:12:26 +00:00
2021-08-05 14:41:32 +00:00
site = environ.get("DOMAIN").strip()
2021-08-02 14:27:20 +00:00
2021-07-21 01:12:26 +00:00
beams_client = PushNotifications(
2021-08-21 11:06:28 +00:00
instance_id=PUSHER_INSTANCE_ID,
2021-07-21 01:12:26 +00:00
secret_key=PUSHER_KEY,
)
2021-08-31 21:11:15 +00:00
@app.post("/pay_rent")
@auth_required
def pay_rent(v):
2021-09-01 15:01:04 +00:00
if v.coins < 500: return "You must have more than 500 coins."
2021-08-31 21:11:15 +00:00
v.coins -= 500
v.rent_utc = int(time.time())
g.db.add(v)
u = get_account(253)
u.coins += 500
g.db.add(u)
send_notification(NOTIFICATIONS_ACCOUNT, u, f"@{v.username} has paid rent!")
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-09-08 11:05:31 +00:00
return {"message": "Rent paid!"}
2021-08-31 21:11:15 +00:00
@app.post("/steal")
2021-08-31 21:27:15 +00:00
@is_not_banned
2021-08-31 21:11:15 +00:00
def steal(v):
2021-08-31 21:23:43 +00:00
if int(time.time()) - v.created_utc < 604800:
2021-08-31 23:09:00 +00:00
return "You must have an account older than 1 week in order to attempt stealing."
if v.coins < 5000:
2021-09-01 10:47:11 +00:00
return "You must have more than 5000 coins in order to attempt stealing."
2021-08-31 23:09:00 +00:00
u = get_account(253)
if random.randint(1, 10) < 5:
2021-08-31 23:17:51 +00:00
v.coins += 700
2021-08-31 21:11:15 +00:00
v.steal_utc = int(time.time())
g.db.add(v)
2021-08-31 23:17:51 +00:00
u.coins -= 700
2021-08-31 21:11:15 +00:00
g.db.add(u)
2021-08-31 23:17:51 +00:00
send_notification(NOTIFICATIONS_ACCOUNT, u, f"Some [grubby little rentoid](/@{v.username}) has absconded with 700 of your hard-earned dramacoins to fuel his Funko Pop addiction. Stop being so trusting.")
send_notification(NOTIFICATIONS_ACCOUNT, v, f"You have successfully shorted your heroic landlord 700 dramacoins in rent. You're slightly less materially poor, but somehow even moreso morally. Are you proud of yourself?")
2021-09-16 17:15:07 +00:00
g.db.commit()
2021-09-08 11:05:31 +00:00
return {"message": "Attempt successful!"}
2021-08-31 21:11:15 +00:00
else:
2021-08-31 23:09:00 +00:00
if random.random() < 0.15:
send_notification(NOTIFICATIONS_ACCOUNT, u, f"You caught [this sniveling little renthog](/@{v.username}) trying to rob you. After beating him within an inch of his life, you sold his Nintendo Switch for 500 dramacoins and called the cops. He was sentenced to one (1) day in renthog prison.")
send_notification(NOTIFICATIONS_ACCOUNT, v, f"The ever-vigilant landchad has caught you trying to steal his hard-earned rent money. The police take you away and laugh as you impotently stutter A-ACAB :sob: You are fined 500 dramacoins and sentenced to one (1) day in renthog prison.")
v.ban(days=1, reason="Jailed thief")
v.fail_utc = int(time.time())
else:
send_notification(NOTIFICATIONS_ACCOUNT, u, f"You caught [this sniveling little renthog](/@{v.username}) trying to rob you. After beating him within an inch of his life, you showed mercy in exchange for a 500 dramacoin tip. This time.")
send_notification(NOTIFICATIONS_ACCOUNT, v, f"The ever-vigilant landchad has caught you trying to steal his hard-earned rent money. You were able to convince him to spare your life with a 500 dramacoin tip. This time.")
v.fail2_utc = int(time.time())
v.coins -= 500
2021-08-31 21:11:15 +00:00
g.db.add(v)
2021-08-31 23:09:00 +00:00
u.coins += 500
g.db.add(u)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-09-08 11:05:31 +00:00
return {"message": "Attempt failed!"}
2021-08-31 21:11:15 +00:00
@app.get("/rentoids")
@auth_desired
def rentoids(v):
2021-09-17 08:29:05 +00:00
users = g.db.query(User).options(lazyload('*')).filter(User.rent_utc > 0).all()
2021-08-31 21:11:15 +00:00
return render_template("rentoids.html", v=v, users=users)
@app.get("/thiefs")
@auth_desired
2021-08-31 21:13:34 +00:00
def thiefs(v):
2021-09-17 08:29:05 +00:00
successful = g.db.query(User).options(lazyload('*')).filter(User.steal_utc > 0).all()
failed = g.db.query(User).options(lazyload('*')).filter(User.fail_utc > 0).all()
failed2 = g.db.query(User).options(lazyload('*')).filter(User.fail2_utc > 0).all()
2021-08-31 23:09:00 +00:00
return render_template("thiefs.html", v=v, successful=successful, failed=failed, failed2=failed2)
2021-08-31 21:11:15 +00:00
2021-07-24 20:49:07 +00:00
@app.post("/@<username>/suicide")
2021-07-24 19:44:21 +00:00
@auth_required
def suicide(v, username):
2021-07-24 20:50:36 +00:00
t = int(time.time())
2021-09-08 11:05:31 +00:00
if v.admin_level == 0 and t - v.suicide_utc < 86400: return {"message": "You're on 1-day cooldown!"}
2021-07-24 19:44:21 +00:00
user = get_user(username)
2021-08-04 16:00:57 +00:00
suicide = f"Hi there,\n\nA [concerned user]({v.url}) reached out to us about you.\n\nWhen you're in the middle of something painful, it may feel like you don't have a lot of options. But whatever you're going through, you deserve help and there are people who are here for you.\n\nThere are resources available in your area that are free, confidential, and available 24/7:\n\n- Call, Text, or Chat with Canada's [Crisis Services Canada](https://www.crisisservicescanada.ca/en/)\n- Call, Email, or Visit the UK's [Samaritans](https://www.samaritans.org/)\n- Text CHAT to America's [Crisis Text Line](https://www.crisistextline.org/) at 741741.\nIf you don't see a resource in your area above, the moderators at r/SuicideWatch keep a comprehensive list of resources and hotlines for people organized by location. Find Someone Now\n\nIf you think you may be depressed or struggling in another way, don't ignore it or brush it aside. Take yourself and your feelings seriously, and reach out to someone.\n\nIt may not feel like it, but you have options. There are people available to listen to you, and ways to move forward.\n\nYour fellow users care about you and there are people who want to help."
2021-08-21 11:06:28 +00:00
send_notification(NOTIFICATIONS_ACCOUNT, user, suicide)
2021-07-24 20:34:40 +00:00
v.suicide_utc = t
g.db.add(v)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-09-08 11:05:31 +00:00
return {"message": "Help message sent!"}
2021-07-24 19:44:21 +00:00
2021-08-31 21:11:15 +00:00
@app.get("/@<username>/coins")
@auth_required
def get_coins(v, username):
user = get_user(username)
if user is not None: return {"coins": user.coins}, 200
else: return {"error": "invalid_user"}, 404
@app.post("/@<username>/transfer_coins")
2021-08-31 23:09:00 +00:00
@is_not_banned
@validate_formkey
def transfer_coins(v, username):
2021-09-22 15:28:24 +00:00
TAX_RECEIVER_ID = 747
TAX_RATE = 0.01
2021-09-21 18:06:41 +00:00
receiver = g.db.query(User).filter_by(username=username).first()
2021-09-22 15:28:24 +00:00
tax_receiver = g.db.query(User).filter_by(id=TAX_RECEIVER_ID).first()
if receiver is None: return {"error": "That user doesn't exist."}, 404
if receiver.id != v.id:
2021-09-19 13:11:34 +00:00
amount = request.values.get("amount", "")
amount = int(amount) if amount.isdigit() else None
2021-08-09 12:21:29 +00:00
if amount is None or amount <= 0: return {"error": f"Invalid amount of {app.config['COINS_NAME']}."}, 400
if v.coins < amount: return {"error": f"You don't have enough {app.config['COINS_NAME']}"}, 400
if amount < 100: return {"error": f"You have to gift at least 100 {app.config['COINS_NAME']}."}, 400
2021-09-22 15:28:24 +00:00
tax = math.ceil(amount*TAX_RATE)
2021-09-21 19:44:47 +00:00
v.coins -= amount
2021-09-21 18:06:41 +00:00
receiver.coins += amount-tax
tax_receiver.coins += tax
g.db.add(receiver)
2021-09-21 18:06:41 +00:00
g.db.add(tax_receiver)
g.db.add(v)
2021-08-09 12:21:29 +00:00
transfer_message = f"🤑 [@{v.username}]({v.url}) has gifted you {amount} {app.config['COINS_NAME']}!"
2021-09-16 21:06:30 +00:00
send_notification(NOTIFICATIONS_ACCOUNT, receiver, transfer_message)
2021-09-22 15:28:24 +00:00
log_message = f"[@{v.username}]({v.url}) has transferred {amount} {app.config['COINS_NAME']} to [@{receiver.username}]({receiver.url})"
send_notification(NOTIFICATIONS_ACCOUNT, TAX_RECEIVER_ID, log_message)
2021-09-17 08:55:55 +00:00
g.db.commit()
2021-09-21 18:06:41 +00:00
return {"message": f"{amount-tax} {app.config['COINS_NAME']} transferred!"}, 200
return {"message": f"You can't transfer {app.config['COINS_NAME']} to yourself!"}, 400
2021-09-08 11:05:31 +00:00
2021-07-27 22:31:28 +00:00
@app.get("/leaderboard")
2021-07-21 01:12:26 +00:00
@auth_desired
def leaderboard(v):
2021-07-27 00:21:03 +00:00
users = g.db.query(User).options(lazyload('*'))
2021-08-04 16:51:16 +00:00
users1 = users.order_by(User.coins.desc()).limit(25).all()
2021-07-27 00:13:15 +00:00
users2 = users.order_by(User.stored_subscriber_count.desc()).limit(10).all()
2021-07-28 03:55:47 +00:00
users3 = users.order_by(User.post_count.desc()).limit(10).all()
users4 = users.order_by(User.comment_count.desc()).limit(10).all()
2021-09-05 17:42:39 +00:00
users5 = users.order_by(User.received_award_count.desc()).limit(10).all()
2021-09-13 17:03:46 +00:00
if 'pcmemes.net' in request.host:
2021-09-05 17:42:39 +00:00
users6 = users.order_by(User.basedcount.desc()).limit(10).all()
return render_template("leaderboard.html", v=v, users1=users1, users2=users2, users3=users3, users4=users4, users5=users5, users6=users6)
return render_template("leaderboard.html", v=v, users1=users1, users2=users2, users3=users3, users4=users4, users5=users5)
2021-09-01 19:03:05 +00:00
2021-09-20 20:50:35 +00:00
2021-07-21 01:12:26 +00:00
@app.get("/@<username>/css")
def get_css(username):
user = get_user(username)
if user.css: css = user.css
else: css = ""
resp=make_response(css)
resp.headers.add("Content-Type", "text/css")
return resp
@app.get("/@<username>/profilecss")
def get_profilecss(username):
user = get_user(username)
if user.profilecss: profilecss = user.profilecss
else: profilecss = ""
resp=make_response(profilecss)
resp.headers.add("Content-Type", "text/css")
return resp
2021-07-27 22:31:28 +00:00
@app.get("/songs/<id>")
2021-07-21 01:12:26 +00:00
def songs(id):
try: id = int(id)
2021-09-08 10:57:56 +00:00
except: return "", 400
2021-09-17 08:29:05 +00:00
user = g.db.query(User).options(lazyload('*')).filter_by(id=id).first()
2021-08-31 15:43:37 +00:00
return redirect(f"/song/{user.song}.mp3")
@app.get("/song/<song>")
2021-08-31 15:45:36 +00:00
def song(song):
2021-08-31 15:43:37 +00:00
resp = make_response(send_from_directory('/songs/', song))
resp.headers.remove("Cache-Control")
resp.headers.add("Cache-Control", "public, max-age=2628000")
return resp
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.post("/subscribe/<post_id>")
2021-07-21 01:12:26 +00:00
@auth_required
def subscribe(v, post_id):
new_sub = Subscription(user_id=v.id, submission_id=post_id)
g.db.add(new_sub)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-09-08 11:05:31 +00:00
return {"message": "Post subscribed!"}
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.post("/unsubscribe/<post_id>")
2021-07-21 01:12:26 +00:00
@auth_required
def unsubscribe(v, post_id):
2021-09-17 08:29:05 +00:00
sub=g.db.query(Subscription).options(lazyload('*')).filter_by(user_id=v.id, submission_id=post_id).first()
2021-07-21 01:12:26 +00:00
g.db.delete(sub)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-09-08 11:05:31 +00:00
return {"message": "Post unsubscribed!"}
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.post("/@<username>/message")
2021-07-30 21:30:31 +00:00
@limiter.limit("10/hour")
2021-07-21 01:12:26 +00:00
@auth_required
def message2(v, username):
2021-07-30 21:38:29 +00:00
2021-07-21 01:12:26 +00:00
user = get_user(username, v=v)
2021-09-22 18:41:22 +00:00
if hasattr(user, 'is_blocking') and user.is_blocking: return {"error": "You're blocking this user."}, 403
if hasattr(user, 'is_blocked') and user.is_blocked: return {"error": "This user is blocking you."}, 403
2021-09-19 13:11:34 +00:00
message = request.values.get("message", "")[:1000].strip()
2021-07-29 05:46:33 +00:00
# check existing
2021-09-17 08:29:05 +00:00
existing = g.db.query(Comment).join(CommentAux).options(lazyload('*')).filter(Comment.author_id == v.id,
2021-07-29 05:50:04 +00:00
Comment.sentto == user.id,
CommentAux.body == message,
).options(contains_eager(Comment.comment_aux)).first()
2021-08-12 23:23:33 +00:00
if existing: return redirect('/notifications?messages=true')
2021-07-29 05:46:33 +00:00
2021-09-22 22:27:09 +00:00
text = re.sub('([^\n])\n([^\n])', r'\1\n\n\2', message)
text_html = Renderer().render(mistletoe.Document(text))
text_html = sanitize(text_html, True)
new_comment = Comment(author_id=v.id,
parent_submission=None,
level=1,
sentto=user.id
)
g.db.add(new_comment)
g.db.flush()
new_aux = CommentAux(id=new_comment.id, body=text, body_html=text_html)
g.db.add(new_aux)
notif = Notification(comment_id=new_comment.id, user_id=user.id)
g.db.add(notif)
try:
beams_client.publish_to_interests(
interests=[str(user.id)],
publish_body={
'web': {
'notification': {
'title': f'New message from @{v.username}',
'body': message,
'deep_link': f'https://{site}/notifications',
},
2021-07-21 01:12:26 +00:00
},
},
)
2021-08-11 02:54:52 +00:00
except Exception as e:
print(e)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-08-28 11:34:26 +00:00
return redirect(f"/@{username}")
2021-07-21 01:12:26 +00:00
2021-08-31 16:23:34 +00:00
2021-08-31 17:01:09 +00:00
@app.post("/reply")
2021-08-31 16:23:34 +00:00
@auth_required
2021-08-31 17:01:37 +00:00
def messagereply(v):
2021-08-31 16:23:34 +00:00
2021-09-19 13:11:34 +00:00
message = request.values.get("body", "")[:1000].strip()
id = int(request.values.get("parent_id"))
2021-08-31 17:20:10 +00:00
parent = get_comment(id, v=v)
2021-08-31 17:15:46 +00:00
user = parent.author
2021-09-22 22:27:09 +00:00
message = re.sub('([^\n])\n([^\n])', r'\1\n\n\2', message)
2021-08-31 16:23:34 +00:00
# check existing
2021-09-17 08:29:05 +00:00
existing = g.db.query(Comment).join(CommentAux).options(lazyload('*')).filter(Comment.author_id == v.id,
2021-08-31 17:15:46 +00:00
Comment.sentto == user.id,
2021-08-31 16:23:34 +00:00
CommentAux.body == message,
).options(contains_eager(Comment.comment_aux)).first()
if existing:
if existing.parent_comment_id: return redirect(f'/notifications?messages=true#comment-{existing.parent_comment_id}')
else: return redirect(f'/notifications?messages=true#comment-{existing.id}')
2021-09-13 11:56:23 +00:00
text_html = Renderer().render(mistletoe.Document(message))
2021-08-31 21:54:34 +00:00
text_html = sanitize(text_html, True)
2021-08-31 16:23:34 +00:00
new_comment = Comment(author_id=v.id,
parent_submission=None,
parent_comment_id=id,
level=parent.level + 1,
sentto=user.id
)
g.db.add(new_comment)
g.db.flush()
new_aux = CommentAux(id=new_comment.id, body=message, body_html=text_html)
g.db.add(new_aux)
2021-08-31 17:21:11 +00:00
notif = Notification(comment_id=new_comment.id, user_id=user.id)
2021-08-31 16:23:34 +00:00
g.db.add(notif)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-08-31 16:50:06 +00:00
return jsonify({"html": render_template("comments.html",
v=v,
2021-08-31 17:16:22 +00:00
comments=[new_comment],
2021-08-31 16:50:06 +00:00
)})
2021-08-31 16:23:34 +00:00
2021-07-27 22:31:28 +00:00
@app.get("/2faqr/<secret>")
2021-07-21 01:12:26 +00:00
@auth_required
def mfa_qr(secret, v):
x = pyotp.TOTP(secret)
qr = qrcode.QRCode(
error_correction=qrcode.constants.ERROR_CORRECT_L
)
2021-08-04 16:00:57 +00:00
qr.add_data(x.provisioning_uri(v.username, issuer_name=app.config["SITE_NAME"]))
2021-07-23 20:37:33 +00:00
img = qr.make_image(fill_color="#000000", back_color="white")
2021-07-21 01:12:26 +00:00
mem = io.BytesIO()
2021-09-10 03:03:37 +00:00
img.save(mem, format="PNG")
2021-07-21 01:12:26 +00:00
mem.seek(0, 0)
return send_file(mem, mimetype="image/png", as_attachment=False)
2021-07-31 04:48:47 +00:00
@app.get("/is_available/<name>")
2021-07-21 01:12:26 +00:00
@auth_desired
def api_is_available(name, v):
name=name.strip()
if len(name)<3 or len(name)>25:
2021-07-31 05:28:05 +00:00
return {name:False}
2021-07-21 01:12:26 +00:00
name=name.replace('_','\_')
x= g.db.query(User).options(
lazyload('*')
).filter(
or_(
User.username.ilike(name),
User.original_username.ilike(name)
)
).first()
if x:
2021-07-31 05:28:05 +00:00
return {name: False}
2021-07-21 01:12:26 +00:00
else:
2021-07-31 05:28:05 +00:00
return {name: True}
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.get("/id/<id>")
2021-07-25 18:01:43 +00:00
def user_id(id):
2021-07-21 01:12:26 +00:00
2021-07-25 18:01:43 +00:00
user = get_account(int(id))
2021-07-27 00:05:58 +00:00
return redirect(user.url)
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.get("/u/<username>")
2021-07-21 01:12:26 +00:00
def redditor_moment_redirect(username):
return redirect(f"/@{username}")
2021-07-27 22:31:28 +00:00
@app.get("/@<username>/followers")
2021-07-21 01:12:26 +00:00
@auth_required
def followers(username, v):
2021-08-23 17:48:55 +00:00
2021-07-21 01:12:26 +00:00
u = get_user(username, v=v)
users = [x.user for x in u.followers]
return render_template("followers.html", v=v, u=u, users=users)
2021-07-23 18:52:11 +00:00
@app.get("/views")
2021-07-23 15:51:26 +00:00
@auth_required
2021-07-23 18:52:11 +00:00
def visitors(v):
2021-07-23 19:28:25 +00:00
if v.admin_level < 1 and not v.patron: return render_template("errors/patron.html", v=v)
2021-07-23 20:02:47 +00:00
viewers=sorted(v.viewers, key = lambda x: x.last_view_utc, reverse=True)
2021-07-23 15:51:26 +00:00
return render_template("viewers.html", v=v, viewers=viewers)
2021-08-13 01:38:51 +00:00
2021-07-27 22:31:28 +00:00
@app.get("/@<username>")
2021-08-13 01:38:51 +00:00
@app.get("/logged_out/@<username>")
2021-07-21 01:12:26 +00:00
@auth_desired
def u_username(username, v=None):
2021-08-23 17:48:55 +00:00
2021-07-21 01:12:26 +00:00
2021-08-13 01:38:51 +00:00
if not v and "logged_out" not in request.path: return redirect(f"/logged_out/@{username}")
2021-08-13 18:53:11 +00:00
if v and "logged_out" in request.full_path: v = None
2021-08-13 01:55:23 +00:00
2021-07-21 01:12:26 +00:00
# username is unique so at most this returns one result. Otherwise 404
# case insensitive search
u = get_user(username, v=v)
# check for wrong cases
if username != u.username:
return redirect(request.path.replace(username, u.username))
if u.reserved:
2021-07-31 05:59:25 +00:00
if request.headers.get("Authorization"): return {"error": f"That username is reserved for: {u.reserved}"}
2021-07-31 06:00:42 +00:00
else: return render_template("userpage_reserved.html", u=u, v=v)
2021-07-21 01:12:26 +00:00
2021-07-23 15:51:26 +00:00
# viewers
2021-07-23 15:54:07 +00:00
if v and u.id != v.id:
2021-09-17 08:29:05 +00:00
view = g.db.query(ViewerRelationship).options(lazyload('*')).filter(
2021-07-23 15:51:26 +00:00
and_(
ViewerRelationship.viewer_id == v.id,
ViewerRelationship.user_id == u.id
)
).first()
if view:
view.last_view_utc = g.timestamp
else:
view = ViewerRelationship(user_id = u.id,
viewer_id = v.id)
g.db.add(view)
2021-09-16 17:15:07 +00:00
g.db.commit()
2021-07-23 15:51:26 +00:00
2021-07-29 06:29:13 +00:00
2021-07-21 01:12:26 +00:00
if u.is_private and (not v or (v.id != u.id and v.admin_level < 3)):
2021-07-29 06:29:13 +00:00
2021-08-31 21:11:15 +00:00
if v and u.id == 253:
2021-08-31 21:12:10 +00:00
if int(time.time()) - v.rent_utc > 600:
2021-08-31 21:11:15 +00:00
if request.headers.get("Authorization"): return {"error": "That userpage is private"}
2021-08-31 21:30:46 +00:00
else: return render_template("userpage_private.html", time=int(time.time()), u=u, v=v)
2021-08-31 21:11:15 +00:00
else:
if request.headers.get("Authorization"): return {"error": "That userpage is private"}
2021-08-31 21:30:46 +00:00
else: return render_template("userpage_private.html", time=int(time.time()), u=u, v=v)
2021-07-31 05:59:25 +00:00
2021-09-22 18:41:22 +00:00
if hasattr(u, 'is_blocking') and u.is_blocking and (not v or v.admin_level < 3):
2021-07-31 05:59:25 +00:00
if request.headers.get("Authorization"): return {"error": f"You are blocking @{u.username}."}
else: return render_template("userpage_blocking.html", u=u, v=v)
2021-07-21 01:12:26 +00:00
2021-09-22 18:41:22 +00:00
if hasattr(u, 'is_blocked') and u.is_blocked and (not v or v.admin_level < 3):
2021-07-31 05:59:25 +00:00
if request.headers.get("Authorization"): return {"error": "This person is blocking you."}
else: return render_template("userpage_blocked.html", u=u, v=v)
2021-07-21 01:12:26 +00:00
2021-09-19 13:11:34 +00:00
sort = request.values.get("sort", "new")
t = request.values.get("t", "all")
page = int(request.values.get("page", "1"))
2021-07-21 01:12:26 +00:00
page = max(page, 1)
2021-09-14 13:33:10 +00:00
ids = u.userpagelisting(v=v, page=page, sort=sort, t=t)
2021-07-21 01:12:26 +00:00
# we got 26 items just to see if a next page exists
2021-09-10 06:04:28 +00:00
next_exists = (len(ids) > 25)
2021-07-28 10:57:41 +00:00
ids = ids[:25]
2021-07-21 01:12:26 +00:00
# If page 1, check for sticky
if page == 1:
sticky = []
2021-09-17 08:29:05 +00:00
sticky = g.db.query(Submission).options(lazyload('*')).filter_by(is_pinned=True, author_id=u.id).all()
2021-07-21 01:12:26 +00:00
if sticky:
for p in sticky:
ids = [p.id] + ids
2021-07-25 02:55:25 +00:00
listing = get_posts(ids, v=v)
2021-07-21 01:12:26 +00:00
if u.unban_utc:
2021-07-31 06:13:16 +00:00
if request.headers.get("Authorization"): {"data": [x.json for x in listing]}
2021-07-31 05:59:25 +00:00
else: return render_template("userpage.html",
2021-07-31 06:13:16 +00:00
unban=u.unban_string,
u=u,
v=v,
listing=listing,
page=page,
sort=sort,
t=t,
next_exists=next_exists,
is_following=(v and u.has_follower(v)))
if request.headers.get("Authorization"): return {"data": [x.json for x in listing]}
else: return render_template("userpage.html",
u=u,
v=v,
listing=listing,
page=page,
sort=sort,
t=t,
next_exists=next_exists,
is_following=(v and u.has_follower(v)))
2021-07-31 05:59:25 +00:00
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.get("/@<username>/comments")
2021-08-13 01:38:51 +00:00
@app.get("/logged_out/@<username>/comments")
2021-07-21 01:12:26 +00:00
@auth_desired
def u_username_comments(username, v=None):
2021-08-23 17:48:55 +00:00
2021-07-21 01:12:26 +00:00
2021-08-13 01:38:51 +00:00
if not v and "logged_out" not in request.path: return redirect(f"/logged_out/@{username}/comments")
2021-08-13 18:53:11 +00:00
if v and "logged_out" in request.full_path: v = None
2021-08-13 01:55:23 +00:00
2021-07-21 01:12:26 +00:00
# username is unique so at most this returns one result. Otherwise 404
# case insensitive search
user = get_user(username, v=v)
# check for wrong cases
if username != user.username: return redirect(f'{user.url}/comments')
u = user
if u.reserved:
2021-07-31 05:59:25 +00:00
if request.headers.get("Authorization"): return {"error": f"That username is reserved for: {u.reserved}"}
else: return render_template("userpage_reserved.html",
2021-07-21 01:12:26 +00:00
u=u,
2021-07-31 05:59:25 +00:00
v=v)
2021-07-21 01:12:26 +00:00
if u.is_private and (not v or (v.id != u.id and v.admin_level < 3)):
2021-08-31 21:11:15 +00:00
if v and u.id == 253:
2021-08-31 21:12:10 +00:00
if int(time.time()) - v.rent_utc > 600:
2021-08-31 21:11:15 +00:00
if request.headers.get("Authorization"): return {"error": "That userpage is private"}
2021-08-31 21:30:46 +00:00
else: return render_template("userpage_private.html", time=int(time.time()), u=u, v=v)
2021-08-31 21:11:15 +00:00
else:
if request.headers.get("Authorization"): return {"error": "That userpage is private"}
2021-08-31 21:30:46 +00:00
else: return render_template("userpage_private.html", time=int(time.time()), u=u, v=v)
2021-07-21 01:12:26 +00:00
2021-09-22 18:41:22 +00:00
if hasattr(u, 'is_blocking') and u.is_blocking and (not v or v.admin_level < 3):
2021-07-31 05:59:25 +00:00
if request.headers.get("Authorization"): return {"error": f"You are blocking @{u.username}."}
else: return render_template("userpage_blocking.html",
u=u,
v=v)
2021-07-21 01:12:26 +00:00
2021-09-22 18:41:22 +00:00
if hasattr(u, 'is_blocked') and u.is_blocked and (not v or v.admin_level < 3):
2021-07-31 05:59:25 +00:00
if request.headers.get("Authorization"): return {"error": "This person is blocking you."}
else: return render_template("userpage_blocked.html",
u=u,
v=v)
2021-07-21 01:12:26 +00:00
2021-09-19 13:11:34 +00:00
page = int(request.values.get("page", "1"))
sort=request.values.get("sort","new")
t=request.values.get("t","all")
2021-07-21 01:12:26 +00:00
2021-09-14 04:49:25 +00:00
comments = u.comments.options(lazyload('*')).filter(Comment.parent_submission != None)
if (not v) or (v.id != u.id and v.admin_level == 0):
comments = comments.filter(Comment.deleted_utc == 0)
comments = comments.filter(Comment.is_banned == False)
now = int(time.time())
if t == 'hour':
cutoff = now - 3600
elif t == 'day':
cutoff = now - 86400
elif t == 'week':
cutoff = now - 604800
elif t == 'month':
cutoff = now - 2592000
elif t == 'year':
cutoff = now - 31536000
else:
cutoff = 0
comments = comments.filter(Comment.created_utc >= cutoff)
if sort == "new":
comments = comments.order_by(Comment.created_utc.desc()).all()
elif sort == "old":
comments = comments.order_by(Comment.created_utc.asc()).all()
elif sort == "controversial":
comments = sorted(comments.all(), key=lambda x: x.score_disputed, reverse=True)
elif sort == "top":
comments = sorted(comments.all(), key=lambda x: x.score, reverse=True)
elif sort == "bottom":
comments = sorted(comments.all(), key=lambda x: x.score)
firstrange = 25 * (page - 1)
secondrange = firstrange + 26
ids = [x.id for x in comments[firstrange:secondrange]]
2021-07-21 01:12:26 +00:00
# we got 26 items just to see if a next page exists
2021-09-10 06:04:28 +00:00
next_exists = (len(ids) > 25)
2021-07-28 10:57:41 +00:00
ids = ids[:25]
2021-07-21 01:12:26 +00:00
2021-09-14 18:18:28 +00:00
listing = get_comments(ids, v=v)
2021-07-21 01:12:26 +00:00
is_following = (v and user.has_follower(v))
2021-07-31 06:05:09 +00:00
if request.headers.get("Authorization"): return {"data": [c.json for c in listing]}
2021-07-31 05:28:05 +00:00
else: return render_template("userpage_comments.html", u=user, v=v, listing=listing, page=page, sort=sort, t=t,next_exists=next_exists, is_following=is_following, standalone=True)
2021-07-21 01:12:26 +00:00
2021-09-14 17:35:24 +00:00
2021-07-31 04:48:47 +00:00
@app.get("/@<username>/info")
2021-07-21 01:12:26 +00:00
@auth_desired
def u_username_info(username, v=None):
user=get_user(username, v=v)
2021-09-22 18:41:22 +00:00
if hasattr(user, 'is_blocking') and user.is_blocking:
2021-07-31 05:28:05 +00:00
return {"error": "You're blocking this user."}, 401
2021-09-22 18:41:22 +00:00
elif hasattr(user, 'is_blocked') and user.is_blocked:
2021-07-31 05:28:05 +00:00
return {"error": "This user is blocking you."}, 403
2021-07-21 01:12:26 +00:00
2021-07-31 05:28:05 +00:00
return user.json
2021-07-21 01:12:26 +00:00
2021-07-31 04:48:47 +00:00
@app.post("/follow/<username>")
2021-07-21 01:12:26 +00:00
@auth_required
def follow_user(username, v):
target = get_user(username)
2021-07-31 05:28:05 +00:00
if target.id==v.id: return {"error": "You can't follow yourself!"}, 400
2021-07-21 01:12:26 +00:00
# check for existing follow
2021-09-17 08:29:05 +00:00
if g.db.query(Follow).options(lazyload('*')).filter_by(user_id=v.id, target_id=target.id).first(): return {"message": "User followed!"}
2021-07-21 01:12:26 +00:00
2021-07-29 00:11:37 +00:00
new_follow = Follow(user_id=v.id, target_id=target.id)
2021-07-21 01:12:26 +00:00
g.db.add(new_follow)
2021-07-29 00:11:37 +00:00
2021-09-21 22:55:39 +00:00
g.db.flush()
2021-09-17 08:29:05 +00:00
target.stored_subscriber_count = g.db.query(Follow).options(lazyload('*')).filter_by(target_id=target.id).count()
2021-07-21 01:12:26 +00:00
g.db.add(target)
2021-09-17 08:29:05 +00:00
existing = g.db.query(Notification).options(lazyload('*')).filter_by(followsender=v.id, user_id=target.id).first()
2021-07-21 01:12:26 +00:00
if not existing: send_follow_notif(v.id, target.id, f"@{v.username} has followed you!")
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-09-08 11:05:31 +00:00
return {"message": "User followed!"}
2021-07-21 01:12:26 +00:00
2021-07-31 04:48:47 +00:00
@app.post("/unfollow/<username>")
2021-07-21 01:12:26 +00:00
@auth_required
def unfollow_user(username, v):
target = get_user(username)
# check for existing follow
2021-09-17 08:29:05 +00:00
follow = g.db.query(Follow).options(lazyload('*')).filter_by(user_id=v.id, target_id=target.id).first()
2021-07-21 01:12:26 +00:00
2021-09-08 11:05:31 +00:00
if not follow: return {"message": "User unfollowed!"}
2021-07-21 01:12:26 +00:00
g.db.delete(follow)
2021-09-21 22:55:39 +00:00
g.db.flush()
2021-09-17 08:29:05 +00:00
target.stored_subscriber_count = g.db.query(Follow).options(lazyload('*')).filter_by(target_id=target.id).count()
2021-07-26 02:17:14 +00:00
g.db.add(target)
2021-07-21 01:12:26 +00:00
2021-09-17 08:29:05 +00:00
existing = g.db.query(Notification).options(lazyload('*')).filter_by(unfollowsender=v.id, user_id=target.id).first()
2021-07-21 01:12:26 +00:00
if not existing: send_unfollow_notif(v.id, target.id, f"@{v.username} has unfollowed you!")
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-09-12 02:59:52 +00:00
return {"message": "User unfollowed!"}
2021-07-21 01:12:26 +00:00
2021-08-02 07:00:38 +00:00
@app.route("/uid/<id>/pic/profile")
2021-07-21 01:12:26 +00:00
@limiter.exempt
2021-08-02 07:00:38 +00:00
def user_profile_uid(id):
try: id = int(id)
2021-07-31 06:59:18 +00:00
except:
2021-08-02 07:00:38 +00:00
try: id = int(id, 36)
2021-07-31 06:59:18 +00:00
except: abort(404)
2021-08-02 07:00:38 +00:00
x=get_account(id)
2021-07-21 01:12:26 +00:00
return redirect(x.profile_url)
2021-07-30 06:05:38 +00:00
2021-07-27 22:31:28 +00:00
@app.get("/@<username>/saved/posts")
2021-07-21 01:12:26 +00:00
@auth_required
def saved_posts(v, username):
2021-09-19 13:11:34 +00:00
page=int(request.values.get("page",1))
2021-07-21 01:12:26 +00:00
ids=v.saved_idlist(page=page)
next_exists=len(ids)==26
2021-07-28 10:57:41 +00:00
ids=ids[:25]
2021-07-21 01:12:26 +00:00
2021-07-25 02:55:25 +00:00
listing = get_posts(ids, v=v)
2021-07-21 01:12:26 +00:00
2021-07-31 06:05:09 +00:00
if request.headers.get("Authorization"): return {"data": [x.json for x in listing]}
2021-07-31 05:59:25 +00:00
else: return render_template("userpage.html",
2021-07-21 01:12:26 +00:00
u=v,
v=v,
listing=listing,
page=page,
next_exists=next_exists,
2021-07-31 05:59:25 +00:00
)
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.get("/@<username>/saved/comments")
2021-07-21 01:12:26 +00:00
@auth_required
def saved_comments(v, username):
2021-09-19 13:11:34 +00:00
page=int(request.values.get("page",1))
2021-07-21 01:12:26 +00:00
ids=v.saved_comment_idlist(page=page)
next_exists=len(ids)==26
2021-07-28 10:57:41 +00:00
ids=ids[:25]
2021-07-21 01:12:26 +00:00
2021-07-29 07:00:10 +00:00
listing = get_comments(ids, v=v)
2021-07-21 01:12:26 +00:00
2021-07-31 06:05:09 +00:00
if request.headers.get("Authorization"): return {"data": [x.json for x in listing]}
2021-07-31 05:59:25 +00:00
else: return render_template("userpage_comments.html",
2021-07-21 01:12:26 +00:00
u=v,
v=v,
listing=listing,
page=page,
next_exists=next_exists,
standalone=True)