mirror of
https://codeberg.org/rimu/pyfedi
synced 2025-01-23 19:36:56 -08:00
Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
522a607647
12 changed files with 96 additions and 265 deletions
|
@ -16,7 +16,8 @@ from app.activitypub.routes import process_inbox_request, process_delete_request
|
|||
from app.activitypub.signature import post_request, default_context
|
||||
from app.activitypub.util import instance_allowed, instance_blocked, extract_domain_and_actor
|
||||
from app.admin.forms import FederationForm, SiteMiscForm, SiteProfileForm, EditCommunityForm, EditUserForm, \
|
||||
EditTopicForm, SendNewsletterForm, AddUserForm, PreLoadCommunitiesForm, ImportExportBannedListsForm
|
||||
EditTopicForm, SendNewsletterForm, AddUserForm, PreLoadCommunitiesForm, ImportExportBannedListsForm, \
|
||||
EditInstanceForm
|
||||
from app.admin.util import unsubscribe_from_everything_then_delete, unsubscribe_from_community, send_newsletter, \
|
||||
topics_for_form
|
||||
from app.community.util import save_icon_file, save_banner_file, search_for_community
|
||||
|
|
|
@ -25,13 +25,7 @@ def get_community_list(auth, data):
|
|||
page = int(data['page']) if data and 'page' in data else 1
|
||||
limit = int(data['limit']) if data and 'limit' in data else 10
|
||||
|
||||
if auth:
|
||||
try:
|
||||
user_id = authorise_api_user(auth)
|
||||
except:
|
||||
raise
|
||||
else:
|
||||
user_id = None
|
||||
user_id = authorise_api_user(auth) if auth else None
|
||||
|
||||
communities = cached_community_list(type, user_id)
|
||||
|
||||
|
@ -57,71 +51,36 @@ def get_community(auth, data):
|
|||
elif 'name' in data:
|
||||
community = data['name']
|
||||
|
||||
if auth:
|
||||
try:
|
||||
user_id = authorise_api_user(auth)
|
||||
except:
|
||||
raise
|
||||
else:
|
||||
user_id = None
|
||||
user_id = authorise_api_user(auth) if auth else None
|
||||
|
||||
try:
|
||||
community_json = community_view(community=community, variant=3, stub=False, user_id=user_id)
|
||||
return community_json
|
||||
except:
|
||||
raise
|
||||
|
||||
|
||||
# would be in app/constants.py
|
||||
SRC_API = 3
|
||||
|
||||
def post_community_follow(auth, data):
|
||||
try:
|
||||
required(['community_id', 'follow'], data)
|
||||
integer_expected(['community_id'], data)
|
||||
boolean_expected(['follow'], data)
|
||||
except:
|
||||
raise
|
||||
|
||||
community_id = data['community_id']
|
||||
follow = data['follow']
|
||||
|
||||
if auth:
|
||||
try:
|
||||
user_id = authorise_api_user(auth)
|
||||
except:
|
||||
raise
|
||||
else:
|
||||
user_id = None
|
||||
|
||||
try:
|
||||
if follow == True:
|
||||
user_id = join_community(community_id, SRC_API, auth)
|
||||
else:
|
||||
user_id = leave_community(community_id, SRC_API, auth)
|
||||
user_id = join_community(community_id, SRC_API, auth) if follow else leave_community(community_id, SRC_API, auth)
|
||||
community_json = community_view(community=community_id, variant=4, stub=False, user_id=user_id)
|
||||
return community_json
|
||||
except:
|
||||
raise
|
||||
|
||||
|
||||
def post_community_block(auth, data):
|
||||
try:
|
||||
required(['community_id', 'block'], data)
|
||||
integer_expected(['community_id'], data)
|
||||
boolean_expected(['block'], data)
|
||||
except:
|
||||
raise
|
||||
|
||||
community_id = data['community_id']
|
||||
block = data['block']
|
||||
|
||||
try:
|
||||
if block == True:
|
||||
user_id = block_community(community_id, SRC_API, auth)
|
||||
else:
|
||||
user_id = unblock_community(community_id, SRC_API, auth)
|
||||
user_id = block_community(community_id, SRC_API, auth) if block else unblock_community(community_id, SRC_API, auth)
|
||||
community_json = community_view(community=community_id, variant=5, user_id=user_id)
|
||||
return community_json
|
||||
except:
|
||||
raise
|
||||
|
|
|
@ -55,10 +55,7 @@ def get_post_list(auth, data, user_id=None):
|
|||
limit = int(data['limit']) if data and 'limit' in data else 10
|
||||
|
||||
if auth:
|
||||
try:
|
||||
user_id = authorise_api_user(auth)
|
||||
except:
|
||||
raise
|
||||
|
||||
# user_id: the logged in user
|
||||
# person_id: the author of the posts being requested
|
||||
|
@ -92,13 +89,7 @@ def get_post(auth, data):
|
|||
|
||||
id = int(data['id'])
|
||||
|
||||
if auth:
|
||||
try:
|
||||
user_id = authorise_api_user(auth)
|
||||
except Exception as e:
|
||||
raise e
|
||||
else:
|
||||
user_id = None
|
||||
user_id = authorise_api_user(auth) if auth else None
|
||||
|
||||
post_json = post_view(post=id, variant=3, user_id=user_id)
|
||||
if post_json:
|
||||
|
@ -111,11 +102,8 @@ def get_post(auth, data):
|
|||
SRC_API = 3
|
||||
|
||||
def post_post_like(auth, data):
|
||||
try:
|
||||
required(['post_id', 'score'], data)
|
||||
integer_expected(['post_id', 'score'], data)
|
||||
except:
|
||||
raise
|
||||
|
||||
post_id = data['post_id']
|
||||
score = data['score']
|
||||
|
@ -127,58 +115,34 @@ def post_post_like(auth, data):
|
|||
score = 0
|
||||
direction = 'reversal'
|
||||
|
||||
try:
|
||||
user_id = vote_for_post(post_id, direction, SRC_API, auth)
|
||||
cache.delete_memoized(cached_post_list)
|
||||
post_json = post_view(post=post_id, variant=4, user_id=user_id, my_vote=score)
|
||||
return post_json
|
||||
except:
|
||||
raise
|
||||
|
||||
|
||||
def put_post_save(auth, data):
|
||||
try:
|
||||
required(['post_id', 'save'], data)
|
||||
integer_expected(['post_id'], data)
|
||||
boolean_expected(['save'], data)
|
||||
except:
|
||||
raise
|
||||
|
||||
post_id = data['post_id']
|
||||
save = data['save']
|
||||
|
||||
try:
|
||||
if save is True:
|
||||
user_id = bookmark_the_post(post_id, SRC_API, auth)
|
||||
else:
|
||||
user_id = remove_the_bookmark_from_post(post_id, SRC_API, auth)
|
||||
user_id = bookmark_the_post(post_id, SRC_API, auth) if save else remove_the_bookmark_from_post(post_id, SRC_API, auth)
|
||||
post_json = post_view(post=post_id, variant=4, user_id=user_id)
|
||||
return post_json
|
||||
except:
|
||||
raise
|
||||
|
||||
|
||||
def put_post_subscribe(auth, data):
|
||||
try:
|
||||
required(['post_id', 'subscribe'], data)
|
||||
integer_expected(['post_id'], data)
|
||||
boolean_expected(['subscribe'], data)
|
||||
except:
|
||||
raise
|
||||
|
||||
post_id = data['post_id']
|
||||
subscribe = data['subscribe'] # not actually processed - is just a toggle
|
||||
|
||||
try:
|
||||
user_id = toggle_post_notification(post_id, SRC_API, auth)
|
||||
post_json = post_view(post=post_id, variant=4, user_id=user_id)
|
||||
return post_json
|
||||
except:
|
||||
raise
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -42,10 +42,7 @@ def get_reply_list(auth, data, user_id=None):
|
|||
raise Exception('missing_parameters')
|
||||
else:
|
||||
if auth:
|
||||
try:
|
||||
user_id = authorise_api_user(auth)
|
||||
except:
|
||||
raise
|
||||
replies = cached_reply_list(post_id, person_id, sort, max_depth, user_id)
|
||||
|
||||
# user_id: the logged in user
|
||||
|
@ -72,11 +69,8 @@ def get_reply_list(auth, data, user_id=None):
|
|||
SRC_API = 3
|
||||
|
||||
def post_reply_like(auth, data):
|
||||
try:
|
||||
required(['comment_id', 'score'], data)
|
||||
integer_expected(['comment_id', 'score'], data)
|
||||
except:
|
||||
raise
|
||||
|
||||
score = data['score']
|
||||
reply_id = data['comment_id']
|
||||
|
@ -88,51 +82,33 @@ def post_reply_like(auth, data):
|
|||
score = 0
|
||||
direction = 'reversal'
|
||||
|
||||
try:
|
||||
user_id = vote_for_reply(reply_id, direction, SRC_API, auth)
|
||||
cache.delete_memoized(cached_reply_list)
|
||||
reply_json = reply_view(reply=reply_id, variant=4, user_id=user_id, my_vote=score)
|
||||
return reply_json
|
||||
except:
|
||||
raise
|
||||
|
||||
|
||||
def put_reply_save(auth, data):
|
||||
try:
|
||||
required(['comment_id', 'save'], data)
|
||||
integer_expected(['comment_id'], data)
|
||||
boolean_expected(['save'], data)
|
||||
except:
|
||||
raise
|
||||
|
||||
reply_id = data['comment_id']
|
||||
save = data['save']
|
||||
|
||||
try:
|
||||
if save is True:
|
||||
user_id = bookmark_the_post_reply(reply_id, SRC_API, auth)
|
||||
else:
|
||||
user_id = remove_the_bookmark_from_post_reply(reply_id, SRC_API, auth)
|
||||
user_id = bookmark_the_post_reply(reply_id, SRC_API, auth) if save else remove_the_bookmark_from_post_reply(reply_id, SRC_API, auth)
|
||||
reply_json = reply_view(reply=reply_id, variant=4, user_id=user_id)
|
||||
return reply_json
|
||||
except:
|
||||
raise
|
||||
|
||||
|
||||
def put_reply_subscribe(auth, data):
|
||||
try:
|
||||
required(['comment_id', 'subscribe'], data)
|
||||
integer_expected(['comment_id'], data)
|
||||
boolean_expected(['subscribe'], data)
|
||||
except:
|
||||
raise
|
||||
|
||||
reply_id = data['comment_id']
|
||||
subscribe = data['subscribe'] # not actually processed - is just a toggle
|
||||
|
||||
try:
|
||||
user_id = toggle_post_reply_notification(reply_id, SRC_API, auth)
|
||||
reply_json = reply_view(reply=reply_id, variant=4, user_id=user_id)
|
||||
return reply_json
|
||||
except:
|
||||
raise
|
||||
|
|
|
@ -13,10 +13,7 @@ def users_total():
|
|||
|
||||
def get_site(auth):
|
||||
if auth:
|
||||
try:
|
||||
user = authorise_api_user(auth, return_type='model')
|
||||
except Exception as e:
|
||||
raise e
|
||||
else:
|
||||
user = None
|
||||
|
||||
|
|
|
@ -19,48 +19,33 @@ def get_user(auth, data):
|
|||
|
||||
user_id = None
|
||||
if auth:
|
||||
try:
|
||||
user_id = authorise_api_user(auth)
|
||||
if 'username' in data:
|
||||
data['person_id'] = user_id
|
||||
person_id = int(user_id)
|
||||
auth = None # avoid authenticating user again in get_post_list and get_reply_list
|
||||
except Exception as e:
|
||||
raise e
|
||||
|
||||
# bit unusual. have to help construct the json here rather than in views, to avoid circular dependencies
|
||||
post_list = get_post_list(auth, data, user_id)
|
||||
reply_list = get_reply_list(auth, data, user_id)
|
||||
|
||||
try:
|
||||
user_json = user_view(user=person_id, variant=3)
|
||||
user_json['posts'] = post_list['posts']
|
||||
user_json['comments'] = reply_list['comments']
|
||||
return user_json
|
||||
except:
|
||||
raise
|
||||
|
||||
|
||||
# would be in app/constants.py
|
||||
SRC_API = 3
|
||||
|
||||
def post_user_block(auth, data):
|
||||
try:
|
||||
required(['person_id', 'block'], data)
|
||||
integer_expected(['post_id'], data)
|
||||
boolean_expected(['block'], data)
|
||||
except:
|
||||
raise
|
||||
|
||||
person_id = data['person_id']
|
||||
block = data['block']
|
||||
|
||||
try:
|
||||
if block == True:
|
||||
user_id = block_another_user(person_id, SRC_API, auth)
|
||||
else:
|
||||
user_id = unblock_another_user(person_id, SRC_API, auth)
|
||||
user_id = block_another_user(person_id, SRC_API, auth) if block else unblock_another_user(person_id, SRC_API, auth)
|
||||
user_json = user_view(user=person_id, variant=4, user_id=user_id)
|
||||
return user_json
|
||||
except:
|
||||
raise
|
||||
|
|
|
@ -82,12 +82,9 @@ def post_view(post: Post | int, variant, stub=False, user_id=None, my_vote=0):
|
|||
'saved': saved, 'read': False, 'hidden': False, 'unread_comments': post.reply_count, 'my_vote': my_vote, 'activity_alert': activity_alert,
|
||||
'creator_banned_from_community': creator_banned_from_community, 'creator_is_moderator': creator_is_moderator, 'creator_is_admin': creator_is_admin}
|
||||
|
||||
try:
|
||||
creator = user_view(user=post.user_id, variant=1, stub=True)
|
||||
community = community_view(community=post.community_id, variant=1, stub=True)
|
||||
v2.update({'creator': creator, 'community': community})
|
||||
except:
|
||||
raise
|
||||
|
||||
return v2
|
||||
|
||||
|
@ -319,13 +316,10 @@ def reply_view(reply: PostReply | int, variant, user_id=None, my_vote=0):
|
|||
v2 = {'comment': reply_view(reply=reply, variant=1), 'counts': counts, 'banned_from_community': False, 'subscribed': 'NotSubscribed',
|
||||
'saved': saved, 'creator_blocked': False, 'my_vote': my_vote, 'activity_alert': activity_alert,
|
||||
'creator_banned_from_community': creator_banned_from_community, 'creator_is_moderator': creator_is_moderator, 'creator_is_admin': creator_is_admin}
|
||||
try:
|
||||
creator = user_view(user=reply.user_id, variant=1, stub=True)
|
||||
community = community_view(community=reply.community_id, variant=1, stub=True)
|
||||
post = post_view(post=reply.post_id, variant=1)
|
||||
v2.update({'creator': creator, 'community': community, 'post': post})
|
||||
except:
|
||||
raise
|
||||
|
||||
return v2
|
||||
|
||||
|
|
|
@ -22,11 +22,8 @@ def log_user_in(input, src):
|
|||
username = input.user_name.data
|
||||
password = input.password.data
|
||||
elif src == SRC_API:
|
||||
try:
|
||||
required(["username_or_email", "password"], input)
|
||||
string_expected(["username_or_email", "password"], input)
|
||||
except Exception:
|
||||
raise
|
||||
|
||||
username = input['username_or_email']
|
||||
password = input['password']
|
||||
|
|
|
@ -20,10 +20,7 @@ def join_community(community_id: int, src, auth=None, user_id=None, main_user_na
|
|||
community = Community.query.get(community_id)
|
||||
if not community:
|
||||
raise Exception('community_not_found')
|
||||
try:
|
||||
user = authorise_api_user(auth, return_type='model')
|
||||
except:
|
||||
raise
|
||||
else:
|
||||
community = Community.query.get_or_404(community_id)
|
||||
if not user_id:
|
||||
|
@ -118,10 +115,7 @@ def leave_community(community_id: int, src, auth=None):
|
|||
community = Community.query.get(community_id)
|
||||
if not community:
|
||||
raise Exception('community_not_found')
|
||||
try:
|
||||
user = authorise_api_user(auth, return_type='model')
|
||||
except:
|
||||
raise
|
||||
else:
|
||||
community = Community.query.get_or_404(community_id)
|
||||
user = current_user
|
||||
|
@ -183,10 +177,7 @@ def leave_community(community_id: int, src, auth=None):
|
|||
|
||||
def block_community(community_id, src, auth=None):
|
||||
if src == SRC_API:
|
||||
try:
|
||||
user_id = authorise_api_user(auth)
|
||||
except:
|
||||
raise
|
||||
else:
|
||||
user_id = current_user.id
|
||||
|
||||
|
@ -204,10 +195,7 @@ def block_community(community_id, src, auth=None):
|
|||
|
||||
def unblock_community(community_id, src, auth=None):
|
||||
if src == SRC_API:
|
||||
try:
|
||||
user_id = authorise_api_user(auth)
|
||||
except:
|
||||
raise
|
||||
else:
|
||||
user_id = current_user.id
|
||||
|
||||
|
|
|
@ -23,10 +23,7 @@ def vote_for_post(post_id: int, vote_direction, src, auth=None):
|
|||
post = Post.query.get(post_id)
|
||||
if not post:
|
||||
raise Exception('post_not_found')
|
||||
try:
|
||||
user = authorise_api_user(auth, return_type='model')
|
||||
except:
|
||||
raise
|
||||
else:
|
||||
post = Post.query.get_or_404(post_id)
|
||||
user = current_user
|
||||
|
@ -103,10 +100,7 @@ def bookmark_the_post(post_id: int, src, auth=None):
|
|||
post = Post.query.get(post_id)
|
||||
if not post or post.deleted:
|
||||
raise Exception('post_not_found')
|
||||
try:
|
||||
user_id = authorise_api_user(auth)
|
||||
except:
|
||||
raise
|
||||
else:
|
||||
post = Post.query.get_or_404(post_id)
|
||||
if post.deleted:
|
||||
|
@ -136,10 +130,7 @@ def remove_the_bookmark_from_post(post_id: int, src, auth=None):
|
|||
post = Post.query.get(post_id)
|
||||
if not post or post.deleted:
|
||||
raise Exception('post_not_found')
|
||||
try:
|
||||
user_id = authorise_api_user(auth)
|
||||
except:
|
||||
raise
|
||||
else:
|
||||
post = Post.query.get_or_404(post_id)
|
||||
if post.deleted:
|
||||
|
@ -168,10 +159,7 @@ def toggle_post_notification(post_id: int, src, auth=None):
|
|||
post = Post.query.get(post_id)
|
||||
if not post or post.deleted:
|
||||
raise Exception('post_not_found')
|
||||
try:
|
||||
user_id = authorise_api_user(auth)
|
||||
except:
|
||||
raise
|
||||
else:
|
||||
post = Post.query.get_or_404(post_id)
|
||||
if post.deleted:
|
||||
|
|
|
@ -23,10 +23,7 @@ def vote_for_reply(reply_id: int, vote_direction, src, auth=None):
|
|||
reply = PostReply.query.get(reply_id)
|
||||
if not reply:
|
||||
raise Exception('reply_not_found')
|
||||
try:
|
||||
user = authorise_api_user(auth, return_type='model')
|
||||
except:
|
||||
raise
|
||||
else:
|
||||
reply = PostReply.query.get_or_404(post_id)
|
||||
user = current_user
|
||||
|
@ -103,10 +100,7 @@ def bookmark_the_post_reply(comment_id: int, src, auth=None):
|
|||
post_reply = PostReply.query.get(comment_id)
|
||||
if not post_reply or post_reply.deleted:
|
||||
raise Exception('comment_not_found')
|
||||
try:
|
||||
user_id = authorise_api_user(auth)
|
||||
except:
|
||||
raise
|
||||
else:
|
||||
post_reply = PostReply.query.get_or_404(comment_id)
|
||||
if post_reply.deleted:
|
||||
|
@ -137,10 +131,7 @@ def remove_the_bookmark_from_post_reply(comment_id: int, src, auth=None):
|
|||
post_reply = PostReply.query.get(comment_id)
|
||||
if not post_reply or post_reply.deleted:
|
||||
raise Exception('comment_not_found')
|
||||
try:
|
||||
user_id = authorise_api_user(auth)
|
||||
except:
|
||||
raise
|
||||
else:
|
||||
post_reply = PostReply.query.get_or_404(comment_id)
|
||||
if post_reply.deleted:
|
||||
|
@ -169,10 +160,7 @@ def toggle_post_reply_notification(post_reply_id: int, src, auth=None):
|
|||
post_reply = PostReply.query.get(post_reply_id)
|
||||
if not post_reply or post_reply.deleted:
|
||||
raise Exception('comment_not_found')
|
||||
try:
|
||||
user_id = authorise_api_user(auth)
|
||||
except:
|
||||
raise
|
||||
else:
|
||||
post_reply = PostReply.query.get_or_404(post_reply_id)
|
||||
if post_reply.deleted:
|
||||
|
|
|
@ -21,10 +21,7 @@ SRC_API = 3
|
|||
|
||||
def block_another_user(person_id, src, auth=None):
|
||||
if src == SRC_API:
|
||||
try:
|
||||
user_id = authorise_api_user(auth)
|
||||
except:
|
||||
raise
|
||||
else:
|
||||
user_id = current_user.id
|
||||
|
||||
|
@ -63,10 +60,7 @@ def block_another_user(person_id, src, auth=None):
|
|||
|
||||
def unblock_another_user(person_id, src, auth=None):
|
||||
if src == SRC_API:
|
||||
try:
|
||||
user_id = authorise_api_user(auth)
|
||||
except:
|
||||
raise
|
||||
else:
|
||||
user_id = current_user.id
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue