Add Mastodon API endpoints for following and unfollowing hashtags

pull/539/head
Christof Dorner 2023-03-12 20:59:50 +01:00
rodzic 17ca48a2fd
commit 6c1c57625b
4 zmienionych plików z 48 dodań i 4 usunięć

Wyświetl plik

@ -175,9 +175,14 @@ class Hashtag(StatorModel):
results[date(year, month, day)] = val
return dict(sorted(results.items(), reverse=True)[:num])
def to_mastodon_json(self):
return {
def to_mastodon_json(self, followed: bool | None = None):
value = {
"name": self.hashtag,
"url": self.urls.view.full(),
"url": self.urls.view.full(), # type: ignore
"history": [],
}
if followed is not None:
value["followed"] = followed
return value

Wyświetl plik

@ -276,13 +276,15 @@ class Tag(Schema):
name: str
url: str
history: dict
followed: bool | None
@classmethod
def from_hashtag(
cls,
hashtag: activities_models.Hashtag,
followed: bool | None = None,
) -> "Tag":
return cls(**hashtag.to_mastodon_json())
return cls(**hashtag.to_mastodon_json(followed=followed))
class FeaturedTag(Schema):

Wyświetl plik

@ -95,6 +95,8 @@ urlpatterns = [
path("v1/statuses/<id>/unbookmark", statuses.unbookmark_status),
# Tags
path("v1/followed_tags", tags.followed_tags),
path("v1/tags/<id>/follow", tags.follow),
path("v1/tags/<id>/unfollow", tags.unfollow),
# Timelines
path("v1/timelines/home", timelines.home),
path("v1/timelines/public", timelines.public),

Wyświetl plik

@ -1,4 +1,5 @@
from django.http import HttpRequest
from django.shortcuts import get_object_or_404
from hatchway import api_view
from activities.models import Hashtag
@ -32,3 +33,37 @@ def followed_tags(
request=request,
include_params=["limit"],
)
@scope_required("write:follows")
@api_view.post
def follow(
request: HttpRequest,
id: str,
) -> schemas.Tag:
hashtag = get_object_or_404(
Hashtag,
pk=id,
)
request.identity.hashtag_follows.get_or_create(hashtag=hashtag)
return schemas.Tag.from_hashtag(
hashtag,
followed=True,
)
@scope_required("write:follows")
@api_view.post
def unfollow(
request: HttpRequest,
id: str,
) -> schemas.Tag:
hashtag = get_object_or_404(
Hashtag,
pk=id,
)
request.identity.hashtag_follows.filter(hashtag=hashtag).delete()
return schemas.Tag.from_hashtag(
hashtag,
followed=False,
)