diff --git a/server/szurubooru/func/comments.py b/server/szurubooru/func/comments.py index 136c0c09..80462f84 100644 --- a/server/szurubooru/func/comments.py +++ b/server/szurubooru/func/comments.py @@ -5,19 +5,19 @@ from szurubooru.func import users, scores, util class CommentNotFoundError(errors.NotFoundError): pass class EmptyCommentTextError(errors.ValidationError): pass -def serialize_comment(comment, authenticated_user, options=None): +def serialize_comment(comment, auth_user, options=None): return util.serialize_entity( comment, { 'id': lambda: comment.comment_id, - 'user': lambda: users.serialize_micro_user(comment.user), + 'user': lambda: users.serialize_micro_user(comment.user, auth_user), 'postId': lambda: comment.post.post_id, 'version': lambda: comment.version, 'text': lambda: comment.text, 'creationTime': lambda: comment.creation_time, 'lastEditTime': lambda: comment.last_edit_time, 'score': lambda: comment.score, - 'ownScore': lambda: scores.get_score(comment, authenticated_user), + 'ownScore': lambda: scores.get_score(comment, auth_user), }, options) diff --git a/server/szurubooru/func/posts.py b/server/szurubooru/func/posts.py index d8cc59bb..23e2d28e 100644 --- a/server/szurubooru/func/posts.py +++ b/server/szurubooru/func/posts.py @@ -68,7 +68,7 @@ def serialize_note(note): 'text': note.text, } -def serialize_post(post, authenticated_user, options=None): +def serialize_post(post, auth_user, options=None): return util.serialize_entity( post, { @@ -93,16 +93,17 @@ def serialize_post(post, authenticated_user, options=None): { post['id']: post for post in [ - serialize_micro_post(rel) for rel in post.relations + serialize_micro_post(rel, auth_user) \ + for rel in post.relations ] }.values(), key=lambda post: post['id']), - 'user': lambda: users.serialize_micro_user(post.user), + 'user': lambda: users.serialize_micro_user(post.user, auth_user), 'score': lambda: post.score, - 'ownScore': lambda: scores.get_score(post, authenticated_user), + 'ownScore': lambda: scores.get_score(post, auth_user), 'ownFavorite': lambda: len( [user for user in post.favorited_by \ - if user.user_id == authenticated_user.user_id]) > 0, + if user.user_id == auth_user.user_id]) > 0, 'tagCount': lambda: post.tag_count, 'favoriteCount': lambda: post.favorite_count, 'commentCount': lambda: post.comment_count, @@ -111,7 +112,7 @@ def serialize_post(post, authenticated_user, options=None): 'featureCount': lambda: post.feature_count, 'lastFeatureTime': lambda: post.last_feature_time, 'favoritedBy': lambda: [ - users.serialize_micro_user(rel.user) \ + users.serialize_micro_user(rel.user, auth_user) \ for rel in post.favorited_by], 'hasCustomThumbnail': lambda: files.has(get_post_thumbnail_backup_path(post)), @@ -119,7 +120,7 @@ def serialize_post(post, authenticated_user, options=None): [serialize_note(note) for note in post.notes], key=lambda x: x['polygon']), 'comments': lambda: [ - comments.serialize_comment(comment, authenticated_user) \ + comments.serialize_comment(comment, auth_user) \ for comment in sorted( post.comments, key=lambda comment: comment.creation_time)], @@ -127,10 +128,10 @@ def serialize_post(post, authenticated_user, options=None): }, options) -def serialize_micro_post(post): +def serialize_micro_post(post, auth_user): return serialize_post( post, - authenticated_user=None, + auth_user=auth_user, options=['id', 'thumbnailUrl']) def get_post_count(): diff --git a/server/szurubooru/func/users.py b/server/szurubooru/func/users.py index 2f00a389..da69524d 100644 --- a/server/szurubooru/func/users.py +++ b/server/szurubooru/func/users.py @@ -27,30 +27,30 @@ def _get_avatar_url(user): return '%s/avatars/%s.png' % ( config.config['data_url'].rstrip('/'), user.name.lower()) -def _get_email(user, authenticated_user, force_show_email): +def _get_email(user, auth_user, force_show_email): assert user - assert authenticated_user + assert auth_user if not force_show_email \ - and authenticated_user.user_id != user.user_id \ - and not auth.has_privilege(authenticated_user, 'users:edit:any:email'): + and auth_user.user_id != user.user_id \ + and not auth.has_privilege(auth_user, 'users:edit:any:email'): return False return user.email -def _get_liked_post_count(user, authenticated_user): +def _get_liked_post_count(user, auth_user): assert user - assert authenticated_user - if authenticated_user.user_id != user.user_id: + assert auth_user + if auth_user.user_id != user.user_id: return False return user.liked_post_count -def _get_disliked_post_count(user, authenticated_user): +def _get_disliked_post_count(user, auth_user): assert user - assert authenticated_user - if authenticated_user.user_id != user.user_id: + assert auth_user + if auth_user.user_id != user.user_id: return False return user.disliked_post_count -def serialize_user(user, authenticated_user, options=None, force_show_email=False): +def serialize_user(user, auth_user, options=None, force_show_email=False): return util.serialize_entity( user, { @@ -65,18 +65,18 @@ def serialize_user(user, authenticated_user, options=None, force_show_email=Fals 'uploadedPostCount': lambda: user.post_count, 'favoritePostCount': lambda: user.favorite_post_count, 'likedPostCount': - lambda: _get_liked_post_count(user, authenticated_user), + lambda: _get_liked_post_count(user, auth_user), 'dislikedPostCount': - lambda: _get_disliked_post_count(user, authenticated_user), + lambda: _get_disliked_post_count(user, auth_user), 'email': - lambda: _get_email(user, authenticated_user, force_show_email), + lambda: _get_email(user, auth_user, force_show_email), }, options) -def serialize_micro_user(user): +def serialize_micro_user(user, auth_user): return serialize_user( user, - authenticated_user=None, + auth_user=auth_user, options=['name', 'avatarUrl']) def get_user_count(): @@ -162,7 +162,7 @@ def update_user_email(user, email): raise InvalidEmailError('E-mail is invalid.') user.email = email -def update_user_rank(user, rank, authenticated_user): +def update_user_rank(user, rank, auth_user): assert user if not rank: raise InvalidRankError('Rank cannot be empty.') @@ -173,7 +173,7 @@ def update_user_rank(user, rank, authenticated_user): 'Rank can be either of %r.' % all_ranks) if rank in (db.User.RANK_ANONYMOUS, db.User.RANK_NOBODY): raise InvalidRankError('Rank %r cannot be used.' % auth.RANK_MAP[rank]) - if all_ranks.index(authenticated_user.rank) \ + if all_ranks.index(auth_user.rank) \ < all_ranks.index(rank) and get_user_count() > 0: raise errors.AuthError('Trying to set higher rank than your own.') user.rank = rank diff --git a/server/szurubooru/tests/func/test_posts.py b/server/szurubooru/tests/func/test_posts.py index 24fc939f..c26cb309 100644 --- a/server/szurubooru/tests/func/test_posts.py +++ b/server/szurubooru/tests/func/test_posts.py @@ -71,7 +71,8 @@ def test_serialize_post( unittest.mock.patch('szurubooru.func.users.serialize_micro_user'), \ unittest.mock.patch('szurubooru.func.posts.files.has', return_value=True), \ unittest.mock.patch('szurubooru.func.snapshots.get_serialized_history'): - users.serialize_micro_user.side_effect = lambda user: user.name + users.serialize_micro_user.side_effect \ + = lambda user, auth_user: user.name comments.serialize_comment.side_effect \ = lambda comment, auth_user: comment.user.name snapshots.get_serialized_history.return_value = 'snapshot history'