Skip to content

Commit

Permalink
Merge pull request #315 from jamesvandyne/bluesky
Browse files Browse the repository at this point in the history
Add Bridgy Bluesky support
  • Loading branch information
jamesvandyne authored Dec 7, 2024
2 parents 505ba69 + b8839eb commit 69f3838
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 22 deletions.
2 changes: 1 addition & 1 deletion apps/application/entry/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from application.entry._open_graph import get_open_graph_meta_for_entry

from ._create_entry import Bookmark, Checkin, Location, Reply, create_entry
from ._post_to_bridgy import post_to_mastodon
from ._post_to_bridgy import post_to_bridgy
from ._update_entry import update_entry

__all__ = ["get_open_graph_meta_for_entry", "create_entry"]
12 changes: 8 additions & 4 deletions apps/application/entry/_post_to_bridgy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@
class AlreadySentWebmention(Exception): ...


def post_to_mastodon(t_entry: entry_models.TEntry, entry_absolute_url: str):
def post_to_bridgy(
t_entry: entry_models.TEntry,
entry_absolute_url: str,
target_bridgy_url: entry_constants.BridgySyndicationUrls,
) -> None:
try:
t_entry.bridgy_publish_url.get(url=entry_constants.BridgySyndicationUrls.mastodon)
t_entry.bridgy_publish_url.get(url=target_bridgy_url)
except entry_models.BridgyPublishUrl.DoesNotExist:
t_entry.new_bridgy_url(entry_constants.BridgySyndicationUrls.mastodon)
t_entry.new_bridgy_url(target_bridgy_url)

try:
_send_webmention(
t_entry=t_entry,
t_post=t_entry.t_post,
source=entry_absolute_url,
target=entry_constants.BridgySyndicationUrls.mastodon,
target=target_bridgy_url,
)
except AlreadySentWebmention:
pass
Expand Down
1 change: 1 addition & 0 deletions apps/data/entry/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

class BridgySyndicationUrls(TextChoices):
mastodon = "https://brid.gy/publish/mastodon"
bluesky = "https://brid.gy/publish/bluesky"
24 changes: 18 additions & 6 deletions apps/domain/entry/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,29 @@ def is_syndicated_to_mastodon(entry: entry_models.TEntry) -> bool:
"""
Determine if an entry has been sent to Mastodon with Bri.gy.
"""
return _get_mastodon_webmention(entry).exists()
return _get_bridgy_webmention(entry, target=entry_constants.BridgySyndicationUrls.mastodon).exists()


def mastodon_syndication_url(entry: entry_models.TEntry) -> str | None:
mastodon_wm = _get_mastodon_webmention(entry)
mastodon_wm = _get_bridgy_webmention(entry, target=entry_constants.BridgySyndicationUrls.mastodon)
for wm in mastodon_wm:
return wm.response_body.get("url")
return None


def _get_mastodon_webmention(entry: entry_models.TEntry):
return entry.t_post.ref_t_webmention_send.filter(
target=entry_constants.BridgySyndicationUrls.mastodon, success=True
)
def is_syndicated_to_bluesky(entry: entry_models.TEntry) -> bool:
"""
Determine if an entry has been sent to Bluesky with Bri.gy.
"""
return _get_bridgy_webmention(entry, target=entry_constants.BridgySyndicationUrls.bluesky).exists()


def bluesky_syndication_url(entry: entry_models.TEntry) -> str | None:
mastodon_wm = _get_bridgy_webmention(entry, target=entry_constants.BridgySyndicationUrls.bluesky)
for wm in mastodon_wm:
return wm.response_body.get("url")
return None


def _get_bridgy_webmention(entry: entry_models.TEntry, target: entry_constants.BridgySyndicationUrls):
return entry.t_post.ref_t_webmention_send.filter(target=target, success=True)
12 changes: 11 additions & 1 deletion apps/interfaces/dashboard/entry/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.urls import path

from data.entry import constants as entry_constants
from interfaces.dashboard.entry import views

