From 0b99b98f9e7c5a1b6d0f2e5398c95c63167b6443 Mon Sep 17 00:00:00 2001 From: Eva Date: Thu, 27 Mar 2025 13:33:48 +0100 Subject: [PATCH 1/3] server/snapshots: treat invalid users as anonymous Fixes the crash described in #682 --- server/szurubooru/func/snapshots.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/server/szurubooru/func/snapshots.py b/server/szurubooru/func/snapshots.py index afb26ea5..f5f4c824 100644 --- a/server/szurubooru/func/snapshots.py +++ b/server/szurubooru/func/snapshots.py @@ -107,6 +107,9 @@ def _create( entity ) + if auth_user and not auth_user.name: + auth_user = None + snapshot = model.Snapshot() snapshot.creation_time = datetime.utcnow() snapshot.operation = operation From 3c51d1d694c736fae788f783ded2d69ca5511511 Mon Sep 17 00:00:00 2001 From: Eva Date: Fri, 4 Apr 2025 08:49:35 +0200 Subject: [PATCH 2/3] server/posts: allow anonymous users to feature posts --- server/szurubooru/func/posts.py | 2 ++ server/szurubooru/func/users.py | 6 +++-- ...c940b4e78_make_featuring_users_nullable.py | 26 +++++++++++++++++++ server/szurubooru/model/post.py | 4 +-- 4 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 server/szurubooru/migrations/versions/5b5c940b4e78_make_featuring_users_nullable.py diff --git a/server/szurubooru/func/posts.py b/server/szurubooru/func/posts.py index be2259cf..fdaf9a91 100644 --- a/server/szurubooru/func/posts.py +++ b/server/szurubooru/func/posts.py @@ -794,6 +794,8 @@ def update_post_flags(post: model.Post, flags: List[str]) -> None: def feature_post(post: model.Post, user: Optional[model.User]) -> None: assert post + if user and not user.name: + user = None post_feature = model.PostFeature() post_feature.time = datetime.utcnow() post_feature.post = post diff --git a/server/szurubooru/func/users.py b/server/szurubooru/func/users.py index 5cbe3cc0..1c9ba116 100644 --- a/server/szurubooru/func/users.py +++ b/server/szurubooru/func/users.py @@ -43,12 +43,14 @@ def get_avatar_path(user_name: str) -> str: def get_avatar_url(user: model.User) -> str: assert user if user.avatar_style == user.AVATAR_GRAVATAR: - assert user.email or user.name + if not user.email and not user.name: + return "" return "https://gravatar.com/avatar/%s?d=retro&s=%d" % ( util.get_md5((user.email or user.name).lower()), config.config["thumbnails"]["avatar_width"], ) - assert user.name + if not user.name: + return "" return "%s/avatars/%s.png" % ( config.config["data_url"].rstrip("/"), user.name.lower(), diff --git a/server/szurubooru/migrations/versions/5b5c940b4e78_make_featuring_users_nullable.py b/server/szurubooru/migrations/versions/5b5c940b4e78_make_featuring_users_nullable.py new file mode 100644 index 00000000..2312c7dc --- /dev/null +++ b/server/szurubooru/migrations/versions/5b5c940b4e78_make_featuring_users_nullable.py @@ -0,0 +1,26 @@ +''' +make featuring users nullable + +Revision ID: 5b5c940b4e78 +Created at: 2025-04-04 08:24:39.000603 +''' + +import sqlalchemy as sa +from alembic import op + +revision = '5b5c940b4e78' +down_revision = 'adcd63ff76a2' +branch_labels = None +depends_on = None + +def upgrade(): + op.alter_column( + "post_feature", "user_id", nullable=True, existing_nullable=False + ) + pass + +def downgrade(): + op.alter_column( + "post_feature", "user_id", nullable=False, existing_nullable=True + ) + pass diff --git a/server/szurubooru/model/post.py b/server/szurubooru/model/post.py index 49e748dc..eb394167 100644 --- a/server/szurubooru/model/post.py +++ b/server/szurubooru/model/post.py @@ -24,8 +24,8 @@ class PostFeature(Base): user_id = sa.Column( "user_id", sa.Integer, - sa.ForeignKey("user.id"), - nullable=False, + sa.ForeignKey("user.id", ondelete="SET NULL"), + nullable=True, index=True, ) time = sa.Column("time", sa.DateTime, nullable=False) From bfba05cfae4e7f0d3a2d264a112b88ae1e6e28e9 Mon Sep 17 00:00:00 2001 From: Eva Date: Fri, 4 Apr 2025 08:50:04 +0200 Subject: [PATCH 3/3] server/comments: allow anonymous users to leave comments --- client/html/comment.tpl | 2 +- server/szurubooru/api/comment_api.py | 2 +- server/szurubooru/model/comment.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/client/html/comment.tpl b/client/html/comment.tpl index 6bea7045..df7b3664 100644 --- a/client/html/comment.tpl +++ b/client/html/comment.tpl @@ -26,7 +26,7 @@ %>'><% %><% } %><% - %><%- ctx.user ? ctx.user.name : 'Deleted user' %><% + %><%- ctx.user ? ctx.user.name : 'Anonymous' %><% %><% if (ctx.user && ctx.user.name && ctx.canViewUsers) { %><% %><% diff --git a/server/szurubooru/api/comment_api.py b/server/szurubooru/api/comment_api.py index d60d23ed..0dbc93de 100644 --- a/server/szurubooru/api/comment_api.py +++ b/server/szurubooru/api/comment_api.py @@ -48,7 +48,7 @@ def create_comment( text = ctx.get_param_as_string("text") post_id = ctx.get_param_as_int("postId") post = posts.get_post_by_id(post_id) - comment = comments.create_comment(ctx.user, post, text) + comment = comments.create_comment(ctx.user if ctx.user.name else None, post, text) ctx.session.add(comment) ctx.session.commit() return _serialize(ctx, comment) diff --git a/server/szurubooru/model/comment.py b/server/szurubooru/model/comment.py index e64961e6..d35c5af1 100644 --- a/server/szurubooru/model/comment.py +++ b/server/szurubooru/model/comment.py @@ -46,7 +46,7 @@ class Comment(Base): user_id = sa.Column( "user_id", sa.Integer, - sa.ForeignKey("user.id"), + sa.ForeignKey("user.id", ondelete="SET NULL"), nullable=True, index=True, )