Change how the user avatar is exchanged between client und server

This commit is contained in:
Patrick Jentsch 2023-03-16 09:54:48 +01:00
parent 7ea7b6d7a7
commit f8e94a721f
3 changed files with 4 additions and 7 deletions

View File

@ -838,6 +838,7 @@ class User(HashidMixin, UserMixin, db.Model):
json_serializeable = { json_serializeable = {
'id': self.hashid, 'id': self.hashid,
'confirmed': self.confirmed, 'confirmed': self.confirmed,
'avatar': url_for('users.profile_avatar', user_id=self.id),
'email': self.email, 'email': self.email,
'last_seen': ( 'last_seen': (
None if self.last_seen is None None if self.last_seen is None
@ -857,10 +858,6 @@ class User(HashidMixin, UserMixin, db.Model):
'show_last_seen': self.has_profile_privacy_setting(ProfilePrivacySettings.SHOW_LAST_SEEN), 'show_last_seen': self.has_profile_privacy_setting(ProfilePrivacySettings.SHOW_LAST_SEEN),
'show_member_since': self.has_profile_privacy_setting(ProfilePrivacySettings.SHOW_MEMBER_SINCE) 'show_member_since': self.has_profile_privacy_setting(ProfilePrivacySettings.SHOW_MEMBER_SINCE)
} }
json_serializeable['avatar'] = (
None if self.avatar is None
else self.avatar.to_json_serializeable(relationships=True)
)
if backrefs: if backrefs:
json_serializeable['role'] = \ json_serializeable['role'] = \
self.role.to_json_serializeable(backrefs=True) self.role.to_json_serializeable(backrefs=True)

View File

@ -72,7 +72,7 @@ class UserList extends ResourceList {
return { return {
'id': user.id, 'id': user.id,
'member-since': user.member_since, 'member-since': user.member_since,
'avatar': user.avatar ? `/users/${user.id}/avatar` : '/static/images/user_avatar.png', 'avatar': user.avatar,
'username': user.username, 'username': user.username,
'full-name': user.full_name ? user.full_name : '', 'full-name': user.full_name ? user.full_name : '',
'location': user.location ? user.location : '', 'location': user.location ? user.location : '',

View File

@ -64,10 +64,10 @@ def user(user_id):
@login_required @login_required
def profile_avatar(user_id): def profile_avatar(user_id):
user = User.query.get_or_404(user_id) user = User.query.get_or_404(user_id)
if not (user.is_public or user == current_user or current_user.is_administrator()):
abort(403)
if user.avatar is None: if user.avatar is None:
return redirect(url_for('static', filename='images/user_avatar.png')) return redirect(url_for('static', filename='images/user_avatar.png'))
if not user.is_public and not (user == current_user or current_user.is_administrator()):
abort(403)
return send_from_directory( return send_from_directory(
os.path.dirname(user.avatar.path), os.path.dirname(user.avatar.path),
os.path.basename(user.avatar.path), os.path.basename(user.avatar.path),