2021-07-23 19:12:10 +00:00
import qrcode
2021-07-21 01:12:26 +00:00
import io
from datetime import datetime
2021-07-24 20:50:36 +00:00
import time
2021-07-21 01:12:26 +00:00
2021-07-23 15:51:26 +00:00
from drama . classes . user import ViewerRelationship
2021-07-22 19:19:49 +00:00
from drama . helpers . alerts import *
from drama . helpers . sanitize import *
from drama . helpers . markdown import *
from drama . mail import *
2021-07-21 01:12:26 +00:00
from flask import *
2021-07-26 18:47:42 +00:00
from drama . __main__ import app , limiter
2021-07-21 01:12:26 +00:00
from pusher_push_notifications import PushNotifications
PUSHER_KEY = environ . get ( " PUSHER_KEY " , " " ) . strip ( )
beams_client = PushNotifications (
instance_id = ' 02ddcc80-b8db-42be-9022-44c546b4dce6 ' ,
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-07-27 00:05:58 +00:00
suicide = f " Hi there, \n \n A [concerned dramatard]( { 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 dramatards care about you and there are people who want to help. "
2021-07-24 19:44:21 +00:00
send_notification ( 1046 , 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-07-27 22:31:28 +00:00
@app.get ( " /api/v1/user/<username> " )
2021-07-21 01:12:26 +00:00
@auth_desired
@api ( " read " )
def user_info ( v , username ) :
user = get_user ( username , v = v )
return jsonify ( user . json )
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-21 01:40:26 +00:00
if v and v . is_banned and not v . unban_utc : return render_template ( " seized.html " )
2021-07-27 00:21:03 +00:00
users = g . db . query ( User ) . options ( lazyload ( ' * ' ) )
2021-07-27 00:13:15 +00:00
users1 = users . order_by ( User . dramacoins . desc ( ) ) . limit ( 25 ) . all ( )
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-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-25 20:14:11 +00:00
message = message . replace ( " \n " , " \n \n " )
2021-07-21 01:12:26 +00:00
with CustomRenderer ( ) as renderer : text_html = renderer . render ( mistletoe . Document ( message ) )
text_html = sanitize ( text_html , linkgen = True )
parent = get_comment ( int ( id ) , v = v )
new_comment = Comment ( author_id = v . id ,
parent_submission = None ,
parent_fullname = parent . fullname ,
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 )
g . db . commit ( )
return redirect ( ' /notifications?all=true ' )
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 )
g . db . commit ( )
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-21 01:12:26 +00:00
@auth_required
def message2 ( v , username ) :
user = get_user ( username , v = v )
if user . is_blocking : return jsonify ( { " error " : " You ' re blocking this user. " } ) , 403
if user . is_blocked : return jsonify ( { " error " : " This user is blocking you. " } ) , 403
2021-07-24 18:15:26 +00:00
message = request . form . get ( " message " , " " ) [ : 1000 ] . strip ( )
2021-07-21 01:12:26 +00:00
send_pm ( v . id , user , message )
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://rdrama.net/notifications ' ,
} ,
} ,
} ,
)
2021-07-23 14:17:25 +00:00
return redirect ( ' /notifications?all=true ' )
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
)
qr . add_data ( x . provisioning_uri ( v . username , issuer_name = " Drama " ) )
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-27 22:31:28 +00:00
@app.get ( " /api/is_available/<name> " )
@app.get ( " /api/v1/is_available/<name> " )
2021-07-21 01:12:26 +00:00
@auth_desired
@api ( " read " )
def api_is_available ( name , v ) :
name = name . strip ( )
if len ( name ) < 3 or len ( name ) > 25 :
return jsonify ( { name : False } )
name = name . replace ( ' _ ' , ' \ _ ' )
x = g . db . query ( User ) . options (
lazyload ( ' * ' )
) . filter (
or_ (
User . username . ilike ( name ) ,
User . original_username . ilike ( name )
)
) . first ( )
if x :
return jsonify ( { name : False } )
else :
return jsonify ( { name : True } )
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
# Allow Id of user to be queryied, and then redirect the bot to the
# actual user api endpoint.
# So they get the data and then there will be no need to reinvent
# the wheel.
2021-07-27 22:31:28 +00:00
@app.get ( " /api/v1/uid/<uid> " )
2021-07-21 01:12:26 +00:00
@auth_desired
@api ( " read " )
def user_by_uid ( uid , v = None ) :
user = get_account ( uid )
return redirect ( f ' /api/v1/user/ { user . username } /info ' )
2021-07-27 22:31:28 +00:00
@app.get ( " /u/<username> " )
2021-07-21 01:12:26 +00:00
def redditor_moment_redirect ( username ) :
return redirect ( f " /@ { username } " )
2021-07-27 22:31:28 +00:00
@app.get ( " /@<username>/followers " )
2021-07-21 01:12:26 +00:00
@auth_required
def followers ( username , v ) :
if v and v . is_banned and not v . unban_utc : return render_template ( " seized.html " )
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-07-27 22:31:28 +00:00
@app.get ( " /@<username> " )
@app.get ( " /api/v1/user/<username>/listing " )
2021-07-21 01:12:26 +00:00
@auth_desired
@public ( " read " )
def u_username ( username , v = None ) :
if v and v . is_banned and not v . unban_utc : return render_template ( " seized.html " )
# 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 :
return { ' html ' : lambda : render_template ( " userpage_reserved.html " ,
u = u ,
v = v ) ,
' api ' : lambda : { " error " : f " That username is reserved for: { u . reserved } " }
}
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-21 01:12:26 +00:00
if u . is_private and ( not v or ( v . id != u . id and v . admin_level < 3 ) ) :
return { ' html ' : lambda : render_template ( " userpage_private.html " ,
u = u ,
v = v ) ,
' api ' : lambda : { " error " : " That userpage is private " }
}
if u . is_blocking and ( not v or v . admin_level < 3 ) :
return { ' html ' : lambda : render_template ( " userpage_blocking.html " ,
u = u ,
v = v ) ,
' api ' : lambda : { " error " : f " You are blocking @ { u . username } . " }
}
if u . is_blocked and ( not v or v . admin_level < 3 ) :
return { ' html ' : lambda : render_template ( " userpage_blocked.html " ,
u = u ,
v = v ) ,
' api ' : lambda : { " error " : " This person is blocking you. " }
}
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-28 15:37:38 +00:00
#unban = datetime.fromtimestamp(u.unban_utc).strftime('%c')
2021-07-21 01:12:26 +00:00
return { ' html ' : lambda : render_template ( " userpage.html " ,
2021-07-28 15:37:38 +00:00
unban = u . unban_string ,
2021-07-21 01:12:26 +00:00
u = u ,
v = v ,
listing = listing ,
page = page ,
sort = sort ,
t = t ,
next_exists = next_exists ,
is_following = ( v and u . has_follower ( v ) ) ) ,
' api ' : lambda : jsonify ( { " data " : [ x . json for x in listing ] } )
}
return { ' html ' : lambda : 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 ) ) ) ,
' api ' : lambda : jsonify ( { " data " : [ x . json for x in listing ] } )
}
2021-07-27 22:31:28 +00:00
@app.get ( " /@<username>/comments " )
@app.get ( " /api/v1/user/<username>/comments " )
2021-07-21 01:12:26 +00:00
@auth_desired
@public ( " read " )
def u_username_comments ( username , v = None ) :
if v and v . is_banned and not v . unban_utc : return render_template ( " seized.html " )
# 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 :
return { ' html ' : lambda : render_template ( " userpage_reserved.html " ,
u = u ,
v = v ) ,
' api ' : lambda : { " error " : f " That username is reserved for: { u . reserved } " }
}
if u . is_private and ( not v or ( v . id != u . id and v . admin_level < 3 ) ) :
return { ' html ' : lambda : render_template ( " userpage_private.html " ,
u = u ,
v = v ) ,
' api ' : lambda : { " error " : " That userpage is private " }
}
if u . is_blocking and ( not v or v . admin_level < 3 ) :
return { ' html ' : lambda : render_template ( " userpage_blocking.html " ,
u = u ,
v = v ) ,
' api ' : lambda : { " error " : f " You are blocking @ { u . username } . " }
}
if u . is_blocked and ( not v or v . admin_level < 3 ) :
return { ' html ' : lambda : render_template ( " userpage_blocked.html " ,
u = u ,
v = v ) ,
' api ' : lambda : { " error " : " This person is blocking you. " }
}
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 ) )
return { " html " : lambda : 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 ) ,
" api " : lambda : jsonify ( { " data " : [ c . json for c in listing ] } )
}
2021-07-27 22:31:28 +00:00
@app.get ( " /api/v1/user/<username>/info " )
2021-07-21 01:12:26 +00:00
@auth_desired
@public ( " read " )
def u_username_info ( username , v = None ) :
user = get_user ( username , v = v )
if user . is_blocking :
return jsonify ( { " error " : " You ' re blocking this user. " } ) , 401
elif user . is_blocked :
return jsonify ( { " error " : " This user is blocking you. " } ) , 403
return jsonify ( user . json )
2021-07-27 22:31:28 +00:00
@app.post ( " /api/follow/<username> " )
2021-07-21 01:12:26 +00:00
@auth_required
def follow_user ( username , v ) :
target = get_user ( username )
if target . id == v . id :
return jsonify ( { " error " : " You can ' t follow yourself! " } ) , 400
# check for existing follow
if g . db . query ( Follow ) . filter_by ( user_id = v . id , target_id = target . id ) . first ( ) :
abort ( 409 )
new_follow = Follow ( user_id = v . id ,
target_id = target . id )
g . db . add ( new_follow )
2021-07-26 02:17:14 +00:00
target . stored_subscriber_count = g . db . query ( Follow ) . filter_by ( target_id = target . id )
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-27 22:31:28 +00:00
@app.post ( " /api/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
follow = g . db . query ( Follow ) . filter_by (
user_id = v . id , target_id = target . id ) . first ( )
if not follow :
abort ( 409 )
g . db . delete ( follow )
2021-07-26 02:17:14 +00:00
target . stored_subscriber_count = g . db . query ( Follow ) . filter_by ( target_id = target . id )
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
@app.route ( " /@<username>/pic/profile " )
@limiter.exempt
def user_profile ( username ) :
x = get_user ( username )
return redirect ( x . profile_url )
@app.route ( " /uid/<uid>/pic/profile " )
@limiter.exempt
def user_profile_uid ( uid ) :
x = get_account ( uid )
return redirect ( x . profile_url )
2021-07-27 22:31:28 +00:00
@app.get ( " /@<username>/saved/posts " )
@app.get ( " /api/v1/saved/posts " )
2021-07-21 01:12:26 +00:00
@auth_required
@api ( " read " )
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
return { ' html ' : lambda : render_template ( " userpage.html " ,
u = v ,
v = v ,
listing = listing ,
page = page ,
next_exists = next_exists ,
) ,
' api ' : lambda : jsonify ( { " data " : [ x . json for x in listing ] } )
}
2021-07-27 22:31:28 +00:00
@app.get ( " /@<username>/saved/comments " )
@app.get ( " /api/v1/saved/comments " )
2021-07-21 01:12:26 +00:00
@auth_required
@api ( " read " )
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
listing = get_comments ( ids , v = v , sort = " new " )
return { ' html ' : lambda : render_template ( " userpage_comments.html " ,
u = v ,
v = v ,
listing = listing ,
page = page ,
next_exists = next_exists ,
standalone = True ) ,
' api ' : lambda : jsonify ( { " data " : [ x . json for x in listing ] } )
2021-07-21 01:40:26 +00:00
}