add tags to content for posting
This commit is contained in:
parent
db5519048b
commit
a20779ce31
5 changed files with 19 additions and 24 deletions
|
@ -125,6 +125,15 @@ class Mark:
|
|||
def tags(self) -> list[str]:
|
||||
return self.owner.tag_manager.get_item_tags(self.item)
|
||||
|
||||
@cached_property
|
||||
def tag_text(self) -> str:
|
||||
tags = [f"#{t}" for t in self.tags]
|
||||
appending = self.owner.user.preference.mastodon_append_tag
|
||||
if appending:
|
||||
tags.append(appending)
|
||||
tag_text = f"\n{' '.join(tags)}\n" if tags else ""
|
||||
return tag_text
|
||||
|
||||
@cached_property
|
||||
def rating(self):
|
||||
return Rating.objects.filter(owner=self.owner, item=self.item).first()
|
||||
|
|
|
@ -55,13 +55,13 @@ class Tag(List):
|
|||
|
||||
@staticmethod
|
||||
def cleanup_title(title, replace=True):
|
||||
t = re.sub(r"\s+", " ", title.strip())
|
||||
return "_" if not title and replace else t
|
||||
t = re.sub(r"\s+", " ", title.rstrip().lstrip("# "))
|
||||
return "_" if not t and replace else t
|
||||
|
||||
@staticmethod
|
||||
def deep_cleanup_title(title):
|
||||
"""Remove all non-word characters, only for public index purpose"""
|
||||
return re.sub(r"\W+", " ", title).strip()
|
||||
return re.sub(r"\W+", " ", title).rstrip().lstrip("# ").lower() or "_"
|
||||
|
||||
|
||||
class TagManager:
|
||||
|
@ -78,7 +78,7 @@ class TagManager:
|
|||
[
|
||||
t
|
||||
for t in set(map(lambda t: Tag.deep_cleanup_title(t["title"]), tags))
|
||||
if t
|
||||
if t and t != "_"
|
||||
]
|
||||
)
|
||||
return tag_titles
|
||||
|
|
|
@ -144,6 +144,10 @@ class TagTest(TestCase):
|
|||
self.user2 = User.register(email="x@b.com", username="user2")
|
||||
self.user3 = User.register(email="y@b.com", username="user3")
|
||||
|
||||
def test_cleanup(self):
|
||||
self.assertEqual(Tag.cleanup_title("# "), "_")
|
||||
self.assertEqual(Tag.deep_cleanup_title("# C "), "c")
|
||||
|
||||
def test_user_tag(self):
|
||||
t1 = "tag 1"
|
||||
t2 = "tag 2"
|
||||
|
|
|
@ -590,23 +590,13 @@ def share_mark(mark, post_as_new=False):
|
|||
|
||||
user = mark.owner.user
|
||||
visibility = get_toot_visibility(mark.visibility, user)
|
||||
tags = (
|
||||
"\n"
|
||||
+ user.preference.mastodon_append_tag.replace(
|
||||
"[category]", str(ItemCategory(mark.item.category).label)
|
||||
)
|
||||
if user.preference.mastodon_append_tag
|
||||
else ""
|
||||
)
|
||||
site = MastodonApplication.objects.filter(domain_name=user.mastodon_site).first()
|
||||
stars = rating_to_emoji(
|
||||
mark.rating_grade,
|
||||
site.star_mode if site else 0,
|
||||
)
|
||||
spoiler_text, txt = get_spoiler_text(mark.comment_text or "", mark.item)
|
||||
content = (
|
||||
f"{mark.get_action_for_feed()} {stars}\n{mark.item.absolute_url}\n{txt}{tags}"
|
||||
)
|
||||
content = f"{mark.get_action_for_feed()} {stars}\n{mark.item.absolute_url}\n{txt}{mark.tag_text}"
|
||||
update_id = (
|
||||
None
|
||||
if post_as_new
|
||||
|
|
|
@ -686,19 +686,11 @@ class Takahe:
|
|||
from catalog.common import ItemCategory
|
||||
|
||||
user = mark.owner.user
|
||||
tags = (
|
||||
user.preference.mastodon_append_tag.replace(
|
||||
"[category]", str(ItemCategory(mark.item.category).label)
|
||||
)
|
||||
+ "\n"
|
||||
if user.preference.mastodon_append_tag
|
||||
else ""
|
||||
)
|
||||
stars = _rating_to_emoji(mark.rating_grade, 1)
|
||||
item_link = f"{settings.SITE_INFO['site_url']}/~neodb~{mark.item.url}"
|
||||
pre_conetent = mark.get_action_for_feed(item_link=item_link)
|
||||
spoiler, txt = Takahe.get_spoiler_text(mark.comment_text, mark.item)
|
||||
content = f"{stars} \n{txt}\n{tags}"
|
||||
content = f"{stars} \n{txt}\n{mark.tag_text}"
|
||||
data = {
|
||||
"object": {
|
||||
"tag": [mark.item.ap_object_ref],
|
||||
|
|
Loading…
Add table
Reference in a new issue