server/posts: allow anonymous users to feature posts

This commit is contained in:
Eva
2025-04-04 08:49:35 +02:00
parent 0b99b98f9e
commit 3c51d1d694
4 changed files with 34 additions and 4 deletions

View File

@ -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

View File

@ -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(),

View File

@ -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

View File

@ -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)