Skip to content

Commit

Permalink
fix spoiler text
Browse files Browse the repository at this point in the history
  • Loading branch information
Her Email authored and alphatownsman committed Dec 16, 2023
1 parent e44761e commit dc5ef33
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions mastodon/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,9 @@ def share_mark(mark):
mark.rating_grade,
site.star_mode if site else 0,
)
content = f"{mark.action_label}{mark.item.display_title}{stars}\n{mark.item.absolute_url}\n{mark.comment_text or ''}{tags}"
spoiler_text, txt = get_spoiler_text(mark.comment_text or "", mark.item)
content = f"{mark.action_label}{mark.item.display_title}{stars}\n{mark.item.absolute_url}\n{txt}{tags}"
update_id = None # get_status_id_by_url(mark.shared_link)
spoiler_text, content = get_spoiler_text(content, mark.item)
response = post_toot(
user.mastodon_site,
content,
Expand Down
28 changes: 26 additions & 2 deletions takahe/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ def post(
pre_conetent: str,
content: str,
visibility: Visibilities,
summary: str | None = None,
sensitive: bool = False,
data: dict | None = None,
post_pk: int | None = None,
post_time: datetime.datetime | None = None,
Expand All @@ -392,6 +394,8 @@ def post(
post.edit_local(
pre_conetent,
content,
summary,
sensitive,
visibility=visibility,
type_data=data,
published=post_time,
Expand All @@ -401,6 +405,8 @@ def post(
identity,
pre_conetent,
content,
summary,
sensitive,
visibility=visibility,
type_data=data,
published=post_time,
Expand All @@ -427,6 +433,14 @@ def delete_posts(post_pks):
# TimelineEvent.objects.filter(subject_post__in=[post.pk]).delete()
PostInteraction.objects.filter(post__in=post_pks).update(state="undone")

@staticmethod
def get_spoiler_text(text, item):
if text and text.find(">!") != -1:
spoiler_text = f"关于《{item.display_title}》 可能有关键情节等敏感内容"
return spoiler_text, text.replace(">!", "").replace("!<", "")
else:
return None, text or ""

@staticmethod
def visibility_n2t(visibility: int, post_public_mode: int) -> Visibilities:
if visibility == 1:
Expand Down Expand Up @@ -476,6 +490,8 @@ def post_collection(collection: "Collection"):
pre_conetent,
content,
visibility,
None,
False,
data,
existing_post.pk if existing_post else None,
collection.created_time,
Expand All @@ -499,7 +515,8 @@ def post_comment(comment, share_as_new_post: bool) -> Post | None:
item_link = f"{settings.SITE_INFO['site_url']}/~neodb~{comment.item_url}"
action_label = "评论" if comment.text else "分享"
pre_conetent = f'{action_label}{category}<a href="{item_link}">《{comment.item.display_title}》</a><br>'
content = f"{comment.text}\n{tags}"
spoiler, txt = Takahe.get_spoiler_text(comment.text, comment.item)
content = f"{txt}\n{tags}"
data = {
"object": {
"tag": [comment.item.ap_object_ref],
Expand All @@ -513,6 +530,8 @@ def post_comment(comment, share_as_new_post: bool) -> Post | None:
pre_conetent,
content,
v,
spoiler,
spoiler is not None,
data,
existing_post.pk if existing_post else None,
comment.created_time,
Expand Down Expand Up @@ -553,6 +572,8 @@ def post_review(review, share_as_new_post: bool) -> Post | None:
pre_conetent,
content,
v,
None,
False,
data,
existing_post.pk if existing_post else None,
review.created_time,
Expand Down Expand Up @@ -581,7 +602,8 @@ def post_mark(mark, share_as_new_post: bool, append_content="") -> Post | None:
pre_conetent = (
f'{mark.action_label}<a href="{item_link}">《{mark.item.display_title}》</a>'
)
content = f"{stars} \n{mark.comment_text or ''}{tags}"
spoiler, txt = Takahe.get_spoiler_text(mark.comment_text, mark.item)
content = f"{stars} \n{txt}{tags}"
data = {
"object": {
"tag": [mark.item.ap_object_ref],
Expand All @@ -605,6 +627,8 @@ def post_mark(mark, share_as_new_post: bool, append_content="") -> Post | None:
pre_conetent,
content + append_content,
v,
spoiler,
spoiler is not None,
data,
existing_post.pk if existing_post else None,
mark.shelfmember.created_time,
Expand Down

0 comments on commit dc5ef33

Please sign in to comment.