2022-05-04 23:09:46 +00:00
import io
2022-11-15 09:19:08 +00:00
import json
2022-05-04 23:09:46 +00:00
import math
2022-11-15 09:19:08 +00:00
import time
from collections import Counter
import gevent
import qrcode
2023-05-05 01:04:07 +00:00
from sqlalchemy . orm import aliased , load_only
2023-09-06 14:25:01 +00:00
from sqlalchemy . exc import IntegrityError
2022-11-15 09:19:08 +00:00
from files . classes import *
2022-10-28 08:42:02 +00:00
from files . classes . leaderboard import Leaderboard
2022-09-13 15:22:18 +00:00
from files . classes . transactions import *
2022-11-15 09:19:08 +00:00
from files . classes . views import *
2023-02-07 03:31:49 +00:00
from files . helpers . actions import execute_under_siege
2022-05-04 23:09:46 +00:00
from files . helpers . alerts import *
2022-12-11 23:44:34 +00:00
from files . helpers . config . const import *
2022-11-15 09:19:08 +00:00
from files . helpers . mail import *
from files . helpers . sanitize import *
2022-07-09 10:32:49 +00:00
from files . helpers . sorting_and_time import *
2022-11-15 09:19:08 +00:00
from files . helpers . useractions import badge_grant
2023-10-05 10:09:58 +00:00
from files . helpers . can_see import *
2022-12-22 21:24:36 +00:00
from files . routes . routehelpers import check_for_alts , add_alt
2022-11-15 09:19:08 +00:00
from files . routes . wrappers import *
2023-07-25 21:26:34 +00:00
from files . routes . comments import _mark_comment_as_read
2022-11-15 09:19:08 +00:00
from files . __main__ import app , cache , limiter
2023-07-25 21:26:34 +00:00
def _add_profile_view ( vid , uid ) :
db = db_session ( )
2023-07-26 12:31:44 +00:00
view = db . query ( ViewerRelationship ) . options ( load_only ( ViewerRelationship . viewer_id ) ) . filter_by ( viewer_id = vid , user_id = uid ) . one_or_none ( )
2023-07-25 21:26:34 +00:00
if view : view . last_view_utc = int ( time . time ( ) )
else : view = ViewerRelationship ( viewer_id = vid , user_id = uid )
db . add ( view )
2023-09-07 15:26:31 +00:00
2023-09-06 14:25:01 +00:00
try :
db . commit ( )
except IntegrityError as e :
db . rollback ( )
if not str ( e ) . startswith ( ' (psycopg2.errors.UniqueViolation) duplicate key value violates unique constraint " ' ) :
print ( STARS + e + STARS , flush = True )
2023-07-25 21:26:34 +00:00
db . close ( )
stdout . flush ( )
2023-07-25 20:06:18 +00:00
def claim_rewards_all_users ( ) :
2023-10-18 18:17:50 +00:00
g . db . flush ( )
2023-08-11 13:15:34 +00:00
emails = [ x [ 0 ] for x in g . db . query ( Transaction . email ) . filter_by ( claimed = None ) ]
2023-07-25 20:06:18 +00:00
users = g . db . query ( User ) . filter ( User . email . in_ ( emails ) ) . order_by ( User . truescore . desc ( ) ) . all ( )
for user in users :
2023-08-20 02:00:35 +00:00
g . db . flush ( )
2023-07-25 20:06:18 +00:00
transactions = g . db . query ( Transaction ) . filter_by ( email = user . email , claimed = None ) . all ( )
highest_tier = 0
marseybux = 0
for transaction in transactions :
for t , money in TIER_TO_MONEY . items ( ) :
2023-10-18 18:17:50 +00:00
if transaction . amount < money : break
2023-07-25 20:06:18 +00:00
tier = t
2023-10-13 12:31:01 +00:00
marseybux + = transaction . amount * 500
2023-07-25 20:06:18 +00:00
if tier > highest_tier :
highest_tier = tier
transaction . claimed = True
g . db . add ( transaction )
if marseybux :
user . pay_account ( ' marseybux ' , marseybux )
send_repeatable_notification ( user . id , f " You have received { marseybux } Marseybux! You can use them to buy awards or hats in the [shop](/shop/awards) or gamble them in the [casino](/casino). " )
g . db . add ( user )
user . patron_utc = int ( time . time ( ) ) + 2937600
if highest_tier > user . patron :
user . patron = highest_tier
badge_id = 20 + highest_tier
badges_to_remove = g . db . query ( Badge ) . filter (
Badge . user_id == user . id ,
Badge . badge_id > badge_id ,
Badge . badge_id < 29 ,
) . all ( )
for badge in badges_to_remove :
g . db . delete ( badge )
for x in range ( 22 , badge_id + 1 ) :
badge_grant ( badge_id = x , user = user )
2023-08-20 02:00:35 +00:00
g . db . flush ( )
2023-08-01 07:16:47 +00:00
user . lifetimedonated = g . db . query ( func . sum ( Transaction . amount ) ) . filter_by ( email = user . email ) . scalar ( )
if user . lifetimedonated > = 100 :
2023-07-25 20:06:18 +00:00
badge_grant ( badge_id = 257 , user = user )
2023-08-01 07:16:47 +00:00
if user . lifetimedonated > = 500 :
2023-07-25 20:06:18 +00:00
badge_grant ( badge_id = 258 , user = user )
2023-08-01 07:16:47 +00:00
if user . lifetimedonated > = 2500 :
2023-07-25 20:06:18 +00:00
badge_grant ( badge_id = 259 , user = user )
2023-08-01 07:16:47 +00:00
if user . lifetimedonated > = 5000 :
2023-07-25 20:06:18 +00:00
badge_grant ( badge_id = 260 , user = user )
2023-08-01 07:16:47 +00:00
if user . lifetimedonated > = 10000 :
2023-07-25 20:06:18 +00:00
badge_grant ( badge_id = 261 , user = user )
print ( f ' @ { user . username } rewards claimed successfully! ' , flush = True )
2023-08-05 19:26:42 +00:00
for user in g . db . query ( User ) . options ( load_only ( User . id ) ) . order_by ( User . lifetimedonated . desc ( ) ) . limit ( 10 ) :
2023-08-01 07:16:47 +00:00
badge_grant ( badge_id = 294 , user = user )
2023-08-01 06:43:50 +00:00
2023-07-30 00:42:06 +00:00
def transfer_currency ( v , username , currency_name , apply_tax ) :
2023-07-25 20:06:18 +00:00
MIN_CURRENCY_TRANSFER = 100
TAX_PCT = 0.03
receiver = get_user ( username , v = v )
if receiver . id == v . id : abort ( 400 , f " You can ' t transfer { currency_name } to yourself! " )
amount = request . values . get ( " amount " , " " ) . strip ( )
amount = int ( amount ) if amount . isdigit ( ) else None
if amount is None or amount < = 0 : abort ( 400 , f " Invalid number of { currency_name } " )
if amount < MIN_CURRENCY_TRANSFER : abort ( 400 , f " You have to gift at least { MIN_CURRENCY_TRANSFER } { currency_name } " )
tax = 0
if apply_tax and not v . patron and not receiver . patron :
tax = math . ceil ( amount * TAX_PCT )
reason = request . values . get ( " reason " , " " ) . strip ( )
log_message = f " @ { v . username } has transferred { amount } { currency_name } to @ { receiver . username } "
notif_text = f " :marseycapitalistmanlet: @ { v . username } has gifted you { amount - tax } { currency_name } ! "
if reason :
if len ( reason ) > TRANSFER_MESSAGE_LENGTH_LIMIT :
abort ( 400 , f " Reason is too long, max { TRANSFER_MESSAGE_LENGTH_LIMIT } characters " )
notif_text + = f " \n \n > { reason } "
log_message + = f " \n \n > { reason } "
if not v . charge_account ( currency_name , amount ) [ 0 ] :
abort ( 400 , f " You don ' t have enough { currency_name } " )
2023-10-04 19:34:00 +00:00
if currency_name == ' marseybux ' :
receiver . pay_account ( ' marseybux ' , amount - tax )
elif currency_name == ' coins ' :
receiver . pay_account ( ' coins ' , amount - tax )
else :
raise ValueError ( f " Invalid currency ' { currency_name } ' got when transferring { amount } from { v . id } to { receiver . id } " )
if GIFT_NOTIF_ID : send_repeatable_notification ( GIFT_NOTIF_ID , log_message )
send_repeatable_notification ( receiver . id , notif_text )
2023-07-25 20:06:18 +00:00
return { " message " : f " { amount - tax } { currency_name } have been transferred to @ { receiver . username } " }
2022-05-04 23:09:46 +00:00
2023-08-05 16:03:14 +00:00
def upvoters_downvoters ( v , username , username2 , cls , vote_cls , vote_dir , template , standalone ) :
2023-02-27 15:38:12 +00:00
u = get_user ( username , v = v )
2022-10-30 07:31:21 +00:00
if not u . is_visible_to ( v ) : abort ( 403 )
2022-05-04 23:09:46 +00:00
id = u . id
2023-09-07 15:26:31 +00:00
2023-09-07 16:16:13 +00:00
uid = get_user ( username2 , attributes = [ User . id ] ) . id
2022-05-04 23:09:46 +00:00
2023-05-05 05:23:59 +00:00
page = get_page ( )
2022-05-04 23:09:46 +00:00
2023-05-05 21:44:24 +00:00
listing = g . db . query ( cls ) . options ( load_only ( cls . id ) ) . join ( vote_cls ) . filter (
cls . ghost == False ,
cls . is_banned == False ,
cls . deleted_utc == 0 ,
vote_cls . vote_type == vote_dir ,
cls . author_id == id ,
vote_cls . user_id == uid ,
)
2023-05-05 21:45:25 +00:00
2023-05-05 21:44:24 +00:00
total = listing . count ( )
2023-05-05 21:45:25 +00:00
2023-05-05 21:44:24 +00:00
listing = listing . order_by ( cls . created_utc . desc ( ) ) . offset ( PAGE_SIZE * ( page - 1 ) ) . limit ( PAGE_SIZE ) . all ( )
listing = [ x . id for x in listing ]
2022-05-04 23:09:46 +00:00
2023-06-07 23:26:32 +00:00
if cls == Post :
2023-07-17 14:49:26 +00:00
listing = get_posts ( listing , v = v )
2022-10-16 08:17:20 +00:00
elif cls == Comment :
listing = get_comments ( listing , v = v )
else :
listing = [ ]
2022-05-04 23:09:46 +00:00
2023-05-05 21:44:24 +00:00
return render_template ( template , total = total , listing = listing , page = page , v = v , standalone = standalone )
2022-10-09 04:21:25 +00:00
2023-08-05 16:03:14 +00:00
@app.get ( " /@<username>/upvoters/@<username2>/posts " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-10-09 04:21:25 +00:00
@auth_required
2023-08-05 16:03:14 +00:00
def upvoters_posts ( v , username , username2 ) :
return upvoters_downvoters ( v , username , username2 , Post , Vote , 1 , " userpage/voted_posts.html " , None )
2022-05-04 23:09:46 +00:00
2023-08-05 16:03:14 +00:00
@app.get ( " /@<username>/upvoters/@<username2>/comments " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-08-05 16:03:14 +00:00
def upvoters_comments ( v , username , username2 ) :
return upvoters_downvoters ( v , username , username2 , Comment , CommentVote , 1 , " userpage/voted_comments.html " , True )
2022-05-04 23:09:46 +00:00
2023-08-05 16:03:14 +00:00
@app.get ( " /@<username>/downvoters/@<username2>/posts " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-08-05 16:03:14 +00:00
def downvoters_posts ( v , username , username2 ) :
return upvoters_downvoters ( v , username , username2 , Post , Vote , - 1 , " userpage/voted_posts.html " , None )
2022-05-04 23:09:46 +00:00
2023-08-05 16:03:14 +00:00
@app.get ( " /@<username>/downvoters/@<username2>/comments " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-08-05 16:03:14 +00:00
def downvoters_comments ( v , username , username2 ) :
return upvoters_downvoters ( v , username , username2 , Comment , CommentVote , - 1 , " userpage/voted_comments.html " , True )
2022-10-09 04:21:25 +00:00
2023-08-05 16:03:14 +00:00
def upvoting_downvoting ( v , username , username2 , cls , vote_cls , vote_dir , template , standalone ) :
2023-02-27 15:38:12 +00:00
u = get_user ( username , v = v )
2022-10-30 07:31:21 +00:00
if not u . is_visible_to ( v ) : abort ( 403 )
2022-05-04 23:09:46 +00:00
id = u . id
2023-08-05 16:03:14 +00:00
2023-09-07 16:16:13 +00:00
uid = get_user ( username2 , attributes = [ User . id ] ) . id
2022-05-04 23:09:46 +00:00
2023-05-05 05:23:59 +00:00
page = get_page ( )
2022-05-04 23:09:46 +00:00
2023-05-05 21:44:24 +00:00
listing = g . db . query ( cls ) . options ( load_only ( cls . id ) ) . join ( vote_cls ) . filter (
cls . ghost == False ,
cls . is_banned == False ,
cls . deleted_utc == 0 ,
vote_cls . vote_type == vote_dir ,
vote_cls . user_id == id ,
cls . author_id == uid ,
)
2023-05-05 21:45:25 +00:00
2023-05-05 21:44:24 +00:00
total = listing . count ( )
2023-05-05 21:45:25 +00:00
2023-05-05 21:44:24 +00:00
listing = listing . order_by ( cls . created_utc . desc ( ) ) . offset ( PAGE_SIZE * ( page - 1 ) ) . limit ( PAGE_SIZE ) . all ( )
listing = [ x . id for x in listing ]
2023-01-01 11:36:20 +00:00
2023-06-07 23:26:32 +00:00
if cls == Post :
2023-07-17 14:49:26 +00:00
listing = get_posts ( listing , v = v )
2022-10-11 13:37:40 +00:00
elif cls == Comment :
listing = get_comments ( listing , v = v )
else :
listing = [ ]
2022-05-04 23:09:46 +00:00
2023-05-05 21:44:24 +00:00
return render_template ( template , total = total , listing = listing , page = page , v = v , standalone = standalone )
2022-05-04 23:09:46 +00:00
2023-08-05 16:03:14 +00:00
@app.get ( " /@<username>/upvoting/@<username2>/posts " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-08-05 16:03:14 +00:00
def upvoting_posts ( v , username , username2 ) :
return upvoting_downvoting ( v , username , username2 , Post , Vote , 1 , " userpage/voted_posts.html " , None )
2022-05-04 23:09:46 +00:00
2023-08-05 16:03:14 +00:00
@app.get ( " /@<username>/upvoting/@<username2>/comments " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-08-05 16:03:14 +00:00
def upvoting_comments ( v , username , username2 ) :
return upvoting_downvoting ( v , username , username2 , Comment , CommentVote , 1 , " userpage/voted_comments.html " , True )
2022-05-04 23:09:46 +00:00
2023-08-05 16:03:14 +00:00
@app.get ( " /@<username>/downvoting/@<username2>/posts " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-08-05 16:03:14 +00:00
def downvoting_posts ( v , username , username2 ) :
return upvoting_downvoting ( v , username , username2 , Post , Vote , - 1 , " userpage/voted_posts.html " , None )
2022-05-04 23:09:46 +00:00
2023-08-05 16:03:14 +00:00
@app.get ( " /@<username>/downvoting/@<username2>/comments " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-08-05 16:03:14 +00:00
def downvoting_comments ( v , username , username2 ) :
return upvoting_downvoting ( v , username , username2 , Comment , CommentVote , - 1 , " userpage/voted_comments.html " , True )
2022-10-09 04:21:25 +00:00
2022-11-22 21:28:30 +00:00
def user_voted ( v , username , cls , vote_cls , template , standalone ) :
2023-02-27 15:38:12 +00:00
u = get_user ( username , v = v )
2022-10-30 07:31:21 +00:00
if not u . is_visible_to ( v ) : abort ( 403 )
2022-05-04 23:09:46 +00:00
2023-05-05 05:23:59 +00:00
page = get_page ( )
2022-05-04 23:09:46 +00:00
2023-03-16 06:27:58 +00:00
listing = g . db . query ( cls ) . join ( vote_cls ) . filter (
2022-10-09 04:21:25 +00:00
cls . ghost == False ,
cls . is_banned == False ,
cls . deleted_utc == 0 ,
cls . author_id != u . id ,
vote_cls . user_id == u . id ,
2023-05-05 21:44:24 +00:00
)
total = listing . count ( )
2023-05-05 21:45:25 +00:00
2023-08-10 17:23:33 +00:00
listing = listing . order_by ( vote_cls . created_utc . desc ( ) ) . offset ( PAGE_SIZE * ( page - 1 ) ) . limit ( PAGE_SIZE ) . all ( )
2023-05-05 21:44:24 +00:00
listing = [ x . id for x in listing ]
2022-05-04 23:09:46 +00:00
2023-06-07 23:26:32 +00:00
if cls == Post :
2023-07-17 14:49:26 +00:00
listing = get_posts ( listing , v = v )
2022-10-16 08:17:20 +00:00
elif cls == Comment :
listing = get_comments ( listing , v = v )
else :
listing = [ ]
2022-05-04 23:09:46 +00:00
2023-05-05 21:44:24 +00:00
return render_template ( template , total = total , listing = listing , page = page , v = v , standalone = standalone )
2022-05-04 23:09:46 +00:00
2022-11-22 21:28:30 +00:00
@app.get ( " /@<username>/voted/posts " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-09-23 21:33:46 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def user_voted_posts ( v , username ) :
2023-06-07 23:26:32 +00:00
return user_voted ( v , username , Post , Vote , " userpage/voted_posts.html " , None )
2022-09-23 21:33:46 +00:00
2022-11-22 21:28:30 +00:00
@app.get ( " /@<username>/voted/comments " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-09-23 21:33:46 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def user_voted_comments ( v , username ) :
2022-11-22 21:28:30 +00:00
return user_voted ( v , username , Comment , CommentVote , " userpage/voted_comments.html " , True )
2022-09-23 21:33:46 +00:00
2022-12-10 04:46:19 +00:00
@app.get ( " /banned " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-12-10 04:46:19 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def banned ( v ) :
2023-03-16 06:27:58 +00:00
users = g . db . query ( User ) . filter (
2022-12-13 22:02:53 +00:00
User . is_banned != None ,
2022-12-10 08:23:56 +00:00
or_ ( User . unban_utc == 0 , User . unban_utc > time . time ( ) ) ,
2023-01-21 05:50:48 +00:00
) . order_by ( User . ban_reason )
2023-02-27 15:38:12 +00:00
2022-12-10 04:46:19 +00:00
users = users . all ( )
return render_template ( " banned.html " , v = v , users = users )
2022-05-04 23:09:46 +00:00
@app.get ( " /grassed " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def grassed ( v ) :
2023-03-16 06:27:58 +00:00
users = g . db . query ( User ) . filter (
2022-12-10 08:11:11 +00:00
User . ban_reason . like ( ' grass award used by @ % ' ) ,
2022-12-10 08:23:56 +00:00
User . unban_utc > time . time ( ) ,
2022-12-10 08:11:11 +00:00
)
2023-02-27 15:38:12 +00:00
2022-11-03 08:33:37 +00:00
users = users . all ( )
2022-05-04 23:09:46 +00:00
return render_template ( " grassed.html " , v = v , users = users )
2022-10-15 06:55:54 +00:00
@app.get ( " /chuds " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def chuds ( v ) :
2023-03-16 06:27:58 +00:00
users = g . db . query ( User ) . filter (
2023-06-23 11:07:47 +00:00
or_ ( User . chud == 1 , User . chud > time . time ( ) ) ,
2022-12-10 04:46:19 +00:00
)
if v . admin_level > = PERMS [ ' VIEW_LAST_ACTIVE ' ] :
2022-12-15 18:31:51 +00:00
users = users . order_by ( User . truescore . desc ( ) )
2023-02-27 15:38:12 +00:00
2022-11-03 08:33:37 +00:00
users = users . order_by ( User . username ) . all ( )
2022-10-15 06:55:54 +00:00
return render_template ( " chuds.html " , v = v , users = users )
2022-05-04 23:09:46 +00:00
2023-07-30 00:42:06 +00:00
def all_upvoters_downvoters ( v , username , vote_dir , is_who_simps_hates ) :
2022-12-21 20:14:33 +00:00
if username == ' Snappy ' :
2023-02-26 08:41:04 +00:00
abort ( 403 , " For performance reasons, you can ' t see Snappy ' s statistics! " )
2022-10-09 06:41:06 +00:00
vote_str = ' votes '
simps_haters = ' voters '
vote_name = ' Neutral '
if vote_dir == 1 :
vote_str = ' upvotes '
simps_haters = ' simps for ' if is_who_simps_hates else ' simps '
vote_name = ' Up '
elif vote_dir == - 1 :
vote_str = ' downvotes '
simps_haters = ' hates ' if is_who_simps_hates else ' haters '
vote_name = ' Down '
2022-05-04 23:09:46 +00:00
2023-02-27 15:38:12 +00:00
id = get_user ( username , v = v ) . id
2022-10-09 06:42:32 +00:00
votes = [ ]
votes2 = [ ]
2022-10-09 06:41:06 +00:00
if is_who_simps_hates :
2023-06-07 23:26:32 +00:00
votes = g . db . query ( Post . author_id , func . count ( Post . author_id ) ) . join ( Vote ) . filter ( Post . ghost == False , Post . is_banned == False , Post . deleted_utc == 0 , Vote . vote_type == vote_dir , Vote . user_id == id ) . group_by ( Post . author_id ) . order_by ( func . count ( Post . author_id ) . desc ( ) ) . all ( )
2023-03-16 06:27:58 +00:00
votes2 = g . db . query ( Comment . author_id , func . count ( Comment . author_id ) ) . join ( CommentVote ) . filter ( Comment . ghost == False , Comment . is_banned == False , Comment . deleted_utc == 0 , CommentVote . vote_type == vote_dir , CommentVote . user_id == id ) . group_by ( Comment . author_id ) . order_by ( func . count ( Comment . author_id ) . desc ( ) ) . all ( )
2022-10-09 06:41:06 +00:00
else :
2023-06-07 23:26:32 +00:00
votes = g . db . query ( Vote . user_id , func . count ( Vote . user_id ) ) . join ( Post ) . filter ( Post . ghost == False , Post . is_banned == False , Post . deleted_utc == 0 , Vote . vote_type == vote_dir , Post . author_id == id ) . group_by ( Vote . user_id ) . order_by ( func . count ( Vote . user_id ) . desc ( ) ) . all ( )
2023-03-16 06:27:58 +00:00
votes2 = g . db . query ( CommentVote . user_id , func . count ( CommentVote . user_id ) ) . join ( Comment ) . filter ( Comment . ghost == False , Comment . is_banned == False , Comment . deleted_utc == 0 , CommentVote . vote_type == vote_dir , Comment . author_id == id ) . group_by ( CommentVote . user_id ) . order_by ( func . count ( CommentVote . user_id ) . desc ( ) ) . all ( )
2023-02-22 17:30:09 +00:00
votes = Counter ( dict ( votes ) ) + Counter ( dict ( votes2 ) )
2023-05-07 18:35:11 +00:00
total_items = sum ( votes . values ( ) )
2023-05-07 18:25:37 +00:00
2023-05-09 20:39:30 +00:00
users = g . db . query ( User ) . filter ( User . id . in_ ( votes . keys ( ) ) )
2023-08-11 13:15:34 +00:00
users = [ ( user , votes [ user . id ] ) for user in users ]
2023-05-09 20:39:30 +00:00
users = sorted ( users , key = lambda x : x [ 1 ] , reverse = True )
2022-10-09 06:41:06 +00:00
2022-05-04 23:09:46 +00:00
try :
pos = [ x [ 0 ] . id for x in users ] . index ( v . id )
pos = ( pos + 1 , users [ pos ] [ 1 ] )
except : pos = ( len ( users ) + 1 , 0 )
2022-10-09 06:41:06 +00:00
received_given = ' given ' if is_who_simps_hates else ' received '
2023-05-07 18:35:11 +00:00
if total_items == 1 : vote_str = vote_str [ : - 1 ] # we want to unpluralize if only 1 vote
total_items = f ' { total_items } { vote_str } { received_given } '
2022-06-24 23:22:20 +00:00
2022-11-28 01:45:20 +00:00
name2 = f ' Who @ { username } { simps_haters } ' if is_who_simps_hates else f " @ { username } ' s { simps_haters } "
2022-05-04 23:09:46 +00:00
2023-05-05 05:23:59 +00:00
page = get_page ( )
2023-01-01 11:36:20 +00:00
2023-05-07 18:35:11 +00:00
total = len ( users )
2023-05-09 20:39:30 +00:00
users = users [ PAGE_SIZE * ( page - 1 ) : ]
users = users [ : PAGE_SIZE ]
2022-11-28 01:45:20 +00:00
2023-05-07 18:35:11 +00:00
return render_template ( " userpage/voters.html " , v = v , users = users , pos = pos , name = vote_name , name2 = name2 , page = page , total_items = total_items , total = total )
2022-05-04 23:09:46 +00:00
2022-10-09 06:41:06 +00:00
@app.get ( " /@<username>/upvoters " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-10-09 06:41:06 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def upvoters ( v , username ) :
2022-10-09 06:41:06 +00:00
return all_upvoters_downvoters ( v , username , 1 , False )
2022-05-04 23:09:46 +00:00
@app.get ( " /@<username>/downvoters " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def downvoters ( v , username ) :
2022-10-09 06:41:06 +00:00
return all_upvoters_downvoters ( v , username , - 1 , False )
2022-05-04 23:09:46 +00:00
@app.get ( " /@<username>/upvoting " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def upvoting ( v , username ) :
2022-10-09 06:41:06 +00:00
return all_upvoters_downvoters ( v , username , 1 , True )
2022-05-04 23:09:46 +00:00
@app.get ( " /@<username>/downvoting " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def downvoting ( v , username ) :
2022-10-09 06:41:06 +00:00
return all_upvoters_downvoters ( v , username , - 1 , True )
2022-05-04 23:09:46 +00:00
@app.post ( " /@<username>/suicide " )
2022-11-14 15:11:05 +00:00
@feature_required ( ' USERS_SUICIDE ' )
2023-02-27 05:33:45 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath )
2023-04-02 06:52:26 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath , key_func = get_ID )
2023-07-13 13:50:46 +00:00
@limiter.limit ( " 5/day " , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( " 5/day " , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def suicide ( v , username ) :
2022-05-04 23:09:46 +00:00
user = get_user ( username )
2023-02-26 10:20:32 +00:00
suicide = f " Hi there, \n \n A [concerned user](/id/ { v . id } ) reached out to us about you. \n \n When 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 \n There 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 \n - Call, Email, or Visit the UK ' s [Samaritans](https://www.samaritans.org/) \n \n - Text CHAT to America ' s [Crisis Text Line](https://www.crisistextline.org/) at 741741. \n \n If 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 \n If 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 \n It may not feel like it, but you have options. There are people available to listen to you, and ways to move forward. \n \n Your fellow users care about you and there are people who want to help. "
2023-10-04 19:34:00 +00:00
send_notification ( user . id , suicide )
2022-09-11 14:32:00 +00:00
return { " message " : f " Help message sent to @ { user . username } " }
2022-05-04 23:09:46 +00:00
@app.get ( " /@<username>/coins " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def get_coins ( v , username ) :
2023-02-27 15:38:12 +00:00
user = get_user ( username , v = v )
2022-09-30 22:48:34 +00:00
return { " coins " : user . coins }
2022-05-04 23:09:46 +00:00
@app.post ( " /@<username>/transfer_coins " )
2023-02-27 05:33:45 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath )
2023-04-02 06:52:26 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath , key_func = get_ID )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2023-09-14 16:49:46 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def transfer_coins ( v , username ) :
2022-10-30 07:05:02 +00:00
return transfer_currency ( v , username , ' coins ' , True )
2022-05-04 23:09:46 +00:00
@app.post ( " /@<username>/transfer_bux " )
2022-11-21 23:08:29 +00:00
@feature_required ( ' MARSEYBUX ' )
2023-02-27 05:33:45 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath )
2023-04-02 06:52:26 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath , key_func = get_ID )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2023-09-14 16:49:46 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def transfer_bux ( v , username ) :
2022-11-21 23:08:29 +00:00
return transfer_currency ( v , username , ' marseybux ' , False )
2022-05-04 23:09:46 +00:00
2023-08-03 22:01:34 +00:00
@cache.memoize ( )
2023-09-05 19:10:49 +00:00
def leaderboard_cached ( v ) :
2023-03-16 06:27:58 +00:00
users = g . db . query ( User )
2022-05-04 23:09:46 +00:00
2023-06-08 00:49:37 +00:00
coins = Leaderboard ( " Coins " , " coins " , " coins " , " Coins " , None , Leaderboard . get_simple_lb , User . coins , v , lambda u : u . coins , users )
marseybux = Leaderboard ( " Marseybux " , " marseybux " , " marseybux " , " Marseybux " , None , Leaderboard . get_simple_lb , User . marseybux , v , lambda u : u . marseybux , users )
subscribers = Leaderboard ( " Followers " , " followers " , " followers " , " Followers " , " followers " , Leaderboard . get_simple_lb , User . stored_subscriber_count , v , lambda u : u . stored_subscriber_count , users )
posts = Leaderboard ( " Posts " , " post count " , " posts " , " Posts " , " posts " , Leaderboard . get_simple_lb , User . post_count , v , lambda u : u . post_count , users )
comments = Leaderboard ( " Comments " , " comment count " , " comments " , " Comments " , " comments " , Leaderboard . get_simple_lb , User . comment_count , v , lambda u : u . comment_count , users )
received_awards = Leaderboard ( " Received awards " , " received awards " , " received-awards " , " Received Awards " , None , Leaderboard . get_simple_lb , User . received_award_count , v , lambda u : u . received_award_count , users )
coins_spent = Leaderboard ( " Coins spent on awards " , " coins spent on awards " , " spent " , " Coins " , None , Leaderboard . get_simple_lb , User . coins_spent , v , lambda u : u . coins_spent , users )
truescore = Leaderboard ( " Truescore " , " truescore " , " truescore " , " Truescore " , None , Leaderboard . get_simple_lb , User . truescore , v , lambda u : u . truescore , users )
2022-10-28 08:42:02 +00:00
2023-06-24 14:50:44 +00:00
badges = Leaderboard ( " Badges " , " badges " , " badges " , " Badges " , None , Leaderboard . get_badge_emoji_lb , Badge . user_id , v , None , None )
2022-10-28 08:42:02 +00:00
2023-06-08 00:49:37 +00:00
blocks = Leaderboard ( " Most blocked " , " most blocked " , " most-blocked " , " Blocked By " , " blockers " , Leaderboard . get_blockers_lb , UserBlock . target_id , v , None , None )
2022-10-28 08:42:02 +00:00
2023-06-08 00:49:37 +00:00
owned_hats = Leaderboard ( " Owned hats " , " owned hats " , " owned-hats " , " Owned Hats " , None , Leaderboard . get_hat_lb , User . owned_hats , v , None , None )
2022-10-28 08:42:02 +00:00
2023-05-13 23:23:09 +00:00
leaderboards = [ coins , marseybux , coins_spent , truescore , subscribers , posts , comments , received_awards , badges , blocks , owned_hats ]
2022-11-27 00:27:33 +00:00
2023-10-26 21:01:00 +00:00
if FEATURES [ " HAT_SUBMISSIONS " ] :
2023-06-08 00:49:37 +00:00
leaderboards . append ( Leaderboard ( " Designed hats " , " designed hats " , " designed-hats " , " Designed Hats " , None , Leaderboard . get_hat_lb , User . designed_hats , v , None , None ) )
2023-10-26 20:58:10 +00:00
2023-10-26 21:01:00 +00:00
if FEATURES [ " EMOJI_SUBMISSIONS " ] :
leaderboards . append ( Leaderboard ( " Emojis made " , " emojis made " , " emojis-made " , " Emojis Made " , None , Leaderboard . get_badge_emoji_lb , Emoji . author_id , v , None , None ) )
2022-10-28 08:42:02 +00:00
2023-06-08 00:49:37 +00:00
leaderboards . append ( Leaderboard ( " Upvotes given " , " upvotes given " , " upvotes-given " , " Upvotes Given " , " upvoting " , Leaderboard . get_upvotes_lb , None , v , None , None ) )
2023-04-25 09:38:42 +00:00
2023-06-08 00:49:37 +00:00
leaderboards . append ( Leaderboard ( " Downvotes received " , " downvotes received " , " downvotes-received " , " Downvotes Received " , " downvoters " , Leaderboard . get_downvotes_lb , None , v , None , None ) )
2023-04-25 09:50:47 +00:00
2023-08-03 22:14:16 +00:00
leaderboards . append ( Leaderboard ( " Casino winnings (top) " , " casino winnings " , " casino-winnings-top " , " Casino Winnings " , None , Leaderboard . get_winnings_lb , CasinoGame . winnings , v , None , None ) )
leaderboards . append ( Leaderboard ( " Casino winnings (bottom) " , " casino winnings " , " casino-winnings-bottom " , " Casino Winnings " , None , Leaderboard . get_winnings_lb , CasinoGame . winnings , v , None , None , 25 , False ) )
2023-08-03 21:57:50 +00:00
2023-09-15 15:46:58 +00:00
leaderboards . append ( Leaderboard ( " Average upvotes per post " , " average upvotes per post " , " average-upvotes-per-post " , " Average Upvotes per Post " , " posts " , Leaderboard . get_avg_upvotes_lb , Post , v , None , None ) )
leaderboards . append ( Leaderboard ( " Average upvotes per comment " , " average upvotes per comment " , " average-upvotes-per-comment " , " Average Upvotes per Comment " , " comments " , Leaderboard . get_avg_upvotes_lb , Comment , v , None , None ) )
2023-09-15 14:26:20 +00:00
2023-09-05 19:10:49 +00:00
return render_template ( " leaderboard_cached.html " , v = v , leaderboards = leaderboards )
@app.get ( " /leaderboard " )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
@auth_required
def leaderboard ( v ) :
return render_template ( " leaderboard.html " , v = v , leaderboard_cached = leaderboard_cached ( v ) )
2022-05-04 23:09:46 +00:00
2023-09-07 16:16:13 +00:00
@app.get ( " /@<username>/css " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
2023-09-07 16:16:13 +00:00
def get_css ( username ) :
user = get_user ( username , attributes = [ User . css , User . background ] )
css = user . css
bg = user . background
2022-12-29 14:20:27 +00:00
if bg :
if not css : css = ' '
css + = f '''
body { {
2023-01-01 09:59:29 +00:00
background : url ( " {bg} " ) center center fixed ;
2022-12-29 14:20:27 +00:00
} }
'''
if ' anime/ ' not in bg and not bg . startswith ( ' /images/ ' ) :
css + = ' body { background-size: cover} '
2022-06-26 05:26:45 +00:00
if not css : abort ( 404 )
2022-12-29 14:20:27 +00:00
resp = make_response ( css )
2022-06-26 05:07:28 +00:00
resp . headers [ " Content-Type " ] = " text/css "
return resp
2023-09-07 16:16:13 +00:00
@app.get ( " /@<username>/profilecss " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
2023-09-07 16:16:13 +00:00
def get_profilecss ( username ) :
user = get_user ( username , attributes = [ User . profilecss , User . profile_background ] )
css = user . profilecss
bg = user . profile_background
2022-12-29 14:20:27 +00:00
if bg :
if not css : css = ' '
2023-01-01 09:59:29 +00:00
css + = f '''
body { {
background : url ( " {bg} " ) center center fixed ;
background - size : auto ;
} }
'''
2022-06-26 05:26:45 +00:00
if not css : abort ( 404 )
2022-12-29 14:20:27 +00:00
resp = make_response ( css )
2022-06-26 05:07:28 +00:00
resp . headers [ " Content-Type " ] = " text/css "
return resp
2022-12-29 10:39:10 +00:00
@app.post ( " /subscribe/<int:post_id> " )
2023-02-27 05:33:45 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath )
2023-04-02 06:52:26 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath , key_func = get_ID )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
def subscribe ( v , post_id ) :
2023-06-07 23:26:32 +00:00
existing = g . db . query ( Subscription ) . filter_by ( user_id = v . id , post_id = post_id ) . one_or_none ( )
2022-09-27 00:19:52 +00:00
if not existing :
2023-06-07 23:26:32 +00:00
new_sub = Subscription ( user_id = v . id , post_id = post_id )
2023-03-16 06:27:58 +00:00
g . db . add ( new_sub )
2023-06-08 01:56:12 +00:00
cache . delete_memoized ( userpagelisting )
2022-09-11 14:32:00 +00:00
return { " message " : " Subscribed to post successfully! " }
2023-01-01 11:36:20 +00:00
2022-12-29 10:39:10 +00:00
@app.post ( " /unsubscribe/<int:post_id> " )
2023-02-27 05:33:45 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath )
2023-04-02 06:52:26 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath , key_func = get_ID )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
def unsubscribe ( v , post_id ) :
2023-06-07 23:26:32 +00:00
existing = g . db . query ( Subscription ) . filter_by ( user_id = v . id , post_id = post_id ) . one_or_none ( )
2022-09-27 00:19:52 +00:00
if existing :
2023-03-16 06:27:58 +00:00
g . db . delete ( existing )
2023-06-08 01:56:12 +00:00
cache . delete_memoized ( userpagelisting )
2022-09-11 14:32:00 +00:00
return { " message " : " Unsubscribed from post successfully! " }
2022-05-04 23:09:46 +00:00
@app.post ( " /@<username>/message " )
2023-07-11 14:36:46 +00:00
@app.post ( " /id/<int:id>/message " )
2023-02-27 05:33:45 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath )
2023-04-02 06:52:26 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath , key_func = get_ID )
2023-07-13 13:50:46 +00:00
@limiter.limit ( " 10/minute;20/hour;50/day " , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( " 10/minute;20/hour;50/day " , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2023-09-14 16:49:46 +00:00
@auth_required
2023-10-05 09:27:33 +00:00
def message ( v , username = None , id = None ) :
2023-07-11 14:36:46 +00:00
if id :
user = get_account ( id , v = v , include_blocks = True )
else :
user = get_user ( username , v = v , include_blocks = True )
2022-07-03 17:55:25 +00:00
2022-11-17 22:50:06 +00:00
if user . id == MODMAIL_ID :
abort ( 403 , " Please use /contact to contact the admins " )
2022-07-03 17:55:25 +00:00
if hasattr ( user , ' is_blocking ' ) and user . is_blocking :
2022-11-12 10:11:46 +00:00
abort ( 403 , f " You ' re blocking @ { user . username } " )
2022-05-04 23:09:46 +00:00
2022-10-06 05:16:09 +00:00
if v . admin_level < = PERMS [ ' MESSAGE_BLOCKED_USERS ' ] and hasattr ( user , ' is_blocked ' ) and user . is_blocked :
2023-01-27 11:57:29 +00:00
abort ( 403 , f " @ { user . username } is blocking you! " )
2022-05-04 23:09:46 +00:00
2023-09-06 18:02:47 +00:00
if user . has_muted ( v ) :
abort ( 403 , f " @ { user . username } is muting notifications from you, so messaging them is pointless! " )
2023-07-29 19:13:37 +00:00
body = request . values . get ( " message " , " " )
2023-07-29 19:17:50 +00:00
body = body [ : COMMENT_BODY_LENGTH_LIMIT ] . strip ( )
2023-01-23 09:58:38 +00:00
2023-05-12 15:27:46 +00:00
if not g . is_tor and get_setting ( " dm_media " ) :
2023-05-04 21:52:37 +00:00
body = process_files ( request . files , v , body , is_dm = True , dm_user = user )
2023-07-30 06:20:02 +00:00
body = body [ : COMMENT_BODY_LENGTH_LIMIT ] . strip ( ) #process_files potentially adds characters to the post
2023-01-23 09:58:38 +00:00
2023-05-04 21:52:37 +00:00
if not body : abort ( 400 , " Message is empty! " )
2022-10-13 13:26:59 +00:00
2023-05-04 21:52:37 +00:00
body_html = sanitize ( body )
2022-05-04 23:09:46 +00:00
2023-03-16 06:27:58 +00:00
existing = g . db . query ( Comment . id ) . filter (
2022-12-10 14:00:03 +00:00
Comment . author_id == v . id ,
Comment . sentto == user . id ,
Comment . body_html == body_html
) . first ( )
2022-05-04 23:09:46 +00:00
2023-01-27 11:57:29 +00:00
if existing : abort ( 403 , " Message already exists! " )
2022-05-04 23:09:46 +00:00
c = Comment ( author_id = v . id ,
2023-06-23 13:46:42 +00:00
parent_post = None ,
2022-09-04 23:15:37 +00:00
level = 1 ,
sentto = user . id ,
2023-05-04 21:52:37 +00:00
body = body ,
2022-09-04 23:15:37 +00:00
body_html = body_html
)
2023-03-16 06:27:58 +00:00
g . db . add ( c )
g . db . flush ( )
2022-10-21 00:28:05 +00:00
execute_blackjack ( v , c , c . body_html , ' message ' )
2022-11-30 17:37:35 +00:00
execute_under_siege ( v , c , c . body_html , ' message ' )
2022-05-04 23:09:46 +00:00
c . top_comment_id = c . id
2023-10-05 10:09:58 +00:00
if user . id not in BOT_IDs and can_see ( user , v ) :
2023-09-05 12:04:24 +00:00
g . db . flush ( )
2023-03-16 06:27:58 +00:00
notif = g . db . query ( Notification ) . filter_by ( comment_id = c . id , user_id = user . id ) . one_or_none ( )
2022-05-28 02:20:31 +00:00
if not notif :
notif = Notification ( comment_id = c . id , user_id = user . id )
2023-03-16 06:27:58 +00:00
g . db . add ( notif )
2022-05-04 23:09:46 +00:00
2023-08-20 02:36:43 +00:00
title = f ' New message from @ { c . author_name } '
2022-06-27 19:02:24 +00:00
2023-08-20 02:36:43 +00:00
url = f ' { SITE_FULL } /notifications/messages '
2022-06-27 19:02:24 +00:00
2023-08-20 02:36:43 +00:00
push_notif ( { user . id } , title , body , url )
2022-05-04 23:09:46 +00:00
return { " message " : " Message sent! " }
@app.get ( " /2faqr/<secret> " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def mfa_qr ( v , secret ) :
2022-05-04 23:09:46 +00:00
x = pyotp . TOTP ( secret )
qr = qrcode . QRCode (
error_correction = qrcode . constants . ERROR_CORRECT_L
)
2022-12-20 21:20:32 +00:00
qr . add_data ( x . provisioning_uri ( v . username , issuer_name = SITE ) )
2022-05-04 23:09:46 +00:00
img = qr . make_image ( fill_color = " #000000 " , back_color = " white " )
mem = io . BytesIO ( )
img . save ( mem , format = " PNG " )
mem . seek ( 0 , 0 )
2022-09-11 01:56:47 +00:00
return send_file ( mem , mimetype = " image/png " , as_attachment = False )
2022-05-04 23:09:46 +00:00
@app.get ( " /is_available/<name> " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( " 100/day " , deduct_when = lambda response : response . status_code < 400 )
2023-07-30 00:42:06 +00:00
def is_available ( name ) :
2022-05-04 23:09:46 +00:00
2023-08-23 21:57:39 +00:00
name = name . strip ( )
2022-05-04 23:09:46 +00:00
2023-09-26 21:57:33 +00:00
if len ( name ) < 3 or len ( name ) > 25 :
return { name : False }
2022-05-04 23:09:46 +00:00
2023-09-26 21:57:33 +00:00
existing = get_user ( name , graceful = True )
2022-05-04 23:09:46 +00:00
2023-09-26 21:57:33 +00:00
if existing :
2022-05-04 23:09:46 +00:00
return { name : False }
else :
return { name : True }
2023-07-12 01:07:58 +00:00
@app.get ( " /id/<int:id> " )
2023-07-11 17:55:38 +00:00
@app.route ( " /id/<int:id>/<path:path> " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2023-07-12 01:11:47 +00:00
@auth_required
2023-07-12 01:13:40 +00:00
def user_id ( v , id , path = ' ' ) :
2022-05-04 23:09:46 +00:00
user = get_account ( id )
2023-07-12 01:13:40 +00:00
if path :
return redirect ( f ' /@ { user . username } / { path } ' )
return redirect ( f ' /@ { user . username } ' )
2023-01-01 11:36:20 +00:00
2022-05-04 23:09:46 +00:00
@app.get ( " /u/<username> " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2023-07-12 01:11:47 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def redditor_moment_redirect ( v , username ) :
2022-05-04 23:09:46 +00:00
return redirect ( f " /@ { username } " )
2023-05-05 06:01:14 +00:00
@app.get ( " /@<username>/blockers " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-09-23 18:30:17 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def blockers ( v , username ) :
2023-02-27 15:38:12 +00:00
u = get_user ( username , v = v )
2022-09-23 18:30:17 +00:00
2023-05-05 05:23:59 +00:00
page = get_page ( )
2022-11-27 00:59:16 +00:00
2023-05-05 06:01:14 +00:00
users = g . db . query ( UserBlock , User ) . join ( UserBlock , UserBlock . target_id == u . id ) \
. filter ( UserBlock . user_id == User . id )
2022-11-27 00:59:16 +00:00
2023-05-05 21:44:24 +00:00
total = users . count ( )
2022-11-27 00:59:16 +00:00
2023-05-05 06:01:14 +00:00
users = users . order_by ( UserBlock . created_utc . desc ( ) ) \
. offset ( PAGE_SIZE * ( page - 1 ) ) . limit ( PAGE_SIZE ) . all ( )
2022-09-23 18:30:17 +00:00
2023-05-05 21:44:24 +00:00
return render_template ( " userpage/blockers.html " , v = v , u = u , users = users , page = page , total = total )
2023-05-05 06:01:14 +00:00
2023-06-24 17:39:50 +00:00
@app.get ( " /@<username>/blocking " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2023-06-24 17:39:50 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def blocking ( v , username ) :
2023-06-24 17:39:50 +00:00
u = get_user ( username , v = v )
page = get_page ( )
users = g . db . query ( UserBlock , User ) . join ( UserBlock , UserBlock . user_id == u . id ) \
. filter ( UserBlock . target_id == User . id )
total = users . count ( )
users = users . order_by ( UserBlock . created_utc . desc ( ) ) \
. offset ( PAGE_SIZE * ( page - 1 ) ) . limit ( PAGE_SIZE ) . all ( )
return render_template ( " userpage/blocking.html " , v = v , u = u , users = users , page = page , total = total )
2023-09-07 09:35:06 +00:00
@app.get ( " /@<username>/muters " )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
@auth_required
def muters ( v , username ) :
u = get_user ( username , v = v )
page = get_page ( )
users = g . db . query ( UserMute , User ) . join ( UserMute , UserMute . target_id == u . id ) \
. filter ( UserMute . user_id == User . id )
total = users . count ( )
users = users . order_by ( UserMute . created_utc . desc ( ) ) \
. offset ( PAGE_SIZE * ( page - 1 ) ) . limit ( PAGE_SIZE ) . all ( )
return render_template ( " userpage/muters.html " , v = v , u = u , users = users , page = page , total = total )
@app.get ( " /@<username>/muting " )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
@auth_required
def muting ( v , username ) :
u = get_user ( username , v = v )
page = get_page ( )
users = g . db . query ( UserMute , User ) . join ( UserMute , UserMute . user_id == u . id ) \
. filter ( UserMute . target_id == User . id )
total = users . count ( )
users = users . order_by ( UserMute . created_utc . desc ( ) ) \
. offset ( PAGE_SIZE * ( page - 1 ) ) . limit ( PAGE_SIZE ) . all ( )
return render_template ( " userpage/muting.html " , v = v , u = u , users = users , page = page , total = total )
2023-09-07 15:26:31 +00:00
2023-05-05 06:01:14 +00:00
@app.get ( " /@<username>/followers " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def followers ( v , username ) :
2023-02-27 15:38:12 +00:00
u = get_user ( username , v = v )
2022-09-05 21:52:56 +00:00
2023-05-05 05:23:59 +00:00
page = get_page ( )
2022-11-27 00:59:16 +00:00
2023-05-05 06:01:14 +00:00
users = g . db . query ( Follow , User ) . join ( Follow , Follow . target_id == u . id ) \
. filter ( Follow . user_id == User . id )
2022-11-27 00:59:16 +00:00
2023-05-05 21:44:24 +00:00
total = users . count ( )
2023-05-05 05:57:02 +00:00
2023-05-05 06:01:14 +00:00
users = users . order_by ( Follow . created_utc . desc ( ) ) \
. offset ( PAGE_SIZE * ( page - 1 ) ) . limit ( PAGE_SIZE ) . all ( )
2022-11-27 00:59:16 +00:00
2023-05-05 21:44:24 +00:00
return render_template ( " userpage/followers.html " , v = v , u = u , users = users , page = page , total = total )
2022-05-04 23:09:46 +00:00
@app.get ( " /@<username>/following " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def following ( v , username ) :
2023-02-27 15:38:12 +00:00
u = get_user ( username , v = v )
2023-09-04 18:48:06 +00:00
2023-05-05 05:23:59 +00:00
page = get_page ( )
2022-11-27 00:59:16 +00:00
2023-09-04 18:48:06 +00:00
users = g . db . query ( Follow , User ) . join ( Follow , Follow . user_id == u . id ) \
2023-05-05 06:01:14 +00:00
. filter ( Follow . target_id == User . id )
2022-11-27 00:59:16 +00:00
2023-05-05 21:44:24 +00:00
total = users . count ( )
2023-05-05 06:01:14 +00:00
users = users . order_by ( Follow . created_utc . desc ( ) ) \
. offset ( PAGE_SIZE * ( page - 1 ) ) . limit ( PAGE_SIZE ) . all ( )
2022-11-27 00:59:16 +00:00
2023-05-05 21:44:24 +00:00
return render_template ( " userpage/following.html " , v = v , u = u , users = users , page = page , total = total )
2022-05-04 23:09:46 +00:00
2022-11-27 00:20:54 +00:00
@app.get ( " /@<username>/views " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def visitors ( v , username ) :
2023-02-27 15:38:12 +00:00
u = get_user ( username , v = v )
2022-11-27 00:20:54 +00:00
2023-05-05 05:23:59 +00:00
page = get_page ( )
2022-11-27 00:20:54 +00:00
2023-05-05 01:08:40 +00:00
views = g . db . query ( ViewerRelationship ) . filter_by ( user_id = u . id )
2023-05-05 21:44:24 +00:00
total = views . count ( )
2023-05-05 01:08:40 +00:00
views = views . order_by ( ViewerRelationship . last_view_utc . desc ( ) ) . offset ( PAGE_SIZE * ( page - 1 ) ) . limit ( PAGE_SIZE ) . all ( )
2022-11-27 00:20:54 +00:00
2023-05-05 21:44:24 +00:00
return render_template ( " userpage/views.html " , v = v , u = u , views = views , total = total , page = page )
2022-05-04 23:09:46 +00:00
2023-02-02 00:42:47 +00:00
@cache.memoize ( )
2023-07-30 00:42:06 +00:00
def userpagelisting ( user , v = None , page = 1 , sort = " new " , t = " all " ) :
2023-06-07 23:26:32 +00:00
posts = g . db . query ( Post ) . filter_by ( author_id = user . id , is_pinned = False ) . options ( load_only ( Post . id ) )
2022-11-15 09:19:08 +00:00
if not ( v and ( v . admin_level > = PERMS [ ' POST_COMMENT_MODERATION ' ] or v . id == user . id ) ) :
posts = posts . filter_by ( is_banned = False , private = False , ghost = False , deleted_utc = 0 )
2023-06-07 23:26:32 +00:00
posts = apply_time_filter ( t , posts , Post )
2023-05-05 21:44:24 +00:00
total = posts . count ( )
2023-06-07 23:26:32 +00:00
posts = sort_objects ( sort , posts , Post )
2023-05-05 01:04:07 +00:00
posts = posts . offset ( PAGE_SIZE * ( page - 1 ) ) . limit ( PAGE_SIZE ) . all ( )
2023-05-05 21:44:24 +00:00
return [ x . id for x in posts ] , total
2022-05-04 23:09:46 +00:00
@app.get ( " /@<username> " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
2023-05-03 14:50:42 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def u_username_wall ( v , username ) :
2023-02-27 15:38:12 +00:00
u = get_user ( username , v = v , include_blocks = True )
2022-12-03 01:49:07 +00:00
if username != u . username :
2022-12-05 01:33:43 +00:00
return redirect ( f " /@ { u . username } " )
2022-12-03 01:49:07 +00:00
if v and hasattr ( u , ' is_blocking ' ) and u . is_blocking :
2023-03-03 01:53:04 +00:00
if g . is_api_or_xhr :
2022-12-03 01:49:07 +00:00
abort ( 403 , f " You are blocking @ { u . username } . " )
2023-06-24 17:39:50 +00:00
return render_template ( " userpage/blocked.html " , u = u , v = v ) , 403
2022-12-03 01:49:07 +00:00
2022-12-05 14:51:47 +00:00
is_following = v and u . has_follower ( v )
2023-10-05 09:44:42 +00:00
if v and v . id != u . id and not v . can_see_shadowbanned and not session . get ( " GLOBAL " ) :
2023-08-20 16:24:26 +00:00
gevent . spawn ( _add_profile_view , v . id , u . id )
2022-12-05 14:51:47 +00:00
2023-05-05 05:23:59 +00:00
page = get_page ( )
2022-12-03 01:49:07 +00:00
2022-12-05 01:23:48 +00:00
if v :
2023-02-27 16:16:12 +00:00
comments , output = get_comments_v_properties ( v , None , Comment . wall_user_id == u . id )
2022-12-05 01:23:48 +00:00
else :
2023-03-16 06:27:58 +00:00
comments = g . db . query ( Comment ) . filter ( Comment . wall_user_id == u . id )
2022-12-03 01:49:07 +00:00
comments = comments . filter ( Comment . level == 1 )
if not v or ( v . id != u . id and v . admin_level < PERMS [ ' POST_COMMENT_MODERATION ' ] ) :
comments = comments . filter (
Comment . is_banned == False ,
Comment . ghost == False ,
Comment . deleted_utc == 0
)
2023-05-05 21:44:24 +00:00
total = comments . count ( )
2022-12-05 01:23:48 +00:00
comments = comments . order_by ( Comment . created_utc . desc ( ) ) \
2023-05-05 01:04:07 +00:00
. offset ( PAGE_SIZE * ( page - 1 ) ) . limit ( PAGE_SIZE ) . all ( )
2022-12-05 01:23:48 +00:00
if v :
comments = [ c [ 0 ] for c in comments ]
2022-12-03 01:49:07 +00:00
2023-03-03 01:53:04 +00:00
if v and v . client :
2023-06-08 00:49:37 +00:00
return { " data " : [ c . json for c in comments ] }
2023-01-01 11:36:20 +00:00
2023-05-05 21:44:24 +00:00
return render_template ( " userpage/wall.html " , u = u , v = v , listing = comments , page = page , total = total , is_following = is_following , standalone = True , render_replies = True , wall = True )
2022-12-03 01:49:07 +00:00
2022-12-29 10:39:10 +00:00
@app.get ( " /@<username>/wall/comment/<int:cid> " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
2023-05-03 14:50:42 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def u_username_wall_comment ( v , username , cid ) :
2022-12-05 03:01:50 +00:00
comment = get_comment ( cid , v = v )
if not comment . wall_user_id : abort ( 400 )
2023-10-05 10:09:58 +00:00
if not can_see ( v , comment ) : abort ( 403 )
2022-12-05 03:01:50 +00:00
u = comment . wall_user
if v and hasattr ( u , ' is_blocking ' ) and u . is_blocking :
2023-03-03 01:53:04 +00:00
if g . is_api_or_xhr :
2022-12-05 03:01:50 +00:00
abort ( 403 , f " You are blocking @ { u . username } . " )
2023-06-24 17:39:50 +00:00
return render_template ( " userpage/blocked.html " , u = u , v = v ) , 403
2022-12-05 03:01:50 +00:00
2022-12-05 14:51:47 +00:00
is_following = v and u . has_follower ( v )
2023-10-05 09:44:42 +00:00
if v and v . id != u . id and not v . can_see_shadowbanned and not session . get ( " GLOBAL " ) :
2023-08-20 16:24:26 +00:00
gevent . spawn ( _add_profile_view , v . id , u . id )
2022-12-05 14:51:47 +00:00
2022-12-05 03:01:50 +00:00
if v and request . values . get ( " read " ) :
2023-08-20 16:24:26 +00:00
gevent . spawn ( _mark_comment_as_read , comment . id , v . id )
2022-12-05 03:01:50 +00:00
2022-12-18 14:48:48 +00:00
try : context = min ( int ( request . values . get ( " context " , 8 ) ) , 8 )
except : context = 8
2022-12-05 03:01:50 +00:00
comment_info = comment
c = comment
while context and c . level > 1 :
c = c . parent_comment
context - = 1
top_comment = c
2023-01-01 11:36:20 +00:00
2022-12-05 03:01:50 +00:00
if v :
# this is required because otherwise the vote and block
# props won't save properly unless you put them in a list
2023-02-27 16:16:12 +00:00
output = get_comments_v_properties ( v , None , Comment . top_comment_id == c . top_comment_id ) [ 1 ]
2023-01-01 11:36:20 +00:00
2023-06-08 00:49:37 +00:00
if v and v . client : return top_comment . json
2022-12-05 03:01:50 +00:00
2023-05-05 21:44:24 +00:00
return render_template ( " userpage/wall.html " , u = u , v = v , listing = [ top_comment ] , page = 1 , is_following = is_following , standalone = True , render_replies = True , wall = True , comment_info = comment_info , total = 1 )
2022-12-05 03:01:50 +00:00
2022-12-03 01:49:07 +00:00
@app.get ( " /@<username>/posts " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
2023-05-03 14:50:42 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def u_username ( v , username ) :
2023-02-27 15:38:12 +00:00
u = get_user ( username , v = v , include_blocks = True )
2022-05-04 23:09:46 +00:00
if username != u . username :
2023-09-20 13:54:34 +00:00
return redirect ( SITE_FULL + request . full_path . replace ( f ' /@ { username } /posts ' , f ' /@ { u . username } /posts ' ) )
2022-12-05 14:51:47 +00:00
2022-05-04 23:09:46 +00:00
if v and hasattr ( u , ' is_blocking ' ) and u . is_blocking :
2023-03-03 01:53:04 +00:00
if g . is_api_or_xhr :
2022-10-11 13:01:39 +00:00
abort ( 403 , f " You are blocking @ { u . username } . " )
2023-06-24 17:39:50 +00:00
return render_template ( " userpage/blocked.html " , u = u , v = v ) , 403
2022-05-04 23:09:46 +00:00
2022-12-05 14:51:47 +00:00
is_following = v and u . has_follower ( v )
2022-12-05 15:16:11 +00:00
if not u . is_visible_to ( v ) :
2023-03-03 01:53:04 +00:00
if g . is_api_or_xhr :
2022-12-05 15:16:11 +00:00
abort ( 403 , f " @ { u . username } ' s userpage is private " )
return render_template ( " userpage/private.html " , u = u , v = v , is_following = is_following ) , 403
2023-10-05 09:44:42 +00:00
if v and v . id != u . id and not v . can_see_shadowbanned and not session . get ( " GLOBAL " ) :
2023-08-20 16:24:26 +00:00
gevent . spawn ( _add_profile_view , v . id , u . id )
2022-05-04 23:09:46 +00:00
sort = request . values . get ( " sort " , " new " )
t = request . values . get ( " t " , " all " )
2023-05-05 05:23:59 +00:00
page = get_page ( )
2022-05-04 23:09:46 +00:00
2023-05-05 21:44:24 +00:00
ids , total = userpagelisting ( u , v = v , page = page , sort = sort , t = t )
2022-05-04 23:09:46 +00:00
2022-11-07 11:27:40 +00:00
if page == 1 and sort == ' new ' :
2022-05-04 23:09:46 +00:00
sticky = [ ]
2023-06-07 23:26:32 +00:00
sticky = g . db . query ( Post ) . filter_by ( is_pinned = True , author_id = u . id , is_banned = False ) . all ( )
2022-05-04 23:09:46 +00:00
if sticky :
for p in sticky :
ids = [ p . id ] + ids
2023-07-17 14:49:26 +00:00
listing = get_posts ( ids , v = v )
2022-05-04 23:09:46 +00:00
if u . unban_utc :
2023-03-03 01:53:04 +00:00
if v and v . client :
2023-06-08 00:49:37 +00:00
return { " data " : [ x . json for x in listing ] }
2023-01-01 11:36:20 +00:00
2023-06-07 23:26:32 +00:00
return render_template ( " userpage/posts.html " ,
2022-05-04 23:09:46 +00:00
unban = u . unban_string ,
u = u ,
v = v ,
listing = listing ,
page = page ,
sort = sort ,
t = t ,
2023-05-05 21:44:24 +00:00
total = total ,
2022-10-08 20:30:27 +00:00
is_following = is_following )
2022-05-04 23:09:46 +00:00
2023-03-03 01:53:04 +00:00
if v and v . client :
2023-06-08 00:49:37 +00:00
return { " data " : [ x . json for x in listing ] }
2023-01-01 11:36:20 +00:00
2023-06-07 23:26:32 +00:00
return render_template ( " userpage/posts.html " ,
2022-05-04 23:09:46 +00:00
u = u ,
v = v ,
listing = listing ,
page = page ,
sort = sort ,
t = t ,
2023-05-05 21:44:24 +00:00
total = total ,
2022-10-08 20:30:27 +00:00
is_following = is_following )
2022-05-04 23:09:46 +00:00
@app.get ( " /@<username>/comments " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
2023-05-03 14:50:42 +00:00
@auth_required
2023-07-08 13:32:14 +00:00
def u_username_comments ( username , v ) :
2023-02-27 15:38:12 +00:00
u = get_user ( username , v = v , include_blocks = True )
2022-10-30 07:36:50 +00:00
if username != u . username :
return redirect ( f " /@ { u . username } /comments " )
2022-05-04 23:09:46 +00:00
if v and hasattr ( u , ' is_blocking ' ) and u . is_blocking :
2023-03-03 01:53:04 +00:00
if g . is_api_or_xhr :
2022-10-11 13:01:39 +00:00
abort ( 403 , f " You are blocking @ { u . username } . " )
2023-06-24 17:39:50 +00:00
return render_template ( " userpage/blocked.html " , u = u , v = v ) , 403
2022-05-04 23:09:46 +00:00
2022-12-05 14:51:47 +00:00
is_following = v and u . has_follower ( v )
2022-12-05 15:16:11 +00:00
if not u . is_visible_to ( v ) :
2023-03-03 01:53:04 +00:00
if g . is_api_or_xhr :
2022-12-05 15:16:11 +00:00
abort ( 403 , f " @ { u . username } ' s userpage is private " )
return render_template ( " userpage/private.html " , u = u , v = v , is_following = is_following ) , 403
2023-10-05 09:44:42 +00:00
if v and v . id != u . id and not v . can_see_shadowbanned and not session . get ( " GLOBAL " ) :
2023-08-20 16:24:26 +00:00
gevent . spawn ( _add_profile_view , v . id , u . id )
2022-12-05 14:51:47 +00:00
2023-05-05 05:23:59 +00:00
page = get_page ( )
2023-01-01 11:36:20 +00:00
2023-08-23 21:57:39 +00:00
sort = request . values . get ( " sort " , " new " )
t = request . values . get ( " t " , " all " )
2022-05-04 23:09:46 +00:00
2022-08-08 22:21:59 +00:00
comment_post_author = aliased ( User )
2023-05-05 01:04:07 +00:00
comments = g . db . query ( Comment ) . options ( load_only ( Comment . id ) ) \
2022-12-07 16:51:51 +00:00
. outerjoin ( Comment . post ) \
2023-06-07 23:26:32 +00:00
. outerjoin ( comment_post_author , Post . author ) \
2022-08-08 22:21:59 +00:00
. filter (
Comment . author_id == u . id ,
2023-06-23 13:46:42 +00:00
or_ ( Comment . parent_post != None , Comment . wall_user_id != None ) ,
2022-08-08 22:21:59 +00:00
)
2022-05-04 23:09:46 +00:00
2022-10-06 01:16:52 +00:00
if not v or ( v . id != u . id and v . admin_level < PERMS [ ' POST_COMMENT_MODERATION ' ] ) :
2022-08-08 22:21:59 +00:00
comments = comments . filter (
Comment . is_banned == False ,
Comment . ghost == False ,
2022-10-06 10:37:45 +00:00
Comment . deleted_utc == 0
2022-08-08 22:21:59 +00:00
)
2022-06-18 23:55:45 +00:00
2022-07-09 10:32:49 +00:00
comments = apply_time_filter ( t , comments , Comment )
2022-05-04 23:09:46 +00:00
2023-05-05 21:44:24 +00:00
total = comments . count ( )
2023-05-05 01:04:07 +00:00
2023-02-27 15:38:12 +00:00
comments = sort_objects ( sort , comments , Comment )
2022-05-04 23:09:46 +00:00
2023-05-05 01:04:07 +00:00
comments = comments . offset ( PAGE_SIZE * ( page - 1 ) ) . limit ( PAGE_SIZE ) . all ( )
2023-03-25 18:25:38 +00:00
ids = [ x . id for x in comments ]
2023-03-22 19:53:59 +00:00
2023-03-25 18:25:38 +00:00
listing = get_comments ( ids , v = v )
2022-05-04 23:09:46 +00:00
2023-03-03 01:53:04 +00:00
if v and v . client :
2023-06-08 00:49:37 +00:00
return { " data " : [ c . json for c in listing ] }
2023-01-01 11:36:20 +00:00
2023-05-05 21:44:24 +00:00
return render_template ( " userpage/comments.html " , u = u , v = v , listing = listing , page = page , sort = sort , t = t , total = total , is_following = is_following , standalone = True )
2023-03-24 16:08:55 +00:00
2022-05-04 23:09:46 +00:00
@app.get ( " /@<username>/info " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-07-08 13:32:14 +00:00
def u_username_info ( username , v ) :
2022-05-04 23:09:46 +00:00
2023-08-23 21:57:39 +00:00
user = get_user ( username , v = v , include_blocks = True )
2022-05-04 23:09:46 +00:00
if hasattr ( user , ' is_blocking ' ) and user . is_blocking :
2022-11-12 10:11:46 +00:00
abort ( 401 , f " You ' re blocking @ { user . username } " )
2022-05-04 23:09:46 +00:00
elif hasattr ( user , ' is_blocked ' ) and user . is_blocked :
2023-01-27 11:57:29 +00:00
abort ( 403 , f " @ { user . username } is blocking you! " )
2022-05-04 23:09:46 +00:00
return user . json
2022-12-29 10:39:10 +00:00
@app.get ( " /<int:id>/info " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-07-08 13:32:14 +00:00
def u_user_id_info ( id , v ) :
2022-05-04 23:09:46 +00:00
2023-07-27 19:47:46 +00:00
user = get_account ( id , v = v , include_blocks = True )
2022-05-04 23:09:46 +00:00
if hasattr ( user , ' is_blocking ' ) and user . is_blocking :
2022-11-12 10:11:46 +00:00
abort ( 403 , f " You ' re blocking @ { user . username } " )
2022-05-04 23:09:46 +00:00
elif hasattr ( user , ' is_blocked ' ) and user . is_blocked :
2023-01-27 11:57:29 +00:00
abort ( 403 , f " @ { user . username } is blocking you! " )
2022-05-04 23:09:46 +00:00
return user . json
@app.post ( " /follow/<username> " )
2023-02-27 05:33:45 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath )
2023-04-02 06:52:26 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath , key_func = get_ID )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
def follow_user ( username , v ) :
2023-02-27 15:38:12 +00:00
target = get_user ( username , v = v )
2022-05-04 23:09:46 +00:00
2022-08-20 19:50:18 +00:00
if target . id == v . id :
2022-10-11 13:01:39 +00:00
abort ( 400 , " You can ' t follow yourself! " )
2022-05-04 23:09:46 +00:00
2023-03-16 06:27:58 +00:00
if g . db . query ( Follow ) . filter_by ( user_id = v . id , target_id = target . id ) . one_or_none ( ) :
2022-09-11 14:32:00 +00:00
return { " message " : f " @ { target . username } has been followed! " }
2022-05-04 23:09:46 +00:00
new_follow = Follow ( user_id = v . id , target_id = target . id )
2023-03-16 06:27:58 +00:00
g . db . add ( new_follow )
2022-05-04 23:09:46 +00:00
2023-08-20 02:00:35 +00:00
target . stored_subscriber_count + = 1
2023-03-16 06:27:58 +00:00
g . db . add ( target )
2022-05-04 23:09:46 +00:00
2023-10-04 19:34:00 +00:00
send_notification ( target . id , f " @ { v . username } has followed you! " )
2022-05-04 23:09:46 +00:00
2022-09-11 14:32:00 +00:00
return { " message " : f " @ { target . username } has been followed! " }
2022-05-04 23:09:46 +00:00
@app.post ( " /unfollow/<username> " )
2023-02-27 05:33:45 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath )
2023-04-02 06:52:26 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath , key_func = get_ID )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
def unfollow_user ( username , v ) :
target = get_user ( username )
2023-03-16 06:27:58 +00:00
follow = g . db . query ( Follow ) . filter_by ( user_id = v . id , target_id = target . id ) . one_or_none ( )
2022-05-04 23:09:46 +00:00
if follow :
2023-03-16 06:27:58 +00:00
g . db . delete ( follow )
2023-01-01 11:36:20 +00:00
2023-08-20 02:00:35 +00:00
target . stored_subscriber_count - = 1
2023-03-16 06:27:58 +00:00
g . db . add ( target )
2022-05-04 23:09:46 +00:00
2023-10-04 19:34:00 +00:00
send_notification ( target . id , f " @ { v . username } has unfollowed you! " )
2022-05-04 23:09:46 +00:00
2023-02-27 00:47:51 +00:00
else :
2023-02-27 00:48:19 +00:00
abort ( 400 , f " You ' re not even following @ { target . username } to begin with! " )
2023-02-27 00:47:51 +00:00
2022-05-04 23:09:46 +00:00
2022-09-11 14:32:00 +00:00
return { " message " : f " @ { target . username } has been unfollowed! " }
2022-05-04 23:09:46 +00:00
@app.post ( " /remove_follow/<username> " )
2023-02-27 05:33:45 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath )
2023-04-02 06:52:26 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath , key_func = get_ID )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
def remove_follow ( username , v ) :
target = get_user ( username )
2023-03-16 06:27:58 +00:00
follow = g . db . query ( Follow ) . filter_by ( user_id = target . id , target_id = v . id ) . one_or_none ( )
2022-05-04 23:09:46 +00:00
2022-09-11 14:32:00 +00:00
if not follow : return { " message " : f " @ { target . username } has been removed as a follower! " }
2022-05-04 23:09:46 +00:00
2023-03-16 06:27:58 +00:00
g . db . delete ( follow )
2023-01-01 11:36:20 +00:00
2023-08-20 02:00:35 +00:00
v . stored_subscriber_count - = 1
2023-03-16 06:27:58 +00:00
g . db . add ( v )
2022-05-04 23:09:46 +00:00
send_repeatable_notification ( target . id , f " @ { v . username } has removed your follow! " )
2022-09-11 14:32:00 +00:00
return { " message " : f " @ { target . username } has been removed as a follower! " }
2022-05-04 23:09:46 +00:00
2023-01-25 03:18:17 +00:00
2022-12-29 10:39:10 +00:00
@app.get ( " /pp/<int:id> " )
@app.get ( " /uid/<int:id>/pic " )
@app.get ( " /uid/<int:id>/pic/profile " )
2022-05-22 17:23:52 +00:00
@limiter.exempt
2022-06-17 19:21:26 +00:00
def user_profile_uid ( id ) :
2023-01-25 03:18:17 +00:00
return redirect ( get_profile_picture ( id ) )
2022-05-04 23:09:46 +00:00
@app.get ( " /@<username>/pic " )
2022-05-22 17:23:52 +00:00
@limiter.exempt
2022-06-17 19:21:26 +00:00
def user_profile_name ( username ) :
2023-01-25 03:18:17 +00:00
return redirect ( get_profile_picture ( username ) )
2022-05-04 23:09:46 +00:00
2023-07-30 00:42:06 +00:00
def get_saves_and_subscribes ( v , template , relationship_cls , page , standalone = False ) :
2022-11-26 04:52:47 +00:00
if relationship_cls in { SaveRelationship , Subscription } :
2023-06-07 23:26:32 +00:00
query = relationship_cls . post_id
2022-10-29 03:04:09 +00:00
join = relationship_cls . post
2023-06-07 23:26:32 +00:00
cls = Post
2022-11-01 17:19:21 +00:00
elif relationship_cls is CommentSaveRelationship :
2022-10-29 03:04:09 +00:00
query = relationship_cls . comment_id
join = relationship_cls . comment
cls = Comment
else :
raise TypeError ( " Relationships supported is SaveRelationship, Subscription, CommentSaveRelationship " )
2023-05-05 21:45:25 +00:00
2023-05-05 21:44:24 +00:00
listing = g . db . query ( query ) . join ( join ) . filter ( relationship_cls . user_id == v . id )
2023-05-05 21:45:25 +00:00
2023-05-05 21:44:24 +00:00
total = listing . count ( )
2023-05-05 21:45:25 +00:00
2023-05-05 21:44:24 +00:00
listing = listing . order_by ( cls . created_utc . desc ( ) ) . offset ( PAGE_SIZE * ( page - 1 ) ) . limit ( PAGE_SIZE ) . all ( )
ids = [ x [ 0 ] for x in listing ]
2022-11-25 21:43:35 +00:00
extra = None
2023-08-31 10:46:07 +00:00
if v . admin_level < PERMS [ ' POST_COMMENT_MODERATION ' ] :
2022-11-25 21:43:35 +00:00
extra = lambda q : q . filter ( cls . is_banned == False , cls . deleted_utc == 0 )
2023-06-07 23:26:32 +00:00
if cls is Post :
2023-07-17 14:49:26 +00:00
listing = get_posts ( ids , v = v , extra = extra )
2022-10-29 03:04:09 +00:00
elif cls is Comment :
2022-11-25 21:43:35 +00:00
listing = get_comments ( ids , v = v , extra = extra )
2022-10-29 03:04:09 +00:00
else :
2023-06-07 23:26:32 +00:00
raise TypeError ( " Only supports Posts and Comments. This is probably the result of a bug with *this* function " )
2023-01-01 11:36:20 +00:00
2023-06-08 00:49:37 +00:00
if v . client : return { " data " : [ x . json for x in listing ] }
2023-05-05 21:44:24 +00:00
return render_template ( template , u = v , v = v , listing = listing , page = page , total = total , standalone = standalone )
2022-10-29 03:04:09 +00:00
2022-05-04 23:09:46 +00:00
@app.get ( " /@<username>/saved/posts " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def saved_posts ( v , username ) :
2023-05-05 05:23:59 +00:00
page = get_page ( )
2022-11-07 21:34:40 +00:00
2023-06-07 23:26:32 +00:00
return get_saves_and_subscribes ( v , " userpage/posts.html " , SaveRelationship , page , False )
2022-05-04 23:09:46 +00:00
@app.get ( " /@<username>/saved/comments " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def saved_comments ( v , username ) :
2023-05-05 05:23:59 +00:00
page = get_page ( )
2022-11-07 21:34:40 +00:00
2022-11-09 06:11:46 +00:00
return get_saves_and_subscribes ( v , " userpage/comments.html " , CommentSaveRelationship , page , True )
2022-05-04 23:09:46 +00:00
2022-07-03 02:43:49 +00:00
@app.get ( " /@<username>/subscribed/posts " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-07-03 02:43:49 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def subscribed_posts ( v , username ) :
2023-05-05 05:23:59 +00:00
page = get_page ( )
2022-11-07 21:34:40 +00:00
2023-06-07 23:26:32 +00:00
return get_saves_and_subscribes ( v , " userpage/posts.html " , Subscription , page , False )
2022-05-04 23:09:46 +00:00
@app.post ( " /fp/<fp> " )
2023-02-27 05:33:45 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath )
2023-04-02 06:52:26 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath , key_func = get_ID )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-05-04 23:09:46 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def fp ( v , fp ) :
2023-03-02 20:29:22 +00:00
if session . get ( " GLOBAL " ) :
2023-10-10 19:34:38 +00:00
return ' '
2023-03-02 20:29:22 +00:00
2022-05-04 23:09:46 +00:00
v . fp = fp
2023-03-16 06:27:58 +00:00
users = g . db . query ( User ) . filter ( User . fp == fp , User . id != v . id ) . all ( )
2023-08-03 08:02:31 +00:00
if v . email and v . email_verified :
alts = g . db . query ( User ) . filter ( User . email == v . email , User . email_verified , User . id != v . id ) . all ( )
2022-05-04 23:09:46 +00:00
if alts :
users + = alts
for u in users :
li = [ v . id , u . id ]
2023-08-22 21:39:04 +00:00
g . db . flush ( )
2023-03-16 06:27:58 +00:00
existing = g . db . query ( Alt ) . filter ( Alt . user1 . in_ ( li ) , Alt . user2 . in_ ( li ) ) . one_or_none ( )
2022-05-04 23:09:46 +00:00
if existing : continue
2022-12-22 21:24:36 +00:00
add_alt ( user1 = v . id , user2 = u . id )
2023-01-01 11:36:20 +00:00
2023-01-25 02:53:52 +00:00
check_for_alts ( v , include_current_session = True )
2023-03-16 06:27:58 +00:00
g . db . add ( v )
2023-10-10 19:34:38 +00:00
return ' '
2022-07-13 17:31:35 +00:00
2023-10-07 17:55:50 +00:00
@app.post ( " /toggle_pins/<hole>/<sort> " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
2023-10-07 17:55:50 +00:00
def toggle_pins ( hole , sort ) :
2022-07-13 19:32:28 +00:00
if sort == ' hot ' : default = True
else : default = False
2023-10-07 17:55:50 +00:00
pins = session . get ( f ' { hole } _ { sort } ' , default )
session [ f ' { hole } _ { sort } ' ] = not pins
2022-07-13 19:32:28 +00:00
2023-08-11 21:50:23 +00:00
return { " message " : " Pins toggled successfully! " }
2022-09-05 20:23:35 +00:00
2022-12-29 10:39:10 +00:00
@app.get ( " /badge_owners/<int:bid> " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-09-05 20:23:35 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def bid_list ( v , bid ) :
2022-09-05 20:23:35 +00:00
2023-05-05 05:23:59 +00:00
page = get_page ( )
2022-09-05 20:23:35 +00:00
2023-08-08 14:49:00 +00:00
users = g . db . query ( User , Badge . created_utc ) . join ( User . badges ) . filter ( Badge . badge_id == bid )
2023-05-05 21:45:25 +00:00
2023-05-05 21:44:24 +00:00
total = users . count ( )
2023-05-05 21:45:25 +00:00
2023-05-05 21:44:24 +00:00
users = users . order_by ( Badge . created_utc . desc ( ) ) . offset ( PAGE_SIZE * ( page - 1 ) ) . limit ( PAGE_SIZE ) . all ( )
2022-09-05 20:23:35 +00:00
2023-08-08 14:49:00 +00:00
return render_template ( " owners.html " , v = v , users = users , page = page , total = total , kind = " Badge " )
2022-09-05 20:23:35 +00:00
2023-02-01 19:49:39 +00:00
KOFI_TOKEN = environ . get ( " KOFI_TOKEN " , " " ) . strip ( )
if KOFI_TOKEN :
@app.post ( " /kofi " )
2023-04-27 15:54:42 +00:00
@limiter.exempt
2023-02-01 19:49:39 +00:00
def kofi ( ) :
data = json . loads ( request . values [ ' data ' ] )
verification_token = data [ ' verification_token ' ]
2023-10-10 18:32:09 +00:00
if verification_token != KOFI_TOKEN : abort ( 400 )
2023-02-01 19:49:39 +00:00
2023-10-25 08:52:40 +00:00
ip = request . headers . get ( ' CF-Connecting-IP ' )
if ip not in { ' 104.45.229.18 ' , ' 137.117.16.235 ' } :
print ( STARS , flush = True )
print ( f ' /kofi fail: { ip } ' )
print ( STARS , flush = True )
abort ( 400 )
2023-02-01 19:49:39 +00:00
id = data [ ' kofi_transaction_id ' ]
created_utc = int ( time . mktime ( time . strptime ( data [ ' timestamp ' ] . split ( ' . ' ) [ 0 ] , " % Y- % m- %d T % H: % M: % SZ " ) ) )
type = data [ ' type ' ]
amount = 0
try :
amount = int ( float ( data [ ' amount ' ] ) )
except :
abort ( 400 , ' invalid amount ' )
2023-10-24 15:01:06 +00:00
email = data [ ' email ' ] . strip ( ) . lower ( )
2023-02-01 19:49:39 +00:00
transaction = Transaction (
id = id ,
created_utc = created_utc ,
type = type ,
amount = amount ,
email = email
)
2023-01-23 08:42:38 +00:00
2023-03-16 06:27:58 +00:00
g . db . add ( transaction )
2023-01-23 08:42:38 +00:00
2023-02-02 19:28:23 +00:00
claim_rewards_all_users ( )
2023-01-23 08:42:38 +00:00
2023-02-01 19:49:39 +00:00
return ' '
2022-09-13 16:53:19 +00:00
2023-10-06 11:38:58 +00:00
2023-01-26 11:25:28 +00:00
@app.post ( " /gumroad " )
2023-04-27 15:54:42 +00:00
@limiter.exempt
2023-01-26 11:25:28 +00:00
def gumroad ( ) :
data = request . values
ip = request . headers . get ( ' CF-Connecting-IP ' )
2023-02-06 03:39:44 +00:00
if ip != ' 34.193.146.117 ' :
2023-03-06 20:49:01 +00:00
print ( STARS , flush = True )
print ( f ' /gumroad fail: { ip } ' )
print ( STARS , flush = True )
2023-01-26 11:25:28 +00:00
abort ( 400 )
id = data [ ' sale_id ' ]
2023-03-23 13:33:57 +00:00
existing = g . db . get ( Transaction , id )
if existing : return ' '
2023-04-27 16:17:00 +00:00
created_utc = int ( time . mktime ( time . strptime ( data [ ' sale_timestamp ' ] . split ( ' . ' ) [ 0 ] , " % Y- % m- %d T % H: % M: % SZ " ) ) )
2023-06-13 22:21:49 +00:00
2023-07-13 10:50:31 +00:00
if data . get ( ' recurrence ' ) :
2023-06-13 22:21:49 +00:00
type = " monthly "
else :
type = " one-time "
2023-01-27 13:33:09 +00:00
amount = int ( data [ ' price ' ] ) / 100
2023-10-24 15:01:06 +00:00
email = data [ ' email ' ] . strip ( ) . lower ( )
2023-02-22 17:27:33 +00:00
2023-01-26 11:25:28 +00:00
transaction = Transaction (
id = id ,
created_utc = created_utc ,
type = type ,
amount = amount ,
email = email
)
2023-03-16 06:27:58 +00:00
g . db . add ( transaction )
2023-01-26 11:25:28 +00:00
2023-02-02 19:28:23 +00:00
claim_rewards_all_users ( )
2023-01-26 11:25:28 +00:00
return ' '
2023-10-23 18:42:23 +00:00
@app.post ( " /bm " )
@limiter.exempt
def bm ( ) :
data = json . loads ( request . data )
ip = request . headers . get ( ' CF-Connecting-IP ' )
if ip != ' 3.23.31.237 ' :
print ( STARS , flush = True )
print ( f ' /bm fail: { ip } ' )
print ( STARS , flush = True )
abort ( 400 )
created_utc = data [ ' created ' ]
data = data [ ' data ' ]
id = str ( data [ ' id ' ] )
existing = g . db . get ( Transaction , id )
if existing : return ' '
if data [ ' object ' ] == ' membership ' :
type = " monthly "
else :
type = " one-time "
amount = int ( data [ ' amount ' ] )
2023-10-24 15:01:06 +00:00
email = data [ ' supporter_email ' ] . strip ( ) . lower ( )
2023-10-23 18:42:23 +00:00
transaction = Transaction (
id = id ,
created_utc = created_utc ,
type = type ,
amount = amount ,
email = email
)
g . db . add ( transaction )
claim_rewards_all_users ( )
return ' '
2023-01-26 11:25:28 +00:00
2022-09-13 16:53:19 +00:00
2023-02-01 19:04:15 +00:00
@app.post ( " /settings/claim_rewards " )
2023-02-27 05:33:45 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath )
2023-04-02 06:52:26 +00:00
@limiter.limit ( ' 1/second ' , scope = rpath , key_func = get_ID )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2022-09-13 16:53:19 +00:00
@auth_required
2023-07-30 00:42:06 +00:00
def settings_claim_rewards ( v ) :
2023-08-03 08:02:31 +00:00
if not ( v . email and v . email_verified ) :
2022-10-11 13:01:39 +00:00
abort ( 400 , f " You must have a verified email to verify { patron } status and claim your rewards! " )
2022-12-01 01:24:38 +00:00
2023-10-26 17:11:35 +00:00
transactions = g . db . query ( Transaction ) . filter_by ( email = v . email ) . all ( )
if not transactions :
abort ( 400 , f " No matching email found. Please ensure you’ re using the same email here that you used on { DONATE_SERVICE } . " )
2023-01-01 11:36:20 +00:00
2023-10-26 17:11:35 +00:00
transactions = g . db . query ( Transaction ) . filter_by ( email = v . email , claimed = None ) . all ( )
2022-12-01 01:24:38 +00:00
if not transactions :
2023-01-23 10:30:34 +00:00
abort ( 400 , f " { patron } rewards already claimed! " )
2022-09-13 16:53:19 +00:00
2023-07-10 01:17:31 +00:00
claim_rewards_all_users ( )
2022-09-18 00:28:09 +00:00
2022-09-29 05:43:29 +00:00
return { " message " : f " { patron } rewards claimed! " }
2023-01-01 14:52:16 +00:00
@app.get ( " /users " )
2023-07-13 13:50:46 +00:00
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
2023-01-01 14:52:16 +00:00
@auth_required
def users_list ( v ) :
2023-05-05 05:23:59 +00:00
page = get_page ( )
2023-01-01 14:52:16 +00:00
2023-05-05 21:44:24 +00:00
users = g . db . query ( User )
2023-05-05 21:45:25 +00:00
2023-05-05 21:44:24 +00:00
total = users . count ( )
2023-05-05 21:45:25 +00:00
2023-05-05 21:44:24 +00:00
users = users . order_by ( User . id . desc ( ) ) . offset ( PAGE_SIZE * ( page - 1 ) ) . limit ( PAGE_SIZE ) . all ( )
2023-01-01 14:52:16 +00:00
return render_template ( " user_cards.html " ,
v = v ,
users = users ,
2023-05-05 21:44:24 +00:00
total = total ,
2023-01-01 14:52:16 +00:00
page = page ,
user_cards_title = " Users Feed " ,
)
2023-09-06 18:02:47 +00:00
@app.post ( " /mute_notifs/<int:uid> " )
@limiter.limit ( ' 1/second ' , scope = rpath )
@limiter.limit ( ' 1/second ' , scope = rpath , key_func = get_ID )
@limiter.limit ( " 20/day " , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( " 20/day " , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
@auth_required
def mute_notifs ( v , uid ) :
user = get_account ( uid )
if user . id == v . id :
abort ( 400 , " You can ' t mute notifications from yourself! " )
if user . id == AUTOJANNY_ID :
abort ( 403 , f " You can ' t mute notifications from @ { user . username } " )
if v . has_muted ( user ) :
abort ( 409 , f " You have already muted notifications from @ { user . username } " )
new_mute = UserMute ( user_id = v . id , target_id = user . id )
g . db . add ( new_mute )
send_notification ( user . id , f " @ { v . username } has muted notifications from you! " )
return { " message " : f " You have muted notifications from @ { user . username } successfully! " }
@app.post ( " /unmute_notifs/<int:uid> " )
@limiter.limit ( ' 1/second ' , scope = rpath )
@limiter.limit ( ' 1/second ' , scope = rpath , key_func = get_ID )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 )
@limiter.limit ( DEFAULT_RATELIMIT , deduct_when = lambda response : response . status_code < 400 , key_func = get_ID )
@auth_required
def unmute_notifs ( v , uid ) :
user = get_account ( uid )
x = v . has_muted ( user )
if not x :
abort ( 409 , " You can ' t unmute notifications from someone you haven ' t muted notifications from! " )
g . db . delete ( x )
send_notification ( user . id , f " @ { v . username } has unmuted notifications from you! " )
return { " message " : f " You have unmuted notifications from @ { user . username } successfully! " }