Skip to content

Commit

Permalink
post activitypub message for review
Browse files Browse the repository at this point in the history
  • Loading branch information
Her Email committed Nov 19, 2023
1 parent 6149189 commit d5a88b3
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 12 deletions.
12 changes: 8 additions & 4 deletions journal/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from catalog.common.models import *
from common.api import *
from mastodon.api import share_review
from mastodon.api import boost_toot

from .models import Mark, Review, ShelfType, TagManager, q_item_in_category

Expand Down Expand Up @@ -195,16 +195,20 @@ def review_item(request, item_uuid: str, review: ReviewInSchema):
item = Item.get_by_url(item_uuid)
if not item:
return 404, {"message": "Item not found"}
Review.update_item_review(
r, p = Review.update_item_review(
item,
request.user,
review.title,
review.body,
review.visibility,
created_time=review.created_time,
)
if review.post_to_fediverse and request.user.mastodon_username:
share_review(review)
if p and review.post_to_fediverse and request.user.mastodon_username:
boost_toot(
request.user.mastodon_site,
request.user.mastodon_token,
p.url,
)

return 200, {"message": "OK"}

Expand Down
23 changes: 20 additions & 3 deletions journal/models/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ def plain_content(self):
" ", _RE_SPOILER_TAG.sub("***", html.replace("\n", " "))
)

@property
def ap_object(self):
return {
"id": self.absolute_url,
"type": "Review",
"name": self.title,
"content": self.html_content,
"published": self.created_time.isoformat(),
"updated": self.edited_time.isoformat(),
"attributedTo": self.owner.actor_uri,
"relatedWith": self.item.absolute_url,
"href": self.absolute_url,
}

@cached_property
def mark(self):
from .mark import Mark
Expand All @@ -55,11 +69,13 @@ def update_item_review(
visibility=0,
created_time=None,
):
from takahe.utils import Takahe

if title is None:
review = Review.objects.filter(owner=owner, item=item).first()
if review is not None:
review.delete()
return None
return None, None
defaults = {
"title": title,
"body": body,
Expand All @@ -69,7 +85,8 @@ def update_item_review(
defaults["created_time"] = (
created_time if created_time < timezone.now() else timezone.now()
)
review, _ = cls.objects.update_or_create(
review, created = cls.objects.update_or_create(
item=item, owner=owner, defaults=defaults
)
return review
post = Takahe.post_review(review, created)
return review, post
2 changes: 1 addition & 1 deletion journal/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def test_mark(self):
self.assertEqual(mark.tags, [])

def test_review(self):
review = Review.update_item_review(
review, post = Review.update_item_review(
self.book1, self.user1.identity, "Critic", "Review"
)
mark = Mark(self.user1.identity, self.book1)
Expand Down
11 changes: 8 additions & 3 deletions journal/views/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from catalog.models import *
from common.utils import AuthedHttpRequest, PageLinksGenerator, get_uuid_or_404
from journal.models.renderers import convert_leading_space_in_md, render_md
from mastodon.api import share_review
from mastodon.api import boost_toot
from users.models import User
from users.models.apidentity import APIdentity

Expand Down Expand Up @@ -74,7 +74,7 @@ def review_edit(request: AuthedHttpRequest, item_uuid, review_uuid=None):
body = form.instance.body
if request.POST.get("leading_space"):
body = convert_leading_space_in_md(body)
review = Review.update_item_review(
review, post = Review.update_item_review(
item,
request.user.identity,
form.cleaned_data["title"],
Expand All @@ -87,8 +87,13 @@ def review_edit(request: AuthedHttpRequest, item_uuid, review_uuid=None):
if (
form.cleaned_data["share_to_mastodon"]
and request.user.mastodon_username
and post
):
share_review(review)
boost_toot(
request.user.mastodon_site,
request.user.mastodon_token,
post.url,
)
return redirect(reverse("journal:review_retrieve", args=[review.uuid]))
else:
raise BadRequest()
Expand Down
48 changes: 47 additions & 1 deletion takahe/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,56 @@ def get_post_url(post_pk: int) -> str | None:
def delete_posts(post_pks):
Post.objects.filter(pk__in=post_pks).update(state="deleted")

@staticmethod
def post_review(review, share_as_new_post: bool) -> Post | None:
from catalog.common import ItemCategory

user = review.owner.user
tags = (
"\n"
+ user.preference.mastodon_append_tag.replace(
"[category]", str(ItemCategory(review.item.category).label)
)
if user.preference.mastodon_append_tag
else ""
)
stars = _rating_to_emoji(review.rating_grade, 1)
item_link = f"{settings.SITE_INFO['site_url']}/~neodb~{review.item.url}"

pre_conetent = f'发布了关于<a href="{item_link}">《{review.item.display_title}》</a>的评论:<br><a href="{review.absolute_url}">{review.title}</a>'
content = f"{stars}\n{tags}"
data = {
"object": {
"tag": [review.item.ap_object_ref],
"relatedWith": [review.ap_object],
}
}
if review.visibility == 1:
v = Takahe.Visibilities.followers
elif review.visibility == 2:
v = Takahe.Visibilities.mentioned
elif user.preference.mastodon_publish_public:
v = Takahe.Visibilities.public
else:
v = Takahe.Visibilities.unlisted
existing_post = None if share_as_new_post else review.latest_post
post = Takahe.post( # TODO post as Article?
review.owner.pk,
pre_conetent,
content,
v,
data,
existing_post.pk if existing_post else None,
review.created_time,
)
if not post:
return
review.link_post(post)
return post

@staticmethod
def post_mark(mark, share_as_new_post: bool) -> Post | None:
from catalog.common import ItemCategory
from takahe.utils import Takahe

user = mark.owner.user
tags = (
Expand Down

0 comments on commit d5a88b3

Please sign in to comment.