2021-07-23 19:12:10 +00:00
import qrcode
2021-07-21 01:12:26 +00:00
import io
2021-07-24 20:50:36 +00:00
import time
2021-08-03 17:55:02 +00:00
import traceback
import sys
2021-07-21 01:12:26 +00:00
2021-08-04 15:35:10 +00:00
from files . classes . user import ViewerRelationship
from files . helpers . alerts import *
from files . helpers . sanitize import *
from files . helpers . markdown import *
2021-08-21 11:06:28 +00:00
from files . helpers . const import *
2021-08-04 15:35:10 +00:00
from files . mail import *
2021-07-21 01:12:26 +00:00
from flask import *
2021-08-04 15:35:10 +00:00
from files . __main__ import app , limiter
2021-08-11 02:54:52 +00:00
from pusher_push_notifications import PushNotifications
2021-07-21 01:12:26 +00:00
2021-08-05 14:41:32 +00:00
site = environ . get ( " DOMAIN " ) . strip ( )
2021-08-02 14:27:20 +00:00
2021-07-21 01:12:26 +00:00
beams_client = PushNotifications (
2021-08-21 11:06:28 +00:00
instance_id = PUSHER_INSTANCE_ID ,
2021-07-21 01:12:26 +00:00
secret_key = PUSHER_KEY ,
)
2021-07-24 20:49:07 +00:00
@app.post ( " /@<username>/suicide " )
2021-07-24 19:44:21 +00:00
@auth_required
def suicide ( v , username ) :
2021-07-24 20:50:36 +00:00
t = int ( time . time ( ) )
2021-07-24 21:41:26 +00:00
if v . admin_level == 0 and t - v . suicide_utc < 86400 : return " " , 204
2021-07-24 19:44:21 +00:00
user = get_user ( username )
2021-08-04 16:00:57 +00:00
suicide = f " Hi there, \n \n A [concerned user]( { v . url } ) 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 - 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. \n If you don ' t see a resource in your area above, the moderators at r/SuicideWatch keep a comprehensive list of resources and hotlines for people organized by location. Find Someone Now \n \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. "
2021-08-21 11:06:28 +00:00
send_notification ( NOTIFICATIONS_ACCOUNT , user , suicide )
2021-07-24 20:34:40 +00:00
v . suicide_utc = t
g . db . add ( v )
2021-07-24 19:44:21 +00:00
return " " , 204
2021-08-08 18:26:15 +00:00
@app.get ( " /@<username>/coins " )
@auth_required
def get_coins ( v , username ) :
user = get_user ( username )
if user is not None : return { " coins " : user . coins } , 200
else : return { " error " : " invalid_user " } , 404
@app.post ( " /@<username>/transfer_coins " )
@auth_required
@validate_formkey
def transfer_coins ( v , username ) :
receiver = get_user ( username )
if receiver is None : return { " error " : " That user doesn ' t exist. " } , 404
if receiver . id != v . id :
amount = request . form . get ( " amount " , " " )
amount = int ( amount ) if amount . isdigit ( ) else None
2021-08-09 12:21:29 +00:00
if amount is None or amount < = 0 : return { " error " : f " Invalid amount of { app . config [ ' COINS_NAME ' ] } . " } , 400
if v . coins < amount : return { " error " : f " You don ' t have enough { app . config [ ' COINS_NAME ' ] } " } , 400
if amount < 100 : return { " error " : f " You have to gift at least 100 { app . config [ ' COINS_NAME ' ] } . " } , 400
2021-08-08 18:26:15 +00:00
v . coins - = amount
receiver . coins + = amount
g . db . add ( receiver )
g . db . add ( v )
2021-08-09 12:21:29 +00:00
transfer_message = f " 🤑 [@ { v . username } ]( { v . url } ) has gifted you { amount } { app . config [ ' COINS_NAME ' ] } ! "
2021-08-08 18:26:15 +00:00
send_notification ( v . id , receiver , transfer_message )
2021-08-09 12:21:29 +00:00
return { " message " : f " { amount } { app . config [ ' COINS_NAME ' ] } transferred! " } , 200
2021-08-08 18:26:15 +00:00
return " " , 204
2021-07-27 22:31:28 +00:00
@app.get ( " /leaderboard " )
2021-07-21 01:12:26 +00:00
@auth_desired
def leaderboard ( v ) :
2021-07-27 00:21:03 +00:00
users = g . db . query ( User ) . options ( lazyload ( ' * ' ) )
2021-08-04 16:51:16 +00:00
users1 = users . order_by ( User . coins . desc ( ) ) . limit ( 25 ) . all ( )
2021-07-27 00:13:15 +00:00
users2 = users . order_by ( User . stored_subscriber_count . desc ( ) ) . limit ( 10 ) . all ( )
2021-07-28 03:55:47 +00:00
users3 = users . order_by ( User . post_count . desc ( ) ) . limit ( 10 ) . all ( )
users4 = users . order_by ( User . comment_count . desc ( ) ) . limit ( 10 ) . all ( )
2021-07-27 00:23:41 +00:00
return render_template ( " leaderboard.html " , v = v , users1 = users1 , users2 = users2 , users3 = users3 , users4 = users4 )
2021-07-21 01:12:26 +00:00
@app.get ( " /@<username>/css " )
def get_css ( username ) :
user = get_user ( username )
if user . css : css = user . css
else : css = " "
resp = make_response ( css )
resp . headers . add ( " Content-Type " , " text/css " )
return resp
@app.get ( " /@<username>/profilecss " )
def get_profilecss ( username ) :
user = get_user ( username )
if user . profilecss : profilecss = user . profilecss
else : profilecss = " "
resp = make_response ( profilecss )
resp . headers . add ( " Content-Type " , " text/css " )
return resp
2021-07-27 22:31:28 +00:00
@app.post ( " /@<username>/reply/<id> " )
2021-07-21 01:12:26 +00:00
@auth_required
def messagereply ( v , username , id ) :
2021-07-30 21:38:29 +00:00
2021-07-24 18:15:26 +00:00
message = request . form . get ( " message " , " " ) [ : 1000 ] . strip ( )
2021-07-21 01:12:26 +00:00
user = get_user ( username )
2021-07-31 08:47:10 +00:00
message = message . replace ( " \n " , " \n \n " ) . replace ( " \n \n \n \n \n \n " , " \n \n " ) . replace ( " \n \n \n \n " , " \n \n " ) . replace ( " \n \n \n " , " \n \n " )
2021-07-29 05:50:04 +00:00
# check existing
existing = g . db . query ( Comment ) . join ( CommentAux ) . filter ( Comment . author_id == v . id ,
Comment . sentto == user . id ,
CommentAux . body == message ,
) . options ( contains_eager ( Comment . comment_aux ) ) . first ( )
2021-08-12 23:23:33 +00:00
if existing : return redirect ( ' /notifications?messages=true ' )
2021-07-29 05:50:04 +00:00
2021-07-21 01:12:26 +00:00
with CustomRenderer ( ) as renderer : text_html = renderer . render ( mistletoe . Document ( message ) )
2021-08-21 12:57:16 +00:00
text_html = sanitize ( text_html )
2021-07-21 01:12:26 +00:00
parent = get_comment ( int ( id ) , v = v )
new_comment = Comment ( author_id = v . id ,
parent_submission = None ,
parent_comment_id = id ,
level = parent . level + 1 ,
sentto = user . id
)
g . db . add ( new_comment )
g . db . flush ( )
new_aux = CommentAux ( id = new_comment . id , body = message , body_html = text_html )
g . db . add ( new_aux )
notif = Notification ( comment_id = new_comment . id , user_id = user . id )
g . db . add ( notif )
2021-08-26 10:33:26 +00:00
2021-08-26 10:39:44 +00:00
if request . referrer . endswith ( ' /notifications ' ) : return redirect ( " /notifications?all=true " )
2021-08-26 10:33:26 +00:00
else : return redirect ( request . referrer )
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.get ( " /songs/<id> " )
2021-07-21 01:12:26 +00:00
def songs ( id ) :
try : id = int ( id )
except : return ' ' , 400
user = g . db . query ( User ) . filter_by ( id = id ) . first ( )
return send_from_directory ( ' /songs/ ' , f ' { user . song } .mp3 ' )
2021-07-27 22:31:28 +00:00
@app.post ( " /subscribe/<post_id> " )
2021-07-21 01:12:26 +00:00
@auth_required
def subscribe ( v , post_id ) :
new_sub = Subscription ( user_id = v . id , submission_id = post_id )
g . db . add ( new_sub )
return " " , 204
2021-07-27 22:31:28 +00:00
@app.post ( " /unsubscribe/<post_id> " )
2021-07-21 01:12:26 +00:00
@auth_required
def unsubscribe ( v , post_id ) :
sub = g . db . query ( Subscription ) . filter_by ( user_id = v . id , submission_id = post_id ) . first ( )
g . db . delete ( sub )
return " " , 204
2021-07-27 22:31:28 +00:00
@app.post ( " /@<username>/message " )
2021-07-30 21:30:31 +00:00
@limiter.limit ( " 10/hour " )
2021-07-21 01:12:26 +00:00
@auth_required
def message2 ( v , username ) :
2021-07-30 21:38:29 +00:00
2021-07-21 01:12:26 +00:00
user = get_user ( username , v = v )
2021-07-31 05:28:05 +00:00
if user . is_blocking : return { " error " : " You ' re blocking this user. " } , 403
if user . is_blocked : return { " error " : " This user is blocking you. " } , 403
2021-07-24 18:15:26 +00:00
message = request . form . get ( " message " , " " ) [ : 1000 ] . strip ( )
2021-07-29 05:46:33 +00:00
2021-07-31 08:47:10 +00:00
message = message . replace ( " \n " , " \n \n " ) . replace ( " \n \n \n \n \n \n " , " \n \n " ) . replace ( " \n \n \n \n " , " \n \n " ) . replace ( " \n \n \n " , " \n \n " )
2021-07-29 05:50:04 +00:00
2021-07-29 05:46:33 +00:00
# check existing
existing = g . db . query ( Comment ) . join ( CommentAux ) . filter ( Comment . author_id == v . id ,
2021-07-29 05:50:04 +00:00
Comment . sentto == user . id ,
CommentAux . body == message ,
) . options ( contains_eager ( Comment . comment_aux ) ) . first ( )
2021-08-12 23:23:33 +00:00
if existing : return redirect ( ' /notifications?messages=true ' )
2021-07-29 05:46:33 +00:00
2021-07-21 01:12:26 +00:00
send_pm ( v . id , user , message )
2021-08-03 17:55:02 +00:00
try :
beams_client . publish_to_interests (
interests = [ str ( user . id ) ] ,
publish_body = {
' web ' : {
' notification ' : {
' title ' : f ' New message from @ { v . username } ' ,
' body ' : message ,
' deep_link ' : f ' https:// { site } /notifications ' ,
} ,
2021-07-21 01:12:26 +00:00
} ,
} ,
2021-08-03 17:55:02 +00:00
)
2021-08-11 02:54:52 +00:00
except Exception as e :
print ( e )
2021-08-03 17:55:02 +00:00
2021-08-25 17:23:38 +00:00
return redirect ( request . referrer )
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.get ( " /2faqr/<secret> " )
2021-07-21 01:12:26 +00:00
@auth_required
def mfa_qr ( secret , v ) :
x = pyotp . TOTP ( secret )
qr = qrcode . QRCode (
error_correction = qrcode . constants . ERROR_CORRECT_L
)
2021-08-04 16:00:57 +00:00
qr . add_data ( x . provisioning_uri ( v . username , issuer_name = app . config [ " SITE_NAME " ] ) )
2021-07-23 20:37:33 +00:00
img = qr . make_image ( fill_color = " #000000 " , back_color = " white " )
2021-07-21 01:12:26 +00:00
mem = io . BytesIO ( )
img . save ( mem , format = " PNG " )
mem . seek ( 0 , 0 )
return send_file ( mem , mimetype = " image/png " , as_attachment = False )
2021-07-31 04:48:47 +00:00
@app.get ( " /is_available/<name> " )
2021-07-21 01:12:26 +00:00
@auth_desired
def api_is_available ( name , v ) :
name = name . strip ( )
if len ( name ) < 3 or len ( name ) > 25 :
2021-07-31 05:28:05 +00:00
return { name : False }
2021-07-21 01:12:26 +00:00
name = name . replace ( ' _ ' , ' \ _ ' )
x = g . db . query ( User ) . options (
lazyload ( ' * ' )
) . filter (
or_ (
User . username . ilike ( name ) ,
User . original_username . ilike ( name )
)
) . first ( )
if x :
2021-07-31 05:28:05 +00:00
return { name : False }
2021-07-21 01:12:26 +00:00
else :
2021-07-31 05:28:05 +00:00
return { name : True }
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.get ( " /id/<id> " )
2021-07-25 18:01:43 +00:00
def user_id ( id ) :
2021-07-21 01:12:26 +00:00
2021-07-25 18:01:43 +00:00
user = get_account ( int ( id ) )
2021-07-27 00:05:58 +00:00
return redirect ( user . url )
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.get ( " /u/<username> " )
2021-07-21 01:12:26 +00:00
def redditor_moment_redirect ( username ) :
return redirect ( f " /@ { username } " )
2021-07-31 07:08:36 +00:00
# @app.get("/rentoids")
# @auth_desired
# def rentoids(v):
2021-08-23 17:48:55 +00:00
#
2021-07-29 06:42:45 +00:00
2021-07-31 07:08:36 +00:00
# users = g.db.query(User).filter(User.rent_utc > 0).all()
# return render_template("rentoids.html", v=v, users=users)
2021-07-29 06:42:45 +00:00
2021-07-27 22:31:28 +00:00
@app.get ( " /@<username>/followers " )
2021-07-21 01:12:26 +00:00
@auth_required
def followers ( username , v ) :
2021-08-23 17:48:55 +00:00
2021-07-21 01:12:26 +00:00
u = get_user ( username , v = v )
users = [ x . user for x in u . followers ]
return render_template ( " followers.html " , v = v , u = u , users = users )
2021-07-23 18:52:11 +00:00
@app.get ( " /views " )
2021-07-23 15:51:26 +00:00
@auth_required
2021-07-23 18:52:11 +00:00
def visitors ( v ) :
2021-07-23 19:28:25 +00:00
if v . admin_level < 1 and not v . patron : return render_template ( " errors/patron.html " , v = v )
2021-07-23 20:02:47 +00:00
viewers = sorted ( v . viewers , key = lambda x : x . last_view_utc , reverse = True )
2021-07-23 15:51:26 +00:00
return render_template ( " viewers.html " , v = v , viewers = viewers )
2021-08-13 01:38:51 +00:00
2021-07-27 22:31:28 +00:00
@app.get ( " /@<username> " )
2021-08-13 01:38:51 +00:00
@app.get ( " /logged_out/@<username> " )
2021-07-21 01:12:26 +00:00
@auth_desired
def u_username ( username , v = None ) :
2021-08-23 17:48:55 +00:00
2021-07-21 01:12:26 +00:00
2021-08-13 01:38:51 +00:00
if not v and " logged_out " not in request . path : return redirect ( f " /logged_out/@ { username } " )
2021-08-13 18:53:11 +00:00
if v and " logged_out " in request . full_path : v = None
2021-08-13 01:55:23 +00:00
2021-07-21 01:12:26 +00:00
# username is unique so at most this returns one result. Otherwise 404
# case insensitive search
u = get_user ( username , v = v )
# check for wrong cases
if username != u . username :
return redirect ( request . path . replace ( username , u . username ) )
if u . reserved :
2021-07-31 05:59:25 +00:00
if request . headers . get ( " Authorization " ) : return { " error " : f " That username is reserved for: { u . reserved } " }
2021-07-31 06:00:42 +00:00
else : return render_template ( " userpage_reserved.html " , u = u , v = v )
2021-07-21 01:12:26 +00:00
2021-07-23 15:51:26 +00:00
# viewers
2021-07-23 15:54:07 +00:00
if v and u . id != v . id :
2021-07-23 15:51:26 +00:00
view = g . db . query ( ViewerRelationship ) . filter (
and_ (
ViewerRelationship . viewer_id == v . id ,
ViewerRelationship . user_id == u . id
)
) . first ( )
if view :
view . last_view_utc = g . timestamp
else :
view = ViewerRelationship ( user_id = u . id ,
viewer_id = v . id )
g . db . add ( view )
2021-07-29 06:29:13 +00:00
2021-07-21 01:12:26 +00:00
if u . is_private and ( not v or ( v . id != u . id and v . admin_level < 3 ) ) :
2021-07-29 06:29:13 +00:00
2021-07-31 05:59:25 +00:00
# paidrent = False
# if v and u.id == 253:
# if int(time.time()) - v.rent_utc < 600: paidrent = True
2021-08-04 16:00:57 +00:00
# elif request.args.get("rent") == "true" and v.coins > 500:
# v.coins -= 500
2021-07-31 05:59:25 +00:00
# v.rent_utc = int(time.time())
# g.db.add(v)
2021-08-04 16:00:57 +00:00
# u.coins += 500
2021-07-31 05:59:25 +00:00
# g.db.add(u)
2021-08-21 11:06:28 +00:00
# send_notification(NOTIFICATIONS_ACCOUNT, u, f"@{v.username} has paid rent!")
2021-07-31 05:59:25 +00:00
# paidrent = True
# if not paidrent:
if request . headers . get ( " Authorization " ) : return { " error " : " That userpage is private " }
else : return render_template ( " userpage_private.html " , u = u , v = v )
2021-07-21 01:12:26 +00:00
if u . is_blocking and ( not v or v . admin_level < 3 ) :
2021-07-31 05:59:25 +00:00
if request . headers . get ( " Authorization " ) : return { " error " : f " You are blocking @ { u . username } . " }
else : return render_template ( " userpage_blocking.html " , u = u , v = v )
2021-07-21 01:12:26 +00:00
if u . is_blocked and ( not v or v . admin_level < 3 ) :
2021-07-31 05:59:25 +00:00
if request . headers . get ( " Authorization " ) : return { " error " : " This person is blocking you. " }
else : return render_template ( " userpage_blocked.html " , u = u , v = v )
2021-07-21 01:12:26 +00:00
sort = request . args . get ( " sort " , " new " )
t = request . args . get ( " t " , " all " )
page = int ( request . args . get ( " page " , " 1 " ) )
page = max ( page , 1 )
ids = u . userpagelisting ( v = v , page = page , sort = sort , t = t )
# we got 26 items just to see if a next page exists
next_exists = ( len ( ids ) == 26 )
2021-07-28 10:57:41 +00:00
ids = ids [ : 25 ]
2021-07-21 01:12:26 +00:00
# If page 1, check for sticky
if page == 1 :
sticky = [ ]
sticky = g . db . query ( Submission ) . filter_by ( is_pinned = True , author_id = u . id ) . all ( )
if sticky :
for p in sticky :
ids = [ p . id ] + ids
2021-07-25 02:55:25 +00:00
listing = get_posts ( ids , v = v )
2021-07-21 01:12:26 +00:00
if u . unban_utc :
2021-07-31 06:13:16 +00:00
if request . headers . get ( " Authorization " ) : { " data " : [ x . json for x in listing ] }
2021-07-31 05:59:25 +00:00
else : return render_template ( " userpage.html " ,
2021-07-31 06:13:16 +00:00
unban = u . unban_string ,
u = u ,
v = v ,
listing = listing ,
page = page ,
sort = sort ,
t = t ,
next_exists = next_exists ,
is_following = ( v and u . has_follower ( v ) ) )
if request . headers . get ( " Authorization " ) : return { " data " : [ x . json for x in listing ] }
else : return render_template ( " userpage.html " ,
u = u ,
v = v ,
listing = listing ,
page = page ,
sort = sort ,
t = t ,
next_exists = next_exists ,
is_following = ( v and u . has_follower ( v ) ) )
2021-07-31 05:59:25 +00:00
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.get ( " /@<username>/comments " )
2021-08-13 01:38:51 +00:00
@app.get ( " /logged_out/@<username>/comments " )
2021-07-21 01:12:26 +00:00
@auth_desired
def u_username_comments ( username , v = None ) :
2021-08-23 17:48:55 +00:00
2021-07-21 01:12:26 +00:00
2021-08-13 01:38:51 +00:00
if not v and " logged_out " not in request . path : return redirect ( f " /logged_out/@ { username } /comments " )
2021-08-13 18:53:11 +00:00
if v and " logged_out " in request . full_path : v = None
2021-08-13 01:55:23 +00:00
2021-07-21 01:12:26 +00:00
# username is unique so at most this returns one result. Otherwise 404
# case insensitive search
user = get_user ( username , v = v )
# check for wrong cases
if username != user . username : return redirect ( f ' { user . url } /comments ' )
u = user
if u . reserved :
2021-07-31 05:59:25 +00:00
if request . headers . get ( " Authorization " ) : return { " error " : f " That username is reserved for: { u . reserved } " }
else : return render_template ( " userpage_reserved.html " ,
2021-07-21 01:12:26 +00:00
u = u ,
2021-07-31 05:59:25 +00:00
v = v )
2021-07-21 01:12:26 +00:00
if u . is_private and ( not v or ( v . id != u . id and v . admin_level < 3 ) ) :
2021-07-31 05:59:25 +00:00
# paidrent = False
# if v and u.id == 253:
# if int(time.time()) - v.rent_utc < 600: paidrent = True
2021-08-04 16:00:57 +00:00
# elif request.args.get("rent") == "true" and v.coins > 500:
# v.coins -= 500
2021-07-31 05:59:25 +00:00
# v.rent_utc = int(time.time())
# g.db.add(v)
2021-08-04 16:00:57 +00:00
# u.coins += 500
2021-07-31 05:59:25 +00:00
# g.db.add(u)
2021-08-21 11:06:28 +00:00
# send_notification(NOTIFICATIONS_ACCOUNT, u, f"@{v.username} has paid rent!")
2021-07-31 05:59:25 +00:00
# paidrent = True
# if not paidrent:
if request . headers . get ( " Authorization " ) : return { " error " : " That userpage is private " }
else : return render_template ( " userpage_private.html " ,
2021-07-29 13:55:11 +00:00
u = u ,
2021-07-31 05:59:25 +00:00
v = v )
2021-07-21 01:12:26 +00:00
if u . is_blocking and ( not v or v . admin_level < 3 ) :
2021-07-31 05:59:25 +00:00
if request . headers . get ( " Authorization " ) : return { " error " : f " You are blocking @ { u . username } . " }
else : return render_template ( " userpage_blocking.html " ,
u = u ,
v = v )
2021-07-21 01:12:26 +00:00
if u . is_blocked and ( not v or v . admin_level < 3 ) :
2021-07-31 05:59:25 +00:00
if request . headers . get ( " Authorization " ) : return { " error " : " This person is blocking you. " }
else : return render_template ( " userpage_blocked.html " ,
u = u ,
v = v )
2021-07-21 01:12:26 +00:00
page = int ( request . args . get ( " page " , " 1 " ) )
sort = request . args . get ( " sort " , " new " )
t = request . args . get ( " t " , " all " )
ids = user . commentlisting (
v = v ,
page = page ,
sort = sort ,
t = t ,
)
# we got 26 items just to see if a next page exists
next_exists = ( len ( ids ) == 26 )
2021-07-28 10:57:41 +00:00
ids = ids [ : 25 ]
2021-07-21 01:12:26 +00:00
listing = get_comments ( ids , v = v )
is_following = ( v and user . has_follower ( v ) )
2021-07-31 06:05:09 +00:00
if request . headers . get ( " Authorization " ) : return { " data " : [ c . json for c in listing ] }
2021-07-31 05:28:05 +00:00
else : return render_template ( " userpage_comments.html " , u = user , v = v , listing = listing , page = page , sort = sort , t = t , next_exists = next_exists , is_following = is_following , standalone = True )
2021-07-21 01:12:26 +00:00
2021-07-31 04:48:47 +00:00
@app.get ( " /@<username>/info " )
2021-07-21 01:12:26 +00:00
@auth_desired
def u_username_info ( username , v = None ) :
user = get_user ( username , v = v )
if user . is_blocking :
2021-07-31 05:28:05 +00:00
return { " error " : " You ' re blocking this user. " } , 401
2021-07-21 01:12:26 +00:00
elif user . is_blocked :
2021-07-31 05:28:05 +00:00
return { " error " : " This user is blocking you. " } , 403
2021-07-21 01:12:26 +00:00
2021-07-31 05:28:05 +00:00
return user . json
2021-07-21 01:12:26 +00:00
2021-07-31 04:48:47 +00:00
@app.post ( " /follow/<username> " )
2021-07-21 01:12:26 +00:00
@auth_required
def follow_user ( username , v ) :
target = get_user ( username )
2021-07-31 05:28:05 +00:00
if target . id == v . id : return { " error " : " You can ' t follow yourself! " } , 400
2021-07-21 01:12:26 +00:00
# check for existing follow
2021-08-11 19:57:29 +00:00
if g . db . query ( Follow ) . filter_by ( user_id = v . id , target_id = target . id ) . first ( ) : return " " , 204
2021-07-21 01:12:26 +00:00
2021-07-29 00:11:37 +00:00
new_follow = Follow ( user_id = v . id , target_id = target . id )
2021-07-21 01:12:26 +00:00
g . db . add ( new_follow )
2021-07-29 00:11:37 +00:00
target . stored_subscriber_count = g . db . query ( Follow ) . filter_by ( target_id = target . id ) . count ( )
2021-07-21 01:12:26 +00:00
g . db . add ( target )
existing = g . db . query ( Notification ) . filter_by ( followsender = v . id , user_id = target . id ) . first ( )
if not existing : send_follow_notif ( v . id , target . id , f " @ { v . username } has followed you! " )
return " " , 204
2021-07-31 04:48:47 +00:00
@app.post ( " /unfollow/<username> " )
2021-07-21 01:12:26 +00:00
@auth_required
def unfollow_user ( username , v ) :
target = get_user ( username )
# check for existing follow
2021-07-29 00:11:37 +00:00
follow = g . db . query ( Follow ) . filter_by ( user_id = v . id , target_id = target . id ) . first ( )
2021-07-21 01:12:26 +00:00
2021-08-11 19:57:29 +00:00
if not follow : return " " , 204
2021-07-21 01:12:26 +00:00
g . db . delete ( follow )
2021-07-29 00:11:37 +00:00
target . stored_subscriber_count = g . db . query ( Follow ) . filter_by ( target_id = target . id ) . count ( )
2021-07-26 02:17:14 +00:00
g . db . add ( target )
2021-07-21 01:12:26 +00:00
existing = g . db . query ( Notification ) . filter_by ( followsender = v . id , user_id = target . id ) . first ( )
if not existing : send_unfollow_notif ( v . id , target . id , f " @ { v . username } has unfollowed you! " )
return " " , 204
2021-08-02 07:00:38 +00:00
@app.route ( " /uid/<id>/pic/profile " )
2021-07-21 01:12:26 +00:00
@limiter.exempt
2021-08-02 07:00:38 +00:00
def user_profile_uid ( id ) :
try : id = int ( id )
2021-07-31 06:59:18 +00:00
except :
2021-08-02 07:00:38 +00:00
try : id = int ( id , 36 )
2021-07-31 06:59:18 +00:00
except : abort ( 404 )
2021-08-02 07:00:38 +00:00
x = get_account ( id )
2021-07-21 01:12:26 +00:00
return redirect ( x . profile_url )
2021-07-30 06:05:38 +00:00
2021-07-27 22:31:28 +00:00
@app.get ( " /@<username>/saved/posts " )
2021-07-21 01:12:26 +00:00
@auth_required
def saved_posts ( v , username ) :
page = int ( request . args . get ( " page " , 1 ) )
ids = v . saved_idlist ( page = page )
next_exists = len ( ids ) == 26
2021-07-28 10:57:41 +00:00
ids = ids [ : 25 ]
2021-07-21 01:12:26 +00:00
2021-07-25 02:55:25 +00:00
listing = get_posts ( ids , v = v )
2021-07-21 01:12:26 +00:00
2021-07-31 06:05:09 +00:00
if request . headers . get ( " Authorization " ) : return { " data " : [ x . json for x in listing ] }
2021-07-31 05:59:25 +00:00
else : return render_template ( " userpage.html " ,
2021-07-21 01:12:26 +00:00
u = v ,
v = v ,
listing = listing ,
page = page ,
next_exists = next_exists ,
2021-07-31 05:59:25 +00:00
)
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.get ( " /@<username>/saved/comments " )
2021-07-21 01:12:26 +00:00
@auth_required
def saved_comments ( v , username ) :
page = int ( request . args . get ( " page " , 1 ) )
ids = v . saved_comment_idlist ( page = page )
next_exists = len ( ids ) == 26
2021-07-28 10:57:41 +00:00
ids = ids [ : 25 ]
2021-07-21 01:12:26 +00:00
2021-07-29 07:00:10 +00:00
listing = get_comments ( ids , v = v )
2021-07-21 01:12:26 +00:00
2021-07-31 06:05:09 +00:00
if request . headers . get ( " Authorization " ) : return { " data " : [ x . json for x in listing ] }
2021-07-31 05:59:25 +00:00
else : return render_template ( " userpage_comments.html " ,
2021-07-21 01:12:26 +00:00
u = v ,
v = v ,
listing = listing ,
page = page ,
next_exists = next_exists ,
2021-08-03 17:55:02 +00:00
standalone = True )