Skip to content

Commit

Permalink
annual summary can be shared to bluesky
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name authored and alphatownsman committed Nov 28, 2024
1 parent 6e3e8ce commit ae7c650
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
10 changes: 5 additions & 5 deletions journal/templates/wrapped.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ site_name }} - {{ identity.display_name }} - {{ year }} 年度统计</title>
<title>{{ site_name }} - {{ identity.display_name }} - {{ year }} {% trans "annual summary" %}</title>
{% include "common_libs.html" %}
{% comment %} <script src="{{ cdn_url }}/npm/[email protected]/dist/roughviz.umd.min.js"></script> {% endcomment %}
<script src="{% static 'js/roughviz.umd.js' %}"></script>
Expand All @@ -35,20 +35,20 @@
<div class="grid__main">
<span class="action">
<span>
<a onclick="restyle()" title="随机风格"><i class="fa-solid fa-shuffle"></i></a>
<a onclick="restyle()" title="{% trans 'another style' %}"><i class="fa-solid fa-shuffle"></i></a>
</span>
<span>
<a hx-get="{% url 'journal:wrapped_share' year %}"
hx-target="body"
hx-swap="beforeend"
title="转发到时间轴"><i class="fa-solid fa-share-from-square"></i></a>
title="{% trans 'share' %}"><i class="fa-solid fa-share-from-square"></i></a>
</span>
<span>
<a onclick="saveSvgAsPng($('#viz0').children('svg')[0], '{{ year }}-wrapped.png');"
title="下载图片"><i class="fa-solid fa-download"></i></a>
title="{% trans 'download' %}"><i class="fa-solid fa-download"></i></a>
</span>
</span>
<h5>{{ year }} 年度统计</h5>
<h5>{{ year }} {% trans "annual summary" %}</h5>
<div id="viz0" style="max-width: 100%; aspect-ratio: 1 / 1;"></div>
{{ by_cat|json_script:"cat-data" }}
{{ monthly|json_script:"mon-data" }}
Expand Down
5 changes: 5 additions & 0 deletions journal/views/wrapped.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
)
from journal.models import Comment, ShelfType
from journal.models.common import VisibilityType
from mastodon.models.bluesky import EmbedObj
from takahe.utils import Takahe
from users.models import User

Expand Down Expand Up @@ -137,5 +138,9 @@ def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
pass
elif post and user.mastodon:
user.mastodon.boost_later(post.url)
if visibility == VisibilityType.Public and user.bluesky:
o = EmbedObj("🧩", "", user.absolute_url)
txt = comment.rstrip() + "\n\n##obj##"
user.bluesky.post(txt, obj=o, images=[img])
messages.add_message(request, messages.INFO, _("Summary posted to timeline."))
return HttpResponseRedirect(request.META.get("HTTP_REFERER", "/"))
29 changes: 27 additions & 2 deletions mastodon/models/bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from atproto_client.exceptions import AtProtocolError
from atproto_identity.did.resolver import DidResolver
from atproto_identity.handle.resolver import HandleResolver
from django.conf import settings
from django.utils import timezone
from loguru import logger

Expand Down Expand Up @@ -230,8 +231,9 @@ def post(
self,
content,
reply_to_id=None,
obj: "Item | Content | None" = None,
obj: "Item | Content | EmbedObj | None" = None,
rating=None,
images=[],
**kwargs,
):
from journal.models.renderers import render_rating
Expand All @@ -257,12 +259,27 @@ def post(
else:
first = False
richtext.text(t)
if obj:
if images:
refs = [self._client.upload_blob(image).blob for image in images]
embed_images = [
models.AppBskyEmbedImages.Image(alt="", image=r) for r in refs
]
embed = models.AppBskyEmbedImages.Main(images=embed_images)
elif obj:
cover = getattr(obj, "cover", None)
blob = (
cover.read()
if cover and cover != settings.DEFAULT_ITEM_COVER
else getattr(obj, "image", None)
)
blob_ref = self._client.upload_blob(blob).blob if blob else None
# blob_ref = None
embed = models.AppBskyEmbedExternal.Main(
external=models.AppBskyEmbedExternal.External(
title=obj.display_title,
description=obj.brief_description,
uri=obj.absolute_url,
thumb=blob_ref,
)
)
else:
Expand All @@ -273,3 +290,11 @@ def post(

def delete_post(self, post_uri):
self._client.delete_post(post_uri)


class EmbedObj:
def __init__(self, title, description, uri, image=None):
self.display_title = title
self.brief_description = description
self.absolute_url = uri
self.image = image

0 comments on commit ae7c650

Please sign in to comment.