rDrama/files/routes/users.py

1048 lines
38 KiB
Python
Raw Normal View History

2021-10-15 14:08:27 +00:00
import qrcode
import io
import time
import math
from files.classes.user import ViewerRelationship
from files.helpers.alerts import *
from files.helpers.sanitize import *
from files.helpers.const import *
from files.mail import *
from flask import *
2022-01-11 20:42:35 +00:00
from files.__main__ import app, limiter, db_session
2021-10-15 14:08:27 +00:00
from pusher_push_notifications import PushNotifications
2021-11-15 01:18:16 +00:00
from collections import Counter
2022-01-18 17:07:45 +00:00
import gevent
2021-10-15 14:08:27 +00:00
2022-01-18 11:19:32 +00:00
if PUSHER_ID: beams_client = PushNotifications(instance_id=PUSHER_ID, secret_key=PUSHER_KEY)
2021-11-15 01:18:16 +00:00
2022-01-18 17:07:45 +00:00
def leaderboard_thread():
2022-01-23 23:06:34 +00:00
global users9, users9_25, users13, users13_25
2022-01-18 17:07:45 +00:00
db = db_session()
votes1 = db.query(Submission.author_id, func.count(Submission.author_id)).join(Vote, Vote.submission_id==Submission.id).filter(Vote.vote_type==-1).group_by(Submission.author_id).order_by(func.count(Submission.author_id).desc()).all()
votes2 = db.query(Comment.author_id, func.count(Comment.author_id)).join(CommentVote, CommentVote.comment_id==Comment.id).filter(CommentVote.vote_type==-1).group_by(Comment.author_id).order_by(func.count(Comment.author_id).desc()).all()
votes3 = Counter(dict(votes1)) + Counter(dict(votes2))
users8 = db.query(User).filter(User.id.in_(votes3.keys())).all()
users9 = []
for user in users8: users9.append((user, votes3[user.id]))
users9 = sorted(users9, key=lambda x: x[1], reverse=True)
2022-01-21 12:30:29 +00:00
users9_25 = users9[:25]
2022-01-18 17:07:45 +00:00
votes1 = db.query(Vote.user_id, func.count(Vote.user_id)).filter(Vote.vote_type==1).group_by(Vote.user_id).order_by(func.count(Vote.user_id).desc()).all()
votes2 = db.query(CommentVote.user_id, func.count(CommentVote.user_id)).filter(CommentVote.vote_type==1).group_by(CommentVote.user_id).order_by(func.count(CommentVote.user_id).desc()).all()
votes3 = Counter(dict(votes1)) + Counter(dict(votes2))
users14 = db.query(User).filter(User.id.in_(votes3.keys())).all()
2022-01-23 23:06:34 +00:00
users13 = []
2022-01-18 17:07:45 +00:00
for user in users14:
2022-01-23 23:06:34 +00:00
users13.append((user, votes3[user.id]-user.post_count-user.comment_count))
users13 = sorted(users13, key=lambda x: x[1], reverse=True)
users13_25 = users13[:25]
2022-01-18 17:07:45 +00:00
db.close()
2022-01-19 07:33:21 +00:00
gevent.spawn(leaderboard_thread())
2022-01-03 10:39:41 +00:00
@app.get("/grassed")
2022-01-11 21:54:41 +00:00
@auth_required
2022-01-03 10:39:41 +00:00
def grassed(v):
users = g.db.query(User).filter(User.ban_reason.like('grass award used by @%')).all()
2022-01-14 12:04:34 +00:00
return render_template("grassed.html", v=v, users=users)
2022-01-03 10:39:41 +00:00
@app.get("/agendaposters")
2022-01-11 21:54:41 +00:00
@auth_required
2022-01-03 10:39:41 +00:00
def agendaposters(v):
2022-01-03 11:05:25 +00:00
users = [x for x in g.db.query(User).filter_by(agendaposter = True).order_by(User.username).all()]
2022-01-14 12:04:34 +00:00
return render_template("agendaposters.html", v=v, users=users)
2022-01-03 10:39:41 +00:00
2021-11-15 01:18:16 +00:00
@app.get("/@<username>/upvoters")
2022-01-11 21:54:41 +00:00
@auth_required
2021-11-15 01:18:16 +00:00
def upvoters(v, username):
2021-11-15 02:11:13 +00:00
id = get_user(username).id
2021-11-15 01:18:16 +00:00
2021-11-27 00:31:51 +00:00
votes = g.db.query(Vote.user_id, func.count(Vote.user_id)).join(Submission, Vote.submission_id==Submission.id).filter(Vote.vote_type==1, Submission.author_id==id).group_by(Vote.user_id).order_by(func.count(Vote.user_id).desc()).all()
2021-11-15 01:18:16 +00:00
2021-11-27 00:31:51 +00:00
votes2 = g.db.query(CommentVote.user_id, func.count(CommentVote.user_id)).join(Comment, CommentVote.comment_id==Comment.id).filter(CommentVote.vote_type==1, Comment.author_id==id).group_by(CommentVote.user_id).order_by(func.count(CommentVote.user_id).desc()).all()
2021-11-15 01:18:16 +00:00
votes = Counter(dict(votes)) + Counter(dict(votes2))
users = g.db.query(User).filter(User.id.in_(votes.keys())).all()
users2 = []
for user in users: users2.append((user, votes[user.id]))
2022-01-31 01:41:04 +00:00
users = sorted(users2, key=lambda x: x[1], reverse=True)
try:
pos = [x[0].id for x in users].index(v.id)
pos = (pos+1, users[pos][1])
except: pos = (len(users)+1, 0)
2021-11-15 01:18:16 +00:00
2022-01-31 01:41:04 +00:00
return render_template("voters.html", v=v, users=users[:25], pos=pos, name='Up', name2=f'@{username} biggest simps')
2021-11-15 01:18:16 +00:00
@app.get("/@<username>/downvoters")
2022-01-11 21:54:41 +00:00
@auth_required
2021-11-15 01:18:16 +00:00
def downvoters(v, username):
2021-11-15 02:11:13 +00:00
id = get_user(username).id
2021-11-15 01:18:16 +00:00
2021-11-27 00:31:51 +00:00
votes = g.db.query(Vote.user_id, func.count(Vote.user_id)).join(Submission, Vote.submission_id==Submission.id).filter(Vote.vote_type==-1, Submission.author_id==id).group_by(Vote.user_id).order_by(func.count(Vote.user_id).desc()).all()
2021-11-15 01:18:16 +00:00
2021-11-27 00:31:51 +00:00
votes2 = g.db.query(CommentVote.user_id, func.count(CommentVote.user_id)).join(Comment, CommentVote.comment_id==Comment.id).filter(CommentVote.vote_type==-1, Comment.author_id==id).group_by(CommentVote.user_id).order_by(func.count(CommentVote.user_id).desc()).all()
2021-11-15 01:18:16 +00:00
votes = Counter(dict(votes)) + Counter(dict(votes2))
users = g.db.query(User).filter(User.id.in_(votes.keys())).all()
users2 = []
for user in users: users2.append((user, votes[user.id]))
2022-01-31 01:41:04 +00:00
users = sorted(users2, key=lambda x: x[1], reverse=True)
try:
pos = [x[0].id for x in users].index(v.id)
pos = (pos+1, users[pos][1])
except: pos = (len(users)+1, 0)
2021-11-15 01:18:16 +00:00
2022-01-31 01:41:04 +00:00
return render_template("voters.html", v=v, users=users[:25], pos=pos, name='Down', name2=f'@{username} biggest haters')
2021-11-15 23:13:29 +00:00
@app.get("/@<username>/upvoting")
2022-01-11 21:54:41 +00:00
@auth_required
2021-11-15 23:13:29 +00:00
def upvoting(v, username):
id = get_user(username).id
2022-01-22 10:14:15 +00:00
votes = g.db.query(Submission.author_id, func.count(Submission.author_id)).join(Vote, Vote.submission_id==Submission.id).filter(Submission.ghost==None, Vote.vote_type==1, Vote.user_id==id).group_by(Submission.author_id).order_by(func.count(Submission.author_id).desc()).all()
2021-11-15 23:13:29 +00:00
2022-01-22 10:14:15 +00:00
votes2 = g.db.query(Comment.author_id, func.count(Comment.author_id)).join(CommentVote, CommentVote.comment_id==Comment.id).filter(Comment.ghost==None, CommentVote.vote_type==1, CommentVote.user_id==id).group_by(Comment.author_id).order_by(func.count(Comment.author_id).desc()).all()
2021-11-15 23:13:29 +00:00
2022-01-21 17:21:46 +00:00
votes = Counter(dict(votes)) + Counter(dict(votes2))
2021-11-15 23:13:29 +00:00
users = g.db.query(User).filter(User.id.in_(votes.keys())).all()
users2 = []
for user in users: users2.append((user, votes[user.id]))
2022-01-31 01:41:04 +00:00
users = sorted(users2, key=lambda x: x[1], reverse=True)
try:
pos = [x[0].id for x in users].index(v.id)
pos = (pos+1, users[pos][1])
except: pos = (len(users)+1, 0)
2021-11-15 23:13:29 +00:00
2022-01-31 01:41:04 +00:00
return render_template("voters.html", v=v, users=users[:25], pos=pos, name='Up', name2=f'Who @{username} simps for')
2021-11-15 23:13:29 +00:00
@app.get("/@<username>/downvoting")
2022-01-11 21:54:41 +00:00
@auth_required
2021-11-15 23:13:29 +00:00
def downvoting(v, username):
id = get_user(username).id
2022-01-22 10:14:15 +00:00
votes = g.db.query(Submission.author_id, func.count(Submission.author_id)).join(Vote, Vote.submission_id==Submission.id).filter(Submission.ghost==None, Vote.vote_type==-1, Vote.user_id==id).group_by(Submission.author_id).order_by(func.count(Submission.author_id).desc()).all()
2021-11-15 23:13:29 +00:00
2022-01-22 10:14:15 +00:00
votes2 = g.db.query(Comment.author_id, func.count(Comment.author_id)).join(CommentVote, CommentVote.comment_id==Comment.id).filter(Comment.ghost==None, CommentVote.vote_type==-1, CommentVote.user_id==id).group_by(Comment.author_id).order_by(func.count(Comment.author_id).desc()).all()
2021-11-15 23:13:29 +00:00
2022-01-21 17:21:46 +00:00
votes = Counter(dict(votes)) + Counter(dict(votes2))
2021-11-15 23:13:29 +00:00
users = g.db.query(User).filter(User.id.in_(votes.keys())).all()
users2 = []
for user in users: users2.append((user, votes[user.id]))
2022-01-31 01:41:04 +00:00
users = sorted(users2, key=lambda x: x[1], reverse=True)
try:
pos = [x[0].id for x in users].index(v.id)
pos = (pos+1, users[pos][1])
except: pos = (len(users)+1, 0)
2021-11-15 23:13:29 +00:00
2022-01-31 01:41:04 +00:00
return render_template("voters.html", v=v, users=users[:25], pos=pos, name='Down', name2=f'Who @{username} hates')
2021-10-15 14:08:27 +00:00
@app.post("/pay_rent")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2021-10-15 14:08:27 +00:00
@auth_required
def pay_rent(v):
2022-01-03 11:37:39 +00:00
if v.coins < 500: return {"error":"You must have more than 500 coins."}
2021-10-15 14:08:27 +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)
2021-12-20 20:03:59 +00:00
send_repeatable_notification(u.id, f"@{v.username} has paid rent!")
2021-10-15 14:08:27 +00:00
g.db.commit()
return {"message": "Rent paid!"}
@app.post("/steal")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-01-06 16:46:09 +00:00
@auth_required
2021-10-15 14:08:27 +00:00
def steal(v):
if int(time.time()) - v.created_utc < 604800:
2021-12-29 12:38:54 +00:00
return {"error":"You must have an account older than 1 week in order to attempt stealing."}
2021-10-15 14:08:27 +00:00
if v.coins < 5000:
2021-12-29 12:38:54 +00:00
return {"error":"You must have more than 5000 coins in order to attempt stealing."}
2021-10-15 14:08:27 +00:00
u = get_account(253)
if random.randint(1, 10) < 5:
v.coins += 700
v.steal_utc = int(time.time())
g.db.add(v)
u.coins -= 700
g.db.add(u)
2021-12-28 13:53:08 +00:00
send_repeatable_notification(u.id, f"Some [grubby little rentoid](/@{v.username}) has absconded with 700 of your hard-earned coins to fuel his Funko Pop addiction. Stop being so trusting.")
2022-01-07 21:03:14 +00:00
send_repeatable_notification(v.id, "You have successfully shorted your heroic landlord 700 coins in rent. You're slightly less materially poor, but somehow even moreso morally. Are you proud of yourself?")
2021-10-15 14:08:27 +00:00
g.db.commit()
return {"message": "Attempt successful!"}
else:
if random.random() < 0.15:
2021-12-28 13:53:08 +00:00
send_repeatable_notification(u.id, 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 coins and called the cops. He was sentenced to one (1) day in renthog prison.")
2022-01-07 21:03:14 +00:00
send_repeatable_notification(v.id, "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 coins and sentenced to one (1) day in renthog prison.")
2021-10-15 14:08:27 +00:00
v.ban(days=1, reason="Jailed thief")
v.fail_utc = int(time.time())
else:
2021-12-20 20:03:59 +00:00
send_repeatable_notification(u.id, 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.")
2022-01-07 21:03:14 +00:00
send_repeatable_notification(v.id, "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.")
2021-10-15 14:08:27 +00:00
v.fail2_utc = int(time.time())
v.coins -= 500
g.db.add(v)
u.coins += 500
g.db.add(u)
g.db.commit()
return {"message": "Attempt failed!"}
@app.get("/rentoids")
2022-01-11 21:54:41 +00:00
@auth_required
2021-10-15 14:08:27 +00:00
def rentoids(v):
2022-01-12 04:24:05 +00:00
users = g.db.query(User).filter(User.rent_utc > 0).all()
2022-01-14 12:04:34 +00:00
return render_template("rentoids.html", v=v, users=users)
2021-10-15 14:08:27 +00:00
@app.get("/thiefs")
2022-01-11 21:54:41 +00:00
@auth_required
2021-10-15 14:08:27 +00:00
def thiefs(v):
2022-01-12 04:24:05 +00:00
successful = g.db.query(User).filter(User.steal_utc > 0).all()
failed = g.db.query(User).filter(User.fail_utc > 0).all()
failed2 = g.db.query(User).filter(User.fail2_utc > 0).all()
2022-01-14 12:04:34 +00:00
return render_template("thiefs.html", v=v, successful=successful, failed=failed, failed2=failed2)
2021-10-15 14:08:27 +00:00
@app.post("/@<username>/suicide")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2021-10-15 14:08:27 +00:00
@auth_required
def suicide(v, username):
t = int(time.time())
if v.admin_level == 0 and t - v.suicide_utc < 86400: return {"message": "You're on 1-day cooldown!"}
user = get_user(username)
2022-01-04 13:18:37 +00:00
suicide = f"Hi there,\n\nA [concerned user](/id/{v.id}) 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 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-12-20 20:03:59 +00:00
send_repeatable_notification(user.id, suicide)
2021-10-15 14:08:27 +00:00
v.suicide_utc = t
g.db.add(v)
g.db.commit()
return {"message": "Help message sent!"}
@app.get("/@<username>/coins")
@auth_required
def get_coins(v, username):
user = get_user(username)
if user != None: return {"coins": user.coins}, 200
else: return {"error": "invalid_user"}, 404
@app.post("/@<username>/transfer_coins")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-01-06 16:46:09 +00:00
@is_not_permabanned
2021-10-15 14:08:27 +00:00
def transfer_coins(v, username):
2022-01-02 00:06:46 +00:00
receiver = g.db.query(User).filter_by(username=username).one_or_none()
2021-10-15 14:08:27 +00:00
if receiver is None: return {"error": "That user doesn't exist."}, 404
if receiver.id != v.id:
amount = request.values.get("amount", "").strip()
amount = int(amount) if amount.isdigit() else None
2022-01-19 12:44:05 +00:00
if amount is None or amount <= 0: return {"error": f"Invalid amount of coins."}, 400
if v.coins < amount: return {"error": f"You don't have enough coins."}, 400
if amount < 100: return {"error": f"You have to gift at least 100 coins."}, 400
2021-10-15 14:08:27 +00:00
2022-01-25 03:37:55 +00:00
if not v.patron and not receiver.patron and not v.alts_patron and not receiver.alts_patron: tax = math.ceil(amount*0.03)
else: tax = 0
2021-11-18 14:43:50 +00:00
2022-01-23 16:54:57 +00:00
log_message = f"@{v.username} has transferred {amount} coins to @{receiver.username}"
2022-01-24 15:44:27 +00:00
send_repeatable_notification(TAX_NOTIF_ID, log_message)
2022-01-17 12:06:26 +00:00
2021-11-07 13:53:42 +00:00
receiver.coins += amount-tax
2021-10-24 18:59:50 +00:00
v.coins -= amount
2022-01-19 12:44:05 +00:00
send_repeatable_notification(receiver.id, f":marseycapitalistmanlet: @{v.username} has gifted you {amount-tax} coins!")
2021-10-24 18:59:50 +00:00
g.db.add(receiver)
g.db.add(v)
2021-10-15 14:08:27 +00:00
g.db.commit()
2022-01-19 12:44:05 +00:00
return {"message": f"{amount-tax} coins transferred!"}, 200
2021-10-15 14:08:27 +00:00
2022-01-19 12:44:05 +00:00
return {"message": f"You can't transfer coins to yourself!"}, 400
2021-10-15 14:08:27 +00:00
2021-12-23 14:55:44 +00:00
@app.post("/@<username>/transfer_bux")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-01-06 16:46:09 +00:00
@is_not_permabanned
2021-12-23 15:00:07 +00:00
def transfer_bux(v, username):
2022-01-02 00:06:46 +00:00
receiver = g.db.query(User).filter_by(username=username).one_or_none()
2021-12-23 14:55:44 +00:00
if not receiver: return {"error": "That user doesn't exist."}, 404
if receiver.id != v.id:
amount = request.values.get("amount", "").strip()
amount = int(amount) if amount.isdigit() else None
2022-01-07 21:03:14 +00:00
if not amount or amount < 0: return {"error": "Invalid amount of marseybux."}, 400
if v.procoins < amount: return {"error": "You don't have enough marseybux"}, 400
if amount < 100: return {"error": "You have to gift at least 100 marseybux."}, 400
2021-12-23 14:55:44 +00:00
2022-01-23 16:54:57 +00:00
log_message = f"@{v.username} has transferred {amount} Marseybux to @{receiver.username}"
send_repeatable_notification(CARP_ID, log_message)
2022-01-17 17:28:28 +00:00
2021-12-23 14:55:44 +00:00
receiver.procoins += amount
v.procoins -= amount
2022-01-24 15:44:27 +00:00
send_repeatable_notification(TAX_NOTIF_ID, f":marseycapitalistmanlet: @{v.username} has gifted you {amount} marseybux!")
2021-12-23 14:55:44 +00:00
g.db.add(receiver)
g.db.add(v)
g.db.commit()
return {"message": f"{amount} marseybux transferred!"}, 200
2022-01-07 21:03:14 +00:00
return {"message": "You can't transfer marseybux to yourself!"}, 400
2021-12-23 14:55:44 +00:00
2021-10-15 14:08:27 +00:00
@app.get("/leaderboard")
2022-01-11 21:54:41 +00:00
@auth_required
2021-10-15 14:08:27 +00:00
def leaderboard(v):
2022-01-19 06:43:15 +00:00
users = g.db.query(User)
users1 = users.order_by(User.coins.desc()).limit(25).all()
2022-01-18 12:49:55 +00:00
sq = g.db.query(User.id, func.rank().over(order_by=User.coins.desc()).label("rank")).subquery()
pos1 = g.db.query(sq.c.id, sq.c.rank).filter(sq.c.id == v.id).limit(1).one()[1]
2022-01-18 12:24:16 +00:00
2022-01-19 08:14:33 +00:00
users2 = users.order_by(User.stored_subscriber_count.desc()).limit(25).all()
2022-01-18 12:51:33 +00:00
sq = g.db.query(User.id, func.rank().over(order_by=User.stored_subscriber_count.desc()).label("rank")).subquery()
pos2 = g.db.query(sq.c.id, sq.c.rank).filter(sq.c.id == v.id).limit(1).one()[1]
2022-01-18 12:08:32 +00:00
2022-01-19 08:14:33 +00:00
users3 = users.order_by(User.post_count.desc()).limit(25).all()
2022-01-18 12:51:33 +00:00
sq = g.db.query(User.id, func.rank().over(order_by=User.post_count.desc()).label("rank")).subquery()
pos3 = g.db.query(sq.c.id, sq.c.rank).filter(sq.c.id == v.id).limit(1).one()[1]
2022-01-18 12:08:32 +00:00
2022-01-19 08:14:33 +00:00
users4 = users.order_by(User.comment_count.desc()).limit(25).all()
2022-01-18 12:51:33 +00:00
sq = g.db.query(User.id, func.rank().over(order_by=User.comment_count.desc()).label("rank")).subquery()
pos4 = g.db.query(sq.c.id, sq.c.rank).filter(sq.c.id == v.id).limit(1).one()[1]
2022-01-18 12:08:32 +00:00
2022-01-19 08:14:33 +00:00
users5 = users.order_by(User.received_award_count.desc()).limit(25).all()
2022-01-18 12:51:33 +00:00
sq = g.db.query(User.id, func.rank().over(order_by=User.received_award_count.desc()).label("rank")).subquery()
pos5 = g.db.query(sq.c.id, sq.c.rank).filter(sq.c.id == v.id).limit(1).one()[1]
2022-01-18 12:08:32 +00:00
if request.host == 'pcmemes.net':
2022-01-19 08:14:33 +00:00
users6 = users.order_by(User.basedcount.desc()).limit(25).all()
2022-01-18 12:51:33 +00:00
sq = g.db.query(User.id, func.rank().over(order_by=User.basedcount.desc()).label("rank")).subquery()
pos6 = g.db.query(sq.c.id, sq.c.rank).filter(sq.c.id == v.id).limit(1).one()[1]
2022-01-19 08:14:33 +00:00
else:
users6 = None
pos6 = None
users7 = users.order_by(User.coins_spent.desc()).limit(25).all()
2022-01-18 12:51:33 +00:00
sq = g.db.query(User.id, func.rank().over(order_by=User.coins_spent.desc()).label("rank")).subquery()
pos7 = g.db.query(sq.c.id, sq.c.rank).filter(sq.c.id == v.id).limit(1).one()[1]
2022-01-18 12:08:32 +00:00
2022-01-19 06:59:46 +00:00
try:
pos9 = [x[0].id for x in users9].index(v.id)
pos9 = (pos9+1, users9[pos9][1])
except: pos9 = (len(users9)+1, 0)
2022-01-18 16:17:02 +00:00
2022-01-19 08:14:33 +00:00
users10 = users.order_by(User.truecoins.desc()).limit(25).all()
sq = g.db.query(User.id, func.rank().over(order_by=User.truecoins.desc()).label("rank")).subquery()
pos10 = g.db.query(sq.c.id, sq.c.rank).filter(sq.c.id == v.id).limit(1).one()[1]
2022-01-19 11:45:12 +00:00
sq = g.db.query(Badge.user_id, func.count(Badge.user_id).label("count"), func.rank().over(order_by=func.count(Badge.user_id).desc()).label("rank")).group_by(Badge.user_id).subquery()
2022-01-23 23:06:34 +00:00
users11 = g.db.query(User, sq.c.count).join(sq, User.id==sq.c.user_id).order_by(sq.c.count.desc())
2022-01-19 14:14:38 +00:00
pos11 = g.db.query(User.id, sq.c.rank, sq.c.count).join(sq, User.id==sq.c.user_id).filter(User.id == v.id).one_or_none()
if pos11: pos11 = (pos11[1],pos11[2])
2022-01-23 23:06:34 +00:00
else: pos11 = (users11.count()+1, 0)
users11 = users11.limit(25).all()
2022-01-19 10:08:30 +00:00
2022-01-24 20:04:23 +00:00
if SITE_NAME == 'Drama':
2022-01-23 23:06:34 +00:00
sq = g.db.query(Marsey.author_id, func.count(Marsey.author_id).label("count"), func.rank().over(order_by=func.count(Marsey.author_id).desc()).label("rank")).group_by(Marsey.author_id).subquery()
users12 = g.db.query(User, sq.c.count).join(sq, User.id==sq.c.author_id).order_by(sq.c.count.desc())
pos12 = g.db.query(User.id, sq.c.rank, sq.c.count).join(sq, User.id==sq.c.author_id).filter(User.id == v.id).one_or_none()
if pos12: pos12 = (pos12[1],pos12[2])
else: pos12 = (users12.count()+1, 0)
users12 = users12.limit(25).all()
else:
users12 = None
pos12 = None
2022-01-18 16:19:49 +00:00
2022-01-19 06:59:46 +00:00
try:
2022-01-23 23:06:34 +00:00
pos13 = [x[0].id for x in users13].index(v.id)
pos13 = (pos13+1, users13[pos13][1])
except: pos13 = (len(users13)+1, 0)
2022-01-18 15:56:42 +00:00
2022-01-25 01:59:58 +00:00
users14 = users.order_by(User.winnings.desc()).limit(25).all()
sq = g.db.query(User.id, func.rank().over(order_by=User.winnings.desc()).label("rank")).subquery()
pos14 = g.db.query(sq.c.id, sq.c.rank).filter(sq.c.id == v.id).limit(1).one()[1]
users15 = users.order_by(User.winnings).limit(25).all()
sq = g.db.query(User.id, func.rank().over(order_by=User.winnings).label("rank")).subquery()
pos15 = g.db.query(sq.c.id, sq.c.rank).filter(sq.c.id == v.id).limit(1).one()[1]
return render_template("leaderboard.html", v=v, users1=users1, pos1=pos1, users2=users2, pos2=pos2, users3=users3, pos3=pos3, users4=users4, pos4=pos4, users5=users5, pos5=pos5, users6=users6, pos6=pos6, users7=users7, pos7=pos7, users9=users9_25, pos9=pos9, users10=users10, pos10=pos10, users11=users11, pos11=pos11, users12=users12, pos12=pos12, users13=users13_25, pos13=pos13, users14=users14, pos14=pos14, users15=users15, pos15=pos15)
2021-10-15 14:08:27 +00:00
@app.get("/@<username>/css")
2022-01-19 10:39:22 +00:00
def get_css(username):
2021-10-15 14:08:27 +00:00
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")
2022-01-11 21:54:41 +00:00
@auth_required
2022-01-11 21:53:49 +00:00
def get_profilecss(v, username):
2021-10-15 14:08:27 +00:00
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
2022-01-22 01:13:41 +00:00
@app.get("/@<username>/song")
2022-01-22 01:13:54 +00:00
def usersong(username):
2022-01-22 01:13:41 +00:00
user = get_user(username)
2022-01-24 20:26:15 +00:00
if user.song: return redirect(f"{SITE_FULL}/static/song/{user.song}.mp3")
2022-01-22 01:13:41 +00:00
else: abort(404)
2021-10-15 14:08:27 +00:00
@app.get("/song/<song>")
2021-12-23 13:32:17 +00:00
@app.get("/static/song/<song>")
2022-01-11 23:32:02 +00:00
def song(song):
2021-12-13 01:00:08 +00:00
resp = make_response(send_from_directory('/songs', song))
2021-10-15 14:08:27 +00:00
resp.headers.remove("Cache-Control")
resp.headers.add("Cache-Control", "public, max-age=2628000")
return resp
@app.post("/subscribe/<post_id>")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2021-10-15 14:08:27 +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)
g.db.commit()
return {"message": "Post subscribed!"}
@app.post("/unsubscribe/<post_id>")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2021-10-15 14:08:27 +00:00
@auth_required
def unsubscribe(v, post_id):
2022-01-02 00:06:46 +00:00
sub=g.db.query(Subscription).filter_by(user_id=v.id, submission_id=post_id).one_or_none()
2021-10-15 14:08:27 +00:00
if sub:
g.db.delete(sub)
g.db.commit()
return {"message": "Post unsubscribed!"}
2021-11-23 20:13:54 +00:00
@app.get("/report_bugs")
@auth_required
def reportbugs(v):
2022-01-24 20:26:15 +00:00
return redirect(f'{SITE_FULL}/post/{BUG_THREAD}')
2021-11-23 20:13:54 +00:00
2021-10-15 14:08:27 +00:00
@app.post("/@<username>/message")
2022-01-30 21:37:46 +00:00
@limiter.limit("1/second;10/minute;20/hour;50/day")
2022-01-06 16:46:09 +00:00
@is_not_permabanned
2021-10-15 14:08:27 +00:00
def message2(v, username):
user = get_user(username, v=v)
if hasattr(user, 'is_blocking') and user.is_blocking: return {"error": "You're blocking this user."}, 403
2022-01-03 19:57:06 +00:00
if v.admin_level <= 1 and hasattr(user, 'is_blocked') and user.is_blocked:
return {"error": "This user is blocking you."}, 403
2021-10-15 14:08:27 +00:00
2022-01-31 23:10:24 +00:00
if v.shadowbanned and user.admin_level < 2: return {"message": "Message sent!"}
2022-01-04 17:21:30 +00:00
2022-01-19 06:20:05 +00:00
message = request.values.get("message", "").strip()[:10000].strip()
2021-10-15 14:08:27 +00:00
2022-01-06 23:07:05 +00:00
if not message: return {"error": "message is empty"}
2022-01-03 19:57:06 +00:00
if 'linkedin.com' in message: return {"error": "This domain 'linkedin.com' is banned."}, 403
2021-12-04 20:16:56 +00:00
2021-11-21 14:11:10 +00:00
message = re.sub('!\[\]\((.*?)\)', r'\1', message)
2021-10-15 14:08:27 +00:00
2022-01-11 19:46:50 +00:00
text_html = sanitize(message, noimages=True)
2021-10-15 14:08:27 +00:00
2021-11-21 14:11:10 +00:00
existing = g.db.query(Comment.id).filter(Comment.author_id == v.id,
Comment.sentto == user.id,
Comment.body_html == text_html,
2022-01-06 15:07:56 +00:00
).first()
2022-01-03 19:57:06 +00:00
if existing: return {"error": "Message already exists."}, 403
2021-11-21 14:11:10 +00:00
2021-10-15 14:08:27 +00:00
new_comment = Comment(author_id=v.id,
parent_submission=None,
level=1,
sentto=user.id,
body_html=text_html,
)
g.db.add(new_comment)
g.db.flush()
notif = Notification(comment_id=new_comment.id, user_id=user.id)
g.db.add(notif)
2022-01-18 11:19:32 +00:00
if PUSHER_ID:
if len(message) > 500: notifbody = message[:500] + '...'
else: notifbody = message
beams_client.publish_to_interests(
interests=[f'{request.host}{user.id}'],
publish_body={
'web': {
'notification': {
'title': f'New message from @{v.username}',
'body': notifbody,
2022-01-24 17:37:37 +00:00
'deep_link': f'{SITE_FULL}/notifications?messages=true',
'icon': f'{SITE_FULL}/assets/images/{SITE_NAME}/icon.webp',
2022-01-18 11:19:32 +00:00
}
2022-01-16 00:29:08 +00:00
},
2022-01-18 11:19:32 +00:00
'fcm': {
'notification': {
'title': f'New message from @{v.username}',
'body': notifbody,
},
'data': {
2022-01-21 11:14:24 +00:00
'url': '/notifications?messages=true',
2022-01-18 11:19:32 +00:00
}
2022-01-16 00:29:08 +00:00
}
2022-01-18 11:19:32 +00:00
},
)
2021-10-15 14:08:27 +00:00
g.db.commit()
2022-01-03 19:57:06 +00:00
return {"message": "Message sent!"}
2021-10-15 14:08:27 +00:00
@app.post("/reply")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;6/minute;50/hour;200/day")
2021-10-15 14:08:27 +00:00
@auth_required
def messagereply(v):
2022-01-19 06:20:05 +00:00
message = request.values.get("body", "").strip()[:10000].strip()
2021-11-21 14:11:10 +00:00
2022-01-06 23:07:05 +00:00
if not message: return {"error": "message is empty"}
2021-12-04 20:16:56 +00:00
if 'linkedin.com' in message: return {"error": "this domain 'linkedin.com' is banned"}
2021-11-21 14:11:10 +00:00
message = re.sub('!\[\]\((.*?)\)', r'\1', message)
2021-10-15 14:08:27 +00:00
id = int(request.values.get("parent_id"))
parent = get_comment(id, v=v)
2022-01-06 16:46:09 +00:00
user_id = parent.author.id
if v.id == user_id: user_id = parent.sentto
2021-10-15 14:08:27 +00:00
2022-01-11 19:46:50 +00:00
text_html = sanitize(message, noimages=True)
2021-11-21 14:11:10 +00:00
2021-10-15 14:08:27 +00:00
new_comment = Comment(author_id=v.id,
parent_submission=None,
parent_comment_id=id,
2022-01-11 04:30:08 +00:00
top_comment_id=parent.top_comment_id if parent.top_comment_id else parent.id,
2021-10-15 14:08:27 +00:00
level=parent.level + 1,
2022-01-06 16:46:09 +00:00
sentto=user_id,
2021-10-15 14:08:27 +00:00
body_html=text_html,
)
g.db.add(new_comment)
g.db.flush()
2022-01-28 06:34:09 +00:00
if user_id != v.id:
2022-01-16 06:24:33 +00:00
notif = Notification(comment_id=new_comment.id, user_id=user_id)
g.db.add(notif)
2022-01-16 02:09:20 +00:00
2022-01-28 06:34:09 +00:00
if PUSHER_ID:
if len(message) > 500: notifbody = message[:500] + '...'
else: notifbody = message
beams_client.publish_to_interests(
interests=[f'{request.host}{user_id}'],
publish_body={
'web': {
'notification': {
'title': f'New message from @{v.username}',
'body': notifbody,
'deep_link': f'{SITE_FULL}/notifications?messages=true',
'icon': f'{SITE_FULL}/assets/images/{SITE_NAME}/icon.webp',
}
2022-01-16 06:24:33 +00:00
},
2022-01-28 06:34:09 +00:00
'fcm': {
'notification': {
'title': f'New message from @{v.username}',
'body': notifbody,
},
'data': {
'url': '/notifications?messages=true',
}
2022-01-16 06:24:33 +00:00
}
2022-01-28 06:34:09 +00:00
},
)
2022-01-16 02:09:20 +00:00
2022-01-11 04:30:08 +00:00
if new_comment.top_comment.sentto == 0:
admins = g.db.query(User).filter(User.admin_level > 2, User.id != v.id, User.id != user_id).all()
for admin in admins:
notif = Notification(comment_id=new_comment.id, user_id=admin.id)
g.db.add(notif)
2021-10-15 14:08:27 +00:00
g.db.commit()
2022-01-30 21:19:59 +00:00
return render_template("comments.html", v=v, comments=[new_comment], ajax=True)
2021-10-15 14:08:27 +00:00
@app.get("/2faqr/<secret>")
@auth_required
def mfa_qr(secret, v):
x = pyotp.TOTP(secret)
qr = qrcode.QRCode(
error_correction=qrcode.constants.ERROR_CORRECT_L
)
qr.add_data(x.provisioning_uri(v.username, issuer_name=app.config["SITE_NAME"]))
img = qr.make_image(fill_color="#000000", back_color="white")
mem = io.BytesIO()
img.save(mem, format="PNG")
mem.seek(0, 0)
return send_file(mem, mimetype="image/png", as_attachment=False)
@app.get("/is_available/<name>")
2022-01-11 21:54:41 +00:00
@auth_required
2021-10-15 14:08:27 +00:00
def api_is_available(name, v):
name=name.strip()
if len(name)<3 or len(name)>25:
return {name:False}
2021-11-30 13:09:17 +00:00
name2 = name.replace('_','\_')
2021-10-15 14:08:27 +00:00
2021-11-07 13:36:11 +00:00
x= g.db.query(User).filter(
2021-10-15 14:08:27 +00:00
or_(
2021-11-30 13:09:17 +00:00
User.username.ilike(name2),
User.original_username.ilike(name2)
2021-10-15 14:08:27 +00:00
)
2022-01-02 00:06:46 +00:00
).one_or_none()
2021-10-15 14:08:27 +00:00
if x:
return {name: False}
else:
return {name: True}
@app.get("/id/<id>")
2022-01-11 21:54:41 +00:00
@auth_required
2022-01-11 22:23:41 +00:00
def user_id(id, v):
2022-01-08 21:37:31 +00:00
try: id = int(id)
except: abort(404)
user = get_account(id)
2021-10-15 14:08:27 +00:00
return redirect(user.url)
@app.get("/u/<username>")
2022-01-11 21:54:41 +00:00
@auth_required
2022-01-11 22:23:41 +00:00
def redditor_moment_redirect(username, v):
2022-01-24 20:26:15 +00:00
return redirect(f"{SITE_FULL}/@{username}")
2021-10-15 14:08:27 +00:00
@app.get("/@<username>/followers")
2022-01-11 21:54:41 +00:00
@auth_required
2021-10-15 14:08:27 +00:00
def followers(username, v):
u = get_user(username, v=v)
2022-01-13 04:07:31 +00:00
users = g.db.query(User).join(Follow, Follow.target_id == u.id).filter(Follow.user_id == User.id).order_by(Follow.id).all()
2022-01-14 12:04:34 +00:00
return render_template("followers.html", v=v, u=u, users=users)
2021-10-15 14:08:27 +00:00
2021-10-27 22:38:14 +00:00
@app.get("/@<username>/following")
2022-01-11 21:54:41 +00:00
@auth_required
2021-10-27 22:38:14 +00:00
def following(username, v):
u = get_user(username, v=v)
2022-01-13 04:07:31 +00:00
users = g.db.query(User).join(Follow, Follow.user_id == u.id).filter(Follow.target_id == User.id).order_by(Follow.id).all()
2022-01-14 12:04:34 +00:00
return render_template("following.html", v=v, u=u, users=users)
2021-10-27 22:38:14 +00:00
2021-10-15 14:08:27 +00:00
@app.get("/views")
@auth_required
def visitors(v):
2022-01-14 12:04:34 +00:00
if request.host == 'rdrama.net' and v.admin_level < 1 and not v.patron: return render_template("errors/patron.html", v=v)
2021-10-15 14:08:27 +00:00
viewers=sorted(v.viewers, key = lambda x: x.last_view_utc, reverse=True)
2022-01-14 12:04:34 +00:00
return render_template("viewers.html", v=v, viewers=viewers)
2021-10-15 14:08:27 +00:00
@app.get("/@<username>")
@app.get("/logged_out/@<username>")
2022-01-11 22:57:05 +00:00
@auth_desired
2021-10-15 14:08:27 +00:00
def u_username(username, v=None):
2022-01-24 19:40:58 +00:00
if not v and not request.path.startswith('/logged_out'): return redirect(f"{SITE_FULL}/logged_out{request.full_path}")
2021-10-15 14:08:27 +00:00
if v and request.path.startswith('/logged_out'): v = None
u = get_user(username, v=v)
if username != u.username:
2022-01-30 20:01:50 +00:00
return redirect(SITE_FULL + request.full_path.replace(username, u.username)[:-1])
2021-10-15 14:08:27 +00:00
if u.reserved:
2022-01-16 06:06:16 +00:00
if request.headers.get("Authorization") or request.headers.get("xhr"): return {"error": f"That username is reserved for: {u.reserved}"}
2022-01-14 12:04:34 +00:00
return render_template("userpage_reserved.html", u=u, v=v)
2021-10-15 14:08:27 +00:00
if v and u.id != v.id:
2021-11-06 15:52:48 +00:00
view = g.db.query(ViewerRelationship).filter(
2021-10-15 14:08:27 +00:00
and_(
ViewerRelationship.viewer_id == v.id,
ViewerRelationship.user_id == u.id
)
2022-01-02 12:10:01 +00:00
).first()
2021-10-15 14:08:27 +00:00
if view:
view.last_view_utc = g.timestamp
else:
view = ViewerRelationship(user_id = u.id,
viewer_id = v.id)
g.db.add(view)
g.db.commit()
2021-11-18 19:15:22 +00:00
if u.is_private and (not v or (v.id != u.id and v.admin_level < 2 and not v.eye)):
2021-10-15 14:08:27 +00:00
2021-10-23 19:40:49 +00:00
if v and u.id == LLM_ID:
2021-10-15 14:08:27 +00:00
if int(time.time()) - v.rent_utc > 600:
2022-01-16 06:06:16 +00:00
if request.headers.get("Authorization") or request.headers.get("xhr"): return {"error": "That userpage is private"}
2022-01-14 12:04:34 +00:00
return render_template("userpage_private.html", time=int(time.time()), u=u, v=v)
2021-10-15 14:08:27 +00:00
else:
2022-01-16 06:06:16 +00:00
if request.headers.get("Authorization") or request.headers.get("xhr"): return {"error": "That userpage is private"}
2022-01-14 12:04:34 +00:00
return render_template("userpage_private.html", time=int(time.time()), u=u, v=v)
2021-10-15 14:08:27 +00:00
2021-11-30 13:09:17 +00:00
if v and hasattr(u, 'is_blocking') and u.is_blocking:
2022-01-16 06:06:16 +00:00
if request.headers.get("Authorization") or request.headers.get("xhr"): return {"error": f"You are blocking @{u.username}."}
2022-01-14 12:04:34 +00:00
return render_template("userpage_blocking.html", u=u, v=v)
2021-10-15 14:08:27 +00:00
2021-11-30 13:09:17 +00:00
if v and v.admin_level < 2 and hasattr(u, 'is_blocked') and u.is_blocked:
2022-01-16 06:06:16 +00:00
if request.headers.get("Authorization") or request.headers.get("xhr"): return {"error": "This person is blocking you."}
2022-01-14 12:04:34 +00:00
return render_template("userpage_blocked.html", u=u, v=v)
2021-10-15 14:08:27 +00:00
sort = request.values.get("sort", "new")
t = request.values.get("t", "all")
page = int(request.values.get("page", "1"))
page = max(page, 1)
ids = u.userpagelisting(v=v, page=page, sort=sort, t=t)
next_exists = (len(ids) > 25)
ids = ids[:25]
if page == 1:
sticky = []
2021-11-06 15:52:48 +00:00
sticky = g.db.query(Submission).filter_by(is_pinned=True, author_id=u.id).all()
2021-10-15 14:08:27 +00:00
if sticky:
for p in sticky:
ids = [p.id] + ids
listing = get_posts(ids, v=v)
if u.unban_utc:
if request.headers.get("Authorization"): {"data": [x.json for x in listing]}
2022-01-14 12:04:34 +00:00
return render_template("userpage.html",
2021-10-15 14:08:27 +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]}
2022-01-14 12:04:34 +00:00
return render_template("userpage.html",
2021-10-15 14:08:27 +00:00
u=u,
v=v,
listing=listing,
page=page,
sort=sort,
t=t,
next_exists=next_exists,
is_following=(v and u.has_follower(v)))
@app.get("/@<username>/comments")
@app.get("/logged_out/@<username>/comments")
2022-01-11 22:57:05 +00:00
@auth_desired
2021-10-15 14:08:27 +00:00
def u_username_comments(username, v=None):
2022-01-24 19:40:58 +00:00
if not v and not request.path.startswith('/logged_out'): return redirect(f"{SITE_FULL}/logged_out{request.full_path}")
2021-10-15 14:08:27 +00:00
if v and request.path.startswith('/logged_out'): v = None
user = get_user(username, v=v)
2022-01-24 20:26:15 +00:00
if username != user.username: return redirect(f'{SITE_FULL}/@{user.username}/comments')
2021-10-15 14:08:27 +00:00
u = user
if u.reserved:
2022-01-16 06:06:16 +00:00
if request.headers.get("Authorization") or request.headers.get("xhr"): return {"error": f"That username is reserved for: {u.reserved}"}
2022-01-14 12:04:34 +00:00
return render_template("userpage_reserved.html",
2021-10-15 14:08:27 +00:00
u=u,
v=v)
2021-11-18 19:15:22 +00:00
if u.is_private and (not v or (v.id != u.id and v.admin_level < 2 and not v.eye)):
2021-10-23 19:40:49 +00:00
if v and u.id == LLM_ID:
2021-10-15 14:08:27 +00:00
if int(time.time()) - v.rent_utc > 600:
2022-01-16 06:06:16 +00:00
if request.headers.get("Authorization") or request.headers.get("xhr"): return {"error": "That userpage is private"}
2022-01-14 12:04:34 +00:00
return render_template("userpage_private.html", time=int(time.time()), u=u, v=v)
2021-10-15 14:08:27 +00:00
else:
2022-01-16 06:06:16 +00:00
if request.headers.get("Authorization") or request.headers.get("xhr"): return {"error": "That userpage is private"}
2022-01-14 12:04:34 +00:00
return render_template("userpage_private.html", time=int(time.time()), u=u, v=v)
2021-10-15 14:08:27 +00:00
2021-12-01 14:12:55 +00:00
if v and hasattr(u, 'is_blocking') and u.is_blocking:
2022-01-16 06:06:16 +00:00
if request.headers.get("Authorization") or request.headers.get("xhr"): return {"error": f"You are blocking @{u.username}."}
2022-01-14 12:04:34 +00:00
return render_template("userpage_blocking.html", u=u, v=v)
2021-10-15 14:08:27 +00:00
2021-12-01 14:11:27 +00:00
if v and v.admin_level < 2 and hasattr(u, 'is_blocked') and u.is_blocked:
2022-01-16 06:06:16 +00:00
if request.headers.get("Authorization") or request.headers.get("xhr"): return {"error": "This person is blocking you."}
2022-01-14 12:04:34 +00:00
return render_template("userpage_blocked.html", u=u, v=v)
2021-10-15 14:08:27 +00:00
2021-12-30 05:27:22 +00:00
page = max(int(request.values.get("page", "1")), 1)
2021-10-15 14:08:27 +00:00
sort=request.values.get("sort","new")
t=request.values.get("t","all")
2021-11-06 15:52:48 +00:00
comments = g.db.query(Comment.id).filter(Comment.author_id == u.id, Comment.parent_submission != None)
2021-10-15 14:08:27 +00:00
2022-01-22 16:36:01 +00:00
if not v or (v.id != u.id and v.admin_level == 0):
2022-01-22 16:37:58 +00:00
comments = comments.filter(Comment.deleted_utc == 0, Comment.is_banned == False, Comment.ghost == None)
2021-10-15 14:08:27 +00:00
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())
elif sort == "old":
comments = comments.order_by(Comment.created_utc.asc())
elif sort == "controversial":
2021-11-30 23:21:29 +00:00
comments = comments.order_by(-1 * Comment.upvotes * Comment.downvotes * Comment.downvotes)
2021-10-15 14:08:27 +00:00
elif sort == "top":
2022-01-17 11:06:12 +00:00
comments = comments.order_by(Comment.downvotes - Comment.upvotes)
2021-10-15 14:08:27 +00:00
elif sort == "bottom":
2021-11-30 23:21:29 +00:00
comments = comments.order_by(Comment.upvotes - Comment.downvotes)
2021-10-15 14:08:27 +00:00
comments = comments.offset(25 * (page - 1)).limit(26).all()
ids = [x.id for x in comments]
next_exists = (len(ids) > 25)
ids = ids[:25]
listing = get_comments(ids, v=v)
is_following = (v and user.has_follower(v))
if request.headers.get("Authorization"): return {"data": [c.json for c in listing]}
2022-01-14 12:04:34 +00:00
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-10-15 14:08:27 +00:00
@app.get("/@<username>/info")
2022-01-11 21:54:41 +00:00
@auth_required
2021-10-15 14:08:27 +00:00
def u_username_info(username, v=None):
user=get_user(username, v=v)
if hasattr(user, 'is_blocking') and user.is_blocking:
return {"error": "You're blocking this user."}, 401
elif hasattr(user, 'is_blocked') and user.is_blocked:
return {"error": "This user is blocking you."}, 403
return user.json
@app.post("/follow/<username>")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2021-10-15 14:08:27 +00:00
@auth_required
def follow_user(username, v):
target = get_user(username)
if target.id==v.id: return {"error": "You can't follow yourself!"}, 400
2022-01-02 00:06:46 +00:00
if g.db.query(Follow).filter_by(user_id=v.id, target_id=target.id).one_or_none(): return {"message": "User followed!"}
2021-10-15 14:08:27 +00:00
new_follow = Follow(user_id=v.id, target_id=target.id)
g.db.add(new_follow)
g.db.flush()
2021-11-06 15:52:48 +00:00
target.stored_subscriber_count = g.db.query(Follow.id).filter_by(target_id=target.id).count()
2021-10-15 14:08:27 +00:00
g.db.add(target)
2021-12-20 20:03:59 +00:00
send_notification(target.id, f"@{v.username} has followed you!")
2021-10-15 14:08:27 +00:00
g.db.commit()
return {"message": "User followed!"}
@app.post("/unfollow/<username>")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2021-10-15 14:08:27 +00:00
@auth_required
def unfollow_user(username, v):
target = get_user(username)
2021-12-10 04:58:55 +00:00
if target.fish: return {"error": "You can't unfollow this user!"}
2021-10-15 14:08:27 +00:00
2022-01-02 00:06:46 +00:00
follow = g.db.query(Follow).filter_by(user_id=v.id, target_id=target.id).one_or_none()
2021-10-15 14:08:27 +00:00
2021-12-20 20:03:59 +00:00
if follow:
g.db.delete(follow)
g.db.flush()
target.stored_subscriber_count = g.db.query(Follow.id).filter_by(target_id=target.id).count()
g.db.add(target)
2021-10-15 14:08:27 +00:00
2021-12-20 20:03:59 +00:00
send_notification(target.id, f"@{v.username} has unfollowed you!")
2021-10-15 14:08:27 +00:00
2021-12-20 20:03:59 +00:00
g.db.commit()
2021-10-15 14:08:27 +00:00
return {"message": "User unfollowed!"}
@app.post("/remove_follow/<username>")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2021-10-15 14:08:27 +00:00
@auth_required
def remove_follow(username, v):
target = get_user(username)
2022-01-02 00:06:46 +00:00
follow = g.db.query(Follow).filter_by(user_id=target.id, target_id=v.id).one_or_none()
2021-10-15 14:08:27 +00:00
if not follow: return {"message": "Follower removed!"}
g.db.delete(follow)
g.db.flush()
2021-11-06 15:52:48 +00:00
v.stored_subscriber_count = g.db.query(Follow.id).filter_by(target_id=v.id).count()
2021-10-15 14:08:27 +00:00
g.db.add(v)
2021-12-20 20:03:59 +00:00
send_repeatable_notification(target.id, f"@{v.username} has removed your follow!")
2021-10-15 14:08:27 +00:00
g.db.commit()
return {"message": "Follower removed!"}
2021-11-04 21:14:38 +00:00
@app.get("/uid/<id>/pic")
2021-10-15 14:08:27 +00:00
@app.get("/uid/<id>/pic/profile")
2022-01-16 00:10:33 +00:00
@app.get("/logged_out/uid/<id>/pic")
2022-01-11 21:59:37 +00:00
@limiter.exempt
2022-01-14 07:42:12 +00:00
@auth_desired
2022-01-11 21:53:49 +00:00
def user_profile_uid(v, id):
2022-01-24 19:40:58 +00:00
if not v and not request.path.startswith('/logged_out'): return redirect(f"{SITE_FULL}/logged_out{request.full_path}")
2022-01-16 00:10:33 +00:00
if v and request.path.startswith('/logged_out'): v = None
2021-10-15 14:08:27 +00:00
try: id = int(id)
except:
try: id = int(id, 36)
except: abort(404)
x=get_account(id)
2021-12-10 20:40:05 +00:00
return redirect(x.profile_url)
2021-10-15 14:08:27 +00:00
2021-11-23 00:04:13 +00:00
@app.get("/@<username>/pic")
2022-01-12 02:46:06 +00:00
@limiter.exempt
2022-01-11 21:54:41 +00:00
@auth_required
2022-01-11 21:53:49 +00:00
def user_profile_name(v, username):
2021-11-23 00:04:13 +00:00
x = get_user(username)
return redirect(x.profile_url)
2021-10-15 14:08:27 +00:00
@app.get("/@<username>/saved/posts")
@auth_required
def saved_posts(v, username):
page=int(request.values.get("page",1))
ids=v.saved_idlist(page=page)
next_exists=len(ids)>25
ids=ids[:25]
listing = get_posts(ids, v=v)
if request.headers.get("Authorization"): return {"data": [x.json for x in listing]}
2022-01-14 12:04:34 +00:00
return render_template("userpage.html",
2021-10-15 14:08:27 +00:00
u=v,
v=v,
listing=listing,
page=page,
next_exists=next_exists,
)
@app.get("/@<username>/saved/comments")
@auth_required
def saved_comments(v, username):
page=int(request.values.get("page",1))
2022-01-29 13:57:55 +00:00
ids=v.saved_comment_idlist(page=page)
2021-10-15 14:08:27 +00:00
next_exists=len(ids) > 25
ids=ids[:25]
listing = get_comments(ids, v=v)
if request.headers.get("Authorization"): return {"data": [x.json for x in listing]}
2022-01-14 12:04:34 +00:00
return render_template("userpage_comments.html",
2021-10-15 14:08:27 +00:00
u=v,
v=v,
listing=listing,
page=page,
next_exists=next_exists,
standalone=True)
2021-11-18 16:04:52 +00:00
@app.post("/fp/<fp>")
@auth_required
def fp(v, fp):
2022-01-29 02:01:16 +00:00
v.fp = fp
users = g.db.query(User).filter(User.fp == fp, User.id != v.id).all()
if users: print(f'{v.username}: fp {v.fp}')
if v.email and v.is_activated:
alts = g.db.query(User).filter(User.email == v.email, User.is_activated, User.id != v.id).all()
if alts:
print(f'{v.username}: email {v.email}')
users += alts
for u in users:
li = [v.id, u.id]
existing = g.db.query(Alt).filter(Alt.user1.in_(li), Alt.user2.in_(li)).first()
if existing: continue
new_alt = Alt(user1=v.id, user2=u.id)
g.db.add(new_alt)
g.db.flush()
print(v.username + ' + ' + u.username)
g.db.add(v)
g.db.commit()
2021-12-29 12:38:54 +00:00
return '', 204