urlpatterns = [
Expand Down Expand Up @@ -39,5 +40,14 @@
path("bookmark/<int:pk>/title", views.BookmarkTitle.as_view(), name="bookmark_title"),
path("bookmark/<int:pk>/change_title", views.ChangeBookmarkTitle.as_view(), name="change_bookmark_title"),
# Bridgy
path("entry/<int:pk>/send_to_mastodon", views.SendToBridgy.as_view(), name="send_to_mastodon"),
path(
"entry/<int:pk>/send_to_mastodon",
views.SendToBridgy.as_view(bridgy_url=entry_constants.BridgySyndicationUrls.mastodon),
name="send_to_mastodon",
),
path(
"entry/<int:pk>/send_to_bluesky",
views.SendToBridgy.as_view(bridgy_url=entry_constants.BridgySyndicationUrls.bluesky),
name="send_to_bluesky",
),
]
20 changes: 14 additions & 6 deletions apps/interfaces/dashboard/entry/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from application import entry as entry_app
from application.indieweb import extract as indieweb_extract
from application.indieweb import webmentions
from data.entry import constants as entry_constants
from data.entry import models
from data.indieweb.constants import MPostKinds, MPostStatuses
from data.post import models as post_models
Expand Down Expand Up @@ -788,6 +789,7 @@ def form_valid(self, form):
class SendToBridgy(FormView):
template_name = "interfaces/dashboard/entry/bridgy/_form.html"
form_class = forms.SendToBridgy
bridgy_url: entry_constants.BridgySyndicationUrls = entry_constants.BridgySyndicationUrls.mastodon
entry: models.TEntry

def setup(self, *args, pk: int, **kwargs):
Expand All @@ -798,21 +800,27 @@ def get_context_data(self, *args, **kwargs) -> dict[str, Any]:
return super().get_context_data(
*args,
t_entry_id=self.entry.id,
is_syndicated=entry_queries.is_syndicated_to_mastodon(self.entry),
syndication_url=entry_queries.mastodon_syndication_url(self.entry),
is_syndicated_to_mastodon=entry_queries.is_syndicated_to_mastodon(self.entry),
is_syndicated_to_bluesky=entry_queries.is_syndicated_to_bluesky(self.entry),
mastodon_syndication_url=entry_queries.mastodon_syndication_url(self.entry),
bluesky_syndication_url=entry_queries.bluesky_syndication_url(self.entry),
**kwargs,
)

def form_valid(self, form):
entry_app.post_to_mastodon(
t_entry=self.entry, entry_absolute_url=self.request.build_absolute_uri(self.entry.t_post.get_absolute_url())
entry_app.post_to_bridgy(
t_entry=self.entry,
entry_absolute_url=self.request.build_absolute_uri(self.entry.t_post.get_absolute_url()),
target_bridgy_url=self.bridgy_url,
)
return TemplateResponse(
self.request,
"interfaces/dashboard/entry/bridgy/_form.html",
{
"t_entry_id": self.entry.id,
"is_syndicated": entry_queries.is_syndicated_to_mastodon(self.entry),
"syndication_url": entry_queries.mastodon_syndication_url(self.entry),
"is_syndicated_to_mastodon": entry_queries.is_syndicated_to_mastodon(self.entry),
"is_syndicated_to_bluesky": entry_queries.is_syndicated_to_bluesky(self.entry),
"mastodon_syndication_url": entry_queries.mastodon_syndication_url(self.entry),
"bluesky_syndication_url": entry_queries.bluesky_syndication_url(self.entry),
},
)
16 changes: 12 additions & 4 deletions apps/templates/interfaces/dashboard/entry/bridgy/_form.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
<div hx-target="this" hx-swap="outerHTML" class="mt-2">
{% if is_syndicated %}
<a href="{{ syndication_url }}" target="_new" class="outline-button">🐘 View on Mastodon</a>
<div hx-target="this" hx-swap="outerHTML" class="mt-2 flex flex-col">
{% if is_syndicated_to_mastodon %}
<a href="{{ mastodon_syndication_url }}" target="_new" class="outline-button text-center">🐘 View on Mastodon</a>
{% else %}
<form hx-post="{% url 'send_to_mastodon' t_entry_id %}" class="flex">
<form hx-post="{% url 'send_to_mastodon' t_entry_id %}" class="flex flex-col">
<button class="outline-button" data-loading-class="hidden"><span id="elephant">🐘</span> Send to Mastodon</button>
<button class="outline-button" data-loading-disable data-loading><span id="elephant" class="animate-spin inline-block" >🐘</span> Sending to Mastodon…</button>
</form>
{% endif %}
{% if is_syndicated_to_bluesky %}
<a href="{{ bluesky_syndication_url }}" target="_new" class="outline-button mt-2 text-center">🦋 View on Bluesky</a>
{% else %}
<form hx-post="{% url 'send_to_bluesky' t_entry_id %}" class="flex flex-col mt-2">
<button class="outline-button text-center" data-loading-class="hidden"><span id="butterfly">🦋</span> Send to Bluesky</button>
<button class="outline-button" data-loading-disable data-loading><span id="butterfly" class="animate-spin inline-block" >🦋</span> Sending to Bluesky…</button>
</form>
{% endif %}
</div>

0 comments on commit 69f3838

Please sign in to comment.