server/users: reduce user fields footprint
This commit is contained in:
parent
9d6878a1aa
commit
805ca845e3
5 changed files with 20 additions and 8 deletions
12
API.md
12
API.md
|
@ -64,6 +64,7 @@
|
|||
3. [Resources](#resources)
|
||||
|
||||
- [User](#user)
|
||||
- [Micro user](#micro-user)
|
||||
- [Tag category](#tag-category)
|
||||
- [Tag](#tag)
|
||||
- [Post](#post)
|
||||
|
@ -1427,6 +1428,11 @@ A single user.
|
|||
|
||||
- `<avatarUrl>`: the URL to the avatar.
|
||||
|
||||
## Micro user
|
||||
**Description**
|
||||
|
||||
A [user resource](#user) stripped down to `name` and `avatarUrl` fields.
|
||||
|
||||
## Tag category
|
||||
**Description**
|
||||
|
||||
|
@ -1584,7 +1590,7 @@ One file together with its metadata posted to the site.
|
|||
to the user by the web client.
|
||||
- `<notes>`: a list of post annotations, serialized as list of [note
|
||||
resources](#note).
|
||||
- `<user>`: who created the post, serialized as [user resource](#user).
|
||||
- `<user>`: who created the post, serialized as [micro user resource](#micro-user).
|
||||
- `<score>`: the collective score (+1/-1 rating) of the given post.
|
||||
- `<own-score>`: the score (+1/-1 rating) of the given post by the
|
||||
authenticated user.
|
||||
|
@ -1595,7 +1601,7 @@ One file together with its metadata posted to the site.
|
|||
- `<feature-count>`: how many times has the post been featured.
|
||||
- `<last-feature-time>`: the last time the post was featured, formatted as per
|
||||
RFC 3339.
|
||||
- `<favorited-by>`: list of users, serialized as [user resources](#user).
|
||||
- `<favorited-by>`: list of users, serialized as [micro user resources](#micro-user).
|
||||
- `<has-custom-thumbnail>`: whether the post uses custom thumbnail.
|
||||
- `<mime-type>`: subsidiary to `<type>`, used to tell exact content format;
|
||||
useful for `<video>` tags for instance.
|
||||
|
@ -1648,7 +1654,7 @@ A comment under a post.
|
|||
- `<id>`: the comment identifier.
|
||||
- `<post-id>`: an id of the post the comment is for.
|
||||
- `<text>`: the comment content. The client should render is as Markdown.
|
||||
- `<author>`: a user resource the comment is created by.
|
||||
- `<author>`: a [micro user resource](#micro-user) the comment is created by.
|
||||
- `<creation-time>`: time the comment was created, formatted as per RFC 3339.
|
||||
- `<last-edit-time>`: time the comment was edited, formatted as per RFC 3339.
|
||||
- `<score>`: the collective score (+1/-1 rating) of the given comment.
|
||||
|
|
|
@ -10,7 +10,7 @@ def serialize_comment(comment, authenticated_user, options=None):
|
|||
comment,
|
||||
{
|
||||
'id': lambda: comment.comment_id,
|
||||
'user': lambda: users.serialize_user(comment.user, authenticated_user),
|
||||
'user': lambda: users.serialize_micro_user(comment.user),
|
||||
'postId': lambda: comment.post.post_id,
|
||||
'text': lambda: comment.text,
|
||||
'creationTime': lambda: comment.creation_time,
|
||||
|
|
|
@ -81,7 +81,7 @@ def serialize_post(post, authenticated_user, options=None):
|
|||
'flags': lambda: post.flags,
|
||||
'tags': lambda: [tag.names[0].name for tag in post.tags],
|
||||
'relations': lambda: [rel.post_id for rel in post.relations],
|
||||
'user': lambda: users.serialize_user(post.user, authenticated_user),
|
||||
'user': lambda: users.serialize_micro_user(post.user),
|
||||
'score': lambda: post.score,
|
||||
'ownScore': lambda: scores.get_score(post, authenticated_user),
|
||||
'tagCount': lambda: post.tag_count,
|
||||
|
@ -91,7 +91,7 @@ def serialize_post(post, authenticated_user, options=None):
|
|||
'featureCount': lambda: post.feature_count,
|
||||
'lastFeatureTime': lambda: post.last_feature_time,
|
||||
'favoritedBy': lambda: [
|
||||
users.serialize_user(rel.user, authenticated_user) \
|
||||
users.serialize_micro_user(rel.user) \
|
||||
for rel in post.favorited_by],
|
||||
'hasCustomThumbnail':
|
||||
lambda: files.has(get_post_thumbnail_backup_path(post)),
|
||||
|
|
|
@ -42,6 +42,12 @@ def serialize_user(user, authenticated_user, options=None, force_show_email=Fals
|
|||
},
|
||||
options)
|
||||
|
||||
def serialize_micro_user(user):
|
||||
return serialize_user(
|
||||
user,
|
||||
authenticated_user=None,
|
||||
options=['name', 'avatarUrl'])
|
||||
|
||||
def get_user_count():
|
||||
return db.session.query(db.User).count()
|
||||
|
||||
|
|
|
@ -68,10 +68,10 @@ def test_serialize_post(
|
|||
post_factory, user_factory, comment_factory, tag_factory, config_injector):
|
||||
config_injector({'data_url': 'http://example.com/'})
|
||||
with unittest.mock.patch('szurubooru.func.comments.serialize_comment'), \
|
||||
unittest.mock.patch('szurubooru.func.users.serialize_user'), \
|
||||
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_user.side_effect = lambda user, auth_user: user.name
|
||||
users.serialize_micro_user.side_effect = lambda user: user.name
|
||||
comments.serialize_comment.side_effect \
|
||||
= lambda comment, auth_user: comment.user.name
|
||||
snapshots.get_serialized_history.return_value = 'snapshot history'
|
||||
|
|
Loading…
Reference in a new issue